1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.messaging.saaj.soap;
  27 
  28 import java.util.logging.Logger;
  29 import java.util.logging.Level;
  30 
  31 import javax.xml.namespace.QName;
  32 import javax.xml.soap.*;
  33 
  34 import com.sun.xml.internal.messaging.saaj.soap.impl.ElementFactory;
  35 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
  36 import com.sun.xml.internal.messaging.saaj.util.*;
  37 
  38 import org.w3c.dom.Element;
  39 import org.w3c.dom.Document;
  40 import org.w3c.dom.NodeList;
  41 import org.w3c.dom.NamedNodeMap;
  42 import org.w3c.dom.Attr;
  43 
  44 public abstract class SOAPFactoryImpl extends SOAPFactory {
  45 
  46     protected static final Logger
  47         log = Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
  48                                "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
  49 
  50     protected abstract SOAPDocumentImpl createDocument();
  51 
  52     @Override
  53     public SOAPElement createElement(String tagName) throws SOAPException {
  54          if (tagName == null) {
  55              log.log(
  56                  Level.SEVERE,"SAAJ0567.soap.null.input",
  57                  new Object[] {"tagName","SOAPFactory.createElement"});
  58              throw new SOAPException("Null tagName argument passed to createElement");
  59          }
  60         return ElementFactory.createElement(createDocument(),
  61                         NameImpl.createFromTagName(tagName));
  62     }
  63 
  64     @Override
  65     public SOAPElement createElement(Name name) throws SOAPException {
  66         // @since SAAJ 1.3
  67         // If the Name was null it would cause a NullPointerException in earlier release
  68         if (name == null) {
  69             log.log(Level.SEVERE,"SAAJ0567.soap.null.input",
  70                         new Object[] {"name","SOAPFactory.createElement"});
  71             throw new SOAPException("Null name argument passed to createElement");
  72         }
  73         return ElementFactory.createElement(createDocument(), name);
  74     }
  75 
  76     @Override
  77     public SOAPElement createElement(QName qname) throws SOAPException {
  78         if (qname == null) {
  79             log.log(Level.SEVERE,"SAAJ0567.soap.null.input",
  80                         new Object[] {"qname","SOAPFactory.createElement"});
  81             throw new SOAPException("Null qname argument passed to createElement");
  82         }
  83         return ElementFactory.createElement(createDocument(),qname);
  84     }
  85 
  86     @Override
  87     public SOAPElement createElement(
  88         String localName,
  89         String prefix,
  90         String uri)  throws SOAPException {
  91 
  92         // @since SAAJ 1.3
  93         // if prefix !=null but localName== null then in earlier releases it would create
  94         // a Qualified Name  <prefix>:null which is not meaningful
  95         if (localName == null) {
  96             log.log(Level.SEVERE,"SAAJ0567.soap.null.input",
  97                         new Object[] {"localName","SOAPFactory.createElement"});
  98             throw new SOAPException("Null localName argument passed to createElement");
  99         }
 100         return ElementFactory.createElement(createDocument(), localName, prefix, uri);
 101     }
 102 
 103     @Override
 104     public Name createName(String localName, String prefix, String uri)
 105         throws SOAPException {
 106         // @since SAAJ 1.3
 107         // if localName==null, earlier impl would create Name with localName=""
 108         // which is absurd.
 109         if (localName == null) {
 110             log.log(
 111                  Level.SEVERE,"SAAJ0567.soap.null.input",
 112                  new Object[] {"localName","SOAPFactory.createName"});
 113             throw new SOAPException("Null localName argument passed to createName");
 114         }
 115         return NameImpl.create(localName, prefix, uri);
 116     }
 117 
 118     @Override
 119     public Name createName(String localName) throws SOAPException {
 120         // @since SAAJ 1.3
 121         // if localName==null, earlier impl would create Name with localName=null
 122         // which is absurd.
 123         if (localName == null) {
 124             log.log(
 125                 Level.SEVERE,"SAAJ0567.soap.null.input",
 126                 new Object[] {"localName","SOAPFactory.createName"});
 127             throw new SOAPException("Null localName argument passed to createName");
 128         }
 129         return NameImpl.createFromUnqualifiedName(localName);
 130     }
 131 
 132     // Note: the child elements might still be org.w3c.dom.Element's, but the
 133     // getChildElements will do the conversion to SOAPElement when called.
 134     @Override
 135     public SOAPElement createElement(Element domElement) throws SOAPException {
 136         if (domElement == null) {
 137             return null;
 138         }
 139         return convertToSoapElement(domElement);
 140     }
 141 
 142     private  SOAPElement convertToSoapElement(Element element) throws SOAPException {
 143 
 144         if (element instanceof SOAPElement) {
 145             return (SOAPElement) element;
 146         }
 147 
 148         SOAPElement copy = createElement(
 149                                 element.getLocalName(),
 150                                 element.getPrefix(),
 151                                 element.getNamespaceURI());
 152 
 153         Document ownerDoc = copy.getOwnerDocument();
 154 
 155         NamedNodeMap attrMap = element.getAttributes();
 156         for (int i=0; i < attrMap.getLength(); i++) {
 157             Attr nextAttr = (Attr)attrMap.item(i);
 158             Attr importedAttr = (Attr)ownerDoc.importNode(nextAttr, true);
 159             copy.setAttributeNodeNS(importedAttr);
 160         }
 161 
 162 
 163         NodeList nl = element.getChildNodes();
 164         for (int i=0; i < nl.getLength(); i++) {
 165             org.w3c.dom.Node next = nl.item(i);
 166             org.w3c.dom.Node imported = ownerDoc.importNode(next, true);
 167             copy.appendChild(imported);
 168         }
 169 
 170         return copy;
 171     }
 172 
 173     @Override
 174     public Detail createDetail() throws SOAPException {
 175         throw new UnsupportedOperationException();
 176     }
 177 
 178     @Override
 179     public  SOAPFault createFault(String reasonText, QName faultCode) throws SOAPException {
 180         throw new UnsupportedOperationException();
 181     }
 182 
 183     @Override
 184     public SOAPFault createFault() throws SOAPException {
 185         throw new UnsupportedOperationException();
 186     }
 187 
 188 }