com.sun.xml.bind.marshaller.DataWriter Maven / Gradle / Ivy
Show all versions of jaxb-runtime Show documentation
/*
* Copyright (c) 2018, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
//@@3RD PARTY CODE@@
// DataWriter.java - XML writer for data-oriented files.
package com.sun.xml.bind.marshaller;
import java.io.IOException;
import java.io.Writer;
import java.util.Stack;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
/**
* Write data- or field-oriented XML.
*
* This filter pretty-prints field-oriented XML without mixed content.
* all added indentation and newlines will be passed on down
* the filter chain (if any).
*
* In general, all whitespace in an XML document is potentially
* significant, so a general-purpose XML writing tool like the
* {@link XMLWriter} class cannot
* add newlines or indentation.
*
* There is, however, a large class of XML documents where information
* is strictly fielded: each element contains either character data
* or other elements, but not both. For this special case, it is possible
* for a writing tool to provide automatic indentation and newlines
* without requiring extra work from the user. Note that this class
* will likely not yield appropriate results for document-oriented
* XML like XHTML pages, which mix character data and elements together.
*
* This writer will automatically place each start tag on a new line,
* optionally indented if an indent step is provided (by default, there
* is no indentation). If an element contains other elements, the end
* tag will also appear on a new line with leading indentation. Consider,
* for example, the following code:
*
*
* DataWriter w = new DataWriter();
*
* w.setIndentStep(2);
* w.startDocument();
* w.startElement("Person");
* w.dataElement("name", "Jane Smith");
* w.dataElement("date-of-birth", "1965-05-23");
* w.dataElement("citizenship", "US");
* w.endElement("Person");
* w.endDocument();
*
*
* This code will produce the following document:
*
* {@code
*
*
*
* Jane Smith
* 1965-05-23
* US
*
* }
*
* This class inherits from {@link XMLWriter},
* and provides all of the same support for Namespaces.
*
* @since 1.0
* @author David Megginson, [email protected]
* @version 0.2
* @see XMLWriter
*/
public class DataWriter extends XMLWriter
{
////////////////////////////////////////////////////////////////////
// Constructors.
////////////////////////////////////////////////////////////////////
/**
* Create a new data writer for the specified output.
*
* @param writer The character stream where the XML document
* will be written.
* @param encoding
* If non-null string is specified, it is written as a part
* of the XML declaration.
*/
public DataWriter ( Writer writer, String encoding, CharacterEscapeHandler _escapeHandler )
{
super(writer,encoding,_escapeHandler);
}
public DataWriter (Writer writer, String encoding ) {
this( writer, encoding, DumbEscapeHandler.theInstance );
}
////////////////////////////////////////////////////////////////////
// Accessors and setters.
////////////////////////////////////////////////////////////////////
/**
* Return the current indent step.
*
* Return the current indent step: each start tag will be
* indented by this number of spaces times the number of
* ancestors that the element has.
*
* @return The number of spaces in each indentation step,
* or 0 or less for no indentation.
* @see #setIndentStep(int)
*
* @deprecated
* Only return the length of the indent string.
*/
public int getIndentStep ()
{
return indentStep.length();
}
/**
* Set the current indent step.
*
* @param indentStep The new indent step (0 or less for no
* indentation).
* @see #getIndentStep()
*
* @deprecated
* Should use the version that takes string.
*/
public void setIndentStep (int indentStep)
{
StringBuilder buf = new StringBuilder();
for( ; indentStep>0; indentStep-- )
buf.append(' ');
setIndentStep(buf.toString());
}
public void setIndentStep(String s) {
this.indentStep = s;
}
////////////////////////////////////////////////////////////////////
// Override methods from XMLWriter.
////////////////////////////////////////////////////////////////////
/**
* Reset the writer so that it can be reused.
*
* This method is especially useful if the writer failed
* with an exception the last time through.
*
* @see XMLWriter#reset()
*/
public void reset ()
{
depth = 0;
state = SEEN_NOTHING;
stateStack = new Stack