1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/streaming/XMLStreamWriterUtil.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,139 @@ 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.streaming; 1.30 + 1.31 +import com.sun.istack.internal.Nullable; 1.32 +import com.sun.xml.internal.ws.encoding.HasEncoding; 1.33 +import com.sun.xml.internal.ws.encoding.SOAPBindingCodec; 1.34 + 1.35 +import javax.xml.namespace.QName; 1.36 +import javax.xml.stream.XMLStreamWriter; 1.37 +import javax.xml.stream.XMLStreamException; 1.38 +import java.util.Map; 1.39 +import java.io.OutputStream; 1.40 + 1.41 +/** 1.42 + * <p>XMLStreamWriterUtil provides some utility methods intended to be used 1.43 + * in conjunction with a StAX XMLStreamWriter. </p> 1.44 + * 1.45 + * @author Santiago.PericasGeertsen@sun.com 1.46 + */ 1.47 +public class XMLStreamWriterUtil { 1.48 + 1.49 + private XMLStreamWriterUtil() { 1.50 + } 1.51 + 1.52 + /** 1.53 + * Gives the underlying stream for XMLStreamWriter. It closes any start elements, and returns the stream so 1.54 + * that JAXB can write infoset directly to the stream. 1.55 + * 1.56 + * @param writer XMLStreamWriter for which stream is required 1.57 + * @return underlying OutputStream, null if writer doesn't provide a way to get it 1.58 + * @throws XMLStreamException if any of writer operations throw the exception 1.59 + */ 1.60 + public static @Nullable OutputStream getOutputStream(XMLStreamWriter writer) throws XMLStreamException { 1.61 + Object obj = null; 1.62 + 1.63 + // Hack for JDK6's SJSXP 1.64 + if (writer instanceof Map) { 1.65 + obj = ((Map) writer).get("sjsxp-outputstream"); 1.66 + } 1.67 + 1.68 + // woodstox 1.69 + if (obj == null) { 1.70 + try { 1.71 + obj = writer.getProperty("com.ctc.wstx.outputUnderlyingStream"); 1.72 + } catch(Exception ie) { 1.73 + // Catch all exceptions. SJSXP in JDK throws NPE 1.74 + // nothing to do here 1.75 + } 1.76 + } 1.77 + 1.78 + // SJSXP 1.79 + if (obj == null) { 1.80 + try { 1.81 + obj = writer.getProperty("http://java.sun.com/xml/stream/properties/outputstream"); 1.82 + } catch(Exception ie) { 1.83 + // Catch all exceptions. SJSXP in JDK throws NPE 1.84 + // nothing to do here 1.85 + } 1.86 + } 1.87 + 1.88 + 1.89 + if (obj != null) { 1.90 + writer.writeCharacters(""); // Force completion of open elems 1.91 + writer.flush(); 1.92 + return (OutputStream)obj; 1.93 + } 1.94 + return null; 1.95 + } 1.96 + 1.97 + /** 1.98 + * Gives the encoding with which XMLStreamWriter is created. 1.99 + * 1.100 + * @param writer XMLStreamWriter for which encoding is required 1.101 + * @return null if cannot be found, else the encoding 1.102 + */ 1.103 + public static @Nullable String getEncoding(XMLStreamWriter writer) { 1.104 + /* 1.105 + * TODO Add reflection logic to handle woodstox writer 1.106 + * as it implements XMLStreamWriter2#getEncoding() 1.107 + * It's not that important since woodstox writer is typically wrapped 1.108 + * in a writer with HasEncoding 1.109 + */ 1.110 + return (writer instanceof HasEncoding) 1.111 + ? ((HasEncoding)writer).getEncoding() 1.112 + : null; 1.113 + } 1.114 + 1.115 + public static String encodeQName(XMLStreamWriter writer, QName qname, 1.116 + PrefixFactory prefixFactory) 1.117 + { 1.118 + // NOTE: Here it is assumed that we do not serialize using default 1.119 + // namespace declarations and therefore that writer.getPrefix will 1.120 + // never return "" 1.121 + 1.122 + try { 1.123 + String namespaceURI = qname.getNamespaceURI(); 1.124 + String localPart = qname.getLocalPart(); 1.125 + 1.126 + if (namespaceURI == null || namespaceURI.equals("")) { 1.127 + return localPart; 1.128 + } 1.129 + else { 1.130 + String prefix = writer.getPrefix(namespaceURI); 1.131 + if (prefix == null) { 1.132 + prefix = prefixFactory.getPrefix(namespaceURI); 1.133 + writer.writeNamespace(prefix, namespaceURI); 1.134 + } 1.135 + return prefix + ":" + localPart; 1.136 + } 1.137 + } 1.138 + catch (XMLStreamException e) { 1.139 + throw new RuntimeException(e); 1.140 + } 1.141 + } 1.142 +}