Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.ws.message.source; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.xml.internal.ws.message.RootElementSniffer; |
aoqi@0 | 29 | import com.sun.xml.internal.ws.streaming.SourceReaderFactory; |
aoqi@0 | 30 | import com.sun.xml.internal.ws.util.xml.XmlUtil; |
aoqi@0 | 31 | import org.w3c.dom.Document; |
aoqi@0 | 32 | import org.w3c.dom.Node; |
aoqi@0 | 33 | |
aoqi@0 | 34 | import javax.xml.namespace.QName; |
aoqi@0 | 35 | import javax.xml.stream.XMLStreamConstants; |
aoqi@0 | 36 | import javax.xml.stream.XMLStreamException; |
aoqi@0 | 37 | import javax.xml.stream.XMLStreamReader; |
aoqi@0 | 38 | import javax.xml.stream.XMLStreamWriter; |
aoqi@0 | 39 | import javax.xml.transform.Source; |
aoqi@0 | 40 | import javax.xml.transform.Transformer; |
aoqi@0 | 41 | import javax.xml.transform.TransformerConfigurationException; |
aoqi@0 | 42 | import javax.xml.transform.TransformerException; |
aoqi@0 | 43 | import javax.xml.transform.dom.DOMSource; |
aoqi@0 | 44 | import javax.xml.transform.sax.SAXResult; |
aoqi@0 | 45 | import javax.xml.transform.sax.SAXSource; |
aoqi@0 | 46 | import javax.xml.transform.stream.StreamSource; |
aoqi@0 | 47 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 48 | |
aoqi@0 | 49 | /** |
aoqi@0 | 50 | * |
aoqi@0 | 51 | * @author Vivek Pandey |
aoqi@0 | 52 | */ |
aoqi@0 | 53 | final class SourceUtils { |
aoqi@0 | 54 | |
aoqi@0 | 55 | int srcType; |
aoqi@0 | 56 | |
aoqi@0 | 57 | private static final int domSource = 1; |
aoqi@0 | 58 | private static final int streamSource = 2; |
aoqi@0 | 59 | private static final int saxSource=4; |
aoqi@0 | 60 | |
aoqi@0 | 61 | public SourceUtils(Source src) { |
aoqi@0 | 62 | if(src instanceof StreamSource){ |
aoqi@0 | 63 | srcType = streamSource; |
aoqi@0 | 64 | }else if(src instanceof DOMSource){ |
aoqi@0 | 65 | srcType = domSource; |
aoqi@0 | 66 | }else if(src instanceof SAXSource){ |
aoqi@0 | 67 | srcType = saxSource; |
aoqi@0 | 68 | } |
aoqi@0 | 69 | } |
aoqi@0 | 70 | |
aoqi@0 | 71 | public boolean isDOMSource(){ |
aoqi@0 | 72 | return (srcType&domSource) == domSource; |
aoqi@0 | 73 | } |
aoqi@0 | 74 | |
aoqi@0 | 75 | public boolean isStreamSource(){ |
aoqi@0 | 76 | return (srcType&streamSource) == streamSource; |
aoqi@0 | 77 | } |
aoqi@0 | 78 | |
aoqi@0 | 79 | public boolean isSaxSource(){ |
aoqi@0 | 80 | return (srcType&saxSource) == saxSource; |
aoqi@0 | 81 | } |
aoqi@0 | 82 | |
aoqi@0 | 83 | /** |
aoqi@0 | 84 | * This would peek into the Source (DOMSource and SAXSource) for the localName and NamespaceURI |
aoqi@0 | 85 | * of the top-level element. |
aoqi@0 | 86 | * @param src |
aoqi@0 | 87 | * @return QName of the payload |
aoqi@0 | 88 | */ |
aoqi@0 | 89 | public QName sniff(Source src) { |
aoqi@0 | 90 | return sniff(src, new RootElementSniffer()); |
aoqi@0 | 91 | } |
aoqi@0 | 92 | |
aoqi@0 | 93 | public QName sniff(Source src, RootElementSniffer sniffer){ |
aoqi@0 | 94 | String localName = null; |
aoqi@0 | 95 | String namespaceUri = null; |
aoqi@0 | 96 | |
aoqi@0 | 97 | if(isDOMSource()){ |
aoqi@0 | 98 | DOMSource domSrc = (DOMSource)src; |
aoqi@0 | 99 | Node n = domSrc.getNode(); |
aoqi@0 | 100 | if(n.getNodeType()== Node.DOCUMENT_NODE) { |
aoqi@0 | 101 | n = ((Document)n).getDocumentElement(); |
aoqi@0 | 102 | } |
aoqi@0 | 103 | localName = n.getLocalName(); |
aoqi@0 | 104 | namespaceUri = n.getNamespaceURI(); |
aoqi@0 | 105 | }else if(isSaxSource()){ |
aoqi@0 | 106 | SAXSource saxSrc = (SAXSource)src; |
aoqi@0 | 107 | SAXResult saxResult = new SAXResult(sniffer); |
aoqi@0 | 108 | try { |
aoqi@0 | 109 | Transformer tr = XmlUtil.newTransformer(); |
aoqi@0 | 110 | tr.transform(saxSrc, saxResult); |
aoqi@0 | 111 | } catch (TransformerConfigurationException e) { |
aoqi@0 | 112 | throw new WebServiceException(e); |
aoqi@0 | 113 | } catch (TransformerException e) { |
aoqi@0 | 114 | // if it's due to aborting the processing after the first element, |
aoqi@0 | 115 | // we can safely ignore this exception. |
aoqi@0 | 116 | // |
aoqi@0 | 117 | // if it's due to error in the object, the same error will be reported |
aoqi@0 | 118 | // when the readHeader() method is used, so we don't have to report |
aoqi@0 | 119 | // an error right now. |
aoqi@0 | 120 | localName = sniffer.getLocalName(); |
aoqi@0 | 121 | namespaceUri = sniffer.getNsUri(); |
aoqi@0 | 122 | } |
aoqi@0 | 123 | } |
aoqi@0 | 124 | return new QName(namespaceUri, localName); |
aoqi@0 | 125 | } |
aoqi@0 | 126 | |
aoqi@0 | 127 | public static void serializeSource(Source src, XMLStreamWriter writer) throws XMLStreamException { |
aoqi@0 | 128 | XMLStreamReader reader = SourceReaderFactory.createSourceReader(src, true); |
aoqi@0 | 129 | int state; |
aoqi@0 | 130 | do { |
aoqi@0 | 131 | state = reader.next(); |
aoqi@0 | 132 | switch (state) { |
aoqi@0 | 133 | case XMLStreamConstants.START_ELEMENT: |
aoqi@0 | 134 | /* |
aoqi@0 | 135 | * TODO: Is this necessary, shouldn't zephyr return "" instead of |
aoqi@0 | 136 | * null for getNamespaceURI() and getPrefix()? |
aoqi@0 | 137 | */ |
aoqi@0 | 138 | String uri = reader.getNamespaceURI(); |
aoqi@0 | 139 | String prefix = reader.getPrefix(); |
aoqi@0 | 140 | String localName = reader.getLocalName(); |
aoqi@0 | 141 | |
aoqi@0 | 142 | if (prefix == null) { |
aoqi@0 | 143 | if (uri == null) { |
aoqi@0 | 144 | writer.writeStartElement(localName); |
aoqi@0 | 145 | } else { |
aoqi@0 | 146 | writer.writeStartElement(uri, localName); |
aoqi@0 | 147 | } |
aoqi@0 | 148 | } else { |
aoqi@0 | 149 | // assert uri != null; |
aoqi@0 | 150 | |
aoqi@0 | 151 | if(prefix.length() > 0){ |
aoqi@0 | 152 | /** |
aoqi@0 | 153 | * Before we write the |
aoqi@0 | 154 | */ |
aoqi@0 | 155 | String writerURI = null; |
aoqi@0 | 156 | if (writer.getNamespaceContext() != null) { |
aoqi@0 | 157 | writerURI = writer.getNamespaceContext().getNamespaceURI(prefix); |
aoqi@0 | 158 | } |
aoqi@0 | 159 | String writerPrefix = writer.getPrefix(uri); |
aoqi@0 | 160 | if(declarePrefix(prefix, uri, writerPrefix, writerURI)){ |
aoqi@0 | 161 | writer.writeStartElement(prefix, localName, uri); |
aoqi@0 | 162 | writer.setPrefix(prefix, uri != null ? uri : ""); |
aoqi@0 | 163 | writer.writeNamespace(prefix, uri); |
aoqi@0 | 164 | }else{ |
aoqi@0 | 165 | writer.writeStartElement(prefix, localName, uri); |
aoqi@0 | 166 | } |
aoqi@0 | 167 | }else{ |
aoqi@0 | 168 | writer.writeStartElement(prefix, localName, uri); |
aoqi@0 | 169 | } |
aoqi@0 | 170 | } |
aoqi@0 | 171 | |
aoqi@0 | 172 | int n = reader.getNamespaceCount(); |
aoqi@0 | 173 | // Write namespace declarations |
aoqi@0 | 174 | for (int i = 0; i < n; i++) { |
aoqi@0 | 175 | String nsPrefix = reader.getNamespacePrefix(i); |
aoqi@0 | 176 | if (nsPrefix == null) { |
aoqi@0 | 177 | nsPrefix = ""; |
aoqi@0 | 178 | } |
aoqi@0 | 179 | // StAX returns null for default ns |
aoqi@0 | 180 | String writerURI = null; |
aoqi@0 | 181 | if (writer.getNamespaceContext() != null) { |
aoqi@0 | 182 | writerURI = writer.getNamespaceContext().getNamespaceURI(nsPrefix); |
aoqi@0 | 183 | } |
aoqi@0 | 184 | |
aoqi@0 | 185 | // Zephyr: Why is this returning null? |
aoqi@0 | 186 | // Compare nsPrefix with prefix because of [1] (above) |
aoqi@0 | 187 | String readerURI = reader.getNamespaceURI(i); |
aoqi@0 | 188 | |
aoqi@0 | 189 | /** |
aoqi@0 | 190 | * write the namespace in 3 conditions |
aoqi@0 | 191 | * - when the namespace URI is not bound to the prefix in writer(writerURI == 0) |
aoqi@0 | 192 | * - when the readerPrefix and writerPrefix are "" |
aoqi@0 | 193 | * - when readerPrefix and writerPrefix are not equal and the URI bound to them |
aoqi@0 | 194 | * are different |
aoqi@0 | 195 | */ |
aoqi@0 | 196 | if (writerURI == null || ((nsPrefix.length() == 0) || (prefix.length() == 0)) || |
aoqi@0 | 197 | (!nsPrefix.equals(prefix) && !writerURI.equals(readerURI))) { |
aoqi@0 | 198 | writer.setPrefix(nsPrefix, readerURI != null ? readerURI : ""); |
aoqi@0 | 199 | writer.writeNamespace(nsPrefix, readerURI != null ? readerURI : ""); |
aoqi@0 | 200 | } |
aoqi@0 | 201 | } |
aoqi@0 | 202 | |
aoqi@0 | 203 | // Write attributes |
aoqi@0 | 204 | n = reader.getAttributeCount(); |
aoqi@0 | 205 | for (int i = 0; i < n; i++) { |
aoqi@0 | 206 | String attrPrefix = reader.getAttributePrefix(i); |
aoqi@0 | 207 | String attrURI = reader.getAttributeNamespace(i); |
aoqi@0 | 208 | |
aoqi@0 | 209 | writer.writeAttribute(attrPrefix != null ? attrPrefix : "", |
aoqi@0 | 210 | attrURI != null ? attrURI : "", |
aoqi@0 | 211 | reader.getAttributeLocalName(i), |
aoqi@0 | 212 | reader.getAttributeValue(i)); |
aoqi@0 | 213 | // if the attribute prefix is undeclared in current writer scope then declare it |
aoqi@0 | 214 | setUndeclaredPrefix(attrPrefix, attrURI, writer); |
aoqi@0 | 215 | } |
aoqi@0 | 216 | break; |
aoqi@0 | 217 | case XMLStreamConstants.END_ELEMENT: |
aoqi@0 | 218 | writer.writeEndElement(); |
aoqi@0 | 219 | break; |
aoqi@0 | 220 | case XMLStreamConstants.CHARACTERS: |
aoqi@0 | 221 | writer.writeCharacters(reader.getText()); |
aoqi@0 | 222 | break; |
aoqi@0 | 223 | default: |
aoqi@0 | 224 | break; |
aoqi@0 | 225 | } |
aoqi@0 | 226 | } while (state != XMLStreamConstants.END_DOCUMENT); |
aoqi@0 | 227 | reader.close(); |
aoqi@0 | 228 | } |
aoqi@0 | 229 | |
aoqi@0 | 230 | /** |
aoqi@0 | 231 | * sets undeclared prefixes on the writer |
aoqi@0 | 232 | * @param prefix |
aoqi@0 | 233 | * @param writer |
aoqi@0 | 234 | * @throws XMLStreamException |
aoqi@0 | 235 | */ |
aoqi@0 | 236 | private static void setUndeclaredPrefix(String prefix, String readerURI, XMLStreamWriter writer) throws XMLStreamException { |
aoqi@0 | 237 | String writerURI = null; |
aoqi@0 | 238 | if (writer.getNamespaceContext() != null) { |
aoqi@0 | 239 | writerURI = writer.getNamespaceContext().getNamespaceURI(prefix); |
aoqi@0 | 240 | } |
aoqi@0 | 241 | |
aoqi@0 | 242 | if (writerURI == null) { |
aoqi@0 | 243 | writer.setPrefix(prefix, readerURI != null ? readerURI : ""); |
aoqi@0 | 244 | writer.writeNamespace(prefix, readerURI != null ? readerURI : ""); |
aoqi@0 | 245 | } |
aoqi@0 | 246 | } |
aoqi@0 | 247 | |
aoqi@0 | 248 | /** |
aoqi@0 | 249 | * check if we need to declare |
aoqi@0 | 250 | * @param rPrefix |
aoqi@0 | 251 | * @param rUri |
aoqi@0 | 252 | * @param wPrefix |
aoqi@0 | 253 | * @param wUri |
aoqi@0 | 254 | */ |
aoqi@0 | 255 | private static boolean declarePrefix(String rPrefix, String rUri, String wPrefix, String wUri){ |
aoqi@0 | 256 | if (wUri == null ||((wPrefix != null) && !rPrefix.equals(wPrefix))|| |
aoqi@0 | 257 | (rUri != null && !wUri.equals(rUri))) { |
aoqi@0 | 258 | return true; |
aoqi@0 | 259 | } |
aoqi@0 | 260 | return false; |
aoqi@0 | 261 | } |
aoqi@0 | 262 | } |