Sun, 18 Jun 2017 23:18:45 +0100
8172297: In java 8, the marshalling with JAX-WS does not escape carriage return
Reviewed-by: lancea
1 /*
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.xml.internal.bind.v2.runtime.output;
28 import java.io.IOException;
30 import javax.xml.stream.XMLEventFactory;
31 import javax.xml.stream.XMLEventWriter;
32 import javax.xml.stream.XMLStreamException;
33 import javax.xml.stream.events.Attribute;
34 import javax.xml.stream.events.Characters;
36 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
38 import org.xml.sax.SAXException;
40 /**
41 * {@link XmlOutput} that writes to StAX {@link XMLEventWriter}.
42 *
43 * @author Kohsuke Kawaguchi
44 */
45 public class XMLEventWriterOutput extends XmlOutputAbstractImpl {
46 private final XMLEventWriter out;
47 private final XMLEventFactory ef;
49 /** One whitespace. */
50 private final Characters sp;
52 public XMLEventWriterOutput(XMLEventWriter out) {
53 this.out = out;
54 ef = XMLEventFactory.newInstance();
55 sp = ef.createCharacters(" ");
56 }
58 // not called if we are generating fragments
59 @Override
60 public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException {
61 super.startDocument(serializer, fragment,nsUriIndex2prefixIndex,nsContext);
62 if(!fragment)
63 out.add(ef.createStartDocument());
64 }
66 public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException {
67 if(!fragment) {
68 out.add(ef.createEndDocument());
69 out.flush();
70 }
71 super.endDocument(fragment);
72 }
74 public void beginStartTag(int prefix, String localName) throws IOException, XMLStreamException {
75 out.add(
76 ef.createStartElement(
77 nsContext.getPrefix(prefix),
78 nsContext.getNamespaceURI(prefix),
79 localName));
81 NamespaceContextImpl.Element nse = nsContext.getCurrent();
82 if(nse.count()>0) {
83 for( int i=nse.count()-1; i>=0; i-- ) {
84 String uri = nse.getNsUri(i);
85 if(uri.length()==0 && nse.getBase()==1)
86 continue; // no point in definint xmlns='' on the root
87 out.add(ef.createNamespace(nse.getPrefix(i),uri));
88 }
89 }
90 }
92 public void attribute(int prefix, String localName, String value) throws IOException, XMLStreamException {
93 Attribute att;
94 if(prefix==-1)
95 att = ef.createAttribute(localName,value);
96 else
97 att = ef.createAttribute(
98 nsContext.getPrefix(prefix),
99 nsContext.getNamespaceURI(prefix),
100 localName, value);
102 out.add(att);
103 }
105 public void endStartTag() throws IOException, SAXException {
106 // noop
107 }
109 public void endTag(int prefix, String localName) throws IOException, SAXException, XMLStreamException {
110 out.add(
111 ef.createEndElement(
112 nsContext.getPrefix(prefix),
113 nsContext.getNamespaceURI(prefix),
114 localName));
115 }
117 public void text(String value, boolean needsSeparatingWhitespace) throws IOException, SAXException, XMLStreamException {
118 if(needsSeparatingWhitespace)
119 out.add(sp);
120 out.add(ef.createCharacters(value));
121 }
123 public void text(Pcdata value, boolean needsSeparatingWhitespace) throws IOException, SAXException, XMLStreamException {
124 text(value.toString(),needsSeparatingWhitespace);
125 }
126 }