src/share/jaxws_classes/com/sun/xml/internal/txw2/output/StreamSerializer.java

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

author
aefimov
date
Sun, 18 Jun 2017 23:18:45 +0100
changeset 1443
dffc222439a1
parent 0
373ffda63c9a
permissions
-rw-r--r--

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

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.txw2.output;
aoqi@0 27
aoqi@0 28 import com.sun.xml.internal.txw2.TxwException;
aoqi@0 29
aoqi@0 30 import javax.xml.transform.stream.StreamResult;
aoqi@0 31 import java.io.BufferedWriter;
aoqi@0 32 import java.io.FileOutputStream;
aoqi@0 33 import java.io.IOException;
aoqi@0 34 import java.io.OutputStream;
aoqi@0 35 import java.io.OutputStreamWriter;
aoqi@0 36 import java.io.Writer;
aoqi@0 37 import java.io.UnsupportedEncodingException;
aoqi@0 38
aoqi@0 39 /**
aoqi@0 40 * {@link XmlSerializer} for {@link javax.xml.transform.stream.StreamResult}.
aoqi@0 41 *
aoqi@0 42 * @author Ryan.Shoemaker@Sun.COM
aoqi@0 43 */
aoqi@0 44 public class StreamSerializer implements XmlSerializer {
aoqi@0 45
aoqi@0 46 // delegate to SaxSerializer
aoqi@0 47 private final SaxSerializer serializer;
aoqi@0 48
aoqi@0 49 private final XMLWriter writer;
aoqi@0 50
aoqi@0 51 public StreamSerializer(OutputStream out) {
aoqi@0 52 this(createWriter(out));
aoqi@0 53 }
aoqi@0 54
aoqi@0 55 public StreamSerializer(OutputStream out,String encoding) throws UnsupportedEncodingException {
aoqi@0 56 this(createWriter(out,encoding));
aoqi@0 57 }
aoqi@0 58
aoqi@0 59 public StreamSerializer(Writer out) {
aoqi@0 60 this(new StreamResult(out));
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 public StreamSerializer(StreamResult streamResult) {
aoqi@0 64 // if this method opened a stream, let it close it
aoqi@0 65 final OutputStream[] autoClose = new OutputStream[1];
aoqi@0 66
aoqi@0 67 if (streamResult.getWriter() != null)
aoqi@0 68 writer = createWriter(streamResult.getWriter());
aoqi@0 69 else if (streamResult.getOutputStream() != null)
aoqi@0 70 writer = createWriter(streamResult.getOutputStream());
aoqi@0 71 else if (streamResult.getSystemId() != null) {
aoqi@0 72 String fileURL = streamResult.getSystemId();
aoqi@0 73
aoqi@0 74 fileURL = convertURL(fileURL);
aoqi@0 75
aoqi@0 76 try {
aoqi@0 77 FileOutputStream fos = new FileOutputStream(fileURL);
aoqi@0 78 autoClose[0] = fos;
aoqi@0 79 writer = createWriter(fos);
aoqi@0 80 } catch (IOException e) {
aoqi@0 81 throw new TxwException(e);
aoqi@0 82 }
aoqi@0 83 } else
aoqi@0 84 throw new IllegalArgumentException();
aoqi@0 85
aoqi@0 86 // now delegate to the SaxSerializer
aoqi@0 87 serializer = new SaxSerializer(writer,writer,false) {
aoqi@0 88 public void endDocument() {
aoqi@0 89 super.endDocument();
aoqi@0 90 if(autoClose[0]!=null) {
aoqi@0 91 try {
aoqi@0 92 autoClose[0].close();
aoqi@0 93 } catch (IOException e) {
aoqi@0 94 throw new TxwException(e);
aoqi@0 95 }
aoqi@0 96 autoClose[0] = null;
aoqi@0 97 }
aoqi@0 98 }
aoqi@0 99 };
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 private StreamSerializer(XMLWriter writer) {
aoqi@0 103 this.writer = writer;
aoqi@0 104 // now delegate to the SaxSerializer
aoqi@0 105 serializer = new SaxSerializer(writer,writer,false);
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 private String convertURL(String url) {
aoqi@0 109 url = url.replace('\\', '/');
aoqi@0 110 url = url.replaceAll("//","/");
aoqi@0 111 url = url.replaceAll("//","/");
aoqi@0 112 if (url.startsWith("file:/")) {
aoqi@0 113 if (url.substring(6).indexOf(":") > 0)
aoqi@0 114 url = url.substring(6);
aoqi@0 115 else
aoqi@0 116 url = url.substring(5);
aoqi@0 117 } // otherwise assume that it's a file name
aoqi@0 118 return url;
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 // XmlSerializer api's - delegate to SaxSerializer
aoqi@0 122 public void startDocument() {
aoqi@0 123 serializer.startDocument();
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 public void beginStartTag(String uri, String localName, String prefix) {
aoqi@0 127 serializer.beginStartTag(uri, localName, prefix);
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 public void writeAttribute(String uri, String localName, String prefix, StringBuilder value) {
aoqi@0 131 serializer.writeAttribute(uri, localName, prefix, value);
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 public void writeXmlns(String prefix, String uri) {
aoqi@0 135 serializer.writeXmlns(prefix, uri);
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 public void endStartTag(String uri, String localName, String prefix) {
aoqi@0 139 serializer.endStartTag(uri, localName, prefix);
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 public void endTag() {
aoqi@0 143 serializer.endTag();
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 public void text(StringBuilder text) {
aoqi@0 147 serializer.text(text);
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 public void cdata(StringBuilder text) {
aoqi@0 151 serializer.cdata(text);
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 public void comment(StringBuilder comment) {
aoqi@0 155 serializer.comment(comment);
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 public void endDocument() {
aoqi@0 159 serializer.endDocument();
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 public void flush() {
aoqi@0 163 serializer.flush();
aoqi@0 164 try {
aoqi@0 165 writer.flush();
aoqi@0 166 } catch (IOException e) {
aoqi@0 167 throw new TxwException(e);
aoqi@0 168 }
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 // other supporting code
aoqi@0 172 private static XMLWriter createWriter(Writer w) {
aoqi@0 173 // buffering improves the performance
aoqi@0 174 DataWriter dw = new DataWriter(new BufferedWriter(w));
aoqi@0 175 dw.setIndentStep(" ");
aoqi@0 176 return dw;
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 private static XMLWriter createWriter(OutputStream os, String encoding) throws UnsupportedEncodingException {
aoqi@0 180 XMLWriter writer = createWriter(new OutputStreamWriter(os,encoding));
aoqi@0 181 writer.setEncoding(encoding);
aoqi@0 182 return writer;
aoqi@0 183 }
aoqi@0 184
aoqi@0 185 private static XMLWriter createWriter(OutputStream os) {
aoqi@0 186 try {
aoqi@0 187 return createWriter(os,"UTF-8");
aoqi@0 188 } catch (UnsupportedEncodingException e) {
aoqi@0 189 // UTF-8 is supported on all platforms.
aoqi@0 190 throw new Error(e);
aoqi@0 191 }
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 }

mercurial