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 Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,262 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, 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 static final int domSource = 1; 1.61 + private static final int streamSource = 2; 1.62 + private static 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 domSrc = (DOMSource)src; 1.102 + Node n = domSrc.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 + } 1.162 + String writerPrefix = writer.getPrefix(uri); 1.163 + if(declarePrefix(prefix, uri, writerPrefix, writerURI)){ 1.164 + writer.writeStartElement(prefix, localName, uri); 1.165 + writer.setPrefix(prefix, uri != null ? uri : ""); 1.166 + writer.writeNamespace(prefix, uri); 1.167 + }else{ 1.168 + writer.writeStartElement(prefix, localName, uri); 1.169 + } 1.170 + }else{ 1.171 + writer.writeStartElement(prefix, localName, uri); 1.172 + } 1.173 + } 1.174 + 1.175 + int n = reader.getNamespaceCount(); 1.176 + // Write namespace declarations 1.177 + for (int i = 0; i < n; i++) { 1.178 + String nsPrefix = reader.getNamespacePrefix(i); 1.179 + if (nsPrefix == null) { 1.180 + nsPrefix = ""; 1.181 + } 1.182 + // StAX returns null for default ns 1.183 + String writerURI = null; 1.184 + if (writer.getNamespaceContext() != null) { 1.185 + writerURI = writer.getNamespaceContext().getNamespaceURI(nsPrefix); 1.186 + } 1.187 + 1.188 + // Zephyr: Why is this returning null? 1.189 + // Compare nsPrefix with prefix because of [1] (above) 1.190 + String readerURI = reader.getNamespaceURI(i); 1.191 + 1.192 + /** 1.193 + * write the namespace in 3 conditions 1.194 + * - when the namespace URI is not bound to the prefix in writer(writerURI == 0) 1.195 + * - when the readerPrefix and writerPrefix are "" 1.196 + * - when readerPrefix and writerPrefix are not equal and the URI bound to them 1.197 + * are different 1.198 + */ 1.199 + if (writerURI == null || ((nsPrefix.length() == 0) || (prefix.length() == 0)) || 1.200 + (!nsPrefix.equals(prefix) && !writerURI.equals(readerURI))) { 1.201 + writer.setPrefix(nsPrefix, readerURI != null ? readerURI : ""); 1.202 + writer.writeNamespace(nsPrefix, readerURI != null ? readerURI : ""); 1.203 + } 1.204 + } 1.205 + 1.206 + // Write attributes 1.207 + n = reader.getAttributeCount(); 1.208 + for (int i = 0; i < n; i++) { 1.209 + String attrPrefix = reader.getAttributePrefix(i); 1.210 + String attrURI = reader.getAttributeNamespace(i); 1.211 + 1.212 + writer.writeAttribute(attrPrefix != null ? attrPrefix : "", 1.213 + attrURI != null ? attrURI : "", 1.214 + reader.getAttributeLocalName(i), 1.215 + reader.getAttributeValue(i)); 1.216 + // if the attribute prefix is undeclared in current writer scope then declare it 1.217 + setUndeclaredPrefix(attrPrefix, attrURI, writer); 1.218 + } 1.219 + break; 1.220 + case XMLStreamConstants.END_ELEMENT: 1.221 + writer.writeEndElement(); 1.222 + break; 1.223 + case XMLStreamConstants.CHARACTERS: 1.224 + writer.writeCharacters(reader.getText()); 1.225 + break; 1.226 + default: 1.227 + break; 1.228 + } 1.229 + } while (state != XMLStreamConstants.END_DOCUMENT); 1.230 + reader.close(); 1.231 + } 1.232 + 1.233 + /** 1.234 + * sets undeclared prefixes on the writer 1.235 + * @param prefix 1.236 + * @param writer 1.237 + * @throws XMLStreamException 1.238 + */ 1.239 + private static void setUndeclaredPrefix(String prefix, String readerURI, XMLStreamWriter writer) throws XMLStreamException { 1.240 + String writerURI = null; 1.241 + if (writer.getNamespaceContext() != null) { 1.242 + writerURI = writer.getNamespaceContext().getNamespaceURI(prefix); 1.243 + } 1.244 + 1.245 + if (writerURI == null) { 1.246 + writer.setPrefix(prefix, readerURI != null ? readerURI : ""); 1.247 + writer.writeNamespace(prefix, readerURI != null ? readerURI : ""); 1.248 + } 1.249 + } 1.250 + 1.251 + /** 1.252 + * check if we need to declare 1.253 + * @param rPrefix 1.254 + * @param rUri 1.255 + * @param wPrefix 1.256 + * @param wUri 1.257 + */ 1.258 + private static boolean declarePrefix(String rPrefix, String rUri, String wPrefix, String wUri){ 1.259 + if (wUri == null ||((wPrefix != null) && !rPrefix.equals(wPrefix))|| 1.260 + (rUri != null && !wUri.equals(rUri))) { 1.261 + return true; 1.262 + } 1.263 + return false; 1.264 + } 1.265 +}