Top Description Fields Constructors Methods
org.glassfish.jaxb.core.marshaller

public Class DataWriter

extends XMLWriter
Class Inheritance
Imports
java.io.IOException, .Writer, java.util.Stack, org.xml.sax.Attributes, .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 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:

<?xml version="1.0" standalone="yes"?>

<Person>
  <name>Jane Smith</name>
  <date-of-birth>1965-05-23</date-of-birth>
  <citizenship>US</citizenship>
</Person>

This class inherits from XMLWriter, and provides all of the same support for Namespaces.

Author
David Megginson, [email protected]
Since
1.0
Version
0.2
See Also
XMLWriter

Field Summary

Modifier and TypeField and Description
private int
private String
private static final Object
private static final Object
private static final Object
private Object
private Stack<Object>

Constructor Summary

AccessConstructor and Description
public
DataWriter(Writer
The character stream where the XML document will be written.
writer
,
String
If non-null string is specified, it is written as a part of the XML declaration.
encoding
,
CharacterEscapeHandler _escapeHandler)

Create a new data writer for the specified output.

public
DataWriter(Writer writer, String encoding)

Method Summary

Modifier and TypeMethod and Description
public void
characters(char[]
The characters to write.
ch
,
int
The starting position in the array.
start
,
int
The number of characters to use.
length
)

Overrides org.glassfish.jaxb.core.marshaller.XMLWriter.characters.

Implements org.xml.sax.ContentHandler.characters.

Write a sequence of characters.

private void
doIndent()

Print indentation for the current level.

public void
endDocument()

Overrides org.glassfish.jaxb.core.marshaller.XMLWriter.endDocument.

Implements org.xml.sax.ContentHandler.endDocument.

Write a newline at the end of the document.

public void
endElement(String
The element's Namespace URI.
uri
,
String
The element's local name.
localName
,
String
The element's qualified (prefixed) name.
qName
)

Overrides org.glassfish.jaxb.core.marshaller.XMLWriter.endElement.

Implements org.xml.sax.ContentHandler.endElement.

Write an end tag.

public int

Returns:

The number of spaces in each indentation step, or 0 or less for no indentation.
getIndentStep
()
Deprecated Only return the length of the indent string.

Return the current indent step.

public void
reset()

Overrides org.glassfish.jaxb.core.marshaller.XMLWriter.reset.

Reset the writer so that it can be reused.

public void
setIndentStep(int
The new indent step (0 or less for no indentation).
indentStep
)
Deprecated Should use the version that takes string.

Set the current indent step.

public void
public void
startElement(String
The element's Namespace URI.
uri
,
String
The element's local name.
localName
,
String
The element's qualified (prefixed) name.
qName
,
Attributes
The element's attribute list.
atts
)

Overrides org.glassfish.jaxb.core.marshaller.XMLWriter.startElement.

Implements org.xml.sax.ContentHandler.startElement.

Write a start tag.

protected void
Inherited from org.glassfish.jaxb.core.marshaller.XMLWriter:
charactersdataElementdataElementdataElementendElementendElementflushignorableWhitespaceprocessingInstructionsetHeadersetOutputsetXmlDeclstartDocumentstartElementstartElementstartPrefixMappingwritewrite

Field Detail

depthback to summary
private int depth
indentStepback to summary
private String indentStep
SEEN_DATAback to summary
private static final Object SEEN_DATA
SEEN_ELEMENTback to summary
private static final Object SEEN_ELEMENT
SEEN_NOTHINGback to summary
private static final Object SEEN_NOTHING
stateback to summary
private Object state
stateStackback to summary
private Stack<Object> stateStack

Constructor Detail

DataWriterback to summary
public DataWriter(Writer writer, String encoding, CharacterEscapeHandler _escapeHandler)

Create a new data writer for the specified output.

Parameters
writer:Writer

The character stream where the XML document will be written.

encoding:String

If non-null string is specified, it is written as a part of the XML declaration.

DataWriterback to summary
public DataWriter(Writer writer, String encoding)

Method Detail

charactersback to summary
public void characters(char[] ch, int start, int length) throws SAXException

Overrides org.glassfish.jaxb.core.marshaller.XMLWriter.characters.

Implements org.xml.sax.ContentHandler.characters.

Write a sequence of characters.

Parameters
ch:char[]

The characters to write.

start:int

The starting position in the array.

length:int

The number of characters to use.

Annotations
@Override
Exceptions
SAXException:
If there is an error writing the characters, or if a filter further down the chain raises an exception.
See Also
XMLWriter#characters(char[], int, int)
doIndentback to summary
private void doIndent() throws SAXException

Print indentation for the current level.

Exceptions
SAXException:
If there is an error writing the indentation characters, or if a filter further down the chain raises an exception.
endDocumentback to summary
public void endDocument() throws SAXException

Overrides org.glassfish.jaxb.core.marshaller.XMLWriter.endDocument.

Implements org.xml.sax.ContentHandler.endDocument.

Doc from org.glassfish.jaxb.core.marshaller.XMLWriter.endDocument.

Write a newline at the end of the document. Pass the event on down the filter chain for further processing.

Annotations
@Override
Exceptions
SAXException:
If there is an error writing the newline, or if a handler further down the filter chain raises an exception.
endElementback to summary
public void endElement(String uri, String localName, String qName) throws SAXException

Overrides org.glassfish.jaxb.core.marshaller.XMLWriter.endElement.

Implements org.xml.sax.ContentHandler.endElement.

Write an end tag.

If the element has contained other elements, the tag will appear indented on a new line; otherwise, it will appear immediately following whatever came before.

The newline and indentation will be passed on down the filter chain through regular characters events.

Parameters
uri:String

The element's Namespace URI.

localName:String

The element's local name.

qName:String

The element's qualified (prefixed) name.

Annotations
@Override
Exceptions
SAXException:
If there is an error writing the end tag, or if a filter further down the chain raises an exception.
See Also
XMLWriter#endElement(String, String, String)
getIndentStepback to summary
public int getIndentStep()

Deprecated

Only return the length of the indent string.

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.

Returns:int

The number of spaces in each indentation step, or 0 or less for no indentation.

Annotations
@Deprecated
See Also
setIndentStep(int)
resetback to summary
public void reset()

Overrides org.glassfish.jaxb.core.marshaller.XMLWriter.reset.

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.

Annotations
@Override
setIndentStepback to summary
public void setIndentStep(int indentStep)

Deprecated

Should use the version that takes string.

Set the current indent step.

Parameters
indentStep:int

The new indent step (0 or less for no indentation).

Annotations
@Deprecated
See Also
getIndentStep()
setIndentStepback to summary
public void setIndentStep(String s)
startElementback to summary
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException

Overrides org.glassfish.jaxb.core.marshaller.XMLWriter.startElement.

Implements org.xml.sax.ContentHandler.startElement.

Write a start tag.

Each tag will begin on a new line, and will be indented by the current indent step times the number of ancestors that the element has.

The newline and indentation will be passed on down the filter chain through regular characters events.

Parameters
uri:String

The element's Namespace URI.

localName:String

The element's local name.

qName:String

The element's qualified (prefixed) name.

atts:Attributes

The element's attribute list.

Annotations
@Override
Exceptions
SAXException:
If there is an error writing the start tag, or if a filter further down the chain raises an exception.
See Also
XMLWriter#startElement(String, String, String, Attributes)
writeXmlDeclback to summary
protected void writeXmlDecl(String decl) throws IOException

Overrides org.glassfish.jaxb.core.marshaller.XMLWriter.writeXmlDecl.

Annotations
@Override