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.impl;
  27 
  28 import javax.xml.namespace.QName;
  29 import javax.xml.soap.*;
  30 
  31 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
  32 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
  33 import com.sun.xml.internal.messaging.saaj.soap.ver1_1.*;
  34 import com.sun.xml.internal.messaging.saaj.soap.ver1_2.*;
  35 import org.w3c.dom.Element;
  36 
  37 import java.util.Objects;
  38 
  39 
  40 public class ElementFactory {
  41     public static SOAPElement createElement(
  42         SOAPDocumentImpl ownerDocument,
  43         Name name) {
  44         return createElement(
  45             ownerDocument,
  46             name.getLocalName(),
  47             name.getPrefix(),
  48             name.getURI());
  49     }
  50     public static SOAPElement createElement(
  51         SOAPDocumentImpl ownerDocument,
  52         QName name) {
  53         return createElement(
  54             ownerDocument,
  55             name.getLocalPart(),
  56             name.getPrefix(),
  57             name.getNamespaceURI());
  58     }
  59 
  60     /**
  61      * Create element wrapper for existing DOM element.
  62      *
  63      * @param ownerDocument SOAP document wrapper not null
  64      * @param element DOM element not null
  65      * @return SOAP wrapper for DOM element
  66      */
  67     public static SOAPElement createElement(SOAPDocumentImpl ownerDocument, Element element) {
  68         Objects.requireNonNull(ownerDocument);
  69         Objects.requireNonNull(element);
  70 
  71         String localName = element.getLocalName();
  72         String namespaceUri = element.getNamespaceURI();
  73         String prefix = element.getPrefix();
  74 
  75         if (localName.equalsIgnoreCase("Envelope")) {
  76             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
  77                 return new Envelope1_1Impl(ownerDocument, element);
  78             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
  79                 return new Envelope1_2Impl(ownerDocument, element);
  80             }
  81         }
  82         if (localName.equalsIgnoreCase("Body")) {
  83             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
  84                 return new Body1_1Impl(ownerDocument, element);
  85             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
  86                 return new Body1_2Impl(ownerDocument, element);
  87             }
  88         }
  89         if (localName.equalsIgnoreCase("Header")) {
  90             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
  91                 return new Header1_1Impl(ownerDocument, element);
  92             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
  93                 return new Header1_2Impl(ownerDocument, element);
  94             }
  95         }
  96         if (localName.equalsIgnoreCase("Fault")) {
  97             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
  98                 return new Fault1_1Impl(element, ownerDocument);
  99             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
 100                 return new Fault1_2Impl(element, ownerDocument);
 101             }
 102 
 103         }
 104         if (localName.equalsIgnoreCase("Detail")) {
 105             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
 106                 return new Detail1_1Impl(ownerDocument, element);
 107             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
 108                 return new Detail1_2Impl(ownerDocument, element);
 109             }
 110         }
 111         if (localName.equalsIgnoreCase("faultcode")
 112                 || localName.equalsIgnoreCase("faultstring")
 113                 || localName.equalsIgnoreCase("faultactor")) {
 114             // SOAP 1.2 does not have fault(code/string/actor)
 115             // So there is no else case required
 116             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
 117                 return new FaultElement1_1Impl(ownerDocument,
 118                         localName,
 119                         prefix);
 120             }
 121         }
 122 
 123         return new ElementImpl(ownerDocument, element);
 124     }
 125 
 126     public static SOAPElement createElement(
 127         SOAPDocumentImpl ownerDocument,
 128         String localName,
 129         String prefix,
 130         String namespaceUri) {
 131 
 132 
 133         if (ownerDocument == null) {
 134             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
 135                 ownerDocument = new SOAPPart1_1Impl().getDocument();
 136             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
 137                 ownerDocument = new SOAPPart1_2Impl().getDocument();
 138             } else {
 139                 ownerDocument = new SOAPDocumentImpl(null);
 140             }
 141         }
 142 
 143         SOAPElement newElement =
 144             createNamedElement(ownerDocument, localName, prefix, namespaceUri);
 145 
 146         return newElement != null
 147             ? newElement
 148             : new ElementImpl(
 149                 ownerDocument,
 150                 namespaceUri,
 151                 NameImpl.createQName(prefix, localName));
 152     }
 153 
 154     public static SOAPElement createNamedElement(
 155         SOAPDocumentImpl ownerDocument,
 156         String localName,
 157         String prefix,
 158         String namespaceUri) {
 159 
 160         if (prefix == null) {
 161             prefix = NameImpl.SOAP_ENVELOPE_PREFIX;
 162         }
 163 
 164         if (localName.equalsIgnoreCase("Envelope")) {
 165             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
 166                 return new Envelope1_1Impl(ownerDocument, prefix);
 167             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
 168                 return new Envelope1_2Impl(ownerDocument, prefix);
 169             }
 170         }
 171         if (localName.equalsIgnoreCase("Body")) {
 172             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
 173                 return new Body1_1Impl(ownerDocument, prefix);
 174             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
 175                 return new Body1_2Impl(ownerDocument, prefix);
 176             }
 177         }
 178         if (localName.equalsIgnoreCase("Header")) {
 179             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
 180                 return new Header1_1Impl(ownerDocument, prefix);
 181             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
 182                 return new Header1_2Impl(ownerDocument, prefix);
 183             }
 184         }
 185         if (localName.equalsIgnoreCase("Fault")) {
 186             SOAPFault fault = null;
 187             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
 188                 fault = new Fault1_1Impl(ownerDocument, prefix);
 189             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
 190                 fault = new Fault1_2Impl(ownerDocument, prefix);
 191             }
 192 
 193             if (fault != null) {
 194 //                try {
 195 //                    fault.addNamespaceDeclaration(
 196 //                        NameImpl.SOAP_ENVELOPE_PREFIX,
 197 //                        SOAPConstants.URI_NS_SOAP_ENVELOPE);
 198 //                    fault.setFaultCode(
 199 //                        NameImpl.create(
 200 //                            "Server",
 201 //                            NameImpl.SOAP_ENVELOPE_PREFIX,
 202 //                            SOAPConstants.URI_NS_SOAP_ENVELOPE));
 203 //                    fault.setFaultString(
 204 //                        "Fault string, and possibly fault code, not set");
 205 //                } catch (SOAPException e) {
 206 //                }
 207                 return fault;
 208             }
 209 
 210         }
 211         if (localName.equalsIgnoreCase("Detail")) {
 212             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
 213                 return new Detail1_1Impl(ownerDocument, prefix);
 214             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
 215                 return new Detail1_2Impl(ownerDocument, prefix);
 216             }
 217         }
 218         if (localName.equalsIgnoreCase("faultcode")
 219             || localName.equalsIgnoreCase("faultstring")
 220             || localName.equalsIgnoreCase("faultactor")) {
 221             // SOAP 1.2 does not have fault(code/string/actor)
 222             // So there is no else case required
 223             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
 224                 return new FaultElement1_1Impl(ownerDocument,
 225                                                localName,
 226                                                prefix);
 227             }
 228         }
 229 
 230         return null;
 231     }
 232 
 233     protected static void invalidCreate(String msg) {
 234         throw new TreeException(msg);
 235     }
 236 }