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

ohair@286: * TODO: ohair@286: * Finding the optimized FI implementations is a bit hacky and not very ohair@286: * extensible. Can we use the service provider mechanism in general for ohair@286: * concrete implementations of XmlOutputAbstractImpl. ohair@286: * ohair@286: * @author Kohsuke Kawaguchi ohair@286: */ ohair@286: public class XMLStreamWriterOutput extends XmlOutputAbstractImpl { ohair@286: ohair@286: /** ohair@286: * Creates a new {@link XmlOutput} from a {@link XMLStreamWriter}. ohair@286: * This method recognizes an FI StAX writer. ohair@286: */ aefimov@1443: public static XmlOutput create(XMLStreamWriter out, JAXBContextImpl context, CharacterEscapeHandler escapeHandler) { ohair@286: // try optimized path ohair@286: final Class writerClass = out.getClass(); ohair@286: if (writerClass==FI_STAX_WRITER_CLASS) { ohair@286: try { ohair@286: return FI_OUTPUT_CTOR.newInstance(out, context); ohair@286: } catch (Exception e) { ohair@286: } ohair@286: } ohair@286: if (STAXEX_WRITER_CLASS!=null && STAXEX_WRITER_CLASS.isAssignableFrom(writerClass)) { ohair@286: try { ohair@286: return STAXEX_OUTPUT_CTOR.newInstance(out); ohair@286: } catch (Exception e) { ohair@286: } ohair@286: } ohair@286: aefimov@1443: CharacterEscapeHandler xmlStreamEscapeHandler = escapeHandler != null ? aefimov@1443: escapeHandler : NewLineEscapeHandler.theInstance; aefimov@1443: ohair@286: // otherwise the normal writer. aefimov@1443: return new XMLStreamWriterOutput(out, xmlStreamEscapeHandler); ohair@286: } ohair@286: ohair@286: ohair@286: private final XMLStreamWriter out; ohair@286: aefimov@1443: private final CharacterEscapeHandler escapeHandler; aefimov@1443: aefimov@1443: private final XmlStreamOutWriterAdapter writerWrapper; aefimov@1443: ohair@286: protected final char[] buf = new char[256]; ohair@286: aefimov@1443: protected XMLStreamWriterOutput(XMLStreamWriter out, CharacterEscapeHandler escapeHandler) { ohair@286: this.out = out; aefimov@1443: this.escapeHandler = escapeHandler; aefimov@1443: this.writerWrapper = new XmlStreamOutWriterAdapter(out); ohair@286: } ohair@286: ohair@286: // not called if we are generating fragments ohair@286: @Override ohair@286: public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException { ohair@286: super.startDocument(serializer, fragment,nsUriIndex2prefixIndex,nsContext); ohair@286: if(!fragment) ohair@286: out.writeStartDocument(); ohair@286: } ohair@286: ohair@286: @Override ohair@286: public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException { ohair@286: if(!fragment) { ohair@286: out.writeEndDocument(); ohair@286: out.flush(); ohair@286: } ohair@286: super.endDocument(fragment); ohair@286: } ohair@286: ohair@286: public void beginStartTag(int prefix, String localName) throws IOException, XMLStreamException { ohair@286: out.writeStartElement( ohair@286: nsContext.getPrefix(prefix), ohair@286: localName, ohair@286: nsContext.getNamespaceURI(prefix)); ohair@286: ohair@286: NamespaceContextImpl.Element nse = nsContext.getCurrent(); ohair@286: if(nse.count()>0) { ohair@286: for( int i=nse.count()-1; i>=0; i-- ) { ohair@286: String uri = nse.getNsUri(i); ohair@286: if(uri.length()==0 && nse.getBase()==1) ohair@286: continue; // no point in definint xmlns='' on the root ohair@286: out.writeNamespace(nse.getPrefix(i),uri); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: public void attribute(int prefix, String localName, String value) throws IOException, XMLStreamException { ohair@286: if(prefix==-1) ohair@286: out.writeAttribute(localName,value); ohair@286: else ohair@286: out.writeAttribute( ohair@286: nsContext.getPrefix(prefix), ohair@286: nsContext.getNamespaceURI(prefix), ohair@286: localName, value); ohair@286: } ohair@286: ohair@286: public void endStartTag() throws IOException, SAXException { ohair@286: // noop ohair@286: } ohair@286: ohair@286: public void endTag(int prefix, String localName) throws IOException, SAXException, XMLStreamException { ohair@286: out.writeEndElement(); ohair@286: } ohair@286: ohair@286: public void text(String value, boolean needsSeparatingWhitespace) throws IOException, SAXException, XMLStreamException { ohair@286: if(needsSeparatingWhitespace) ohair@286: out.writeCharacters(" "); aefimov@1443: escapeHandler.escape(value.toCharArray(), 0, value.length(), false, writerWrapper); ohair@286: } ohair@286: ohair@286: public void text(Pcdata value, boolean needsSeparatingWhitespace) throws IOException, SAXException, XMLStreamException { ohair@286: if(needsSeparatingWhitespace) ohair@286: out.writeCharacters(" "); ohair@286: ohair@286: int len = value.length(); ohair@286: if(len FI_OUTPUT_CTOR = initFastInfosetOutputClass(); ohair@286: ohair@286: private static Class initFIStAXWriterClass() { ohair@286: try { mkos@514: Class llfisw = Class.forName("com.sun.xml.internal.org.jvnet.fastinfoset.stax.LowLevelFastInfosetStreamWriter"); mkos@514: Class sds = Class.forName("com.sun.xml.internal.fastinfoset.stax.StAXDocumentSerializer"); ohair@286: // Check if StAXDocumentSerializer implements LowLevelFastInfosetStreamWriter ohair@286: if (llfisw.isAssignableFrom(sds)) ohair@286: return sds; ohair@286: else ohair@286: return null; ohair@286: } catch (Throwable e) { ohair@286: return null; ohair@286: } ohair@286: } ohair@286: ohair@286: private static Constructor initFastInfosetOutputClass() { ohair@286: try { ohair@286: if (FI_STAX_WRITER_CLASS == null) ohair@286: return null; mkos@514: Class c = Class.forName("com.sun.xml.internal.bind.v2.runtime.output.FastInfosetStreamWriterOutput"); ohair@286: return c.getConstructor(FI_STAX_WRITER_CLASS, JAXBContextImpl.class); ohair@286: } catch (Throwable e) { ohair@286: return null; ohair@286: } ohair@286: } ohair@286: ohair@286: // ohair@286: // StAX-ex ohair@286: // ohair@286: private static final Class STAXEX_WRITER_CLASS = initStAXExWriterClass(); ohair@286: private static final Constructor STAXEX_OUTPUT_CTOR = initStAXExOutputClass(); ohair@286: ohair@286: private static Class initStAXExWriterClass() { ohair@286: try { mkos@514: return Class.forName("com.sun.xml.internal.org.jvnet.staxex.XMLStreamWriterEx"); ohair@286: } catch (Throwable e) { ohair@286: return null; ohair@286: } ohair@286: } ohair@286: ohair@286: private static Constructor initStAXExOutputClass() { ohair@286: try { mkos@514: Class c = Class.forName("com.sun.xml.internal.bind.v2.runtime.output.StAXExStreamWriterOutput"); ohair@286: return c.getConstructor(STAXEX_WRITER_CLASS); ohair@286: } catch (Throwable e) { ohair@286: return null; ohair@286: } ohair@286: } ohair@286: aefimov@1443: aefimov@1443: /** aefimov@1443: * Performs character escaping only for new lines. aefimov@1443: */ aefimov@1443: private static class NewLineEscapeHandler implements CharacterEscapeHandler { aefimov@1443: aefimov@1443: public static final NewLineEscapeHandler theInstance = new NewLineEscapeHandler(); aefimov@1443: aefimov@1443: @Override aefimov@1443: public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException { aefimov@1443: int limit = start+length; aefimov@1443: int lastEscaped = start; aefimov@1443: aefimov@1443: for (int i = start; i < limit; i++) { aefimov@1443: char c = ch[i]; aefimov@1443: if (c == '\r' || c == '\n') { aefimov@1443: if (i != lastEscaped) { aefimov@1443: out.write(ch, lastEscaped, i - lastEscaped); aefimov@1443: } aefimov@1443: lastEscaped = i + 1; aefimov@1443: if (out instanceof XmlStreamOutWriterAdapter) { aefimov@1443: try { aefimov@1443: ((XmlStreamOutWriterAdapter)out).writeEntityRef("#x" + Integer.toHexString(c)); aefimov@1443: } catch (XMLStreamException e) { aefimov@1443: throw new IOException("Error writing xml stream", e); aefimov@1443: } aefimov@1443: } else { aefimov@1443: out.write("&#x"); aefimov@1443: out.write(Integer.toHexString(c)); aefimov@1443: out.write(';'); aefimov@1443: } aefimov@1443: } aefimov@1443: } aefimov@1443: if (lastEscaped != limit) { aefimov@1443: out.write(ch, lastEscaped, length - lastEscaped); aefimov@1443: } aefimov@1443: } aefimov@1443: } aefimov@1443: aefimov@1443: private static final class XmlStreamOutWriterAdapter extends Writer { aefimov@1443: aefimov@1443: private final XMLStreamWriter writer; aefimov@1443: aefimov@1443: private XmlStreamOutWriterAdapter(XMLStreamWriter writer) { aefimov@1443: this.writer = writer; aefimov@1443: } aefimov@1443: aefimov@1443: @Override aefimov@1443: public void write(char[] cbuf, int off, int len) throws IOException { aefimov@1443: try { aefimov@1443: writer.writeCharacters(cbuf, off, len); aefimov@1443: } catch (XMLStreamException e) { aefimov@1443: throw new IOException("Error writing XML stream", e); aefimov@1443: } aefimov@1443: } aefimov@1443: aefimov@1443: public void writeEntityRef(String entityReference) throws XMLStreamException { aefimov@1443: writer.writeEntityRef(entityReference); aefimov@1443: } aefimov@1443: aefimov@1443: @Override aefimov@1443: public void flush() throws IOException { aefimov@1443: try { aefimov@1443: writer.flush(); aefimov@1443: } catch (XMLStreamException e) { aefimov@1443: throw new IOException("Error flushing XML stream", e); aefimov@1443: } aefimov@1443: } aefimov@1443: aefimov@1443: @Override aefimov@1443: public void close() throws IOException { aefimov@1443: try { aefimov@1443: writer.close(); aefimov@1443: } catch (XMLStreamException e) { aefimov@1443: throw new IOException("Error closing XML stream", e); aefimov@1443: } aefimov@1443: } aefimov@1443: } ohair@286: }