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

     1 /*
     2  * Copyright (c) 2005, 2010, 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.txw2.output;
    28 import com.sun.xml.internal.txw2.TxwException;
    30 import javax.xml.transform.stream.StreamResult;
    31 import java.io.BufferedWriter;
    32 import java.io.FileOutputStream;
    33 import java.io.IOException;
    34 import java.io.OutputStream;
    35 import java.io.OutputStreamWriter;
    36 import java.io.Writer;
    37 import java.io.UnsupportedEncodingException;
    39 /**
    40  * {@link XmlSerializer} for {@link javax.xml.transform.stream.StreamResult}.
    41  *
    42  * @author Ryan.Shoemaker@Sun.COM
    43  */
    44 public class StreamSerializer implements XmlSerializer {
    46     // delegate to SaxSerializer
    47     private final SaxSerializer serializer;
    49     private final XMLWriter writer;
    51     public StreamSerializer(OutputStream out) {
    52         this(createWriter(out));
    53     }
    55     public StreamSerializer(OutputStream out,String encoding) throws UnsupportedEncodingException {
    56         this(createWriter(out,encoding));
    57     }
    59     public StreamSerializer(Writer out) {
    60         this(new StreamResult(out));
    61     }
    63     public StreamSerializer(StreamResult streamResult) {
    64         // if this method opened a stream, let it close it
    65         final OutputStream[] autoClose = new OutputStream[1];
    67         if (streamResult.getWriter() != null)
    68             writer = createWriter(streamResult.getWriter());
    69         else if (streamResult.getOutputStream() != null)
    70             writer = createWriter(streamResult.getOutputStream());
    71         else if (streamResult.getSystemId() != null) {
    72             String fileURL = streamResult.getSystemId();
    74             fileURL = convertURL(fileURL);
    76             try {
    77                 FileOutputStream fos = new FileOutputStream(fileURL);
    78                 autoClose[0] = fos;
    79                 writer = createWriter(fos);
    80             } catch (IOException e) {
    81                 throw new TxwException(e);
    82             }
    83         } else
    84             throw new IllegalArgumentException();
    86         // now delegate to the SaxSerializer
    87         serializer = new SaxSerializer(writer,writer,false) {
    88             public void endDocument() {
    89                 super.endDocument();
    90                 if(autoClose[0]!=null) {
    91                     try {
    92                         autoClose[0].close();
    93                     } catch (IOException e) {
    94                         throw new TxwException(e);
    95                     }
    96                     autoClose[0] = null;
    97                 }
    98             }
    99         };
   100     }
   102     private StreamSerializer(XMLWriter writer) {
   103         this.writer = writer;
   104         // now delegate to the SaxSerializer
   105         serializer = new SaxSerializer(writer,writer,false);
   106     }
   108     private String convertURL(String url) {
   109         url = url.replace('\\', '/');
   110         url = url.replaceAll("//","/");
   111         url = url.replaceAll("//","/");
   112         if (url.startsWith("file:/")) {
   113             if (url.substring(6).indexOf(":") > 0)
   114                 url = url.substring(6);
   115             else
   116                 url = url.substring(5);
   117         } // otherwise assume that it's a file name
   118         return url;
   119     }
   121     // XmlSerializer api's - delegate to SaxSerializer
   122     public void startDocument() {
   123         serializer.startDocument();
   124     }
   126     public void beginStartTag(String uri, String localName, String prefix) {
   127         serializer.beginStartTag(uri, localName, prefix);
   128     }
   130     public void writeAttribute(String uri, String localName, String prefix, StringBuilder value) {
   131         serializer.writeAttribute(uri, localName, prefix, value);
   132     }
   134     public void writeXmlns(String prefix, String uri) {
   135         serializer.writeXmlns(prefix, uri);
   136     }
   138     public void endStartTag(String uri, String localName, String prefix) {
   139         serializer.endStartTag(uri, localName, prefix);
   140     }
   142     public void endTag() {
   143         serializer.endTag();
   144     }
   146     public void text(StringBuilder text) {
   147         serializer.text(text);
   148     }
   150     public void cdata(StringBuilder text) {
   151         serializer.cdata(text);
   152     }
   154     public void comment(StringBuilder comment) {
   155         serializer.comment(comment);
   156     }
   158     public void endDocument() {
   159         serializer.endDocument();
   160     }
   162     public void flush() {
   163         serializer.flush();
   164         try {
   165             writer.flush();
   166         } catch (IOException e) {
   167             throw new TxwException(e);
   168         }
   169     }
   171     // other supporting code
   172     private static XMLWriter createWriter(Writer w) {
   173         // buffering improves the performance
   174         DataWriter dw = new DataWriter(new BufferedWriter(w));
   175         dw.setIndentStep("  ");
   176         return dw;
   177     }
   179     private static XMLWriter createWriter(OutputStream os, String encoding) throws UnsupportedEncodingException {
   180         XMLWriter writer = createWriter(new OutputStreamWriter(os,encoding));
   181         writer.setEncoding(encoding);
   182         return writer;
   183     }
   185     private static XMLWriter createWriter(OutputStream os) {
   186         try {
   187             return createWriter(os,"UTF-8");
   188         } catch (UnsupportedEncodingException e) {
   189             // UTF-8 is supported on all platforms.
   190             throw new Error(e);
   191         }
   192     }
   194 }

mercurial