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