jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/messaging/saaj/soap/SOAPDocumentImpl.java
changeset 43852 93a527059d8a
parent 28326 2b9860c0d68a
child 45678 65fdff10664d
equal deleted inserted replaced
43752:3c68ef249093 43852:93a527059d8a
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    27 *
    27 *
    28 * @author SAAJ RI Development Team
    28 * @author SAAJ RI Development Team
    29 */
    29 */
    30 package com.sun.xml.internal.messaging.saaj.soap;
    30 package com.sun.xml.internal.messaging.saaj.soap;
    31 
    31 
    32 import java.util.logging.Logger;
    32 import com.sun.xml.internal.messaging.saaj.soap.impl.CDATAImpl;
    33 
    33 import com.sun.xml.internal.messaging.saaj.soap.impl.ElementFactory;
    34 import com.sun.org.apache.xerces.internal.dom.DocumentImpl;
    34 import com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl;
    35 import org.w3c.dom.*;
    35 import com.sun.xml.internal.messaging.saaj.soap.impl.SOAPCommentImpl;
    36 
    36 import com.sun.xml.internal.messaging.saaj.soap.impl.SOAPTextImpl;
    37 import com.sun.xml.internal.messaging.saaj.soap.impl.*;
       
    38 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
    37 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
    39 import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants;
    38 import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants;
    40 
    39 import com.sun.xml.internal.messaging.saaj.util.SAAJUtil;
    41 public class SOAPDocumentImpl extends DocumentImpl implements SOAPDocument {
    40 import org.w3c.dom.Attr;
       
    41 import org.w3c.dom.CDATASection;
       
    42 import org.w3c.dom.Comment;
       
    43 import org.w3c.dom.DOMConfiguration;
       
    44 import org.w3c.dom.DOMException;
       
    45 import org.w3c.dom.DOMImplementation;
       
    46 import org.w3c.dom.Document;
       
    47 import org.w3c.dom.DocumentFragment;
       
    48 import org.w3c.dom.DocumentType;
       
    49 import org.w3c.dom.Element;
       
    50 import org.w3c.dom.EntityReference;
       
    51 import org.w3c.dom.NamedNodeMap;
       
    52 import org.w3c.dom.Node;
       
    53 import org.w3c.dom.NodeList;
       
    54 import org.w3c.dom.ProcessingInstruction;
       
    55 import org.w3c.dom.UserDataHandler;
       
    56 
       
    57 import javax.xml.parsers.DocumentBuilder;
       
    58 import javax.xml.parsers.DocumentBuilderFactory;
       
    59 import javax.xml.parsers.ParserConfigurationException;
       
    60 import javax.xml.soap.SOAPElement;
       
    61 import javax.xml.soap.SOAPException;
       
    62 import java.text.MessageFormat;
       
    63 import java.util.HashMap;
       
    64 import java.util.Map;
       
    65 import java.util.logging.Logger;
       
    66 
       
    67 public class SOAPDocumentImpl implements SOAPDocument, javax.xml.soap.Node, Document {
    42 
    68 
    43     private static final String XMLNS = "xmlns".intern();
    69     private static final String XMLNS = "xmlns".intern();
    44     protected static final Logger log =
    70     protected static final Logger log =
    45         Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
    71         Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
    46                          "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
    72                          "com.sun.xml.internal.messaging.saaj.soap.LocalStrings");
    47 
    73 
    48     SOAPPartImpl enclosingSOAPPart;
    74     SOAPPartImpl enclosingSOAPPart;
    49 
    75 
       
    76     private Document document;
       
    77 
       
    78     private Map<Node, javax.xml.soap.Node> domToSoap = new HashMap<>();
       
    79 
    50     public SOAPDocumentImpl(SOAPPartImpl enclosingDocument) {
    80     public SOAPDocumentImpl(SOAPPartImpl enclosingDocument) {
       
    81         document = createDocument();
    51         this.enclosingSOAPPart = enclosingDocument;
    82         this.enclosingSOAPPart = enclosingDocument;
       
    83         register(this);
       
    84     }
       
    85 
       
    86     private Document createDocument() {
       
    87         DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl", SAAJUtil.getSystemClassLoader());
       
    88         try {
       
    89             final DocumentBuilder documentBuilder = docFactory.newDocumentBuilder();
       
    90             return documentBuilder.newDocument();
       
    91         } catch (ParserConfigurationException e) {
       
    92             throw new RuntimeException("Error creating xml document", e);
       
    93         }
    52     }
    94     }
    53 
    95 
    54     //    public SOAPDocumentImpl(boolean grammarAccess) {
    96     //    public SOAPDocumentImpl(boolean grammarAccess) {
    55     //        super(grammarAccess);
    97     //        super(grammarAccess);
    56     //    }
    98     //    }
    79         // SOAP means no DTD, No DTD means no doctype (SOAP 1.2 only?)
   121         // SOAP means no DTD, No DTD means no doctype (SOAP 1.2 only?)
    80         return null;
   122         return null;
    81     }
   123     }
    82 
   124 
    83     public DOMImplementation getImplementation() {
   125     public DOMImplementation getImplementation() {
    84         return super.getImplementation();
   126         return document.getImplementation();
    85     }
   127     }
    86 
   128 
    87     public Element getDocumentElement() {
   129     public Element getDocumentElement() {
    88         // This had better be an Envelope!
   130         // This had better be an Envelope!
    89         getSOAPPart().doGetDocumentElement();
   131         getSOAPPart().doGetDocumentElement();
    90         return doGetDocumentElement();
   132         return doGetDocumentElement();
    91     }
   133     }
    92 
   134 
    93     protected Element doGetDocumentElement() {
   135     protected Element doGetDocumentElement() {
    94         return super.getDocumentElement();
   136         return document.getDocumentElement();
    95     }
   137     }
    96 
   138 
    97     public Element createElement(String tagName) throws DOMException {
   139     public Element createElement(String tagName) throws DOMException {
    98         return ElementFactory.createElement(
   140         return ElementFactory.createElement(
    99             this,
   141             this,
   101             NameImpl.getPrefixFromTagName(tagName),
   143             NameImpl.getPrefixFromTagName(tagName),
   102             null);
   144             null);
   103     }
   145     }
   104 
   146 
   105     public DocumentFragment createDocumentFragment() {
   147     public DocumentFragment createDocumentFragment() {
   106         return new SOAPDocumentFragment(this);
   148         return document.createDocumentFragment();
   107     }
   149     }
   108 
   150 
   109     public org.w3c.dom.Text createTextNode(String data) {
   151     public org.w3c.dom.Text createTextNode(String data) {
   110         return new SOAPTextImpl(this, data);
   152         return new SOAPTextImpl(this, data);
   111     }
   153     }
   137                 nsUri = ElementImpl.XMLNS_URI;
   179                 nsUri = ElementImpl.XMLNS_URI;
   138                 return createAttributeNS(nsUri, name);
   180                 return createAttributeNS(nsUri, name);
   139             }
   181             }
   140         }
   182         }
   141 
   183 
   142         return super.createAttribute(name);
   184         return document.createAttribute(name);
   143     }
   185     }
   144 
   186 
   145     public EntityReference createEntityReference(String name)
   187     public EntityReference createEntityReference(String name)
   146         throws DOMException {
   188         throws DOMException {
   147             log.severe("SAAJ0543.soap.entity.refs.not.allowed.in.docs");
   189             log.severe("SAAJ0543.soap.entity.refs.not.allowed.in.docs");
   148             throw new UnsupportedOperationException("Entity References are not allowed in SOAP documents");
   190             throw new UnsupportedOperationException("Entity References are not allowed in SOAP documents");
   149     }
   191     }
   150 
   192 
   151     public NodeList getElementsByTagName(String tagname) {
   193     public NodeList getElementsByTagName(String tagname) {
   152         return super.getElementsByTagName(tagname);
   194         return document.getElementsByTagName(tagname);
   153     }
   195     }
   154 
   196 
   155     public org.w3c.dom.Node importNode(Node importedNode, boolean deep)
   197     public org.w3c.dom.Node importNode(Node importedNode, boolean deep)
   156         throws DOMException {
   198         throws DOMException {
   157         return super.importNode(importedNode, deep);
   199         final Node node = document.importNode(getDomNode(importedNode), deep);
       
   200         return node instanceof Element ?
       
   201             ElementFactory.createElement(this, (Element) node)
       
   202                 : node;
   158     }
   203     }
   159 
   204 
   160     public Element createElementNS(String namespaceURI, String qualifiedName)
   205     public Element createElementNS(String namespaceURI, String qualifiedName)
   161         throws DOMException {
   206         throws DOMException {
   162         return ElementFactory.createElement(
   207         return ElementFactory.createElement(
   166             namespaceURI);
   211             namespaceURI);
   167     }
   212     }
   168 
   213 
   169     public Attr createAttributeNS(String namespaceURI, String qualifiedName)
   214     public Attr createAttributeNS(String namespaceURI, String qualifiedName)
   170         throws DOMException {
   215         throws DOMException {
   171         return super.createAttributeNS(namespaceURI, qualifiedName);
   216         return document.createAttributeNS(namespaceURI, qualifiedName);
   172     }
   217     }
   173 
   218 
   174     public NodeList getElementsByTagNameNS(
   219     public NodeList getElementsByTagNameNS(
   175         String namespaceURI,
   220         String namespaceURI,
   176         String localName) {
   221         String localName) {
   177         return super.getElementsByTagNameNS(namespaceURI, localName);
   222         return document.getElementsByTagNameNS(namespaceURI, localName);
   178     }
   223     }
   179 
   224 
   180     public Element getElementById(String elementId) {
   225     public Element getElementById(String elementId) {
   181         return super.getElementById(elementId);
   226         return document.getElementById(elementId);
   182     }
   227     }
   183 
   228 
       
   229     @Override
       
   230     public String getInputEncoding() {
       
   231         return document.getInputEncoding();
       
   232     }
       
   233 
       
   234     @Override
       
   235     public String getXmlEncoding() {
       
   236         return document.getXmlEncoding();
       
   237     }
       
   238 
       
   239     @Override
       
   240     public boolean getXmlStandalone() {
       
   241         return document.getXmlStandalone();
       
   242     }
       
   243 
       
   244     @Override
       
   245     public void setXmlStandalone(boolean xmlStandalone) throws DOMException {
       
   246         document.setXmlStandalone(xmlStandalone);
       
   247     }
       
   248 
       
   249     @Override
       
   250     public String getXmlVersion() {
       
   251         return document.getXmlVersion();
       
   252     }
       
   253 
       
   254     @Override
       
   255     public void setXmlVersion(String xmlVersion) throws DOMException {
       
   256         document.setXmlVersion(xmlVersion);
       
   257     }
       
   258 
       
   259     @Override
       
   260     public boolean getStrictErrorChecking() {
       
   261         return document.getStrictErrorChecking();
       
   262     }
       
   263 
       
   264     @Override
       
   265     public void setStrictErrorChecking(boolean strictErrorChecking) {
       
   266         document.setStrictErrorChecking(strictErrorChecking);
       
   267     }
       
   268 
       
   269     @Override
       
   270     public String getDocumentURI() {
       
   271         return document.getDocumentURI();
       
   272     }
       
   273 
       
   274     @Override
       
   275     public void setDocumentURI(String documentURI) {
       
   276         document.setDocumentURI(documentURI);
       
   277     }
       
   278 
       
   279     @Override
       
   280     public Node adoptNode(Node source) throws DOMException {
       
   281         return document.adoptNode(source);
       
   282     }
       
   283 
       
   284     @Override
       
   285     public DOMConfiguration getDomConfig() {
       
   286         return document.getDomConfig();
       
   287     }
       
   288 
       
   289     @Override
       
   290     public void normalizeDocument() {
       
   291         document.normalizeDocument();
       
   292     }
       
   293 
       
   294     @Override
       
   295     public Node renameNode(Node n, String namespaceURI, String qualifiedName) throws DOMException {
       
   296         return document.renameNode(n, namespaceURI, qualifiedName);
       
   297     }
       
   298 
       
   299     @Override
       
   300     public String getNodeName() {
       
   301         return document.getNodeName();
       
   302     }
       
   303 
       
   304     @Override
       
   305     public String getNodeValue() throws DOMException {
       
   306         return document.getNodeValue();
       
   307     }
       
   308 
       
   309     @Override
       
   310     public void setNodeValue(String nodeValue) throws DOMException {
       
   311         document.setNodeValue(nodeValue);
       
   312     }
       
   313 
       
   314     @Override
       
   315     public short getNodeType() {
       
   316         return document.getNodeType();
       
   317     }
       
   318 
       
   319     @Override
       
   320     public Node getParentNode() {
       
   321         return document.getParentNode();
       
   322     }
       
   323 
       
   324     @Override
       
   325     public NodeList getChildNodes() {
       
   326         return document.getChildNodes();
       
   327     }
       
   328 
       
   329     @Override
       
   330     public Node getFirstChild() {
       
   331         return document.getFirstChild();
       
   332     }
       
   333 
       
   334     @Override
       
   335     public Node getLastChild() {
       
   336         return document.getLastChild();
       
   337     }
       
   338 
       
   339     @Override
       
   340     public Node getPreviousSibling() {
       
   341         return document.getPreviousSibling();
       
   342     }
       
   343 
       
   344     @Override
       
   345     public Node getNextSibling() {
       
   346         return document.getNextSibling();
       
   347     }
       
   348 
       
   349     @Override
       
   350     public NamedNodeMap getAttributes() {
       
   351         return document.getAttributes();
       
   352     }
       
   353 
       
   354     @Override
       
   355     public Document getOwnerDocument() {
       
   356         return document.getOwnerDocument();
       
   357     }
       
   358 
       
   359     @Override
       
   360     public Node insertBefore(Node newChild, Node refChild) throws DOMException {
       
   361         return document.insertBefore(getDomNode(newChild), getDomNode(refChild));
       
   362     }
       
   363 
       
   364     @Override
       
   365     public Node replaceChild(Node newChild, Node oldChild) throws DOMException {
       
   366         return document.replaceChild(getDomNode(newChild), getDomNode(oldChild));
       
   367     }
       
   368 
       
   369     @Override
       
   370     public Node removeChild(Node oldChild) throws DOMException {
       
   371         return document.removeChild(getDomNode(oldChild));
       
   372     }
       
   373 
       
   374     @Override
       
   375     public Node appendChild(Node newChild) throws DOMException {
       
   376         return document.appendChild(getDomNode(newChild));
       
   377     }
       
   378 
       
   379     @Override
       
   380     public boolean hasChildNodes() {
       
   381         return document.hasChildNodes();
       
   382     }
       
   383 
       
   384     @Override
   184     public Node cloneNode(boolean deep) {
   385     public Node cloneNode(boolean deep) {
   185         SOAPPartImpl newSoapPart = getSOAPPart().doCloneNode();
   386         return document.cloneNode(deep);
   186         super.cloneNode(newSoapPart.getDocument(), deep);
   387     }
   187         return newSoapPart;
   388 
   188     }
   389     @Override
   189 
   390     public void normalize() {
   190     public void cloneNode(SOAPDocumentImpl newdoc, boolean deep) {
   391         document.normalize();
   191         super.cloneNode(newdoc, deep);
   392     }
       
   393 
       
   394     @Override
       
   395     public boolean isSupported(String feature, String version) {
       
   396         return document.isSupported(feature, version);
       
   397     }
       
   398 
       
   399     @Override
       
   400     public String getNamespaceURI() {
       
   401         return document.getNamespaceURI();
       
   402     }
       
   403 
       
   404     @Override
       
   405     public String getPrefix() {
       
   406         return document.getPrefix();
       
   407     }
       
   408 
       
   409     @Override
       
   410     public void setPrefix(String prefix) throws DOMException {
       
   411         document.setPrefix(prefix);
       
   412     }
       
   413 
       
   414     @Override
       
   415     public String getLocalName() {
       
   416         return document.getLocalName();
       
   417     }
       
   418 
       
   419     @Override
       
   420     public boolean hasAttributes() {
       
   421         return document.hasAttributes();
       
   422     }
       
   423 
       
   424     @Override
       
   425     public String getBaseURI() {
       
   426         return document.getBaseURI();
       
   427     }
       
   428 
       
   429     @Override
       
   430     public short compareDocumentPosition(Node other) throws DOMException {
       
   431         return document.compareDocumentPosition(other);
       
   432     }
       
   433 
       
   434     @Override
       
   435     public String getTextContent() throws DOMException {
       
   436         return document.getTextContent();
       
   437     }
       
   438 
       
   439     @Override
       
   440     public void setTextContent(String textContent) throws DOMException {
       
   441         document.setTextContent(textContent);
       
   442     }
       
   443 
       
   444     @Override
       
   445     public boolean isSameNode(Node other) {
       
   446         return document.isSameNode(other);
       
   447     }
       
   448 
       
   449     @Override
       
   450     public String lookupPrefix(String namespaceURI) {
       
   451         return document.lookupPrefix(namespaceURI);
       
   452     }
       
   453 
       
   454     @Override
       
   455     public boolean isDefaultNamespace(String namespaceURI) {
       
   456         return document.isDefaultNamespace(namespaceURI);
       
   457     }
       
   458 
       
   459     @Override
       
   460     public String lookupNamespaceURI(String prefix) {
       
   461         return document.lookupNamespaceURI(prefix);
       
   462     }
       
   463 
       
   464     @Override
       
   465     public boolean isEqualNode(Node arg) {
       
   466         return document.isEqualNode(arg);
       
   467     }
       
   468 
       
   469     @Override
       
   470     public Object getFeature(String feature, String version) {
       
   471         return document.getFeature(feature, version);
       
   472     }
       
   473 
       
   474     @Override
       
   475     public Object setUserData(String key, Object data, UserDataHandler handler) {
       
   476         return document.setUserData(key, data, handler);
       
   477     }
       
   478 
       
   479     @Override
       
   480     public Object getUserData(String key) {
       
   481         return document.getUserData(key);
       
   482     }
       
   483 
       
   484     public Document getDomDocument() {
       
   485         return document;
       
   486     }
       
   487 
       
   488     /**
       
   489      * Insert a mapping information for {@link org.w3c.dom.Node} - {@link javax.xml.soap.Node}.
       
   490      *
       
   491      * In SAAJ, elements in DOM are expected to be interfaces of SAAJ, on the other hand in JDKs Xerces,
       
   492      * they are casted to internal impl classes. After removal of SAAJ dependency
       
   493      * to JDKs internal classes elements in DOM can never be both of them.
       
   494      *
       
   495      * @param node SAAJ wrapper node for w3c DOM node
       
   496      */
       
   497     public void register(javax.xml.soap.Node node) {
       
   498         final Node domElement = getDomNode(node);
       
   499         if (domToSoap.containsKey(domElement)) {
       
   500             throw new IllegalStateException("Element " + domElement.getNodeName()
       
   501                     + " is already registered");
       
   502         }
       
   503         domToSoap.put(domElement, node);
       
   504     }
       
   505 
       
   506     /**
       
   507      * Find a soap wrapper for w3c dom node.
       
   508      *
       
   509      * @param node w3c dom node nullable
       
   510      * @return soap wrapper for w3c dom node
       
   511      *
       
   512      * @throws
       
   513      */
       
   514     public javax.xml.soap.Node find(Node node) {
       
   515         return find(node, true);
       
   516     }
       
   517 
       
   518     private javax.xml.soap.Node find(Node node, boolean required) {
       
   519         if (node == null) {
       
   520             return null;
       
   521         }
       
   522         if (node instanceof javax.xml.soap.Node) {
       
   523             return (javax.xml.soap.Node) node;
       
   524         }
       
   525         final javax.xml.soap.Node found = domToSoap.get(node);
       
   526         if (found == null && required) {
       
   527             throw new IllegalArgumentException(MessageFormat.format("Cannot find SOAP wrapper for element {0}", node));
       
   528         }
       
   529         return found;
       
   530     }
       
   531 
       
   532     /**
       
   533      * If corresponding soap wrapper exists for w3c dom node it is returned,
       
   534      * if not passed dom element is returned.
       
   535      *
       
   536      * @param node w3c dom node
       
   537      * @return soap wrapper or passed w3c dom node if not found
       
   538      */
       
   539     public Node findIfPresent(Node node) {
       
   540         final javax.xml.soap.Node found = find(node, false);
       
   541         return found != null ? found : node;
       
   542     }
       
   543 
       
   544     /**
       
   545      * Extracts w3c dom node from corresponding soap wrapper.
       
   546      *
       
   547      * @param node soap or dom nullable
       
   548      * @return dom node
       
   549      */
       
   550     public Node getDomNode(Node node) {
       
   551         if (node instanceof SOAPDocumentImpl) {
       
   552             return ((SOAPDocumentImpl)node).getDomElement();
       
   553         } else if (node instanceof ElementImpl) {
       
   554             return ((ElementImpl) node).getDomElement();
       
   555         } else if (node instanceof SOAPTextImpl) {
       
   556             return ((SOAPTextImpl)node).getDomElement();
       
   557         } else if (node instanceof SOAPCommentImpl) {
       
   558             return ((SOAPCommentImpl)node).getDomElement();
       
   559         } else if (node instanceof CDATAImpl) {
       
   560             return ((CDATAImpl) node).getDomElement();
       
   561         }
       
   562         return node;
       
   563     }
       
   564 
       
   565     public Document getDomElement() {
       
   566         return document;
       
   567     }
       
   568 
       
   569     @Override
       
   570     public String getValue() {
       
   571         throw new UnsupportedOperationException();
       
   572     }
       
   573 
       
   574     @Override
       
   575     public void setValue(String value) {
       
   576         throw new UnsupportedOperationException();
       
   577     }
       
   578 
       
   579     @Override
       
   580     public void setParentElement(SOAPElement parent) throws SOAPException {
       
   581         throw new UnsupportedOperationException();
       
   582     }
       
   583 
       
   584     @Override
       
   585     public SOAPElement getParentElement() {
       
   586         throw new UnsupportedOperationException();
       
   587     }
       
   588 
       
   589     @Override
       
   590     public void detachNode() {
       
   591         throw new UnsupportedOperationException();
       
   592     }
       
   593 
       
   594     @Override
       
   595     public void recycleNode() {
       
   596         throw new UnsupportedOperationException();
   192     }
   597     }
   193 }
   598 }