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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/message/source/SourceUtils.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,253 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.message.source;
    1.30 +
    1.31 +import com.sun.xml.internal.ws.message.RootElementSniffer;
    1.32 +import com.sun.xml.internal.ws.streaming.SourceReaderFactory;
    1.33 +import com.sun.xml.internal.ws.util.xml.XmlUtil;
    1.34 +import org.w3c.dom.Document;
    1.35 +import org.w3c.dom.Node;
    1.36 +
    1.37 +import javax.xml.namespace.QName;
    1.38 +import javax.xml.stream.XMLStreamConstants;
    1.39 +import javax.xml.stream.XMLStreamException;
    1.40 +import javax.xml.stream.XMLStreamReader;
    1.41 +import javax.xml.stream.XMLStreamWriter;
    1.42 +import javax.xml.transform.Source;
    1.43 +import javax.xml.transform.Transformer;
    1.44 +import javax.xml.transform.TransformerConfigurationException;
    1.45 +import javax.xml.transform.TransformerException;
    1.46 +import javax.xml.transform.dom.DOMSource;
    1.47 +import javax.xml.transform.sax.SAXResult;
    1.48 +import javax.xml.transform.sax.SAXSource;
    1.49 +import javax.xml.transform.stream.StreamSource;
    1.50 +import javax.xml.ws.WebServiceException;
    1.51 +
    1.52 +/**
    1.53 + *
    1.54 + * @author Vivek Pandey
    1.55 + */
    1.56 +final class SourceUtils {
    1.57 +
    1.58 +    int srcType;
    1.59 +
    1.60 +    private final int domSource = 1;
    1.61 +    private final int streamSource = 2;
    1.62 +    private final int saxSource=4;
    1.63 +
    1.64 +    public SourceUtils(Source src) {
    1.65 +        if(src instanceof StreamSource){
    1.66 +            srcType = streamSource;
    1.67 +        }else if(src instanceof DOMSource){
    1.68 +            srcType = domSource;
    1.69 +        }else if(src instanceof SAXSource){
    1.70 +            srcType = saxSource;
    1.71 +        }
    1.72 +    }
    1.73 +
    1.74 +    public boolean isDOMSource(){
    1.75 +        return (srcType&domSource) == domSource;
    1.76 +    }
    1.77 +
    1.78 +    public boolean isStreamSource(){
    1.79 +        return (srcType&streamSource) == streamSource;
    1.80 +    }
    1.81 +
    1.82 +    public boolean isSaxSource(){
    1.83 +        return (srcType&saxSource) == saxSource;
    1.84 +    }
    1.85 +
    1.86 +    /**
    1.87 +     * This would peek into the Source (DOMSource and SAXSource) for the localName and NamespaceURI
    1.88 +     * of the top-level element.
    1.89 +     * @param src
    1.90 +     * @return QName of the payload
    1.91 +     */
    1.92 +    public QName sniff(Source src) {
    1.93 +        return sniff(src, new RootElementSniffer());
    1.94 +    }
    1.95 +
    1.96 +    public QName sniff(Source src, RootElementSniffer sniffer){
    1.97 +        String localName = null;
    1.98 +        String namespaceUri = null;
    1.99 +
   1.100 +        if(isDOMSource()){
   1.101 +            DOMSource domSource = (DOMSource)src;
   1.102 +            Node n = domSource.getNode();
   1.103 +            if(n.getNodeType()== Node.DOCUMENT_NODE) {
   1.104 +                n = ((Document)n).getDocumentElement();
   1.105 +            }
   1.106 +            localName = n.getLocalName();
   1.107 +            namespaceUri = n.getNamespaceURI();
   1.108 +        }else if(isSaxSource()){
   1.109 +            SAXSource saxSrc = (SAXSource)src;
   1.110 +            SAXResult saxResult = new SAXResult(sniffer);
   1.111 +            try {
   1.112 +                Transformer tr = XmlUtil.newTransformer();
   1.113 +                tr.transform(saxSrc, saxResult);
   1.114 +            } catch (TransformerConfigurationException e) {
   1.115 +                throw new WebServiceException(e);
   1.116 +            } catch (TransformerException e) {
   1.117 +                // if it's due to aborting the processing after the first element,
   1.118 +                // we can safely ignore this exception.
   1.119 +                //
   1.120 +                // if it's due to error in the object, the same error will be reported
   1.121 +                // when the readHeader() method is used, so we don't have to report
   1.122 +                // an error right now.
   1.123 +                localName = sniffer.getLocalName();
   1.124 +                namespaceUri = sniffer.getNsUri();
   1.125 +            }
   1.126 +        }
   1.127 +        return new QName(namespaceUri, localName);
   1.128 +    }
   1.129 +
   1.130 +    public static void serializeSource(Source src, XMLStreamWriter writer) throws XMLStreamException {
   1.131 +        XMLStreamReader reader = SourceReaderFactory.createSourceReader(src, true);
   1.132 +        int state;
   1.133 +        do {
   1.134 +            state = reader.next();
   1.135 +            switch (state) {
   1.136 +                case XMLStreamConstants.START_ELEMENT:
   1.137 +                    /*
   1.138 +                     * TODO: Is this necessary, shouldn't zephyr return "" instead of
   1.139 +                     * null for getNamespaceURI() and getPrefix()?
   1.140 +                     */
   1.141 +                    String uri = reader.getNamespaceURI();
   1.142 +                    String prefix = reader.getPrefix();
   1.143 +                    String localName = reader.getLocalName();
   1.144 +
   1.145 +                    if (prefix == null) {
   1.146 +                        if (uri == null) {
   1.147 +                            writer.writeStartElement(localName);
   1.148 +                        } else {
   1.149 +                            writer.writeStartElement(uri, localName);
   1.150 +                        }
   1.151 +                    } else {
   1.152 +                        assert uri != null;
   1.153 +
   1.154 +                        if(prefix.length() > 0){
   1.155 +                            /**
   1.156 +                             * Before we write the
   1.157 +                             */
   1.158 +                            String writerURI = null;
   1.159 +                            if (writer.getNamespaceContext() != null)
   1.160 +                                writerURI = writer.getNamespaceContext().getNamespaceURI(prefix);
   1.161 +                            String writerPrefix = writer.getPrefix(uri);
   1.162 +                            if(declarePrefix(prefix, uri, writerPrefix, writerURI)){
   1.163 +                                writer.writeStartElement(prefix, localName, uri);
   1.164 +                                writer.setPrefix(prefix, uri != null ? uri : "");
   1.165 +                                writer.writeNamespace(prefix, uri);
   1.166 +                            }else{
   1.167 +                                writer.writeStartElement(prefix, localName, uri);
   1.168 +                            }
   1.169 +                        }else{
   1.170 +                            writer.writeStartElement(prefix, localName, uri);
   1.171 +                        }
   1.172 +                    }
   1.173 +
   1.174 +                    int n = reader.getNamespaceCount();
   1.175 +                    // Write namespace declarations
   1.176 +                    for (int i = 0; i < n; i++) {
   1.177 +                        String nsPrefix = reader.getNamespacePrefix(i);
   1.178 +                        if (nsPrefix == null) nsPrefix = "";
   1.179 +                        // StAX returns null for default ns
   1.180 +                        String writerURI = null;
   1.181 +                        if (writer.getNamespaceContext() != null)
   1.182 +                            writerURI = writer.getNamespaceContext().getNamespaceURI(nsPrefix);
   1.183 +
   1.184 +                        // Zephyr: Why is this returning null?
   1.185 +                        // Compare nsPrefix with prefix because of [1] (above)
   1.186 +                        String readerURI = reader.getNamespaceURI(i);
   1.187 +
   1.188 +                        /**
   1.189 +                         * write the namespace in 3 conditions
   1.190 +                         *  - when the namespace URI is not bound to the prefix in writer(writerURI == 0)
   1.191 +                         *  - when the readerPrefix and writerPrefix are ""
   1.192 +                         *  - when readerPrefix and writerPrefix are not equal and the URI bound to them
   1.193 +                         *    are different
   1.194 +                         */
   1.195 +                        if (writerURI == null || ((nsPrefix.length() == 0) || (prefix.length() == 0)) ||
   1.196 +                                (!nsPrefix.equals(prefix) && !writerURI.equals(readerURI))) {
   1.197 +                            writer.setPrefix(nsPrefix, readerURI != null ? readerURI : "");
   1.198 +                            writer.writeNamespace(nsPrefix, readerURI != null ? readerURI : "");
   1.199 +                        }
   1.200 +                    }
   1.201 +
   1.202 +                    // Write attributes
   1.203 +                    n = reader.getAttributeCount();
   1.204 +                    for (int i = 0; i < n; i++) {
   1.205 +                        String attrPrefix = reader.getAttributePrefix(i);
   1.206 +                        String attrURI = reader.getAttributeNamespace(i);
   1.207 +
   1.208 +                        writer.writeAttribute(attrPrefix != null ? attrPrefix : "",
   1.209 +                            attrURI != null ? attrURI : "",
   1.210 +                            reader.getAttributeLocalName(i),
   1.211 +                            reader.getAttributeValue(i));
   1.212 +                        // if the attribute prefix is undeclared in current writer scope then declare it
   1.213 +                        setUndeclaredPrefix(attrPrefix, attrURI, writer);
   1.214 +                    }
   1.215 +                    break;
   1.216 +                case XMLStreamConstants.END_ELEMENT:
   1.217 +                    writer.writeEndElement();
   1.218 +                    break;
   1.219 +                case XMLStreamConstants.CHARACTERS:
   1.220 +                    writer.writeCharacters(reader.getText());
   1.221 +            }
   1.222 +        } while (state != XMLStreamConstants.END_DOCUMENT);
   1.223 +        reader.close();
   1.224 +    }
   1.225 +
   1.226 +    /**
   1.227 +     * sets undeclared prefixes on the writer
   1.228 +     * @param prefix
   1.229 +     * @param writer
   1.230 +     * @throws XMLStreamException
   1.231 +     */
   1.232 +    private static void setUndeclaredPrefix(String prefix, String readerURI, XMLStreamWriter writer) throws XMLStreamException {
   1.233 +        String writerURI = null;
   1.234 +        if (writer.getNamespaceContext() != null)
   1.235 +            writerURI = writer.getNamespaceContext().getNamespaceURI(prefix);
   1.236 +
   1.237 +        if (writerURI == null) {
   1.238 +            writer.setPrefix(prefix, readerURI != null ? readerURI : "");
   1.239 +            writer.writeNamespace(prefix, readerURI != null ? readerURI : "");
   1.240 +        }
   1.241 +    }
   1.242 +
   1.243 +    /**
   1.244 +     * check if we need to declare
   1.245 +     * @param rPrefix
   1.246 +     * @param rUri
   1.247 +     * @param wPrefix
   1.248 +     * @param wUri
   1.249 +     */
   1.250 +    private static boolean declarePrefix(String rPrefix, String rUri, String wPrefix, String wUri){
   1.251 +        if (wUri == null ||((wPrefix != null) && !rPrefix.equals(wPrefix))||
   1.252 +                (rUri != null && !wUri.equals(rUri)))
   1.253 +            return true;
   1.254 +        return false;
   1.255 +    }
   1.256 +}

mercurial