src/share/jaxws_classes/com/sun/xml/internal/txw2/output/SaxSerializer.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;
    29 import org.xml.sax.ContentHandler;
    30 import org.xml.sax.SAXException;
    31 import org.xml.sax.ext.LexicalHandler;
    32 import org.xml.sax.helpers.AttributesImpl;
    34 import javax.xml.transform.sax.SAXResult;
    35 import java.util.Stack;
    37 /**
    38  * {@link XmlSerializer} for {@link SAXResult} and {@link ContentHandler}.
    39  *
    40  * @author Ryan.Shoemaker@Sun.COM
    41  */
    42 public class SaxSerializer implements XmlSerializer {
    44     private final ContentHandler writer;
    45     private final LexicalHandler lexical;
    47     public SaxSerializer(ContentHandler handler) {
    48         this(handler,null,true);
    49     }
    51     /**
    52      * Creates an {@link XmlSerializer} that writes SAX events.
    53      *
    54      * <p>
    55      * Sepcifying a non-null {@link LexicalHandler} allows applications
    56      * to write comments and CDATA sections.
    57      */
    58     public SaxSerializer(ContentHandler handler,LexicalHandler lex) {
    59         this(handler, lex, true);
    60     }
    62     public SaxSerializer(ContentHandler handler,LexicalHandler lex, boolean indenting) {
    63         if(!indenting) {
    64             writer = handler;
    65             lexical = lex;
    66         } else {
    67             IndentingXMLFilter indenter = new IndentingXMLFilter(handler, lex);
    68             writer = indenter;
    69             lexical = indenter;
    70         }
    71     }
    73     public SaxSerializer(SAXResult result) {
    74         this(result.getHandler(),result.getLexicalHandler());
    75     }
    78     // XmlSerializer implementation
    80     public void startDocument() {
    81         try {
    82             writer.startDocument();
    83         } catch (SAXException e) {
    84             throw new TxwException(e);
    85         }
    86     }
    88     // namespace prefix bindings
    89     // add in #writeXmlns and fired in #endStartTag
    90     private final Stack<String> prefixBindings = new Stack<String>();
    92     public void writeXmlns(String prefix, String uri) {
    93         // defend against parsers that pass null in for "xmlns" prefix
    94         if (prefix == null) {
    95             prefix = "";
    96         }
    98         if (prefix.equals("xml")) {
    99             return;
   100         }
   102         prefixBindings.add(uri);
   103         prefixBindings.add(prefix);
   104     }
   106     // element stack
   107     private final Stack<String> elementBindings = new Stack<String>();
   109     public void beginStartTag(String uri, String localName, String prefix) {
   110         // save element bindings for #endTag
   111         elementBindings.add(getQName(prefix, localName));
   112         elementBindings.add(localName);
   113         elementBindings.add(uri);
   114     }
   116     // attribute storage
   117     // attrs are buffered in #writeAttribute and sent to the content
   118     // handler in #endStartTag
   119     private final AttributesImpl attrs = new AttributesImpl();
   121     public void writeAttribute(String uri, String localName, String prefix, StringBuilder value) {
   122         attrs.addAttribute(uri,
   123                 localName,
   124                 getQName(prefix, localName),
   125                 "CDATA",
   126                 value.toString());
   127     }
   129     public void endStartTag(String uri, String localName, String prefix) {
   130         try {
   131             while (prefixBindings.size() != 0) {
   132                 writer.startPrefixMapping(prefixBindings.pop(), // prefix
   133                         prefixBindings.pop()   // uri
   134                 );
   135             }
   137             writer.startElement(uri,
   138                     localName,
   139                     getQName(prefix, localName),
   140                     attrs);
   142             attrs.clear();
   143         } catch (SAXException e) {
   144             throw new TxwException(e);
   145         }
   146     }
   148     public void endTag() {
   149         try {
   150             writer.endElement(elementBindings.pop(), // uri
   151                     elementBindings.pop(), // localName
   152                     elementBindings.pop()  // qname
   153             );
   154         } catch (SAXException e) {
   155             throw new TxwException(e);
   156         }
   157     }
   159     public void text(StringBuilder text) {
   160         try {
   161             writer.characters(text.toString().toCharArray(), 0, text.length());
   162         } catch (SAXException e) {
   163             throw new TxwException(e);
   164         }
   165     }
   167     public void cdata(StringBuilder text) {
   168         if(lexical==null)
   169             throw new UnsupportedOperationException("LexicalHandler is needed to write PCDATA");
   171         try {
   172             lexical.startCDATA();
   173             text(text);
   174             lexical.endCDATA();
   175         } catch (SAXException e) {
   176             throw new TxwException(e);
   177         }
   178     }
   180     public void comment(StringBuilder comment) {
   181         try {
   182             if(lexical==null)
   183                 throw new UnsupportedOperationException("LexicalHandler is needed to write comments");
   184             else
   185                 lexical.comment(comment.toString().toCharArray(), 0, comment.length() );
   186         } catch (SAXException e) {
   187             throw new TxwException(e);
   188         }
   189     }
   191     public void endDocument() {
   192         try {
   193             writer.endDocument();
   194         } catch (SAXException e) {
   195             throw new TxwException(e);
   196         }
   197     }
   199     public void flush() {
   200         // noop
   201     }
   203     // other methods
   204     private static String getQName(String prefix, String localName) {
   205         final String qName;
   206         if (prefix == null || prefix.length() == 0)
   207             qName = localName;
   208         else
   209             qName = prefix + ':' + localName;
   211         return qName;
   212     }
   213 }

mercurial