Class XMLWriter
- All Implemented Interfaces:
ContentHandler
,DTDHandler
,EntityResolver
,ErrorHandler
,LexicalHandler
,XMLFilter
,XMLReader
- Direct Known Subclasses:
HTMLWriter
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.
- Version:
- $Revision: 1.83 $
- Author:
- James Strachan , Joseph Bowbeer
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final OutputFormat
protected int
Stores the last type of node written so algorithms can refer to the previous node typeprotected static final String[]
protected boolean
Stores the xml:space attribute value of preserve for whitespace flagprotected Writer
The Writer used to output to -
Constructor Summary
ConstructorsConstructorDescriptionXMLWriter
(OutputStream out) XMLWriter
(OutputStream out, OutputFormat format) XMLWriter
(Writer writer, OutputFormat format) XMLWriter
(OutputFormat format) -
Method Summary
Modifier and TypeMethodDescriptionvoid
characters
(char[] ch, int start, int length) void
close()
Closes the underlying Writervoid
comment
(char[] ch, int start, int length) protected Writer
createWriter
(OutputStream outStream, String encoding) Get an OutputStreamWriter, use preferred encoding.protected int
Returns the maximum allowed character code that should be allowed unescaped which defaults to 127 in US-ASCII (7 bit) or 255 in ISO- (8 bit).void
endCDATA()
void
void
endDTD()
void
endElement
(String namespaceURI, String localName, String qName) void
void
endPrefixMapping
(String prefix) protected String
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 Writerint
Returns the maximum allowed character code that should be allowed unescaped which defaults to 127 in US-ASCII (7 bit) or 255 in ISO- (8 bit).protected OutputFormat
Lets subclasses get at the current format object, so they can call setTrimText, setNewLines, etc.getProperty
(String name) protected void
void
ignorableWhitespace
(char[] ch, int start, int length) protected void
indent()
protected void
protected final boolean
isElementSpacePreserved
(Element element) Determines if element is a special case of XML elements where it contains an xml:space attribute of "preserve".boolean
DOCUMENT ME!protected boolean
protected boolean
void
notationDecl
(String name, String publicID, String systemID) void
parse
(InputSource source) void
println()
Writes the new line text to the underlying Writervoid
processingInstruction
(String target, String data) boolean
void
setDocumentLocator
(Locator locator) void
setEscapeText
(boolean escapeText) Sets whether text output should be escaped or not.void
setIndentLevel
(int indentLevel) Set the initial indentation level.void
setLexicalHandler
(LexicalHandler handler) void
setMaximumAllowedCharacter
(int maximumAllowedCharacter) Sets the maximum allowed character code that should be allowed unescaped such as 127 in US-ASCII (7 bit) or 255 in ISO- (8 bit) or -1 to not escape any characters (other than the special XML characters like < > &) If this is not explicitly set then it is defaulted from the encoding.void
void
setProperty
(String name, Object value) void
setResolveEntityRefs
(boolean resolve) void
protected boolean
shouldEncodeChar
(int codepoint) Should the given character be escaped.void
void
void
void
startElement
(String namespaceURI, String localName, String qName, Attributes attributes) void
startEntity
(String name) void
startPrefixMapping
(String prefix, String uri) void
unparsedEntityDecl
(String name, String publicID, String systemID, String notationName) void
Writes the given object which should be a String, a Node or a List of Nodes.void
Print out aString
, Perfoms the necessary entity escaping and whitespace stripping.void
Writes the givenAttribute
.void
Writes the givenCDATA
.void
Writes the givenComment
.void
This will print theDocument
to the current Writer.void
write
(DocumentType docType) Writes the givenDocumentType
.void
void
Writes the givenEntity
.void
Writes the givenNamespace
.void
Writes the givenNode
.void
write
(ProcessingInstruction processingInstruction) Writes the givenProcessingInstruction
.void
Writes the givenText
.protected void
writeAttribute
(String qualifiedName, String value) protected void
writeAttribute
(Attribute attribute) protected void
writeAttribute
(Attributes attributes, int index) protected void
writeAttributes
(Element element) Writes the attributes of the given elementprotected void
writeAttributes
(Attributes attributes) protected void
writeCDATA
(String text) protected void
writeClose
(String qualifiedName) void
writeClose
(Element element) Writes the closing tag of anElement
protected void
writeComment
(String text) protected void
This will write the declaration to the given Writer.protected void
writeDocType
(String name, String publicID, String systemID) protected void
writeDocType
(DocumentType docType) 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
protected void
writeNamespace
(String prefix, String uri) Writes the SAX namepsacesprotected void
writeNamespace
(Namespace namespace) protected void
Writes the SAX namepsacesprotected void
writeNamespaces
(Element element) Writes all namespaces declared directly on element.protected void
protected void
writeNodeText
(Node node) This method is used to write out Nodes that contain text and still allow for xml:space to be handled properly.void
protected void
This will print a new line only if the newlines flag was set to trueprotected void
protected void
writeString
(String text) Methods inherited from class org.xml.sax.helpers.XMLFilterImpl
error, fatalError, getContentHandler, getDTDHandler, getEntityResolver, getErrorHandler, getFeature, getParent, parse, resolveEntity, setContentHandler, setDTDHandler, setEntityResolver, setErrorHandler, setFeature, setParent, skippedEntity, warning
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.xml.sax.ContentHandler
declaration
-
Field Details
-
LEXICAL_HANDLER_NAMES
-
DEFAULT_FORMAT
-
lastOutputNodeType
protected int lastOutputNodeTypeStores the last type of node written so algorithms can refer to the previous node type -
preserve
protected boolean preserveStores the xml:space attribute value of preserve for whitespace flag -
writer
The Writer used to output to
-
-
Constructor Details
-
XMLWriter
-
XMLWriter
-
XMLWriter
public XMLWriter() -
XMLWriter
- Throws:
UnsupportedEncodingException
-
XMLWriter
- Throws:
UnsupportedEncodingException
-
XMLWriter
- Throws:
UnsupportedEncodingException
-
-
Method Details
-
setWriter
-
setOutputStream
- Throws:
UnsupportedEncodingException
-
isEscapeText
public boolean isEscapeText()DOCUMENT ME!- Returns:
- true if text thats output should be escaped. This is enabled by default. It could be disabled if the output format is textual, like in XSLT where we can have xml, html or text output.
-
setEscapeText
public void setEscapeText(boolean escapeText) Sets whether text output should be escaped or not. This is enabled by default. It could be disabled if the output format is textual, like in XSLT where we can have xml, html or text output.- Parameters:
escapeText
- DOCUMENT ME!
-
setIndentLevel
public void setIndentLevel(int indentLevel) Set the initial indentation level. This can be used to output a document (or, more likely, an element) starting at a given indent level, so it's not always flush against the left margin. Default: 0- Parameters:
indentLevel
- the number of indents to start with
-
getMaximumAllowedCharacter
public int getMaximumAllowedCharacter()Returns the maximum allowed character code that should be allowed unescaped which defaults to 127 in US-ASCII (7 bit) or 255 in ISO- (8 bit).- Returns:
- DOCUMENT ME!
-
setMaximumAllowedCharacter
public void setMaximumAllowedCharacter(int maximumAllowedCharacter) Sets the maximum allowed character code that should be allowed unescaped such as 127 in US-ASCII (7 bit) or 255 in ISO- (8 bit) or -1 to not escape any characters (other than the special XML characters like < > &) If this is not explicitly set then it is defaulted from the encoding.- Parameters:
maximumAllowedCharacter
- The maximumAllowedCharacter to set
-
flush
Flushes the underlying Writer- Throws:
IOException
- DOCUMENT ME!
-
close
Closes the underlying Writer- Throws:
IOException
- DOCUMENT ME!
-
println
Writes the new line text to the underlying Writer- Throws:
IOException
- DOCUMENT ME!
-
write
Writes the givenAttribute
.- Parameters:
attribute
-Attribute
to output.- Throws:
IOException
- DOCUMENT ME!
-
write
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.
- Parameters:
doc
-Document
to format.- Throws:
IOException
- if there's any problem writing.
-
write
Writes the
, including itsElement
s, and its value, and all its content (child nodes) to the current Writer.Attribute
- Parameters:
element
-Element
to output.- Throws:
IOException
- DOCUMENT ME!
-
write
Writes the givenCDATA
.- Parameters:
cdata
-CDATA
to output.- Throws:
IOException
- DOCUMENT ME!
-
write
Writes the givenComment
.- Parameters:
comment
-Comment
to output.- Throws:
IOException
- DOCUMENT ME!
-
write
Writes the givenDocumentType
.- Parameters:
docType
-DocumentType
to output.- Throws:
IOException
- DOCUMENT ME!
-
write
Writes the givenEntity
.- Parameters:
entity
-Entity
to output.- Throws:
IOException
- DOCUMENT ME!
-
write
Writes the givenNamespace
.- Parameters:
namespace
-Namespace
to output.- Throws:
IOException
- DOCUMENT ME!
-
write
Writes the givenProcessingInstruction
.- Parameters:
processingInstruction
-ProcessingInstruction
to output.- Throws:
IOException
- DOCUMENT ME!
-
write
Print out a
String
, Perfoms the necessary entity escaping and whitespace stripping.- Parameters:
text
- is the text to output- Throws:
IOException
- DOCUMENT ME!
-
write
Writes the givenText
.- Parameters:
text
-Text
to output.- Throws:
IOException
- DOCUMENT ME!
-
write
Writes the givenNode
.- Parameters:
node
-Node
to output.- Throws:
IOException
- DOCUMENT ME!
-
write
Writes the given object which should be a String, a Node or a List of Nodes.- Parameters:
object
- is the object to output.- Throws:
IOException
- DOCUMENT ME!
-
writeOpen
- Parameters:
element
-Element
to output.- Throws:
IOException
- DOCUMENT ME!
-
writeClose
Writes the closing tag of an
Element
- Parameters:
element
-Element
to output.- Throws:
IOException
- DOCUMENT ME!
-
parse
- Specified by:
parse
in interfaceXMLReader
- Overrides:
parse
in classXMLFilterImpl
- Throws:
IOException
SAXException
-
setProperty
public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException - Specified by:
setProperty
in interfaceXMLReader
- Overrides:
setProperty
in classXMLFilterImpl
- Throws:
SAXNotRecognizedException
SAXNotSupportedException
-
getProperty
- Specified by:
getProperty
in interfaceXMLReader
- Overrides:
getProperty
in classXMLFilterImpl
- Throws:
SAXNotRecognizedException
SAXNotSupportedException
-
setLexicalHandler
-
getLexicalHandler
-
setDocumentLocator
- Specified by:
setDocumentLocator
in interfaceContentHandler
- Overrides:
setDocumentLocator
in classXMLFilterImpl
-
startDocument
- Specified by:
startDocument
in interfaceContentHandler
- Overrides:
startDocument
in classXMLFilterImpl
- Throws:
SAXException
-
endDocument
- Specified by:
endDocument
in interfaceContentHandler
- Overrides:
endDocument
in classXMLFilterImpl
- Throws:
SAXException
-
startPrefixMapping
- Specified by:
startPrefixMapping
in interfaceContentHandler
- Overrides:
startPrefixMapping
in classXMLFilterImpl
- Throws:
SAXException
-
endPrefixMapping
- Specified by:
endPrefixMapping
in interfaceContentHandler
- Overrides:
endPrefixMapping
in classXMLFilterImpl
- Throws:
SAXException
-
startElement
public void startElement(String namespaceURI, String localName, String qName, Attributes attributes) throws SAXException - Specified by:
startElement
in interfaceContentHandler
- Overrides:
startElement
in classXMLFilterImpl
- Throws:
SAXException
-
endElement
- Specified by:
endElement
in interfaceContentHandler
- Overrides:
endElement
in classXMLFilterImpl
- Throws:
SAXException
-
characters
- Specified by:
characters
in interfaceContentHandler
- Overrides:
characters
in classXMLFilterImpl
- Throws:
SAXException
-
ignorableWhitespace
- Specified by:
ignorableWhitespace
in interfaceContentHandler
- Overrides:
ignorableWhitespace
in classXMLFilterImpl
- Throws:
SAXException
-
processingInstruction
- Specified by:
processingInstruction
in interfaceContentHandler
- Overrides:
processingInstruction
in classXMLFilterImpl
- Throws:
SAXException
-
notationDecl
- Specified by:
notationDecl
in interfaceDTDHandler
- Overrides:
notationDecl
in classXMLFilterImpl
- Throws:
SAXException
-
unparsedEntityDecl
public void unparsedEntityDecl(String name, String publicID, String systemID, String notationName) throws SAXException - Specified by:
unparsedEntityDecl
in interfaceDTDHandler
- Overrides:
unparsedEntityDecl
in classXMLFilterImpl
- Throws:
SAXException
-
startDTD
- Specified by:
startDTD
in interfaceLexicalHandler
- Throws:
SAXException
-
endDTD
- Specified by:
endDTD
in interfaceLexicalHandler
- Throws:
SAXException
-
startCDATA
- Specified by:
startCDATA
in interfaceLexicalHandler
- Throws:
SAXException
-
endCDATA
- Specified by:
endCDATA
in interfaceLexicalHandler
- Throws:
SAXException
-
startEntity
- Specified by:
startEntity
in interfaceLexicalHandler
- Throws:
SAXException
-
endEntity
- Specified by:
endEntity
in interfaceLexicalHandler
- Throws:
SAXException
-
comment
- Specified by:
comment
in interfaceLexicalHandler
- Throws:
SAXException
-
writeElement
- Throws:
IOException
-
isElementSpacePreserved
Determines if element is a special case of XML elements where it contains an xml:space attribute of "preserve". If it does, then retain whitespace.- Parameters:
element
- DOCUMENT ME!- Returns:
- DOCUMENT ME!
-
writeElementContent
Outputs the content of the given element. If whitespace trimming is enabled then all adjacent text nodes are appended together before the whitespace trimming occurs to avoid problems with multiple text nodes being created due to text content that spans parser buffers in a SAX parser.- Parameters:
element
- DOCUMENT ME!- Throws:
IOException
- DOCUMENT ME!
-
writeCDATA
- Throws:
IOException
-
writeDocType
- Throws:
IOException
-
writeNamespace
- Throws:
IOException
-
writeNamespaces
Writes the SAX namepsaces- Throws:
IOException
- DOCUMENT ME!
-
writeNamespace
Writes the SAX namepsaces- Parameters:
prefix
- the prefixuri
- the namespace uri- Throws:
IOException
- DOCUMENT ME!
-
writeNamespaces
Writes all namespaces declared directly on element.- Throws:
IOException
-
writeProcessingInstruction
- Throws:
IOException
-
writeString
- Throws:
IOException
-
writeNodeText
This method is used to write out Nodes that contain text and still allow for xml:space to be handled properly.- Parameters:
node
- DOCUMENT ME!- Throws:
IOException
- DOCUMENT ME!
-
writeNode
- Throws:
IOException
-
installLexicalHandler
protected void installLexicalHandler() -
writeDocType
- Throws:
IOException
-
writeEntity
- Throws:
IOException
-
writeEntityRef
- Throws:
IOException
-
writeComment
- Throws:
IOException
-
writeAttributes
Writes the attributes of the given element- Parameters:
element
- DOCUMENT ME!- Throws:
IOException
- DOCUMENT ME!
-
writeAttribute
- Throws:
IOException
-
writeAttribute
- Throws:
IOException
-
writeAttributes
- Throws:
IOException
-
writeAttribute
- Throws:
IOException
-
indent
- Throws:
IOException
-
writePrintln
This will print a new line only if the newlines flag was set to true
- Throws:
IOException
- DOCUMENT ME!
-
createWriter
protected Writer createWriter(OutputStream outStream, String encoding) throws UnsupportedEncodingException Get an OutputStreamWriter, use preferred encoding.- Parameters:
outStream
- DOCUMENT ME!encoding
- DOCUMENT ME!- Returns:
- DOCUMENT ME!
- Throws:
UnsupportedEncodingException
- DOCUMENT ME!
-
writeDeclaration
This will write the declaration to the given Writer. Assumes XML version 1.0 since we don't directly know.
- Throws:
IOException
- DOCUMENT ME!
-
writeClose
- Throws:
IOException
-
writeEmptyElementClose
- Throws:
IOException
-
isExpandEmptyElements
protected boolean isExpandEmptyElements() -
escapeElementEntities
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.- Parameters:
text
- DOCUMENT ME!- Returns:
- DOCUMENT ME!
-
writeEscapeAttributeEntities
- Throws:
IOException
-
escapeAttributeEntities
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.- Parameters:
text
- DOCUMENT ME!- Returns:
- DOCUMENT ME!
-
shouldEncodeChar
protected boolean shouldEncodeChar(int codepoint) Should the given character be escaped. This depends on the encoding of the document.- Parameters:
codepoint
- Unicode codepoint. DOCUMENT ME!- Returns:
- boolean
-
defaultMaximumAllowedCharacter
protected int defaultMaximumAllowedCharacter()Returns the maximum allowed character code that should be allowed unescaped which defaults to 127 in US-ASCII (7 bit) or 255 in ISO- (8 bit).- Returns:
- DOCUMENT ME!
-
isNamespaceDeclaration
-
handleException
- Throws:
SAXException
-
getOutputFormat
Lets subclasses get at the current format object, so they can call setTrimText, setNewLines, etc. Put in to support the HTMLWriter, in the way that it pushes the current newline/trim state onto a stack and overrides the state within preformatted tags.- Returns:
- DOCUMENT ME!
-
resolveEntityRefs
public boolean resolveEntityRefs() -
setResolveEntityRefs
public void setResolveEntityRefs(boolean resolve)
-