ohair@286: /* ohair@286: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.streaming; ohair@286: ohair@286: import com.sun.istack.internal.Nullable; ohair@286: import com.sun.xml.internal.ws.encoding.HasEncoding; ohair@286: import com.sun.xml.internal.ws.encoding.SOAPBindingCodec; ohair@286: ohair@286: import javax.xml.namespace.QName; ohair@286: import javax.xml.stream.XMLStreamWriter; ohair@286: import javax.xml.stream.XMLStreamException; ohair@286: import java.util.Map; ohair@286: import java.io.OutputStream; ohair@286: ohair@286: /** ohair@286: *

XMLStreamWriterUtil provides some utility methods intended to be used ohair@286: * in conjunction with a StAX XMLStreamWriter.

ohair@286: * ohair@286: * @author Santiago.PericasGeertsen@sun.com ohair@286: */ ohair@286: public class XMLStreamWriterUtil { ohair@286: ohair@286: private XMLStreamWriterUtil() { ohair@286: } ohair@286: ohair@286: /** ohair@286: * Gives the underlying stream for XMLStreamWriter. It closes any start elements, and returns the stream so ohair@286: * that JAXB can write infoset directly to the stream. ohair@286: * ohair@286: * @param writer XMLStreamWriter for which stream is required ohair@286: * @return underlying OutputStream, null if writer doesn't provide a way to get it ohair@286: * @throws XMLStreamException if any of writer operations throw the exception ohair@286: */ ohair@286: public static @Nullable OutputStream getOutputStream(XMLStreamWriter writer) throws XMLStreamException { ohair@286: Object obj = null; ohair@286: ohair@286: // Hack for JDK6's SJSXP ohair@286: if (writer instanceof Map) { ohair@286: obj = ((Map) writer).get("sjsxp-outputstream"); ohair@286: } ohair@286: ohair@286: // woodstox ohair@286: if (obj == null) { ohair@286: try { ohair@286: obj = writer.getProperty("com.ctc.wstx.outputUnderlyingStream"); ohair@286: } catch(Exception ie) { ohair@286: // Catch all exceptions. SJSXP in JDK throws NPE ohair@286: // nothing to do here ohair@286: } ohair@286: } ohair@286: ohair@286: // SJSXP ohair@286: if (obj == null) { ohair@286: try { ohair@286: obj = writer.getProperty("http://java.sun.com/xml/stream/properties/outputstream"); ohair@286: } catch(Exception ie) { ohair@286: // Catch all exceptions. SJSXP in JDK throws NPE ohair@286: // nothing to do here ohair@286: } ohair@286: } ohair@286: ohair@286: ohair@286: if (obj != null) { ohair@286: writer.writeCharacters(""); // Force completion of open elems ohair@286: writer.flush(); ohair@286: return (OutputStream)obj; ohair@286: } ohair@286: return null; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Gives the encoding with which XMLStreamWriter is created. ohair@286: * ohair@286: * @param writer XMLStreamWriter for which encoding is required ohair@286: * @return null if cannot be found, else the encoding ohair@286: */ ohair@286: public static @Nullable String getEncoding(XMLStreamWriter writer) { ohair@286: /* ohair@286: * TODO Add reflection logic to handle woodstox writer ohair@286: * as it implements XMLStreamWriter2#getEncoding() ohair@286: * It's not that important since woodstox writer is typically wrapped ohair@286: * in a writer with HasEncoding ohair@286: */ ohair@286: return (writer instanceof HasEncoding) ohair@286: ? ((HasEncoding)writer).getEncoding() ohair@286: : null; ohair@286: } ohair@286: ohair@286: public static String encodeQName(XMLStreamWriter writer, QName qname, ohair@286: PrefixFactory prefixFactory) ohair@286: { ohair@286: // NOTE: Here it is assumed that we do not serialize using default ohair@286: // namespace declarations and therefore that writer.getPrefix will ohair@286: // never return "" ohair@286: ohair@286: try { ohair@286: String namespaceURI = qname.getNamespaceURI(); ohair@286: String localPart = qname.getLocalPart(); ohair@286: ohair@286: if (namespaceURI == null || namespaceURI.equals("")) { ohair@286: return localPart; ohair@286: } ohair@286: else { ohair@286: String prefix = writer.getPrefix(namespaceURI); ohair@286: if (prefix == null) { ohair@286: prefix = prefixFactory.getPrefix(namespaceURI); ohair@286: writer.writeNamespace(prefix, namespaceURI); ohair@286: } ohair@286: return prefix + ":" + localPart; ohair@286: } ohair@286: } ohair@286: catch (XMLStreamException e) { ohair@286: throw new RuntimeException(e); ohair@286: } ohair@286: } ohair@286: }