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

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

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