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

changeset 1634
6ac8c8bf6b78
parent 1609
09b083e0759c
child 1620
6df7b161ae4a
equal deleted inserted replaced
1633:479db640b75a 1634:6ac8c8bf6b78
1 /* 1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this 7 * published by the Free Software Foundation. Oracle designates this
31 31
32 import javax.xml.stream.XMLStreamException; 32 import javax.xml.stream.XMLStreamException;
33 import javax.xml.stream.XMLStreamWriter; 33 import javax.xml.stream.XMLStreamWriter;
34 34
35 import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler; 35 import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler;
36 import com.sun.xml.internal.bind.marshaller.NoEscapeHandler;
36 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; 37 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
37 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; 38 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
38 39
39 import org.xml.sax.SAXException; 40 import org.xml.sax.SAXException;
40 41
69 } catch (Exception e) { 70 } catch (Exception e) {
70 } 71 }
71 } 72 }
72 73
73 CharacterEscapeHandler xmlStreamEscapeHandler = escapeHandler != null ? 74 CharacterEscapeHandler xmlStreamEscapeHandler = escapeHandler != null ?
74 escapeHandler : NewLineEscapeHandler.theInstance; 75 escapeHandler : NoEscapeHandler.theInstance;
75 76
76 // otherwise the normal writer. 77 // otherwise the normal writer.
77 return new XMLStreamWriterOutput(out, xmlStreamEscapeHandler); 78 return new XMLStreamWriterOutput(out, xmlStreamEscapeHandler);
78 } 79 }
79 80
215 } catch (Throwable e) { 216 } catch (Throwable e) {
216 return null; 217 return null;
217 } 218 }
218 } 219 }
219 220
220
221 /**
222 * Performs character escaping only for new lines.
223 */
224 private static class NewLineEscapeHandler implements CharacterEscapeHandler {
225
226 public static final NewLineEscapeHandler theInstance = new NewLineEscapeHandler();
227
228 @Override
229 public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
230 int limit = start+length;
231 int lastEscaped = start;
232
233 for (int i = start; i < limit; i++) {
234 char c = ch[i];
235 if (c == '\r' || c == '\n') {
236 if (i != lastEscaped) {
237 out.write(ch, lastEscaped, i - lastEscaped);
238 }
239 lastEscaped = i + 1;
240 if (out instanceof XmlStreamOutWriterAdapter) {
241 try {
242 ((XmlStreamOutWriterAdapter)out).writeEntityRef("#x" + Integer.toHexString(c));
243 } catch (XMLStreamException e) {
244 throw new IOException("Error writing xml stream", e);
245 }
246 } else {
247 out.write("&#x");
248 out.write(Integer.toHexString(c));
249 out.write(';');
250 }
251 }
252 }
253 if (lastEscaped != limit) {
254 out.write(ch, lastEscaped, length - lastEscaped);
255 }
256 }
257 }
258
259 private static final class XmlStreamOutWriterAdapter extends Writer { 221 private static final class XmlStreamOutWriterAdapter extends Writer {
260 222
261 private final XMLStreamWriter writer; 223 private final XMLStreamWriter writer;
262 224
263 private XmlStreamOutWriterAdapter(XMLStreamWriter writer) { 225 private XmlStreamOutWriterAdapter(XMLStreamWriter writer) {

mercurial