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