1 /*
   2  * Copyright (c) 1997, 2010, 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 
  36 
  37 public class ElementFactory {
  38     public static SOAPElement createElement(
  39         SOAPDocumentImpl ownerDocument,
  40         Name name) {
  41         return createElement(
  42             ownerDocument,
  43             name.getLocalName(),
  44             name.getPrefix(),
  45             name.getURI());
  46     }
  47     public static SOAPElement createElement(
  48         SOAPDocumentImpl ownerDocument,
  49         QName name) {
  50         return createElement(
  51             ownerDocument,
  52             name.getLocalPart(),
  53             name.getPrefix(),
  54             name.getNamespaceURI());
  55     }
  56 
  57     public static SOAPElement createElement(
  58         SOAPDocumentImpl ownerDocument,
  59         String localName,
  60         String prefix,
  61         String namespaceUri) {
  62 
  63 
  64         if (ownerDocument == null) {
  65             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
  66                 ownerDocument = new SOAPPart1_1Impl().getDocument();
  67             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
  68                 ownerDocument = new SOAPPart1_2Impl().getDocument();
  69             } else {
  70                 ownerDocument = new SOAPDocumentImpl(null);
  71             }
  72         }
  73 
  74         SOAPElement newElement =
  75             createNamedElement(ownerDocument, localName, prefix, namespaceUri);
  76 
  77         return newElement != null
  78             ? newElement
  79             : new ElementImpl(
  80                 ownerDocument,
  81                 namespaceUri,
  82                 NameImpl.createQName(prefix, localName));
  83     }
  84 
  85     public static SOAPElement createNamedElement(
  86         SOAPDocumentImpl ownerDocument,
  87         String localName,
  88         String prefix,
  89         String namespaceUri) {
  90 
  91         if (prefix == null) {
  92             prefix = NameImpl.SOAP_ENVELOPE_PREFIX;
  93         }
  94 
  95         if (localName.equalsIgnoreCase("Envelope")) {
  96             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
  97                 return new Envelope1_1Impl(ownerDocument, prefix);
  98             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
  99                 return new Envelope1_2Impl(ownerDocument, prefix);
 100             }
 101         }
 102         if (localName.equalsIgnoreCase("Body")) {
 103             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
 104                 return new Body1_1Impl(ownerDocument, prefix);
 105             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
 106                 return new Body1_2Impl(ownerDocument, prefix);
 107             }
 108         }
 109         if (localName.equalsIgnoreCase("Header")) {
 110             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
 111                 return new Header1_1Impl(ownerDocument, prefix);
 112             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
 113                 return new Header1_2Impl(ownerDocument, prefix);
 114             }
 115         }
 116         if (localName.equalsIgnoreCase("Fault")) {
 117             SOAPFault fault = null;
 118             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
 119                 fault = new Fault1_1Impl(ownerDocument, prefix);
 120             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
 121                 fault = new Fault1_2Impl(ownerDocument, prefix);
 122             }
 123 
 124             if (fault != null) {
 125 //                try {
 126 //                    fault.addNamespaceDeclaration(
 127 //                        NameImpl.SOAP_ENVELOPE_PREFIX,
 128 //                        SOAPConstants.URI_NS_SOAP_ENVELOPE);
 129 //                    fault.setFaultCode(
 130 //                        NameImpl.create(
 131 //                            "Server",
 132 //                            NameImpl.SOAP_ENVELOPE_PREFIX,
 133 //                            SOAPConstants.URI_NS_SOAP_ENVELOPE));
 134 //                    fault.setFaultString(
 135 //                        "Fault string, and possibly fault code, not set");
 136 //                } catch (SOAPException e) {
 137 //                }
 138                 return fault;
 139             }
 140 
 141         }
 142         if (localName.equalsIgnoreCase("Detail")) {
 143             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
 144                 return new Detail1_1Impl(ownerDocument, prefix);
 145             } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) {
 146                 return new Detail1_2Impl(ownerDocument, prefix);
 147             }
 148         }
 149         if (localName.equalsIgnoreCase("faultcode")
 150             || localName.equalsIgnoreCase("faultstring")
 151             || localName.equalsIgnoreCase("faultactor")) {
 152             // SOAP 1.2 does not have fault(code/string/actor)
 153             // So there is no else case required
 154             if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) {
 155                 return new FaultElement1_1Impl(ownerDocument,
 156                                                localName,
 157                                                prefix);
 158             }
 159         }
 160 
 161         return null;
 162     }
 163 
 164     protected static void invalidCreate(String msg) {
 165         throw new TreeException(msg);
 166     }
 167 }