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