src/share/jaxws_classes/com/sun/xml/internal/ws/message/source/SourceUtils.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

     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  */
    26 package com.sun.xml.internal.ws.message.source;
    28 import com.sun.xml.internal.ws.message.RootElementSniffer;
    29 import com.sun.xml.internal.ws.streaming.SourceReaderFactory;
    30 import com.sun.xml.internal.ws.util.xml.XmlUtil;
    31 import org.w3c.dom.Document;
    32 import org.w3c.dom.Node;
    34 import javax.xml.namespace.QName;
    35 import javax.xml.stream.XMLStreamConstants;
    36 import javax.xml.stream.XMLStreamException;
    37 import javax.xml.stream.XMLStreamReader;
    38 import javax.xml.stream.XMLStreamWriter;
    39 import javax.xml.transform.Source;
    40 import javax.xml.transform.Transformer;
    41 import javax.xml.transform.TransformerConfigurationException;
    42 import javax.xml.transform.TransformerException;
    43 import javax.xml.transform.dom.DOMSource;
    44 import javax.xml.transform.sax.SAXResult;
    45 import javax.xml.transform.sax.SAXSource;
    46 import javax.xml.transform.stream.StreamSource;
    47 import javax.xml.ws.WebServiceException;
    49 /**
    50  *
    51  * @author Vivek Pandey
    52  */
    53 final class SourceUtils {
    55     int srcType;
    57     private final int domSource = 1;
    58     private final int streamSource = 2;
    59     private final int saxSource=4;
    61     public SourceUtils(Source src) {
    62         if(src instanceof StreamSource){
    63             srcType = streamSource;
    64         }else if(src instanceof DOMSource){
    65             srcType = domSource;
    66         }else if(src instanceof SAXSource){
    67             srcType = saxSource;
    68         }
    69     }
    71     public boolean isDOMSource(){
    72         return (srcType&domSource) == domSource;
    73     }
    75     public boolean isStreamSource(){
    76         return (srcType&streamSource) == streamSource;
    77     }
    79     public boolean isSaxSource(){
    80         return (srcType&saxSource) == saxSource;
    81     }
    83     /**
    84      * This would peek into the Source (DOMSource and SAXSource) for the localName and NamespaceURI
    85      * of the top-level element.
    86      * @param src
    87      * @return QName of the payload
    88      */
    89     public QName sniff(Source src) {
    90         return sniff(src, new RootElementSniffer());
    91     }
    93     public QName sniff(Source src, RootElementSniffer sniffer){
    94         String localName = null;
    95         String namespaceUri = null;
    97         if(isDOMSource()){
    98             DOMSource domSource = (DOMSource)src;
    99             Node n = domSource.getNode();
   100             if(n.getNodeType()== Node.DOCUMENT_NODE) {
   101                 n = ((Document)n).getDocumentElement();
   102             }
   103             localName = n.getLocalName();
   104             namespaceUri = n.getNamespaceURI();
   105         }else if(isSaxSource()){
   106             SAXSource saxSrc = (SAXSource)src;
   107             SAXResult saxResult = new SAXResult(sniffer);
   108             try {
   109                 Transformer tr = XmlUtil.newTransformer();
   110                 tr.transform(saxSrc, saxResult);
   111             } catch (TransformerConfigurationException e) {
   112                 throw new WebServiceException(e);
   113             } catch (TransformerException e) {
   114                 // if it's due to aborting the processing after the first element,
   115                 // we can safely ignore this exception.
   116                 //
   117                 // if it's due to error in the object, the same error will be reported
   118                 // when the readHeader() method is used, so we don't have to report
   119                 // an error right now.
   120                 localName = sniffer.getLocalName();
   121                 namespaceUri = sniffer.getNsUri();
   122             }
   123         }
   124         return new QName(namespaceUri, localName);
   125     }
   127     public static void serializeSource(Source src, XMLStreamWriter writer) throws XMLStreamException {
   128         XMLStreamReader reader = SourceReaderFactory.createSourceReader(src, true);
   129         int state;
   130         do {
   131             state = reader.next();
   132             switch (state) {
   133                 case XMLStreamConstants.START_ELEMENT:
   134                     /*
   135                      * TODO: Is this necessary, shouldn't zephyr return "" instead of
   136                      * null for getNamespaceURI() and getPrefix()?
   137                      */
   138                     String uri = reader.getNamespaceURI();
   139                     String prefix = reader.getPrefix();
   140                     String localName = reader.getLocalName();
   142                     if (prefix == null) {
   143                         if (uri == null) {
   144                             writer.writeStartElement(localName);
   145                         } else {
   146                             writer.writeStartElement(uri, localName);
   147                         }
   148                     } else {
   149                         assert uri != null;
   151                         if(prefix.length() > 0){
   152                             /**
   153                              * Before we write the
   154                              */
   155                             String writerURI = null;
   156                             if (writer.getNamespaceContext() != null)
   157                                 writerURI = writer.getNamespaceContext().getNamespaceURI(prefix);
   158                             String writerPrefix = writer.getPrefix(uri);
   159                             if(declarePrefix(prefix, uri, writerPrefix, writerURI)){
   160                                 writer.writeStartElement(prefix, localName, uri);
   161                                 writer.setPrefix(prefix, uri != null ? uri : "");
   162                                 writer.writeNamespace(prefix, uri);
   163                             }else{
   164                                 writer.writeStartElement(prefix, localName, uri);
   165                             }
   166                         }else{
   167                             writer.writeStartElement(prefix, localName, uri);
   168                         }
   169                     }
   171                     int n = reader.getNamespaceCount();
   172                     // Write namespace declarations
   173                     for (int i = 0; i < n; i++) {
   174                         String nsPrefix = reader.getNamespacePrefix(i);
   175                         if (nsPrefix == null) nsPrefix = "";
   176                         // StAX returns null for default ns
   177                         String writerURI = null;
   178                         if (writer.getNamespaceContext() != null)
   179                             writerURI = writer.getNamespaceContext().getNamespaceURI(nsPrefix);
   181                         // Zephyr: Why is this returning null?
   182                         // Compare nsPrefix with prefix because of [1] (above)
   183                         String readerURI = reader.getNamespaceURI(i);
   185                         /**
   186                          * write the namespace in 3 conditions
   187                          *  - when the namespace URI is not bound to the prefix in writer(writerURI == 0)
   188                          *  - when the readerPrefix and writerPrefix are ""
   189                          *  - when readerPrefix and writerPrefix are not equal and the URI bound to them
   190                          *    are different
   191                          */
   192                         if (writerURI == null || ((nsPrefix.length() == 0) || (prefix.length() == 0)) ||
   193                                 (!nsPrefix.equals(prefix) && !writerURI.equals(readerURI))) {
   194                             writer.setPrefix(nsPrefix, readerURI != null ? readerURI : "");
   195                             writer.writeNamespace(nsPrefix, readerURI != null ? readerURI : "");
   196                         }
   197                     }
   199                     // Write attributes
   200                     n = reader.getAttributeCount();
   201                     for (int i = 0; i < n; i++) {
   202                         String attrPrefix = reader.getAttributePrefix(i);
   203                         String attrURI = reader.getAttributeNamespace(i);
   205                         writer.writeAttribute(attrPrefix != null ? attrPrefix : "",
   206                             attrURI != null ? attrURI : "",
   207                             reader.getAttributeLocalName(i),
   208                             reader.getAttributeValue(i));
   209                         // if the attribute prefix is undeclared in current writer scope then declare it
   210                         setUndeclaredPrefix(attrPrefix, attrURI, writer);
   211                     }
   212                     break;
   213                 case XMLStreamConstants.END_ELEMENT:
   214                     writer.writeEndElement();
   215                     break;
   216                 case XMLStreamConstants.CHARACTERS:
   217                     writer.writeCharacters(reader.getText());
   218             }
   219         } while (state != XMLStreamConstants.END_DOCUMENT);
   220         reader.close();
   221     }
   223     /**
   224      * sets undeclared prefixes on the writer
   225      * @param prefix
   226      * @param writer
   227      * @throws XMLStreamException
   228      */
   229     private static void setUndeclaredPrefix(String prefix, String readerURI, XMLStreamWriter writer) throws XMLStreamException {
   230         String writerURI = null;
   231         if (writer.getNamespaceContext() != null)
   232             writerURI = writer.getNamespaceContext().getNamespaceURI(prefix);
   234         if (writerURI == null) {
   235             writer.setPrefix(prefix, readerURI != null ? readerURI : "");
   236             writer.writeNamespace(prefix, readerURI != null ? readerURI : "");
   237         }
   238     }
   240     /**
   241      * check if we need to declare
   242      * @param rPrefix
   243      * @param rUri
   244      * @param wPrefix
   245      * @param wUri
   246      */
   247     private static boolean declarePrefix(String rPrefix, String rUri, String wPrefix, String wUri){
   248         if (wUri == null ||((wPrefix != null) && !rPrefix.equals(wPrefix))||
   249                 (rUri != null && !wUri.equals(rUri)))
   250             return true;
   251         return false;
   252     }
   253 }

mercurial