Fri, 14 Feb 2014 10:53:55 +0100
8025030: Enhance stream handling
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Iaroslav Savytskyi, Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu
ohair@286 | 1 | /* |
mkos@514 | 2 | * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.bind.v2.runtime.output; |
ohair@286 | 27 | |
ohair@286 | 28 | import java.io.IOException; |
ohair@286 | 29 | import java.lang.reflect.Constructor; |
ohair@286 | 30 | |
ohair@286 | 31 | import javax.xml.stream.XMLStreamException; |
ohair@286 | 32 | import javax.xml.stream.XMLStreamWriter; |
ohair@286 | 33 | |
ohair@286 | 34 | import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; |
ohair@286 | 35 | import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; |
ohair@286 | 36 | |
ohair@286 | 37 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl; |
ohair@286 | 38 | import org.xml.sax.SAXException; |
ohair@286 | 39 | |
ohair@286 | 40 | /** |
ohair@286 | 41 | * {@link XmlOutput} that writes to StAX {@link XMLStreamWriter}. |
ohair@286 | 42 | * <p> |
ohair@286 | 43 | * TODO: |
ohair@286 | 44 | * Finding the optimized FI implementations is a bit hacky and not very |
ohair@286 | 45 | * extensible. Can we use the service provider mechanism in general for |
ohair@286 | 46 | * concrete implementations of XmlOutputAbstractImpl. |
ohair@286 | 47 | * |
ohair@286 | 48 | * @author Kohsuke Kawaguchi |
ohair@286 | 49 | */ |
ohair@286 | 50 | public class XMLStreamWriterOutput extends XmlOutputAbstractImpl { |
ohair@286 | 51 | |
ohair@286 | 52 | /** |
ohair@286 | 53 | * Creates a new {@link XmlOutput} from a {@link XMLStreamWriter}. |
ohair@286 | 54 | * This method recognizes an FI StAX writer. |
ohair@286 | 55 | */ |
ohair@286 | 56 | public static XmlOutput create(XMLStreamWriter out, JAXBContextImpl context) { |
ohair@286 | 57 | // try optimized path |
ohair@286 | 58 | final Class writerClass = out.getClass(); |
ohair@286 | 59 | if (writerClass==FI_STAX_WRITER_CLASS) { |
ohair@286 | 60 | try { |
ohair@286 | 61 | return FI_OUTPUT_CTOR.newInstance(out, context); |
ohair@286 | 62 | } catch (Exception e) { |
ohair@286 | 63 | } |
ohair@286 | 64 | } |
ohair@286 | 65 | if (STAXEX_WRITER_CLASS!=null && STAXEX_WRITER_CLASS.isAssignableFrom(writerClass)) { |
ohair@286 | 66 | try { |
ohair@286 | 67 | return STAXEX_OUTPUT_CTOR.newInstance(out); |
ohair@286 | 68 | } catch (Exception e) { |
ohair@286 | 69 | } |
ohair@286 | 70 | } |
ohair@286 | 71 | |
ohair@286 | 72 | // otherwise the normal writer. |
ohair@286 | 73 | return new XMLStreamWriterOutput(out); |
ohair@286 | 74 | } |
ohair@286 | 75 | |
ohair@286 | 76 | |
ohair@286 | 77 | private final XMLStreamWriter out; |
ohair@286 | 78 | |
ohair@286 | 79 | protected final char[] buf = new char[256]; |
ohair@286 | 80 | |
ohair@286 | 81 | protected XMLStreamWriterOutput(XMLStreamWriter out) { |
ohair@286 | 82 | this.out = out; |
ohair@286 | 83 | } |
ohair@286 | 84 | |
ohair@286 | 85 | // not called if we are generating fragments |
ohair@286 | 86 | @Override |
ohair@286 | 87 | public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException { |
ohair@286 | 88 | super.startDocument(serializer, fragment,nsUriIndex2prefixIndex,nsContext); |
ohair@286 | 89 | if(!fragment) |
ohair@286 | 90 | out.writeStartDocument(); |
ohair@286 | 91 | } |
ohair@286 | 92 | |
ohair@286 | 93 | @Override |
ohair@286 | 94 | public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException { |
ohair@286 | 95 | if(!fragment) { |
ohair@286 | 96 | out.writeEndDocument(); |
ohair@286 | 97 | out.flush(); |
ohair@286 | 98 | } |
ohair@286 | 99 | super.endDocument(fragment); |
ohair@286 | 100 | } |
ohair@286 | 101 | |
ohair@286 | 102 | public void beginStartTag(int prefix, String localName) throws IOException, XMLStreamException { |
ohair@286 | 103 | out.writeStartElement( |
ohair@286 | 104 | nsContext.getPrefix(prefix), |
ohair@286 | 105 | localName, |
ohair@286 | 106 | nsContext.getNamespaceURI(prefix)); |
ohair@286 | 107 | |
ohair@286 | 108 | NamespaceContextImpl.Element nse = nsContext.getCurrent(); |
ohair@286 | 109 | if(nse.count()>0) { |
ohair@286 | 110 | for( int i=nse.count()-1; i>=0; i-- ) { |
ohair@286 | 111 | String uri = nse.getNsUri(i); |
ohair@286 | 112 | if(uri.length()==0 && nse.getBase()==1) |
ohair@286 | 113 | continue; // no point in definint xmlns='' on the root |
ohair@286 | 114 | out.writeNamespace(nse.getPrefix(i),uri); |
ohair@286 | 115 | } |
ohair@286 | 116 | } |
ohair@286 | 117 | } |
ohair@286 | 118 | |
ohair@286 | 119 | public void attribute(int prefix, String localName, String value) throws IOException, XMLStreamException { |
ohair@286 | 120 | if(prefix==-1) |
ohair@286 | 121 | out.writeAttribute(localName,value); |
ohair@286 | 122 | else |
ohair@286 | 123 | out.writeAttribute( |
ohair@286 | 124 | nsContext.getPrefix(prefix), |
ohair@286 | 125 | nsContext.getNamespaceURI(prefix), |
ohair@286 | 126 | localName, value); |
ohair@286 | 127 | } |
ohair@286 | 128 | |
ohair@286 | 129 | public void endStartTag() throws IOException, SAXException { |
ohair@286 | 130 | // noop |
ohair@286 | 131 | } |
ohair@286 | 132 | |
ohair@286 | 133 | public void endTag(int prefix, String localName) throws IOException, SAXException, XMLStreamException { |
ohair@286 | 134 | out.writeEndElement(); |
ohair@286 | 135 | } |
ohair@286 | 136 | |
ohair@286 | 137 | public void text(String value, boolean needsSeparatingWhitespace) throws IOException, SAXException, XMLStreamException { |
ohair@286 | 138 | if(needsSeparatingWhitespace) |
ohair@286 | 139 | out.writeCharacters(" "); |
ohair@286 | 140 | out.writeCharacters(value); |
ohair@286 | 141 | } |
ohair@286 | 142 | |
ohair@286 | 143 | public void text(Pcdata value, boolean needsSeparatingWhitespace) throws IOException, SAXException, XMLStreamException { |
ohair@286 | 144 | if(needsSeparatingWhitespace) |
ohair@286 | 145 | out.writeCharacters(" "); |
ohair@286 | 146 | |
ohair@286 | 147 | int len = value.length(); |
ohair@286 | 148 | if(len <buf.length) { |
ohair@286 | 149 | value.writeTo(buf,0); |
ohair@286 | 150 | out.writeCharacters(buf,0,len); |
ohair@286 | 151 | } else { |
ohair@286 | 152 | out.writeCharacters(value.toString()); |
ohair@286 | 153 | } |
ohair@286 | 154 | } |
ohair@286 | 155 | |
ohair@286 | 156 | /** |
ohair@286 | 157 | * Reference to FI's XMLStreamWriter class, if FI can be loaded. |
ohair@286 | 158 | */ |
ohair@286 | 159 | private static final Class FI_STAX_WRITER_CLASS = initFIStAXWriterClass(); |
ohair@286 | 160 | private static final Constructor<? extends XmlOutput> FI_OUTPUT_CTOR = initFastInfosetOutputClass(); |
ohair@286 | 161 | |
ohair@286 | 162 | private static Class initFIStAXWriterClass() { |
ohair@286 | 163 | try { |
mkos@514 | 164 | Class<?> llfisw = Class.forName("com.sun.xml.internal.org.jvnet.fastinfoset.stax.LowLevelFastInfosetStreamWriter"); |
mkos@514 | 165 | Class<?> sds = Class.forName("com.sun.xml.internal.fastinfoset.stax.StAXDocumentSerializer"); |
ohair@286 | 166 | // Check if StAXDocumentSerializer implements LowLevelFastInfosetStreamWriter |
ohair@286 | 167 | if (llfisw.isAssignableFrom(sds)) |
ohair@286 | 168 | return sds; |
ohair@286 | 169 | else |
ohair@286 | 170 | return null; |
ohair@286 | 171 | } catch (Throwable e) { |
ohair@286 | 172 | return null; |
ohair@286 | 173 | } |
ohair@286 | 174 | } |
ohair@286 | 175 | |
ohair@286 | 176 | private static Constructor<? extends XmlOutput> initFastInfosetOutputClass() { |
ohair@286 | 177 | try { |
ohair@286 | 178 | if (FI_STAX_WRITER_CLASS == null) |
ohair@286 | 179 | return null; |
mkos@514 | 180 | Class c = Class.forName("com.sun.xml.internal.bind.v2.runtime.output.FastInfosetStreamWriterOutput"); |
ohair@286 | 181 | return c.getConstructor(FI_STAX_WRITER_CLASS, JAXBContextImpl.class); |
ohair@286 | 182 | } catch (Throwable e) { |
ohair@286 | 183 | return null; |
ohair@286 | 184 | } |
ohair@286 | 185 | } |
ohair@286 | 186 | |
ohair@286 | 187 | // |
ohair@286 | 188 | // StAX-ex |
ohair@286 | 189 | // |
ohair@286 | 190 | private static final Class STAXEX_WRITER_CLASS = initStAXExWriterClass(); |
ohair@286 | 191 | private static final Constructor<? extends XmlOutput> STAXEX_OUTPUT_CTOR = initStAXExOutputClass(); |
ohair@286 | 192 | |
ohair@286 | 193 | private static Class initStAXExWriterClass() { |
ohair@286 | 194 | try { |
mkos@514 | 195 | return Class.forName("com.sun.xml.internal.org.jvnet.staxex.XMLStreamWriterEx"); |
ohair@286 | 196 | } catch (Throwable e) { |
ohair@286 | 197 | return null; |
ohair@286 | 198 | } |
ohair@286 | 199 | } |
ohair@286 | 200 | |
ohair@286 | 201 | private static Constructor<? extends XmlOutput> initStAXExOutputClass() { |
ohair@286 | 202 | try { |
mkos@514 | 203 | Class c = Class.forName("com.sun.xml.internal.bind.v2.runtime.output.StAXExStreamWriterOutput"); |
ohair@286 | 204 | return c.getConstructor(STAXEX_WRITER_CLASS); |
ohair@286 | 205 | } catch (Throwable e) { |
ohair@286 | 206 | return null; |
ohair@286 | 207 | } |
ohair@286 | 208 | } |
ohair@286 | 209 | |
ohair@286 | 210 | } |