src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/output/XMLStreamWriterOutput.java

Sun, 18 Jun 2017 23:18:45 +0100

author
aefimov
date
Sun, 18 Jun 2017 23:18:45 +0100
changeset 1491
d5c5a205d7fb
parent 1443
dffc222439a1
child 1546
dc8316632248
child 1609
09b083e0759c
permissions
-rw-r--r--

8172297: In java 8, the marshalling with JAX-WS does not escape carriage return
Reviewed-by: lancea

ohair@286 1 /*
aefimov@1443 2 * Copyright (c) 1997, 2017, 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;
aefimov@1443 29 import java.io.Writer;
ohair@286 30 import java.lang.reflect.Constructor;
ohair@286 31
ohair@286 32 import javax.xml.stream.XMLStreamException;
ohair@286 33 import javax.xml.stream.XMLStreamWriter;
ohair@286 34
aefimov@1443 35 import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler;
ohair@286 36 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
ohair@286 37 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
ohair@286 38
ohair@286 39 import org.xml.sax.SAXException;
ohair@286 40
ohair@286 41 /**
ohair@286 42 * {@link XmlOutput} that writes to StAX {@link XMLStreamWriter}.
ohair@286 43 * <p>
ohair@286 44 * TODO:
ohair@286 45 * Finding the optimized FI implementations is a bit hacky and not very
ohair@286 46 * extensible. Can we use the service provider mechanism in general for
ohair@286 47 * concrete implementations of XmlOutputAbstractImpl.
ohair@286 48 *
ohair@286 49 * @author Kohsuke Kawaguchi
ohair@286 50 */
ohair@286 51 public class XMLStreamWriterOutput extends XmlOutputAbstractImpl {
ohair@286 52
ohair@286 53 /**
ohair@286 54 * Creates a new {@link XmlOutput} from a {@link XMLStreamWriter}.
ohair@286 55 * This method recognizes an FI StAX writer.
ohair@286 56 */
aefimov@1443 57 public static XmlOutput create(XMLStreamWriter out, JAXBContextImpl context, CharacterEscapeHandler escapeHandler) {
ohair@286 58 // try optimized path
ohair@286 59 final Class writerClass = out.getClass();
ohair@286 60 if (writerClass==FI_STAX_WRITER_CLASS) {
ohair@286 61 try {
ohair@286 62 return FI_OUTPUT_CTOR.newInstance(out, context);
ohair@286 63 } catch (Exception e) {
ohair@286 64 }
ohair@286 65 }
ohair@286 66 if (STAXEX_WRITER_CLASS!=null && STAXEX_WRITER_CLASS.isAssignableFrom(writerClass)) {
ohair@286 67 try {
ohair@286 68 return STAXEX_OUTPUT_CTOR.newInstance(out);
ohair@286 69 } catch (Exception e) {
ohair@286 70 }
ohair@286 71 }
ohair@286 72
aefimov@1443 73 CharacterEscapeHandler xmlStreamEscapeHandler = escapeHandler != null ?
aefimov@1443 74 escapeHandler : NewLineEscapeHandler.theInstance;
aefimov@1443 75
ohair@286 76 // otherwise the normal writer.
aefimov@1443 77 return new XMLStreamWriterOutput(out, xmlStreamEscapeHandler);
ohair@286 78 }
ohair@286 79
ohair@286 80
ohair@286 81 private final XMLStreamWriter out;
ohair@286 82
aefimov@1443 83 private final CharacterEscapeHandler escapeHandler;
aefimov@1443 84
aefimov@1443 85 private final XmlStreamOutWriterAdapter writerWrapper;
aefimov@1443 86
ohair@286 87 protected final char[] buf = new char[256];
ohair@286 88
aefimov@1443 89 protected XMLStreamWriterOutput(XMLStreamWriter out, CharacterEscapeHandler escapeHandler) {
ohair@286 90 this.out = out;
aefimov@1443 91 this.escapeHandler = escapeHandler;
aefimov@1443 92 this.writerWrapper = new XmlStreamOutWriterAdapter(out);
ohair@286 93 }
ohair@286 94
ohair@286 95 // not called if we are generating fragments
ohair@286 96 @Override
ohair@286 97 public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException {
ohair@286 98 super.startDocument(serializer, fragment,nsUriIndex2prefixIndex,nsContext);
ohair@286 99 if(!fragment)
ohair@286 100 out.writeStartDocument();
ohair@286 101 }
ohair@286 102
ohair@286 103 @Override
ohair@286 104 public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException {
ohair@286 105 if(!fragment) {
ohair@286 106 out.writeEndDocument();
ohair@286 107 out.flush();
ohair@286 108 }
ohair@286 109 super.endDocument(fragment);
ohair@286 110 }
ohair@286 111
ohair@286 112 public void beginStartTag(int prefix, String localName) throws IOException, XMLStreamException {
ohair@286 113 out.writeStartElement(
ohair@286 114 nsContext.getPrefix(prefix),
ohair@286 115 localName,
ohair@286 116 nsContext.getNamespaceURI(prefix));
ohair@286 117
ohair@286 118 NamespaceContextImpl.Element nse = nsContext.getCurrent();
ohair@286 119 if(nse.count()>0) {
ohair@286 120 for( int i=nse.count()-1; i>=0; i-- ) {
ohair@286 121 String uri = nse.getNsUri(i);
ohair@286 122 if(uri.length()==0 && nse.getBase()==1)
ohair@286 123 continue; // no point in definint xmlns='' on the root
ohair@286 124 out.writeNamespace(nse.getPrefix(i),uri);
ohair@286 125 }
ohair@286 126 }
ohair@286 127 }
ohair@286 128
ohair@286 129 public void attribute(int prefix, String localName, String value) throws IOException, XMLStreamException {
ohair@286 130 if(prefix==-1)
ohair@286 131 out.writeAttribute(localName,value);
ohair@286 132 else
ohair@286 133 out.writeAttribute(
ohair@286 134 nsContext.getPrefix(prefix),
ohair@286 135 nsContext.getNamespaceURI(prefix),
ohair@286 136 localName, value);
ohair@286 137 }
ohair@286 138
ohair@286 139 public void endStartTag() throws IOException, SAXException {
ohair@286 140 // noop
ohair@286 141 }
ohair@286 142
ohair@286 143 public void endTag(int prefix, String localName) throws IOException, SAXException, XMLStreamException {
ohair@286 144 out.writeEndElement();
ohair@286 145 }
ohair@286 146
ohair@286 147 public void text(String value, boolean needsSeparatingWhitespace) throws IOException, SAXException, XMLStreamException {
ohair@286 148 if(needsSeparatingWhitespace)
ohair@286 149 out.writeCharacters(" ");
aefimov@1443 150 escapeHandler.escape(value.toCharArray(), 0, value.length(), false, writerWrapper);
ohair@286 151 }
ohair@286 152
ohair@286 153 public void text(Pcdata value, boolean needsSeparatingWhitespace) throws IOException, SAXException, XMLStreamException {
ohair@286 154 if(needsSeparatingWhitespace)
ohair@286 155 out.writeCharacters(" ");
ohair@286 156
ohair@286 157 int len = value.length();
ohair@286 158 if(len <buf.length) {
ohair@286 159 value.writeTo(buf,0);
ohair@286 160 out.writeCharacters(buf,0,len);
ohair@286 161 } else {
ohair@286 162 out.writeCharacters(value.toString());
ohair@286 163 }
ohair@286 164 }
ohair@286 165
ohair@286 166 /**
ohair@286 167 * Reference to FI's XMLStreamWriter class, if FI can be loaded.
ohair@286 168 */
ohair@286 169 private static final Class FI_STAX_WRITER_CLASS = initFIStAXWriterClass();
ohair@286 170 private static final Constructor<? extends XmlOutput> FI_OUTPUT_CTOR = initFastInfosetOutputClass();
ohair@286 171
ohair@286 172 private static Class initFIStAXWriterClass() {
ohair@286 173 try {
mkos@514 174 Class<?> llfisw = Class.forName("com.sun.xml.internal.org.jvnet.fastinfoset.stax.LowLevelFastInfosetStreamWriter");
mkos@514 175 Class<?> sds = Class.forName("com.sun.xml.internal.fastinfoset.stax.StAXDocumentSerializer");
ohair@286 176 // Check if StAXDocumentSerializer implements LowLevelFastInfosetStreamWriter
ohair@286 177 if (llfisw.isAssignableFrom(sds))
ohair@286 178 return sds;
ohair@286 179 else
ohair@286 180 return null;
ohair@286 181 } catch (Throwable e) {
ohair@286 182 return null;
ohair@286 183 }
ohair@286 184 }
ohair@286 185
ohair@286 186 private static Constructor<? extends XmlOutput> initFastInfosetOutputClass() {
ohair@286 187 try {
ohair@286 188 if (FI_STAX_WRITER_CLASS == null)
ohair@286 189 return null;
mkos@514 190 Class c = Class.forName("com.sun.xml.internal.bind.v2.runtime.output.FastInfosetStreamWriterOutput");
ohair@286 191 return c.getConstructor(FI_STAX_WRITER_CLASS, JAXBContextImpl.class);
ohair@286 192 } catch (Throwable e) {
ohair@286 193 return null;
ohair@286 194 }
ohair@286 195 }
ohair@286 196
ohair@286 197 //
ohair@286 198 // StAX-ex
ohair@286 199 //
ohair@286 200 private static final Class STAXEX_WRITER_CLASS = initStAXExWriterClass();
ohair@286 201 private static final Constructor<? extends XmlOutput> STAXEX_OUTPUT_CTOR = initStAXExOutputClass();
ohair@286 202
ohair@286 203 private static Class initStAXExWriterClass() {
ohair@286 204 try {
mkos@514 205 return Class.forName("com.sun.xml.internal.org.jvnet.staxex.XMLStreamWriterEx");
ohair@286 206 } catch (Throwable e) {
ohair@286 207 return null;
ohair@286 208 }
ohair@286 209 }
ohair@286 210
ohair@286 211 private static Constructor<? extends XmlOutput> initStAXExOutputClass() {
ohair@286 212 try {
mkos@514 213 Class c = Class.forName("com.sun.xml.internal.bind.v2.runtime.output.StAXExStreamWriterOutput");
ohair@286 214 return c.getConstructor(STAXEX_WRITER_CLASS);
ohair@286 215 } catch (Throwable e) {
ohair@286 216 return null;
ohair@286 217 }
ohair@286 218 }
ohair@286 219
aefimov@1443 220
aefimov@1443 221 /**
aefimov@1443 222 * Performs character escaping only for new lines.
aefimov@1443 223 */
aefimov@1443 224 private static class NewLineEscapeHandler implements CharacterEscapeHandler {
aefimov@1443 225
aefimov@1443 226 public static final NewLineEscapeHandler theInstance = new NewLineEscapeHandler();
aefimov@1443 227
aefimov@1443 228 @Override
aefimov@1443 229 public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
aefimov@1443 230 int limit = start+length;
aefimov@1443 231 int lastEscaped = start;
aefimov@1443 232
aefimov@1443 233 for (int i = start; i < limit; i++) {
aefimov@1443 234 char c = ch[i];
aefimov@1443 235 if (c == '\r' || c == '\n') {
aefimov@1443 236 if (i != lastEscaped) {
aefimov@1443 237 out.write(ch, lastEscaped, i - lastEscaped);
aefimov@1443 238 }
aefimov@1443 239 lastEscaped = i + 1;
aefimov@1443 240 if (out instanceof XmlStreamOutWriterAdapter) {
aefimov@1443 241 try {
aefimov@1443 242 ((XmlStreamOutWriterAdapter)out).writeEntityRef("#x" + Integer.toHexString(c));
aefimov@1443 243 } catch (XMLStreamException e) {
aefimov@1443 244 throw new IOException("Error writing xml stream", e);
aefimov@1443 245 }
aefimov@1443 246 } else {
aefimov@1443 247 out.write("&#x");
aefimov@1443 248 out.write(Integer.toHexString(c));
aefimov@1443 249 out.write(';');
aefimov@1443 250 }
aefimov@1443 251 }
aefimov@1443 252 }
aefimov@1443 253 if (lastEscaped != limit) {
aefimov@1443 254 out.write(ch, lastEscaped, length - lastEscaped);
aefimov@1443 255 }
aefimov@1443 256 }
aefimov@1443 257 }
aefimov@1443 258
aefimov@1443 259 private static final class XmlStreamOutWriterAdapter extends Writer {
aefimov@1443 260
aefimov@1443 261 private final XMLStreamWriter writer;
aefimov@1443 262
aefimov@1443 263 private XmlStreamOutWriterAdapter(XMLStreamWriter writer) {
aefimov@1443 264 this.writer = writer;
aefimov@1443 265 }
aefimov@1443 266
aefimov@1443 267 @Override
aefimov@1443 268 public void write(char[] cbuf, int off, int len) throws IOException {
aefimov@1443 269 try {
aefimov@1443 270 writer.writeCharacters(cbuf, off, len);
aefimov@1443 271 } catch (XMLStreamException e) {
aefimov@1443 272 throw new IOException("Error writing XML stream", e);
aefimov@1443 273 }
aefimov@1443 274 }
aefimov@1443 275
aefimov@1443 276 public void writeEntityRef(String entityReference) throws XMLStreamException {
aefimov@1443 277 writer.writeEntityRef(entityReference);
aefimov@1443 278 }
aefimov@1443 279
aefimov@1443 280 @Override
aefimov@1443 281 public void flush() throws IOException {
aefimov@1443 282 try {
aefimov@1443 283 writer.flush();
aefimov@1443 284 } catch (XMLStreamException e) {
aefimov@1443 285 throw new IOException("Error flushing XML stream", e);
aefimov@1443 286 }
aefimov@1443 287 }
aefimov@1443 288
aefimov@1443 289 @Override
aefimov@1443 290 public void close() throws IOException {
aefimov@1443 291 try {
aefimov@1443 292 writer.close();
aefimov@1443 293 } catch (XMLStreamException e) {
aefimov@1443 294 throw new IOException("Error closing XML stream", e);
aefimov@1443 295 }
aefimov@1443 296 }
aefimov@1443 297 }
ohair@286 298 }

mercurial