src/share/jaxws_classes/com/sun/xml/internal/bind/marshaller/DataWriter.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial