aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, 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.bind.v2.runtime.output; aoqi@0: aoqi@0: import java.io.IOException; aoqi@0: import java.lang.reflect.Constructor; aoqi@0: aoqi@0: import javax.xml.stream.XMLStreamException; aoqi@0: import javax.xml.stream.XMLStreamWriter; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl; aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: /** aoqi@0: * {@link XmlOutput} that writes to StAX {@link XMLStreamWriter}. aoqi@0: *

aoqi@0: * TODO: aoqi@0: * Finding the optimized FI implementations is a bit hacky and not very aoqi@0: * extensible. Can we use the service provider mechanism in general for aoqi@0: * concrete implementations of XmlOutputAbstractImpl. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public class XMLStreamWriterOutput extends XmlOutputAbstractImpl { aoqi@0: aoqi@0: /** aoqi@0: * Creates a new {@link XmlOutput} from a {@link XMLStreamWriter}. aoqi@0: * This method recognizes an FI StAX writer. aoqi@0: */ aoqi@0: public static XmlOutput create(XMLStreamWriter out, JAXBContextImpl context) { aoqi@0: // try optimized path aoqi@0: final Class writerClass = out.getClass(); aoqi@0: if (writerClass==FI_STAX_WRITER_CLASS) { aoqi@0: try { aoqi@0: return FI_OUTPUT_CTOR.newInstance(out, context); aoqi@0: } catch (Exception e) { aoqi@0: } aoqi@0: } aoqi@0: if (STAXEX_WRITER_CLASS!=null && STAXEX_WRITER_CLASS.isAssignableFrom(writerClass)) { aoqi@0: try { aoqi@0: return STAXEX_OUTPUT_CTOR.newInstance(out); aoqi@0: } catch (Exception e) { aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // otherwise the normal writer. aoqi@0: return new XMLStreamWriterOutput(out); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: private final XMLStreamWriter out; aoqi@0: aoqi@0: protected final char[] buf = new char[256]; aoqi@0: aoqi@0: protected XMLStreamWriterOutput(XMLStreamWriter out) { aoqi@0: this.out = out; aoqi@0: } aoqi@0: aoqi@0: // not called if we are generating fragments aoqi@0: @Override aoqi@0: public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException { aoqi@0: super.startDocument(serializer, fragment,nsUriIndex2prefixIndex,nsContext); aoqi@0: if(!fragment) aoqi@0: out.writeStartDocument(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException { aoqi@0: if(!fragment) { aoqi@0: out.writeEndDocument(); aoqi@0: out.flush(); aoqi@0: } aoqi@0: super.endDocument(fragment); aoqi@0: } aoqi@0: aoqi@0: public void beginStartTag(int prefix, String localName) throws IOException, XMLStreamException { aoqi@0: out.writeStartElement( aoqi@0: nsContext.getPrefix(prefix), aoqi@0: localName, aoqi@0: nsContext.getNamespaceURI(prefix)); aoqi@0: aoqi@0: NamespaceContextImpl.Element nse = nsContext.getCurrent(); aoqi@0: if(nse.count()>0) { aoqi@0: for( int i=nse.count()-1; i>=0; i-- ) { aoqi@0: String uri = nse.getNsUri(i); aoqi@0: if(uri.length()==0 && nse.getBase()==1) aoqi@0: continue; // no point in definint xmlns='' on the root aoqi@0: out.writeNamespace(nse.getPrefix(i),uri); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void attribute(int prefix, String localName, String value) throws IOException, XMLStreamException { aoqi@0: if(prefix==-1) aoqi@0: out.writeAttribute(localName,value); aoqi@0: else aoqi@0: out.writeAttribute( aoqi@0: nsContext.getPrefix(prefix), aoqi@0: nsContext.getNamespaceURI(prefix), aoqi@0: localName, value); aoqi@0: } aoqi@0: aoqi@0: public void endStartTag() throws IOException, SAXException { aoqi@0: // noop aoqi@0: } aoqi@0: aoqi@0: public void endTag(int prefix, String localName) throws IOException, SAXException, XMLStreamException { aoqi@0: out.writeEndElement(); aoqi@0: } aoqi@0: aoqi@0: public void text(String value, boolean needsSeparatingWhitespace) throws IOException, SAXException, XMLStreamException { aoqi@0: if(needsSeparatingWhitespace) aoqi@0: out.writeCharacters(" "); aoqi@0: out.writeCharacters(value); aoqi@0: } aoqi@0: aoqi@0: public void text(Pcdata value, boolean needsSeparatingWhitespace) throws IOException, SAXException, XMLStreamException { aoqi@0: if(needsSeparatingWhitespace) aoqi@0: out.writeCharacters(" "); aoqi@0: aoqi@0: int len = value.length(); aoqi@0: if(len FI_OUTPUT_CTOR = initFastInfosetOutputClass(); aoqi@0: aoqi@0: private static Class initFIStAXWriterClass() { aoqi@0: try { aoqi@0: Class llfisw = Class.forName("com.sun.xml.internal.org.jvnet.fastinfoset.stax.LowLevelFastInfosetStreamWriter"); aoqi@0: Class sds = Class.forName("com.sun.xml.internal.fastinfoset.stax.StAXDocumentSerializer"); aoqi@0: // Check if StAXDocumentSerializer implements LowLevelFastInfosetStreamWriter aoqi@0: if (llfisw.isAssignableFrom(sds)) aoqi@0: return sds; aoqi@0: else aoqi@0: return null; aoqi@0: } catch (Throwable e) { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private static Constructor initFastInfosetOutputClass() { aoqi@0: try { aoqi@0: if (FI_STAX_WRITER_CLASS == null) aoqi@0: return null; aoqi@0: Class c = Class.forName("com.sun.xml.internal.bind.v2.runtime.output.FastInfosetStreamWriterOutput"); aoqi@0: return c.getConstructor(FI_STAX_WRITER_CLASS, JAXBContextImpl.class); aoqi@0: } catch (Throwable e) { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // StAX-ex aoqi@0: // aoqi@0: private static final Class STAXEX_WRITER_CLASS = initStAXExWriterClass(); aoqi@0: private static final Constructor STAXEX_OUTPUT_CTOR = initStAXExOutputClass(); aoqi@0: aoqi@0: private static Class initStAXExWriterClass() { aoqi@0: try { aoqi@0: return Class.forName("com.sun.xml.internal.org.jvnet.staxex.XMLStreamWriterEx"); aoqi@0: } catch (Throwable e) { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private static Constructor initStAXExOutputClass() { aoqi@0: try { aoqi@0: Class c = Class.forName("com.sun.xml.internal.bind.v2.runtime.output.StAXExStreamWriterOutput"); aoqi@0: return c.getConstructor(STAXEX_WRITER_CLASS); aoqi@0: } catch (Throwable e) { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: }