Top Description Fields Constructors Methods
org.jdom2.output.support

public abstract Class AbstractXMLOutputProcessor

extends AbstractOutputProcessor
implements XMLOutputProcessor
Class Inheritance
All Implemented Interfaces
org.jdom2.output.support.XMLOutputProcessor
Known Direct Subclasses
org.jdom2.output.XMLOutputter.DefaultXMLProcessor
Imports
java.io.IOException, .Writer, java.util.ArrayList, .Collections, .List, javax.xml.transform.Result, org.jdom2.Attribute, .CDATA, .Comment, .Content, .DocType, .Document, .Element, .EntityRef, .IllegalDataException, .Namespace, .ProcessingInstruction, .Text, .Verifier, org.jdom2.output.Format, .Format.TextMode, .XMLOutputter, org.jdom2.util.NamespaceStack

This class provides a concrete implementation of XMLOutputProcessor for supporting the XMLOutputter.

Overview

This class is marked abstract even though all methods are fully implemented. The process*(...) methods are public because they match the XMLOutputProcessor interface but the remaining methods are all protected.

People who want to create a custom XMLOutputProcessor for XMLOutputter are able to extend this class and modify any functionality they want. Before sub-classing this you should first check to see if the Format class can get you the results you want.

Subclasses of this should have reentrant methods. This is easiest to accomplish simply by not allowing any instance fields. If your sub-class has an instance field/variable, then it's probably broken.

The Stacks

One significant feature of this implementation is that it creates and maintains both a NamespaceStack and FormatStack that are managed in the printElement(Writer, FormatStack, NamespaceStack, Element) method. The stacks are pushed and popped in that method only. They significantly improve the performance and readability of the code.

The NamespaceStack is only sent through to the printElement(Writer, FormatStack, NamespaceStack, Element) and printContent(Writer, FormatStack, NamespaceStack, Walker) methods, but the FormatStack is pushed through to all print* Methods.

Text Processing

In XML the concept of 'Text' can be loosely defined as anything that can be found between an Element's start and end tags, excluding Comments and Processing Instructions. When considered from a JDOM perspective, this means Text, CDATA and EntityRef content. This will be referred to as 'Text-like content'

XMLOutputter delegates the management and formatting of Content to a Walker instance. See Walker and its various implementations for details on how the Element content is processed.

Because the Walker interface specifies that Text/CDATA content may be returned as either Text/CDATA instances or as formatted String values this class sometimes uses printCDATA(...) and printText(...), and sometimes uses the more direct textCDATA(Writer, String) or textRaw(Writer, String) as appropriate. In other words, subclasses should probably override these second methods instead of the print methods.

Non-Text Content

Non-text content is processed via the respective print* methods. The usage should be logical based on the method name.

The general observations are:

Final Notes

No methods actually write to the destination Writer except the write(...) methods. Thus, all other methods do their respective processing and delegate the actual destination output to the write(Writer, char) or write(Writer, String) methods.

All Text-like content (printCDATA, printText, and printEntityRef) will ultimately be output through the the text* methods (and no other content).

Author
Rolf Lear
Since
JDOM2
See Also
XMLOutputter, XMLOutputProcessor

Field Summary

Modifier and TypeField and Description
protected static final String
CDATAPOST

Simple constant for a close-CDATA

protected static final String
CDATAPRE

Simple constant for an open-CDATA

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected void
attributeEscapedEntitiesFilter(final Writer
The destination Writer
out
,
final FormatStack fstack, final String
String Attribute value to escape.
value
)

This will take the three pre-defined entities in XML 1.0 ('<', '>', and '&' - used specifically in XML elements) as well as CR/NL and Quote characters which require escaping inside Attribute values and convert their character representation to the appropriate entity reference suitable for XML attribute content.

protected void
printAttribute(final Writer
Writer to use.
out
,
final FormatStack
The current FormatStack
fstack
,
final Attribute
Attribute to output
attribute
)

This will handle printing of an Attribute.

protected void
printCDATA(final Writer
Writer to use.
out
,
final FormatStack
the FormatStack
fstack
,
final CDATA
CDATA to write.
cdata
)

This will handle printing of a CDATA.

protected void
printComment(final Writer
Writer to use.
out
,
final FormatStack
the FormatStack
fstack
,
final Comment
Comment to write.
comment
)

This will handle printing of a Comment.

protected void
printContent(final Writer
Writer to use.
out
,
final FormatStack
the FormatStack
fstack
,
final NamespaceStack
the NamespaceStack
nstack
,
final Walker
Walker of Content to write.
walker
)

This will handle printing of a List of Content.

protected void
printDeclaration(final Writer
Writer to use.
out
,
final FormatStack
the FormatStack
fstack
)

This will handle printing of the XML declaration.

protected void
printDocType(final Writer
Writer to use.
out
,
final FormatStack
the FormatStack
fstack
,
final DocType
DocType to write.
docType
)

This will handle printing of a DocType.

protected void
printDocument(final Writer
Writer to use.
out
,
final FormatStack
the FormatStack
fstack
,
final NamespaceStack
the NamespaceStack
nstack
,
final Document
Document to write.
doc
)

This will handle printing of a Document.

protected void
printElement(final Writer
Writer to use.
out
,
final FormatStack
the FormatStack
fstack
,
final NamespaceStack
the NamespaceStack
nstack
,
final Element
Element to write.
element
)

This will handle printing of an Element.

protected void
printEntityRef(final Writer
Writer to use.
out
,
final FormatStack
the FormatStack
fstack
,
final EntityRef
EntotyRef to write.
entity
)

This will handle printing of an EntityRef.

protected void
printNamespace(final Writer
Writer to use.
out
,
final FormatStack
The current FormatStack
fstack
,
final Namespace
Namespace to print definition of
ns
)

This will handle printing of any needed Namespace declarations.

protected void
printProcessingInstruction(final Writer
Writer to use.
out
,
final FormatStack
the FormatStack
fstack
,
final ProcessingInstruction
ProcessingInstruction to write.
pi
)

This will handle printing of a ProcessingInstruction.

protected void
printText(final Writer
Writer to use.
out
,
final FormatStack
the FormatStack
fstack
,
final Text
Text to write.
text
)

This will handle printing of a Text.

public void
process(final Writer
Writer to use.
out
,
final Format
Format instance specifying output style
format
,
final Document
Document to format.
doc
)

Implements org.jdom2.output.support.XMLOutputProcessor.process.

This will print the Document to the given Writer.
public void
process(final Writer
Writer to use.
out
,
final Format
Format instance specifying output style
format
,
final DocType
DocType to output.
doctype
)

Implements org.jdom2.output.support.XMLOutputProcessor.process.

Print out the DocType.
public void
process(final Writer
Writer to use.
out
,
final Format
Format instance specifying output style
format
,
final Element
Element to output.
element
)

Implements org.jdom2.output.support.XMLOutputProcessor.process.

Print out an Element, including its Attributes, and all contained (child) elements, etc.
public void
process(final Writer
Writer to use.
out
,
final Format
Format instance specifying output style
format
,
final List<? extends Content>
List of nodes.
list
)

Implements org.jdom2.output.support.XMLOutputProcessor.process.

This will handle printing out a list of nodes.
public void
process(final Writer
Writer to use.
out
,
final Format
Format instance specifying output style
format
,
final CDATA
CDATA to output.
cdata
)

Implements org.jdom2.output.support.XMLOutputProcessor.process.

Print out a CDATA node.
public void
process(final Writer
Writer to use.
out
,
final Format
Format instance specifying output style
format
,
final Text
Text to output.
text
)

Implements org.jdom2.output.support.XMLOutputProcessor.process.

Print out a Text node.
public void
process(final Writer
Writer to use.
out
,
final Format
Format instance specifying output style
format
,
final Comment
Comment to output.
comment
)

Implements org.jdom2.output.support.XMLOutputProcessor.process.

Print out a Comment.
public void
process(final Writer
Writer to use.
out
,
final Format
Format instance specifying output style
format
,
final ProcessingInstruction
ProcessingInstruction to output.
pi
)

Implements org.jdom2.output.support.XMLOutputProcessor.process.

Print out a ProcessingInstruction.
public void
process(final Writer
Writer to use.
out
,
final Format
Format instance specifying output style
format
,
final EntityRef
EntityRef to output.
entity
)

Implements org.jdom2.output.support.XMLOutputProcessor.process.

Print out a EntityRef.
protected void
textCDATA(final Writer
the destination Writer
out
,
final String
the CDATA text
text
)

Write a CDATA to the destination

protected void
textEntityRef(final Writer
the destination Writer.
out
,
final String
the EntityRef's name.
name
)

Write an EntityRef to the destination.

protected void
textRaw(final Writer
the destination writer.
out
,
final String
the String to write.
str
)

Convenience method that simply passes the input str to write(Writer, String).

protected void
textRaw(final Writer
the destination Writer.
out
,
final char
the char to write.
ch
)

Convenience method that simply passes the input char to write(Writer, char).

protected void
write(final Writer
The Writer to write to.
out
,
final String
The String to write (can be null).
str
)

Print some string value to the output.

protected void
write(final Writer
The Writer to write to.
out
,
final char
The char to write.
c
)

Write a single character to the output Writer.

Inherited from org.jdom2.output.support.AbstractOutputProcessor:
buildWalker

Field Detail

CDATAPOSTback to summary
protected static final String CDATAPOST

Simple constant for a close-CDATA

CDATAPREback to summary
protected static final String CDATAPRE

Simple constant for an open-CDATA

Constructor Detail

AbstractXMLOutputProcessorback to summary
public AbstractXMLOutputProcessor()

Method Detail

attributeEscapedEntitiesFilterback to summary
protected void attributeEscapedEntitiesFilter(final Writer out, final FormatStack fstack, final String value) throws IOException

This will take the three pre-defined entities in XML 1.0 ('<', '>', and '&' - used specifically in XML elements) as well as CR/NL and Quote characters which require escaping inside Attribute values and convert their character representation to the appropriate entity reference suitable for XML attribute content. Further, some special characters (e.g. characters that are not valid in the current encoding) are converted to escaped representations.

Note

If FormatStack#getEscapeOutput() is false then no escaping will happen.

Parameters
out:Writer

The destination Writer

fstack:FormatStack

The FormatStack

value:String

String Attribute value to escape.

Exceptions
IOException:
if the destination Writer fails.
IllegalDataException:
if an entity can not be escaped
printAttributeback to summary
protected void printAttribute(final Writer out, final FormatStack fstack, final Attribute attribute) throws IOException

This will handle printing of an Attribute.

Parameters
out:Writer

Writer to use.

fstack:FormatStack

The current FormatStack

attribute:Attribute

Attribute to output

Exceptions
IOException:
if the output fails
printCDATAback to summary
protected void printCDATA(final Writer out, final FormatStack fstack, final CDATA cdata) throws IOException

This will handle printing of a CDATA.

Parameters
out:Writer

Writer to use.

fstack:FormatStack

the FormatStack

cdata:CDATA

CDATA to write.

Exceptions
IOException:
if the destination Writer fails
printCommentback to summary
protected void printComment(final Writer out, final FormatStack fstack, final Comment comment) throws IOException

This will handle printing of a Comment.

Parameters
out:Writer

Writer to use.

fstack:FormatStack

the FormatStack

comment:Comment

Comment to write.

Exceptions
IOException:
if the destination Writer fails
printContentback to summary
protected void printContent(final Writer out, final FormatStack fstack, final NamespaceStack nstack, final Walker walker) throws IOException

This will handle printing of a List of Content.

The list of Content is basically processed as one of three types of content

  1. Consecutive text-type (Text, CDATA, and EntityRef) content
  2. Stand-alone text-type content
  3. Non-text-type content.
Although the code looks complex, the theory is conceptually simple:
  1. identify one of the three types (consecutive, stand-alone, non-text)
  2. do indent if any is specified.
  3. send the type to the respective print* handler (e.g. printCDATA(Writer, FormatStack, CDATA), or printComment(Writer, FormatStack, Comment),
  4. do a newline if one is specified.
  5. loop back to 1. until there's no more content to process.
Parameters
out:Writer

Writer to use.

fstack:FormatStack

the FormatStack

nstack:NamespaceStack

the NamespaceStack

walker:Walker

Walker of Content to write.

Exceptions
IOException:
if the destination Writer fails
printDeclarationback to summary
protected void printDeclaration(final Writer out, final FormatStack fstack) throws IOException

This will handle printing of the XML declaration. Assumes XML version 1.0 since we don't directly know.

Parameters
out:Writer

Writer to use.

fstack:FormatStack

the FormatStack

Exceptions
IOException:
if the destination Writer fails
printDocTypeback to summary
protected void printDocType(final Writer out, final FormatStack fstack, final DocType docType) throws IOException

This will handle printing of a DocType.

Parameters
out:Writer

Writer to use.

fstack:FormatStack

the FormatStack

docType:DocType

DocType to write.

Exceptions
IOException:
if the destination Writer fails
printDocumentback to summary
protected void printDocument(final Writer out, final FormatStack fstack, final NamespaceStack nstack, final Document doc) throws IOException

This will handle printing of a Document.

Parameters
out:Writer

Writer to use.

fstack:FormatStack

the FormatStack

nstack:NamespaceStack

the NamespaceStack

doc:Document

Document to write.

Exceptions
IOException:
if the destination Writer fails
printElementback to summary
protected void printElement(final Writer out, final FormatStack fstack, final NamespaceStack nstack, final Element element) throws IOException

This will handle printing of an Element.

This method arranges for outputting the Element infrastructure including Namespace Declarations and Attributes.

Parameters
out:Writer

Writer to use.

fstack:FormatStack

the FormatStack

nstack:NamespaceStack

the NamespaceStack

element:Element

Element to write.

Exceptions
IOException:
if the destination Writer fails
printEntityRefback to summary
protected void printEntityRef(final Writer out, final FormatStack fstack, final EntityRef entity) throws IOException

This will handle printing of an EntityRef.

Parameters
out:Writer

Writer to use.

fstack:FormatStack

the FormatStack

entity:EntityRef

EntotyRef to write.

Exceptions
IOException:
if the destination Writer fails
printNamespaceback to summary
protected void printNamespace(final Writer out, final FormatStack fstack, final Namespace ns) throws IOException

This will handle printing of any needed Namespace declarations.

Parameters
out:Writer

Writer to use.

fstack:FormatStack

The current FormatStack

ns:Namespace

Namespace to print definition of

Exceptions
IOException:
if the output fails
printProcessingInstructionback to summary
protected void printProcessingInstruction(final Writer out, final FormatStack fstack, final ProcessingInstruction pi) throws IOException

This will handle printing of a ProcessingInstruction.

Parameters
out:Writer

Writer to use.

fstack:FormatStack

the FormatStack

pi:ProcessingInstruction

ProcessingInstruction to write.

Exceptions
IOException:
if the destination Writer fails
printTextback to summary
protected void printText(final Writer out, final FormatStack fstack, final Text text) throws IOException

This will handle printing of a Text.

Parameters
out:Writer

Writer to use.

fstack:FormatStack

the FormatStack

text:Text

Text to write.

Exceptions
IOException:
if the destination Writer fails
processback to summary
public void process(final Writer out, final Format format, final Document doc) throws IOException

Implements org.jdom2.output.support.XMLOutputProcessor.process.

Doc from org.jdom2.output.support.XMLOutputProcessor.process.

This will print the Document to the given Writer.

Warning

using your own Writer may cause the outputter's preferred character encoding to be ignored. If you use encodings other than UTF-8, we recommend using the method that takes an OutputStream instead.

Parameters
out:Writer

Writer to use.

format:Format

Format instance specifying output style

doc:Document

Document to format.

Annotations
@Override
Exceptions
IOException:
if there's any problem writing.
processback to summary
public void process(final Writer out, final Format format, final DocType doctype) throws IOException

Implements org.jdom2.output.support.XMLOutputProcessor.process.

Doc from org.jdom2.output.support.XMLOutputProcessor.process.

Print out the DocType.

Parameters
out:Writer

Writer to use.

format:Format

Format instance specifying output style

doctype:DocType

DocType to output.

Annotations
@Override
Exceptions
IOException:
if there's any problem writing.
processback to summary
public void process(final Writer out, final Format format, final Element element) throws IOException

Implements org.jdom2.output.support.XMLOutputProcessor.process.

Doc from org.jdom2.output.support.XMLOutputProcessor.process.

Print out an Element, including its Attributes, and all contained (child) elements, etc.

Parameters
out:Writer

Writer to use.

format:Format

Format instance specifying output style

element:Element

Element to output.

Annotations
@Override
Exceptions
IOException:
if there's any problem writing.
processback to summary
public void process(final Writer out, final Format format, final List<? extends Content> list) throws IOException

Implements org.jdom2.output.support.XMLOutputProcessor.process.

Doc from org.jdom2.output.support.XMLOutputProcessor.process.

This will handle printing out a list of nodes. This can be useful for printing the content of an element that contains HTML, like "<description>JDOM is <b>fun>!</description>".

Parameters
out:Writer

Writer to use.

format:Format

Format instance specifying output style

list:List<? extends Content>

List of nodes.

Annotations
@Override
Exceptions
IOException:
if there's any problem writing.
processback to summary
public void process(final Writer out, final Format format, final CDATA cdata) throws IOException

Implements org.jdom2.output.support.XMLOutputProcessor.process.

Doc from org.jdom2.output.support.XMLOutputProcessor.process.

Print out a CDATA node.

Parameters
out:Writer

Writer to use.

format:Format

Format instance specifying output style

cdata:CDATA

CDATA to output.

Annotations
@Override
Exceptions
IOException:
if there's any problem writing.
processback to summary
public void process(final Writer out, final Format format, final Text text) throws IOException

Implements org.jdom2.output.support.XMLOutputProcessor.process.

Doc from org.jdom2.output.support.XMLOutputProcessor.process.

Print out a Text node. Performs the necessary entity escaping and whitespace stripping.

Parameters
out:Writer

Writer to use.

format:Format

Format instance specifying output style

text:Text

Text to output.

Annotations
@Override
Exceptions
IOException:
if there's any problem writing.
processback to summary
public void process(final Writer out, final Format format, final Comment comment) throws IOException

Implements org.jdom2.output.support.XMLOutputProcessor.process.

Doc from org.jdom2.output.support.XMLOutputProcessor.process.

Print out a Comment.

Parameters
out:Writer

Writer to use.

format:Format

Format instance specifying output style

comment:Comment

Comment to output.

Annotations
@Override
Exceptions
IOException:
if there's any problem writing.
processback to summary
public void process(final Writer out, final Format format, final ProcessingInstruction pi) throws IOException

Implements org.jdom2.output.support.XMLOutputProcessor.process.

Doc from org.jdom2.output.support.XMLOutputProcessor.process.

Print out a ProcessingInstruction.

Parameters
out:Writer

Writer to use.

format:Format

Format instance specifying output style

pi:ProcessingInstruction

ProcessingInstruction to output.

Annotations
@Override
Exceptions
IOException:
if there's any problem writing.
processback to summary
public void process(final Writer out, final Format format, final EntityRef entity) throws IOException

Implements org.jdom2.output.support.XMLOutputProcessor.process.

Doc from org.jdom2.output.support.XMLOutputProcessor.process.

Print out a EntityRef.

Parameters
out:Writer

Writer to use.

format:Format

Format instance specifying output style

entity:EntityRef

EntityRef to output.

Annotations
@Override
Exceptions
IOException:
if there's any problem writing.
textCDATAback to summary
protected void textCDATA(final Writer out, final String text) throws IOException

Write a CDATA to the destination

Parameters
out:Writer

the destination Writer

text:String

the CDATA text

Exceptions
IOException:
if the Writer fails.
textEntityRefback to summary
protected void textEntityRef(final Writer out, final String name) throws IOException

Write an EntityRef to the destination.

Parameters
out:Writer

the destination Writer.

name:String

the EntityRef's name.

Exceptions
IOException:
if the Writer fails.
textRawback to summary
protected void textRaw(final Writer out, final String str) throws IOException

Convenience method that simply passes the input str to write(Writer, String). This could be useful for subclasses to hook in to. All text-type output will come through this or the textRaw(Writer, char) method.

Parameters
out:Writer

the destination writer.

str:String

the String to write.

Exceptions
IOException:
if the Writer fails.
textRawback to summary
protected void textRaw(final Writer out, final char ch) throws IOException

Convenience method that simply passes the input char to write(Writer, char). This could be useful for subclasses to hook in to. All text-type output will come through this or the textRaw(Writer, String) method.

Parameters
out:Writer

the destination Writer.

ch:char

the char to write.

Exceptions
IOException:
if the Writer fails.
writeback to summary
protected void write(final Writer out, final String str) throws IOException

Print some string value to the output. Null values are ignored. This ignore-null property is used for a few tricks.

Parameters
out:Writer

The Writer to write to.

str:String

The String to write (can be null).

Exceptions
IOException:
if the out Writer fails.
writeback to summary
protected void write(final Writer out, final char c) throws IOException

Write a single character to the output Writer.

Parameters
out:Writer

The Writer to write to.

c:char

The char to write.

Exceptions
IOException:
if the Writer fails.