public class XMLWriter extends XMLFilterImpl implements LexicalHandler
XMLWriter
takes a DOM4J tree and formats it to a
stream as XML.
It can also take SAX events too so can be used by SAX clients as this object
implements the ContentHandler
and LexicalHandler
interfaces.
as well. This formatter performs typical document
formatting. The XML declaration and processing instructions are
always on their own lines. An OutputFormat
object can be
used to define how whitespace is handled when printing and allows various
configuration options, such as to allow suppression of the XML declaration,
the encoding declaration or whether empty documents are collapsed.
There are write(...)
methods to print any of the
standard DOM4J classes, including Document
and
Element
, to either a Writer
or an
OutputStream
. Warning: using your own
Writer
may cause the writer's preferred character
encoding to be ignored. If you use encodings other than UTF8, we
recommend using the method that takes an OutputStream instead.
Modifier and Type | Field and Description |
---|---|
private boolean |
autoFlush
Whether a flush should occur after writing a document
|
private StringBuffer |
buffer
buffer used when escaping strings
|
protected static OutputFormat |
DEFAULT_FORMAT |
private static boolean |
ESCAPE_TEXT |
private OutputFormat |
format
The format used by this writer
|
private int |
indentLevel
The initial number of indentations (so you can print a whole
document indented, if you like)
|
private boolean |
inDTD
Is the writer curerntly inside a DTD definition?
|
protected int |
lastOutputNodeType
Stores the last type of node written so algorithms can refer to the
previous node type
|
protected static String[] |
LEXICAL_HANDLER_NAMES |
private LexicalHandler |
lexicalHandler
Lexical handler we should delegate to
|
private NamespaceStack |
namespaceStack
The Stack of namespaceStack written so far
|
private boolean |
showCommentsInDTDs
Whether comments should appear inside DTD declarations - defaults to false
|
private static boolean |
SUPPORT_PAD_TEXT |
protected Writer |
writer
The Writer used to output to
|
Constructor and Description |
---|
XMLWriter() |
XMLWriter(OutputFormat format) |
XMLWriter(OutputStream out) |
XMLWriter(OutputStream out,
OutputFormat format) |
XMLWriter(Writer writer) |
XMLWriter(Writer writer,
OutputFormat format) |
Modifier and Type | Method and Description |
---|---|
void |
characters(char[] ch,
int start,
int length)
Filter a character data event.
|
void |
close()
Closes the underlying Writer
|
void |
comment(char[] ch,
int start,
int length)
Report an XML comment anywhere in the document.
|
protected Writer |
createWriter(OutputStream outStream,
String encoding)
Get an OutputStreamWriter, use preferred encoding.
|
void |
endCDATA()
Report the end of a CDATA section.
|
void |
endDocument()
Filter an end document event.
|
void |
endDTD()
Report the end of DTD declarations.
|
void |
endElement(String namespaceURI,
String localName,
String qName)
Filter an end element event.
|
void |
endEntity(String name)
Report the end of an entity.
|
void |
endPrefixMapping(String prefix)
Filter an end Namespace prefix mapping event.
|
protected String |
escapeAttributeEntities(String text)
This will take the pre-defined entities in XML 1.0 and
convert their character representation to the appropriate
entity reference, suitable for XML attributes.
|
protected String |
escapeElementEntities(String text)
This will take the pre-defined entities in XML 1.0 and
convert their character representation to the appropriate
entity reference, suitable for XML attributes.
|
void |
flush()
Flushes the underlying Writer
|
LexicalHandler |
getLexicalHandler() |
protected String |
getPadText() |
Object |
getProperty(String name)
Look up the value of a property.
|
protected void |
handleException(IOException e) |
void |
ignorableWhitespace(char[] ch,
int start,
int length)
Filter an ignorable whitespace event.
|
protected void |
indent() |
protected void |
installLexicalHandler() |
protected boolean |
isExpandEmptyElements() |
protected boolean |
isNamespaceDeclaration(Namespace ns) |
void |
notationDecl(String name,
String publicID,
String systemID)
Filter a notation declaration event.
|
void |
parse(InputSource source)
Parse a document.
|
void |
println()
Writes the new line text to the underlying Writer
|
void |
processingInstruction(String target,
String data)
Filter a processing instruction event.
|
void |
setDocumentLocator(Locator locator)
Filter a new document locator event.
|
void |
setIndentLevel(int indentLevel)
Set the initial indentation level.
|
void |
setLexicalHandler(LexicalHandler handler) |
void |
setOutputStream(OutputStream out) |
void |
setProperty(String name,
Object value)
Set the value of a property.
|
void |
setWriter(Writer writer) |
void |
startCDATA()
Report the start of a CDATA section.
|
void |
startDocument()
Filter a start document event.
|
void |
startDTD(String name,
String publicID,
String systemID)
Report the start of DTD declarations, if any.
|
void |
startElement(String namespaceURI,
String localName,
String qName,
Attributes attributes)
Filter a start element event.
|
void |
startEntity(String name)
Report the beginning of some internal and external XML entities.
|
void |
startPrefixMapping(String prefix,
String uri)
Filter a start Namespace prefix mapping event.
|
void |
unparsedEntityDecl(String name,
String publicID,
String systemID,
String notationName)
Filter an unparsed entity declaration event.
|
void |
write(Attribute attribute)
Writes the given
Attribute . |
void |
write(CDATA cdata)
Writes the given
CDATA . |
void |
write(Comment comment)
Writes the given
Comment . |
void |
write(Document doc)
This will print the
Document to the current Writer. |
void |
write(DocumentType docType)
Writes the given
DocumentType . |
void |
write(Element element)
|
void |
write(Entity entity)
Writes the given
Entity . |
void |
write(Namespace namespace)
Writes the given
Namespace . |
void |
write(Node node)
Writes the given
Node . |
void |
write(Object object)
Writes the given object which should be a String, a Node or a List
of Nodes.
|
void |
write(ProcessingInstruction processingInstruction)
Writes the given
ProcessingInstruction . |
void |
write(String text)
Print out a
String , Perfoms
the necessary entity escaping and whitespace stripping. |
void |
write(Text text)
Writes the given
Text . |
protected void |
writeAttribute(Attribute attribute) |
protected void |
writeAttribute(Attributes attributes,
int index) |
protected void |
writeAttributes(Attributes attributes) |
protected void |
writeAttributes(Element element)
Writes the attributes of the given element
|
protected void |
writeCDATA(String text) |
void |
writeClose(Element element)
Writes the closing tag of an
Element |
protected void |
writeClose(String qualifiedName) |
protected void |
writeComment(String text) |
protected void |
writeDeclaration()
This will write the declaration to the given Writer.
|
protected void |
writeDocType(DocumentType docType) |
protected void |
writeDocType(String name,
String publicID,
String systemID) |
protected void |
writeElement(Element element) |
protected void |
writeElementContent(Element element)
Outputs the content of the given element.
|
protected void |
writeEmptyElementClose(String qualifiedName) |
protected void |
writeEntity(Entity entity) |
protected void |
writeEntityRef(String name) |
protected void |
writeEscapeAttributeEntities(String text) |
protected void |
writeNamespace(Namespace namespace) |
protected void |
writeNode(Node node) |
void |
writeOpen(Element element)
|
protected void |
writePrintln()
This will print a new line only if the newlines flag was set to true
|
protected void |
writeProcessingInstruction(ProcessingInstruction processingInstruction) |
protected void |
writeString(String text) |
error, fatalError, getContentHandler, getDTDHandler, getEntityResolver, getErrorHandler, getFeature, getParent, parse, resolveEntity, setContentHandler, setDTDHandler, setEntityResolver, setErrorHandler, setFeature, setParent, skippedEntity, warning
protected static final String[] LEXICAL_HANDLER_NAMES
private static final boolean ESCAPE_TEXT
private static final boolean SUPPORT_PAD_TEXT
protected static final OutputFormat DEFAULT_FORMAT
protected int lastOutputNodeType
private NamespaceStack namespaceStack
private OutputFormat format
private int indentLevel
private StringBuffer buffer
private boolean autoFlush
private LexicalHandler lexicalHandler
private boolean showCommentsInDTDs
private boolean inDTD
public XMLWriter(Writer writer, OutputFormat format)
public XMLWriter()
public XMLWriter(OutputStream out) throws UnsupportedEncodingException
UnsupportedEncodingException
public XMLWriter(OutputStream out, OutputFormat format) throws UnsupportedEncodingException
UnsupportedEncodingException
public XMLWriter(OutputFormat format) throws UnsupportedEncodingException
UnsupportedEncodingException
public void setOutputStream(OutputStream out) throws UnsupportedEncodingException
UnsupportedEncodingException
public void setIndentLevel(int indentLevel)
indentLevel
- the number of indents to start withpublic void flush() throws IOException
IOException
public void close() throws IOException
IOException
public void println() throws IOException
IOException
public void write(Attribute attribute) throws IOException
Attribute
.attribute
- Attribute
to output.IOException
public void write(Document doc) throws IOException
This will print the Document
to the current Writer.
Warning: using your own Writer may cause the writer's preferred character encoding to be ignored. If you use encodings other than UTF8, we recommend using the method that takes an OutputStream instead.
Note: as with all Writers, you may need to flush() yours after this method returns.
doc
- Document
to format.IOException
- - if there's any problem writing.IOException
public void write(Element element) throws IOException
Writes the
, including
its Element
s, and its value, and all
its content (child nodes) to the current Writer.Attribute
element
- Element
to output.IOException
public void write(CDATA cdata) throws IOException
CDATA
.cdata
- CDATA
to output.IOException
public void write(Comment comment) throws IOException
Comment
.comment
- Comment
to output.IOException
public void write(DocumentType docType) throws IOException
DocumentType
.docType
- DocumentType
to output.IOException
public void write(Entity entity) throws IOException
Entity
.entity
- Entity
to output.IOException
public void write(Namespace namespace) throws IOException
Namespace
.namespace
- Namespace
to output.IOException
public void write(ProcessingInstruction processingInstruction) throws IOException
ProcessingInstruction
.processingInstruction
- ProcessingInstruction
to output.IOException
public void write(String text) throws IOException
Print out a String
, Perfoms
the necessary entity escaping and whitespace stripping.
text
- is the text to outputIOException
public void write(Text text) throws IOException
Text
.text
- Text
to output.IOException
public void write(Node node) throws IOException
Node
.node
- Node
to output.IOException
public void write(Object object) throws IOException
object
- is the object to output.IOException
public void writeOpen(Element element) throws IOException
element
- Element
to output.IOException
public void writeClose(Element element) throws IOException
Writes the closing tag of an Element
element
- Element
to output.IOException
public void parse(InputSource source) throws IOException, SAXException
XMLFilterImpl
parse
in interface XMLReader
parse
in class XMLFilterImpl
source
- The input source for the document entity.IOException
- An IO exception from the parser,
possibly from a byte stream or character stream
supplied by the application.SAXException
- Any SAX exception, possibly
wrapping another exception.XMLReader.parse(org.xml.sax.InputSource)
public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException
XMLFilterImpl
This will always fail if the parent is null.
setProperty
in interface XMLReader
setProperty
in class XMLFilterImpl
name
- The property name.SAXNotRecognizedException
- When the
XMLReader does not recognize the property name.SAXNotSupportedException
- When the
XMLReader recognizes the property name but
cannot set the requested value.XMLReader.setProperty(java.lang.String, java.lang.Object)
public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException
XMLFilterImpl
getProperty
in interface XMLReader
getProperty
in class XMLFilterImpl
name
- The property name.SAXNotRecognizedException
- When the
XMLReader does not recognize the property name.SAXNotSupportedException
- When the
XMLReader recognizes the property name but
cannot determine its value at this time.XMLReader.setFeature(java.lang.String, boolean)
public void setLexicalHandler(LexicalHandler handler)
public LexicalHandler getLexicalHandler()
public void setDocumentLocator(Locator locator)
XMLFilterImpl
setDocumentLocator
in interface ContentHandler
setDocumentLocator
in class XMLFilterImpl
locator
- The document locator.ContentHandler.setDocumentLocator(org.xml.sax.Locator)
public void startDocument() throws SAXException
XMLFilterImpl
startDocument
in interface ContentHandler
startDocument
in class XMLFilterImpl
SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.startDocument()
public void endDocument() throws SAXException
XMLFilterImpl
endDocument
in interface ContentHandler
endDocument
in class XMLFilterImpl
SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.endDocument()
public void startPrefixMapping(String prefix, String uri) throws SAXException
XMLFilterImpl
startPrefixMapping
in interface ContentHandler
startPrefixMapping
in class XMLFilterImpl
prefix
- The Namespace prefix.uri
- The Namespace URI.SAXException
- The client may throw
an exception during processing.ContentHandler.startPrefixMapping(java.lang.String, java.lang.String)
public void endPrefixMapping(String prefix) throws SAXException
XMLFilterImpl
endPrefixMapping
in interface ContentHandler
endPrefixMapping
in class XMLFilterImpl
prefix
- The Namespace prefix.SAXException
- The client may throw
an exception during processing.ContentHandler.endPrefixMapping(java.lang.String)
public void startElement(String namespaceURI, String localName, String qName, Attributes attributes) throws SAXException
XMLFilterImpl
startElement
in interface ContentHandler
startElement
in class XMLFilterImpl
namespaceURI
- The element's Namespace URI, or the empty string.localName
- The element's local name, or the empty string.qName
- The element's qualified (prefixed) name, or the empty
string.attributes
- The element's attributes.SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
public void endElement(String namespaceURI, String localName, String qName) throws SAXException
XMLFilterImpl
endElement
in interface ContentHandler
endElement
in class XMLFilterImpl
namespaceURI
- The element's Namespace URI, or the empty string.localName
- The element's local name, or the empty string.qName
- The element's qualified (prefixed) name, or the empty
string.SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.endElement(java.lang.String, java.lang.String, java.lang.String)
public void characters(char[] ch, int start, int length) throws SAXException
XMLFilterImpl
characters
in interface ContentHandler
characters
in class XMLFilterImpl
ch
- An array of characters.start
- The starting position in the array.length
- The number of characters to use from the array.SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.characters(char[], int, int)
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
XMLFilterImpl
ignorableWhitespace
in interface ContentHandler
ignorableWhitespace
in class XMLFilterImpl
ch
- An array of characters.start
- The starting position in the array.length
- The number of characters to use from the array.SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.ignorableWhitespace(char[], int, int)
public void processingInstruction(String target, String data) throws SAXException
XMLFilterImpl
processingInstruction
in interface ContentHandler
processingInstruction
in class XMLFilterImpl
target
- The processing instruction target.data
- The text following the target.SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.processingInstruction(java.lang.String, java.lang.String)
public void notationDecl(String name, String publicID, String systemID) throws SAXException
XMLFilterImpl
notationDecl
in interface DTDHandler
notationDecl
in class XMLFilterImpl
name
- The notation name.publicID
- The notation's public identifier, or null.systemID
- The notation's system identifier, or null.SAXException
- Any SAX exception, possibly
wrapping another exception.DTDHandler.notationDecl(java.lang.String, java.lang.String, java.lang.String)
public void unparsedEntityDecl(String name, String publicID, String systemID, String notationName) throws SAXException
XMLFilterImpl
unparsedEntityDecl
in interface DTDHandler
unparsedEntityDecl
in class XMLFilterImpl
name
- The entity name.publicID
- The entity's public identifier, or null.systemID
- The entity's system identifier, or null.notationName
- The name of the associated notation.SAXException
- Any SAX exception, possibly
wrapping another exception.DTDHandler.unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
public void startDTD(String name, String publicID, String systemID) throws SAXException
LexicalHandler
This method is intended to report the beginning of the DOCTYPE declaration; if the document has no DOCTYPE declaration, this method will not be invoked.
All declarations reported through
DTDHandler
or
DeclHandler
events must appear
between the startDTD and endDTD
events.
Declarations are assumed to belong to the internal DTD subset
unless they appear between startEntity
and endEntity
events. Comments and
processing instructions from the DTD should also be reported
between the startDTD and endDTD events.
Note that the start/endDTD events will appear within
the start/endDocument events from ContentHandler and
before the first
startElement
event.
startDTD
in interface LexicalHandler
name
- The document type name.publicID
- The declared public identifier for the
external DTD subset, or null if none was declared.systemID
- The declared system identifier for the
external DTD subset, or null if none was declared.SAXException
- The application may raise an
exception.LexicalHandler.endDTD()
,
LexicalHandler.startEntity(java.lang.String)
public void endDTD() throws SAXException
LexicalHandler
This method is intended to report the end of the DOCTYPE declaration; if the document has no DOCTYPE declaration, this method will not be invoked.
endDTD
in interface LexicalHandler
SAXException
- The application may raise an exception.LexicalHandler.startDTD(java.lang.String, java.lang.String, java.lang.String)
public void startCDATA() throws SAXException
LexicalHandler
The contents of the CDATA section will be reported through
the regular characters
event; this event is intended only to report
the boundary.
startCDATA
in interface LexicalHandler
SAXException
- The application may raise an exception.LexicalHandler.endCDATA()
public void endCDATA() throws SAXException
LexicalHandler
endCDATA
in interface LexicalHandler
SAXException
- The application may raise an exception.LexicalHandler.startCDATA()
public void startEntity(String name) throws SAXException
LexicalHandler
The reporting of parameter entities (including
the external DTD subset) is optional, and SAX2 drivers that
support LexicalHandler may not support it; you can use the
http://xml.org/sax/features/lexical-handler/parameter-entities
feature to query or control the reporting of parameter entities.
General entities are reported with their regular names, parameter entities have '%' prepended to their names, and the external DTD subset has the pseudo-entity name "[dtd]".
When a SAX2 driver is providing these events, all other
events must be properly nested within start/end entity
events. There is no additional requirement that events from
DeclHandler
or
DTDHandler
be properly ordered.
Note that skipped entities will be reported through the
skippedEntity
event, which is part of the ContentHandler interface.
Because of the streaming event model that SAX uses, some entity boundaries cannot be reported under any circumstances:
These will be silently expanded, with no indication of where the original entity boundaries were.
Note also that the boundaries of character references (which are not really entities anyway) are not reported.
All start/endEntity events must be properly nested.
startEntity
in interface LexicalHandler
name
- The name of the entity. If it is a parameter
entity, the name will begin with '%', and if it is the
external DTD subset, it will be "[dtd]".SAXException
- The application may raise an exception.LexicalHandler.endEntity(java.lang.String)
,
DeclHandler.internalEntityDecl(java.lang.String, java.lang.String)
,
DeclHandler.externalEntityDecl(java.lang.String, java.lang.String, java.lang.String)
public void endEntity(String name) throws SAXException
LexicalHandler
endEntity
in interface LexicalHandler
name
- The name of the entity that is ending.SAXException
- The application may raise an exception.LexicalHandler.startEntity(java.lang.String)
public void comment(char[] ch, int start, int length) throws SAXException
LexicalHandler
This callback will be used for comments inside or outside the document element, including comments in the external DTD subset (if read). Comments in the DTD must be properly nested inside start/endDTD and start/endEntity events (if used).
comment
in interface LexicalHandler
ch
- An array holding the characters in the comment.start
- The starting position in the array.length
- The number of characters to use from the array.SAXException
- The application may raise an exception.protected void writeElement(Element element) throws IOException
IOException
protected void writeElementContent(Element element) throws IOException
IOException
protected void writeCDATA(String text) throws IOException
IOException
protected void writeDocType(DocumentType docType) throws IOException
IOException
protected void writeNamespace(Namespace namespace) throws IOException
IOException
protected void writeProcessingInstruction(ProcessingInstruction processingInstruction) throws IOException
IOException
protected void writeString(String text) throws IOException
IOException
protected void writeNode(Node node) throws IOException
IOException
protected void installLexicalHandler()
protected void writeDocType(String name, String publicID, String systemID) throws IOException
IOException
protected void writeEntity(Entity entity) throws IOException
IOException
protected void writeEntityRef(String name) throws IOException
IOException
protected void writeComment(String text) throws IOException
IOException
protected void writeAttributes(Element element) throws IOException
IOException
protected void writeAttribute(Attribute attribute) throws IOException
IOException
protected void writeAttributes(Attributes attributes) throws IOException
IOException
protected void writeAttribute(Attributes attributes, int index) throws IOException
IOException
protected void indent() throws IOException
IOException
protected void writePrintln() throws IOException
This will print a new line only if the newlines flag was set to true
out
- Writer
to write toIOException
protected Writer createWriter(OutputStream outStream, String encoding) throws UnsupportedEncodingException
UnsupportedEncodingException
protected void writeDeclaration() throws IOException
This will write the declaration to the given Writer. Assumes XML version 1.0 since we don't directly know.
IOException
protected void writeClose(String qualifiedName) throws IOException
IOException
protected void writeEmptyElementClose(String qualifiedName) throws IOException
IOException
protected boolean isExpandEmptyElements()
protected String escapeElementEntities(String text)
protected void writeEscapeAttributeEntities(String text) throws IOException
IOException
protected String escapeAttributeEntities(String text)
protected boolean isNamespaceDeclaration(Namespace ns)
protected void handleException(IOException e) throws SAXException
SAXException
protected String getPadText()
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses