1 /*
   2  * Copyright (c) 1997, 2012, 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.impl.ElementImpl;
  36 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
  37 import com.sun.xml.internal.messaging.saaj.util.*;
  38 
  39 import org.w3c.dom.Element;
  40 import org.w3c.dom.Document;
  41 import org.w3c.dom.NodeList;
  42 import org.w3c.dom.NamedNodeMap;
  43 import org.w3c.dom.Attr;
  44 
  45 public abstract class SOAPFactoryImpl extends SOAPFactory {
  46 
  47     protected static final Logger
  48         log = Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
  49                                "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
  50 
  51     protected abstract SOAPDocumentImpl createDocument();
  52 
  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     public SOAPElement createElement(Name name) throws SOAPException {
  65         // @since SAAJ 1.3
  66         // If the Name was null it would cause a NullPointerException in earlier release
  67         if (name == null) {
  68             log.log(Level.SEVERE,"SAAJ0567.soap.null.input",
  69                         new Object[] {"name","SOAPFactory.createElement"});
  70             throw new SOAPException("Null name argument passed to createElement");
  71         }
  72         return ElementFactory.createElement(createDocument(), name);
  73     }
  74 
  75     public SOAPElement createElement(QName qname) throws SOAPException {
  76         if (qname == null) {
  77             log.log(Level.SEVERE,"SAAJ0567.soap.null.input",
  78                         new Object[] {"qname","SOAPFactory.createElement"});
  79             throw new SOAPException("Null qname argument passed to createElement");
  80         }
  81         return ElementFactory.createElement(createDocument(),qname);
  82     }
  83 
  84     public SOAPElement createElement(
  85         String localName,
  86         String prefix,
  87         String uri)  throws SOAPException {
  88 
  89         // @since SAAJ 1.3
  90         // if prefix !=null but localName== null then in earlier releases it would create
  91         // a Qualified Name  <prefix>:null which is not meaningful
  92         if (localName == null) {
  93             log.log(Level.SEVERE,"SAAJ0567.soap.null.input",
  94                         new Object[] {"localName","SOAPFactory.createElement"});
  95             throw new SOAPException("Null localName argument passed to createElement");
  96         }
  97         return ElementFactory.createElement(createDocument(), localName, prefix, uri);
  98     }
  99 
 100     public Name createName(String localName, String prefix, String uri)
 101         throws SOAPException {
 102         // @since SAAJ 1.3
 103         // if localName==null, earlier impl would create Name with localName=""
 104         // which is absurd.
 105         if (localName == null) {
 106             log.log(
 107                  Level.SEVERE,"SAAJ0567.soap.null.input",
 108                  new Object[] {"localName","SOAPFactory.createName"});
 109             throw new SOAPException("Null localName argument passed to createName");
 110         }
 111         return NameImpl.create(localName, prefix, uri);
 112     }
 113 
 114     public Name createName(String localName) throws SOAPException {
 115         // @since SAAJ 1.3
 116         // if localName==null, earlier impl would create Name with localName=null
 117         // which is absurd.
 118         if (localName == null) {
 119             log.log(
 120                 Level.SEVERE,"SAAJ0567.soap.null.input",
 121                 new Object[] {"localName","SOAPFactory.createName"});
 122             throw new SOAPException("Null localName argument passed to createName");
 123         }
 124         return NameImpl.createFromUnqualifiedName(localName);
 125     }
 126 
 127     // Note: the child elements might still be org.w3c.dom.Element's, but the
 128     // getChildElements will do the conversion to SOAPElement when called.
 129     public SOAPElement createElement(Element domElement) throws SOAPException {
 130         if (domElement == null) {
 131             return null;
 132         }
 133         return convertToSoapElement(domElement);
 134     }
 135 
 136     private  SOAPElement convertToSoapElement(Element element) throws SOAPException {
 137 
 138         if (element instanceof SOAPElement) {
 139             return (SOAPElement) element;
 140         }
 141 
 142         SOAPElement copy = createElement(
 143                                 element.getLocalName(),
 144                                 element.getPrefix(),
 145                                 element.getNamespaceURI());
 146 
 147         Document ownerDoc = copy.getOwnerDocument();
 148 
 149         NamedNodeMap attrMap = element.getAttributes();
 150         for (int i=0; i < attrMap.getLength(); i++) {
 151             Attr nextAttr = (Attr)attrMap.item(i);
 152             Attr importedAttr = (Attr)ownerDoc.importNode(nextAttr, true);
 153             copy.setAttributeNodeNS(importedAttr);
 154         }
 155 
 156 
 157         NodeList nl = element.getChildNodes();
 158         for (int i=0; i < nl.getLength(); i++) {
 159             org.w3c.dom.Node next = nl.item(i);
 160             org.w3c.dom.Node imported = ownerDoc.importNode(next, true);
 161             copy.appendChild(imported);
 162         }
 163 
 164         return copy;
 165     }
 166 
 167     public Detail createDetail() throws SOAPException {
 168         throw new UnsupportedOperationException();
 169     }
 170 
 171     public  SOAPFault createFault(String reasonText, QName faultCode) throws SOAPException {
 172         throw new UnsupportedOperationException();
 173     }
 174 
 175     public SOAPFault createFault() throws SOAPException {
 176         throw new UnsupportedOperationException();
 177     }
 178 
 179 }