aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: //@@3RD PARTY CODE@@ aoqi@0: aoqi@0: // DataWriter.java - XML writer for data-oriented files. aoqi@0: aoqi@0: package com.sun.xml.internal.bind.marshaller; aoqi@0: aoqi@0: import java.io.IOException; aoqi@0: import java.io.Writer; aoqi@0: import java.util.Stack; aoqi@0: aoqi@0: import org.xml.sax.Attributes; aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Write data- or field-oriented XML. aoqi@0: * aoqi@0: *
This filter pretty-prints field-oriented XML without mixed content. aoqi@0: * all added indentation and newlines will be passed on down aoqi@0: * the filter chain (if any).
aoqi@0: * aoqi@0: *In general, all whitespace in an XML document is potentially aoqi@0: * significant, so a general-purpose XML writing tool like the aoqi@0: * {@link XMLWriter} class cannot aoqi@0: * add newlines or indentation.
aoqi@0: * aoqi@0: *There is, however, a large class of XML documents where information aoqi@0: * is strictly fielded: each element contains either character data aoqi@0: * or other elements, but not both. For this special case, it is possible aoqi@0: * for a writing tool to provide automatic indentation and newlines aoqi@0: * without requiring extra work from the user. Note that this class aoqi@0: * will likely not yield appropriate results for document-oriented aoqi@0: * XML like XHTML pages, which mix character data and elements together.
aoqi@0: * aoqi@0: *This writer will automatically place each start tag on a new line, aoqi@0: * optionally indented if an indent step is provided (by default, there aoqi@0: * is no indentation). If an element contains other elements, the end aoqi@0: * tag will also appear on a new line with leading indentation. Consider, aoqi@0: * for example, the following code:
aoqi@0: * aoqi@0: *aoqi@0: * DataWriter w = new DataWriter(); aoqi@0: * aoqi@0: * w.setIndentStep(2); aoqi@0: * w.startDocument(); aoqi@0: * w.startElement("Person"); aoqi@0: * w.dataElement("name", "Jane Smith"); aoqi@0: * w.dataElement("date-of-birth", "1965-05-23"); aoqi@0: * w.dataElement("citizenship", "US"); aoqi@0: * w.endElement("Person"); aoqi@0: * w.endDocument(); aoqi@0: *aoqi@0: * aoqi@0: *
This code will produce the following document:
aoqi@0: * aoqi@0: *aoqi@0: * <?xml version="1.0" standalone="yes"?> aoqi@0: * aoqi@0: * <Person> aoqi@0: * <name>Jane Smith</name> aoqi@0: * <date-of-birth>1965-05-23</date-of-birth> aoqi@0: * <citizenship>US</citizenship> aoqi@0: * </Person> aoqi@0: *aoqi@0: * aoqi@0: *
This class inherits from {@link XMLWriter}, aoqi@0: * and provides all of the same support for Namespaces.
aoqi@0: * aoqi@0: * @since 1.0 aoqi@0: * @author David Megginson, david@megginson.com aoqi@0: * @version 0.2 aoqi@0: * @see XMLWriter aoqi@0: */ aoqi@0: public class DataWriter extends XMLWriter aoqi@0: { aoqi@0: aoqi@0: aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: // Constructors. aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Create a new data writer for the specified output. aoqi@0: * aoqi@0: * @param writer The character stream where the XML document aoqi@0: * will be written. aoqi@0: * @param encoding aoqi@0: * If non-null string is specified, it is written as a part aoqi@0: * of the XML declaration. aoqi@0: */ aoqi@0: public DataWriter ( Writer writer, String encoding, CharacterEscapeHandler _escapeHandler ) aoqi@0: { aoqi@0: super(writer,encoding,_escapeHandler); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: public DataWriter (Writer writer, String encoding ) { aoqi@0: this( writer, encoding, DumbEscapeHandler.theInstance ); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: // Accessors and setters. aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Return the current indent step. aoqi@0: * aoqi@0: *Return the current indent step: each start tag will be aoqi@0: * indented by this number of spaces times the number of aoqi@0: * ancestors that the element has.
aoqi@0: * aoqi@0: * @return The number of spaces in each indentation step, aoqi@0: * or 0 or less for no indentation. aoqi@0: * @see #setIndentStep(int) aoqi@0: * aoqi@0: * @deprecated aoqi@0: * Only return the length of the indent string. aoqi@0: */ aoqi@0: public int getIndentStep () aoqi@0: { aoqi@0: return indentStep.length(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Set the current indent step. aoqi@0: * aoqi@0: * @param indentStep The new indent step (0 or less for no aoqi@0: * indentation). aoqi@0: * @see #getIndentStep() aoqi@0: * aoqi@0: * @deprecated aoqi@0: * Should use the version that takes string. aoqi@0: */ aoqi@0: public void setIndentStep (int indentStep) aoqi@0: { aoqi@0: StringBuilder buf = new StringBuilder(); aoqi@0: for( ; indentStep>0; indentStep-- ) aoqi@0: buf.append(' '); aoqi@0: setIndentStep(buf.toString()); aoqi@0: } aoqi@0: aoqi@0: public void setIndentStep(String s) { aoqi@0: this.indentStep = s; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: // Override methods from XMLWriter. aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Reset the writer so that it can be reused. aoqi@0: * aoqi@0: *This method is especially useful if the writer failed aoqi@0: * with an exception the last time through.
aoqi@0: * aoqi@0: * @see XMLWriter#reset() aoqi@0: */ aoqi@0: public void reset () aoqi@0: { aoqi@0: depth = 0; aoqi@0: state = SEEN_NOTHING; aoqi@0: stateStack = new Stack