src/share/jaxws_classes/com/sun/xml/internal/txw2/output/DataWriter.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 //@@3RD PARTY CODE@@
aoqi@0 27
aoqi@0 28 // DataWriter.java - XML writer for data-oriented files.
aoqi@0 29
aoqi@0 30 package com.sun.xml.internal.txw2.output;
aoqi@0 31
aoqi@0 32 import org.xml.sax.Attributes;
aoqi@0 33 import org.xml.sax.SAXException;
aoqi@0 34
aoqi@0 35 import java.io.Writer;
aoqi@0 36 import java.util.Stack;
aoqi@0 37
aoqi@0 38
aoqi@0 39 /**
aoqi@0 40 * Write data- or field-oriented XML.
aoqi@0 41 *
aoqi@0 42 * <p>This filter pretty-prints field-oriented XML without mixed content.
aoqi@0 43 * all added indentation and newlines will be passed on down
aoqi@0 44 * the filter chain (if any).</p>
aoqi@0 45 *
aoqi@0 46 * <p>In general, all whitespace in an XML document is potentially
aoqi@0 47 * significant, so a general-purpose XML writing tool like the
aoqi@0 48 * {@link XMLWriter} class cannot
aoqi@0 49 * add newlines or indentation.</p>
aoqi@0 50 *
aoqi@0 51 * <p>There is, however, a large class of XML documents where information
aoqi@0 52 * is strictly fielded: each element contains either character data
aoqi@0 53 * or other elements, but not both. For this special case, it is possible
aoqi@0 54 * for a writing tool to provide automatic indentation and newlines
aoqi@0 55 * without requiring extra work from the user. Note that this class
aoqi@0 56 * will likely not yield appropriate results for document-oriented
aoqi@0 57 * XML like XHTML pages, which mix character data and elements together.</p>
aoqi@0 58 *
aoqi@0 59 * <p>This writer will automatically place each start tag on a new line,
aoqi@0 60 * optionally indented if an indent step is provided (by default, there
aoqi@0 61 * is no indentation). If an element contains other elements, the end
aoqi@0 62 * tag will also appear on a new line with leading indentation. Consider,
aoqi@0 63 * for example, the following code:</p>
aoqi@0 64 *
aoqi@0 65 * <pre>
aoqi@0 66 * DataWriter w = new DataWriter();
aoqi@0 67 *
aoqi@0 68 * w.setIndentStep(2);
aoqi@0 69 * w.startDocument();
aoqi@0 70 * w.startElement("Person");
aoqi@0 71 * w.dataElement("name", "Jane Smith");
aoqi@0 72 * w.dataElement("date-of-birth", "1965-05-23");
aoqi@0 73 * w.dataElement("citizenship", "US");
aoqi@0 74 * w.endElement("Person");
aoqi@0 75 * w.endDocument();
aoqi@0 76 * </pre>
aoqi@0 77 *
aoqi@0 78 * <p>This code will produce the following document:</p>
aoqi@0 79 *
aoqi@0 80 * <pre>
aoqi@0 81 * &lt;?xml version="1.0" standalone="yes"?>
aoqi@0 82 *
aoqi@0 83 * &lt;Person>
aoqi@0 84 * &lt;name>Jane Smith&lt;/name>
aoqi@0 85 * &lt;date-of-birth>1965-05-23&lt;/date-of-birth>
aoqi@0 86 * &lt;citizenship>US&lt;/citizenship>
aoqi@0 87 * &lt;/Person>
aoqi@0 88 * </pre>
aoqi@0 89 *
aoqi@0 90 * <p>This class inherits from {@link XMLWriter},
aoqi@0 91 * and provides all of the same support for Namespaces.</p>
aoqi@0 92 *
aoqi@0 93 * @since 1.0
aoqi@0 94 * @author David Megginson, david@megginson.com
aoqi@0 95 * @version 0.2
aoqi@0 96 * @see XMLWriter
aoqi@0 97 */
aoqi@0 98 public class DataWriter extends XMLWriter
aoqi@0 99 {
aoqi@0 100
aoqi@0 101
aoqi@0 102
aoqi@0 103 ////////////////////////////////////////////////////////////////////
aoqi@0 104 // Constructors.
aoqi@0 105 ////////////////////////////////////////////////////////////////////
aoqi@0 106
aoqi@0 107
aoqi@0 108 /**
aoqi@0 109 * Create a new data writer for the specified output.
aoqi@0 110 *
aoqi@0 111 * @param writer The character stream where the XML document
aoqi@0 112 * will be written.
aoqi@0 113 * @param encoding
aoqi@0 114 * If non-null string is specified, it is written as a part
aoqi@0 115 * of the XML declaration.
aoqi@0 116 */
aoqi@0 117 public DataWriter ( Writer writer, String encoding, CharacterEscapeHandler _escapeHandler )
aoqi@0 118 {
aoqi@0 119 super(writer,encoding,_escapeHandler);
aoqi@0 120 }
aoqi@0 121
aoqi@0 122
aoqi@0 123 public DataWriter (Writer writer, String encoding ) {
aoqi@0 124 this( writer, encoding, DumbEscapeHandler.theInstance );
aoqi@0 125 }
aoqi@0 126
aoqi@0 127 public DataWriter (Writer writer) {
aoqi@0 128 this( writer, null, DumbEscapeHandler.theInstance );
aoqi@0 129 }
aoqi@0 130
aoqi@0 131
aoqi@0 132
aoqi@0 133 ////////////////////////////////////////////////////////////////////
aoqi@0 134 // Accessors and setters.
aoqi@0 135 ////////////////////////////////////////////////////////////////////
aoqi@0 136
aoqi@0 137
aoqi@0 138 /**
aoqi@0 139 * Return the current indent step.
aoqi@0 140 *
aoqi@0 141 * <p>Return the current indent step: each start tag will be
aoqi@0 142 * indented by this number of spaces times the number of
aoqi@0 143 * ancestors that the element has.</p>
aoqi@0 144 *
aoqi@0 145 * @return The number of spaces in each indentation step,
aoqi@0 146 * or 0 or less for no indentation.
aoqi@0 147 * @see #setIndentStep(int)
aoqi@0 148 *
aoqi@0 149 * @deprecated
aoqi@0 150 * Only return the length of the indent string.
aoqi@0 151 */
aoqi@0 152 public int getIndentStep ()
aoqi@0 153 {
aoqi@0 154 return indentStep.length();
aoqi@0 155 }
aoqi@0 156
aoqi@0 157
aoqi@0 158 /**
aoqi@0 159 * Set the current indent step.
aoqi@0 160 *
aoqi@0 161 * @param indentStep The new indent step (0 or less for no
aoqi@0 162 * indentation).
aoqi@0 163 * @see #getIndentStep()
aoqi@0 164 *
aoqi@0 165 * @deprecated
aoqi@0 166 * Should use the version that takes string.
aoqi@0 167 */
aoqi@0 168 public void setIndentStep (int indentStep)
aoqi@0 169 {
aoqi@0 170 StringBuilder s = new StringBuilder();
aoqi@0 171 for( ; indentStep>0; indentStep-- ) s.append(' ');
aoqi@0 172 setIndentStep(s.toString());
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 public void setIndentStep(String s) {
aoqi@0 176 this.indentStep = s;
aoqi@0 177 }
aoqi@0 178
aoqi@0 179
aoqi@0 180
aoqi@0 181 ////////////////////////////////////////////////////////////////////
aoqi@0 182 // Override methods from XMLWriter.
aoqi@0 183 ////////////////////////////////////////////////////////////////////
aoqi@0 184
aoqi@0 185
aoqi@0 186 /**
aoqi@0 187 * Reset the writer so that it can be reused.
aoqi@0 188 *
aoqi@0 189 * <p>This method is especially useful if the writer failed
aoqi@0 190 * with an exception the last time through.</p>
aoqi@0 191 *
aoqi@0 192 * @see XMLWriter#reset()
aoqi@0 193 */
aoqi@0 194 public void reset ()
aoqi@0 195 {
aoqi@0 196 depth = 0;
aoqi@0 197 state = SEEN_NOTHING;
aoqi@0 198 stateStack = new Stack();
aoqi@0 199 super.reset();
aoqi@0 200 }
aoqi@0 201
aoqi@0 202
aoqi@0 203 /**
aoqi@0 204 * Write a start tag.
aoqi@0 205 *
aoqi@0 206 * <p>Each tag will begin on a new line, and will be
aoqi@0 207 * indented by the current indent step times the number
aoqi@0 208 * of ancestors that the element has.</p>
aoqi@0 209 *
aoqi@0 210 * <p>The newline and indentation will be passed on down
aoqi@0 211 * the filter chain through regular characters events.</p>
aoqi@0 212 *
aoqi@0 213 * @param uri The element's Namespace URI.
aoqi@0 214 * @param localName The element's local name.
aoqi@0 215 * @param qName The element's qualified (prefixed) name.
aoqi@0 216 * @param atts The element's attribute list.
aoqi@0 217 * @exception org.xml.sax.SAXException If there is an error
aoqi@0 218 * writing the start tag, or if a filter further
aoqi@0 219 * down the chain raises an exception.
aoqi@0 220 * @see XMLWriter#startElement(String, String, String, Attributes)
aoqi@0 221 */
aoqi@0 222 public void startElement (String uri, String localName,
aoqi@0 223 String qName, Attributes atts)
aoqi@0 224 throws SAXException
aoqi@0 225 {
aoqi@0 226 stateStack.push(SEEN_ELEMENT);
aoqi@0 227 state = SEEN_NOTHING;
aoqi@0 228 if (depth > 0) {
aoqi@0 229 super.characters("\n");
aoqi@0 230 }
aoqi@0 231 doIndent();
aoqi@0 232 super.startElement(uri, localName, qName, atts);
aoqi@0 233 depth++;
aoqi@0 234 }
aoqi@0 235
aoqi@0 236
aoqi@0 237 /**
aoqi@0 238 * Write an end tag.
aoqi@0 239 *
aoqi@0 240 * <p>If the element has contained other elements, the tag
aoqi@0 241 * will appear indented on a new line; otherwise, it will
aoqi@0 242 * appear immediately following whatever came before.</p>
aoqi@0 243 *
aoqi@0 244 * <p>The newline and indentation will be passed on down
aoqi@0 245 * the filter chain through regular characters events.</p>
aoqi@0 246 *
aoqi@0 247 * @param uri The element's Namespace URI.
aoqi@0 248 * @param localName The element's local name.
aoqi@0 249 * @param qName The element's qualified (prefixed) name.
aoqi@0 250 * @exception org.xml.sax.SAXException If there is an error
aoqi@0 251 * writing the end tag, or if a filter further
aoqi@0 252 * down the chain raises an exception.
aoqi@0 253 * @see XMLWriter#endElement(String, String, String)
aoqi@0 254 */
aoqi@0 255 public void endElement (String uri, String localName, String qName)
aoqi@0 256 throws SAXException
aoqi@0 257 {
aoqi@0 258 depth--;
aoqi@0 259 if (state == SEEN_ELEMENT) {
aoqi@0 260 super.characters("\n");
aoqi@0 261 doIndent();
aoqi@0 262 }
aoqi@0 263 super.endElement(uri, localName, qName);
aoqi@0 264 state = stateStack.pop();
aoqi@0 265 }
aoqi@0 266
aoqi@0 267
aoqi@0 268 // /**
aoqi@0 269 // * Write a empty element tag.
aoqi@0 270 // *
aoqi@0 271 // * <p>Each tag will appear on a new line, and will be
aoqi@0 272 // * indented by the current indent step times the number
aoqi@0 273 // * of ancestors that the element has.</p>
aoqi@0 274 // *
aoqi@0 275 // * <p>The newline and indentation will be passed on down
aoqi@0 276 // * the filter chain through regular characters events.</p>
aoqi@0 277 // *
aoqi@0 278 // * @param uri The element's Namespace URI.
aoqi@0 279 // * @param localName The element's local name.
aoqi@0 280 // * @param qName The element's qualified (prefixed) name.
aoqi@0 281 // * @param atts The element's attribute list.
aoqi@0 282 // * @exception org.xml.sax.SAXException If there is an error
aoqi@0 283 // * writing the empty tag, or if a filter further
aoqi@0 284 // * down the chain raises an exception.
aoqi@0 285 // * @see XMLWriter#emptyElement(String, String, String, Attributes)
aoqi@0 286 // */
aoqi@0 287 // public void emptyElement (String uri, String localName,
aoqi@0 288 // String qName, Attributes atts)
aoqi@0 289 // throws SAXException
aoqi@0 290 // {
aoqi@0 291 // state = SEEN_ELEMENT;
aoqi@0 292 // if (depth > 0) {
aoqi@0 293 // super.characters("\n");
aoqi@0 294 // }
aoqi@0 295 // doIndent();
aoqi@0 296 // super.emptyElement(uri, localName, qName, atts);
aoqi@0 297 // }
aoqi@0 298
aoqi@0 299
aoqi@0 300 /**
aoqi@0 301 * Write a sequence of characters.
aoqi@0 302 *
aoqi@0 303 * @param ch The characters to write.
aoqi@0 304 * @param start The starting position in the array.
aoqi@0 305 * @param length The number of characters to use.
aoqi@0 306 * @exception org.xml.sax.SAXException If there is an error
aoqi@0 307 * writing the characters, or if a filter further
aoqi@0 308 * down the chain raises an exception.
aoqi@0 309 * @see XMLWriter#characters(char[], int, int)
aoqi@0 310 */
aoqi@0 311 public void characters (char ch[], int start, int length)
aoqi@0 312 throws SAXException
aoqi@0 313 {
aoqi@0 314 state = SEEN_DATA;
aoqi@0 315 super.characters(ch, start, length);
aoqi@0 316 }
aoqi@0 317
aoqi@0 318 public void comment(char ch[], int start, int length) throws SAXException {
aoqi@0 319 if (depth > 0) {
aoqi@0 320 super.characters("\n");
aoqi@0 321 }
aoqi@0 322 doIndent();
aoqi@0 323 super.comment(ch,start,length);
aoqi@0 324 }
aoqi@0 325
aoqi@0 326
aoqi@0 327
aoqi@0 328 ////////////////////////////////////////////////////////////////////
aoqi@0 329 // Internal methods.
aoqi@0 330 ////////////////////////////////////////////////////////////////////
aoqi@0 331
aoqi@0 332
aoqi@0 333 /**
aoqi@0 334 * Print indentation for the current level.
aoqi@0 335 *
aoqi@0 336 * @exception org.xml.sax.SAXException If there is an error
aoqi@0 337 * writing the indentation characters, or if a filter
aoqi@0 338 * further down the chain raises an exception.
aoqi@0 339 */
aoqi@0 340 private void doIndent ()
aoqi@0 341 throws SAXException
aoqi@0 342 {
aoqi@0 343 if (depth > 0) {
aoqi@0 344 char[] ch = indentStep.toCharArray();
aoqi@0 345 for( int i=0; i<depth; i++ )
aoqi@0 346 characters(ch, 0, ch.length);
aoqi@0 347 }
aoqi@0 348 }
aoqi@0 349
aoqi@0 350
aoqi@0 351
aoqi@0 352 ////////////////////////////////////////////////////////////////////
aoqi@0 353 // Constants.
aoqi@0 354 ////////////////////////////////////////////////////////////////////
aoqi@0 355
aoqi@0 356 private final static Object SEEN_NOTHING = new Object();
aoqi@0 357 private final static Object SEEN_ELEMENT = new Object();
aoqi@0 358 private final static Object SEEN_DATA = new Object();
aoqi@0 359
aoqi@0 360
aoqi@0 361
aoqi@0 362 ////////////////////////////////////////////////////////////////////
aoqi@0 363 // Internal state.
aoqi@0 364 ////////////////////////////////////////////////////////////////////
aoqi@0 365
aoqi@0 366 private Object state = SEEN_NOTHING;
aoqi@0 367 private Stack stateStack = new Stack();
aoqi@0 368
aoqi@0 369 private String indentStep = "";
aoqi@0 370 private int depth = 0;
aoqi@0 371
aoqi@0 372 }
aoqi@0 373
aoqi@0 374 // end of DataWriter.java

mercurial