org.xml.sax.ext.LexicalHandler
HTMLWriter
public class XMLWriter
extends org.xml.sax.helpers.XMLFilterImpl
implements org.xml.sax.ext.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 | Description |
---|---|---|
protected static OutputFormat |
DEFAULT_FORMAT |
|
protected int |
lastOutputNodeType |
Stores the last type of node written so algorithms can refer to the
previous node type
|
protected static java.lang.String[] |
LEXICAL_HANDLER_NAMES |
|
protected boolean |
preserve |
Stores the xml:space attribute value of preserve for whitespace flag
|
protected java.io.Writer |
writer |
The Writer used to output to
|
Constructor | Description |
---|---|
XMLWriter() |
|
XMLWriter(java.io.OutputStream out) |
|
XMLWriter(java.io.OutputStream out,
OutputFormat format) |
|
XMLWriter(java.io.Writer writer) |
|
XMLWriter(java.io.Writer writer,
OutputFormat format) |
|
XMLWriter(OutputFormat format) |
Modifier and Type | Method | Description |
---|---|---|
void |
close() |
Closes the underlying Writer
|
void |
comment(char[] ch,
int start,
int length) |
|
protected java.io.Writer |
createWriter(java.io.OutputStream outStream,
java.lang.String encoding) |
Get an OutputStreamWriter, use preferred encoding.
|
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).
|
void |
endCDATA() |
|
void |
endDocument() |
|
void |
endDTD() |
|
void |
endElement(java.lang.String namespaceURI,
java.lang.String localName,
java.lang.String qName) |
|
void |
endEntity(java.lang.String name) |
|
void |
endPrefixMapping(java.lang.String prefix) |
|
protected java.lang.String |
escapeAttributeEntities(java.lang.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 java.lang.String |
escapeElementEntities(java.lang.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
|
org.xml.sax.ext.LexicalHandler |
getLexicalHandler() |
|
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).
|
protected OutputFormat |
getOutputFormat() |
Lets subclasses get at the current format object, so they can call
setTrimText, setNewLines, etc.
|
java.lang.Object |
getProperty(java.lang.String name) |
|
protected void |
handleException(java.io.IOException e) |
|
void |
characters(char[] ch,
int start,
int length) |
|
void |
ignorableWhitespace(char[] ch,
int start,
int length) |
|
protected void |
indent() |
|
protected void |
installLexicalHandler() |
|
protected boolean |
isElementSpacePreserved(Element element) |
Determines if element is a special case of XML elements where it contains
an xml:space attribute of "preserve".
|
boolean |
isEscapeText() |
DOCUMENT ME!
|
protected boolean |
isExpandEmptyElements() |
|
protected boolean |
isNamespaceDeclaration(Namespace ns) |
|
void |
notationDecl(java.lang.String name,
java.lang.String publicID,
java.lang.String systemID) |
|
void |
parse(org.xml.sax.InputSource source) |
|
void |
println() |
Writes the new line text to the underlying Writer
|
void |
processingInstruction(java.lang.String target,
java.lang.String data) |
|
boolean |
resolveEntityRefs() |
|
void |
setDocumentLocator(org.xml.sax.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(org.xml.sax.ext.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 |
setOutputStream(java.io.OutputStream out) |
|
void |
setProperty(java.lang.String name,
java.lang.Object value) |
|
void |
setResolveEntityRefs(boolean resolve) |
|
void |
setWriter(java.io.Writer writer) |
|
protected boolean |
shouldEncodeChar(int codepoint) |
Should the given character be escaped.
|
void |
startCDATA() |
|
void |
startDocument() |
|
void |
startDTD(java.lang.String name,
java.lang.String publicID,
java.lang.String systemID) |
|
void |
startElement(java.lang.String namespaceURI,
java.lang.String localName,
java.lang.String qName,
org.xml.sax.Attributes attributes) |
|
void |
startEntity(java.lang.String name) |
|
void |
startPrefixMapping(java.lang.String prefix,
java.lang.String uri) |
|
void |
unparsedEntityDecl(java.lang.String name,
java.lang.String publicID,
java.lang.String systemID,
java.lang.String notationName) |
|
void |
write(java.lang.Object object) |
Writes the given object which should be a String, a Node or a List of
Nodes.
|
void |
write(java.lang.String text) |
Print out a
String , Perfoms the necessary entity escaping and
whitespace stripping. |
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(ProcessingInstruction processingInstruction) |
Writes the given
ProcessingInstruction . |
void |
write(Text text) |
Writes the given
Text . |
protected void |
writeAttribute(Attribute attribute) |
|
protected void |
writeAttribute(org.xml.sax.Attributes attributes,
int index) |
|
protected void |
writeAttributes(Element element) |
Writes the attributes of the given element
|
protected void |
writeAttributes(org.xml.sax.Attributes attributes) |
|
protected void |
writeCDATA(java.lang.String text) |
|
protected void |
writeClose(java.lang.String qualifiedName) |
|
void |
writeClose(Element element) |
Writes the closing tag of an
Element |
protected void |
writeComment(java.lang.String text) |
|
protected void |
writeDeclaration() |
This will write the declaration to the given Writer.
|
protected void |
writeDocType(java.lang.String name,
java.lang.String publicID,
java.lang.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(java.lang.String qualifiedName) |
|
protected void |
writeEntity(Entity entity) |
|
protected void |
writeEntityRef(java.lang.String name) |
|
protected void |
writeEscapeAttributeEntities(java.lang.String txt) |
|
protected void |
writeNamespace(java.lang.String prefix,
java.lang.String uri) |
Writes the SAX namepsaces
|
protected void |
writeNamespace(Namespace namespace) |
|
protected void |
writeNamespaces() |
Writes the SAX namepsaces
|
protected void |
writeNamespaces(Element element) |
Writes all namespaces declared directly on element.
|
protected void |
writeNode(Node node) |
|
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 |
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 pi) |
|
protected void |
writeString(java.lang.String text) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
error, fatalError, getContentHandler, getDTDHandler, getEntityResolver, getErrorHandler, getFeature, getParent, parse, resolveEntity, setContentHandler, setDTDHandler, setEntityResolver, setErrorHandler, setFeature, setParent, skippedEntity, warning
protected static final java.lang.String[] LEXICAL_HANDLER_NAMES
protected static final OutputFormat DEFAULT_FORMAT
protected int lastOutputNodeType
protected boolean preserve
protected java.io.Writer writer
public XMLWriter(java.io.Writer writer)
public XMLWriter(java.io.Writer writer, OutputFormat format)
public XMLWriter()
public XMLWriter(java.io.OutputStream out) throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException
public XMLWriter(java.io.OutputStream out, OutputFormat format) throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException
public XMLWriter(OutputFormat format) throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException
public void setWriter(java.io.Writer writer)
public void setOutputStream(java.io.OutputStream out) throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException
public boolean isEscapeText()
public void setEscapeText(boolean escapeText)
escapeText
- DOCUMENT ME!public void setIndentLevel(int indentLevel)
indentLevel
- the number of indents to start withpublic int getMaximumAllowedCharacter()
public void setMaximumAllowedCharacter(int maximumAllowedCharacter)
maximumAllowedCharacter
- The maximumAllowedCharacter to setpublic void flush() throws java.io.IOException
java.io.IOException
- DOCUMENT ME!public void close() throws java.io.IOException
java.io.IOException
- DOCUMENT ME!public void println() throws java.io.IOException
java.io.IOException
- DOCUMENT ME!public void write(Attribute attribute) throws java.io.IOException
Attribute
.attribute
- Attribute
to output.java.io.IOException
- DOCUMENT ME!public void write(Document doc) throws java.io.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.java.io.IOException
- if there's any problem writing.public void write(Element element) throws java.io.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.java.io.IOException
- DOCUMENT ME!public void write(CDATA cdata) throws java.io.IOException
CDATA
.cdata
- CDATA
to output.java.io.IOException
- DOCUMENT ME!public void write(Comment comment) throws java.io.IOException
Comment
.comment
- Comment
to output.java.io.IOException
- DOCUMENT ME!public void write(DocumentType docType) throws java.io.IOException
DocumentType
.docType
- DocumentType
to output.java.io.IOException
- DOCUMENT ME!public void write(Entity entity) throws java.io.IOException
Entity
.entity
- Entity
to output.java.io.IOException
- DOCUMENT ME!public void write(Namespace namespace) throws java.io.IOException
Namespace
.namespace
- Namespace
to output.java.io.IOException
- DOCUMENT ME!public void write(ProcessingInstruction processingInstruction) throws java.io.IOException
ProcessingInstruction
.processingInstruction
- ProcessingInstruction
to output.java.io.IOException
- DOCUMENT ME!public void write(java.lang.String text) throws java.io.IOException
Print out a String
, Perfoms the necessary entity escaping and
whitespace stripping.
text
- is the text to outputjava.io.IOException
- DOCUMENT ME!public void write(Text text) throws java.io.IOException
Text
.text
- Text
to output.java.io.IOException
- DOCUMENT ME!public void write(Node node) throws java.io.IOException
Node
.node
- Node
to output.java.io.IOException
- DOCUMENT ME!public void write(java.lang.Object object) throws java.io.IOException
object
- is the object to output.java.io.IOException
- DOCUMENT ME!public void writeOpen(Element element) throws java.io.IOException
element
- Element
to output.java.io.IOException
- DOCUMENT ME!public void writeClose(Element element) throws java.io.IOException
Writes the closing tag of an Element
element
- Element
to output.java.io.IOException
- DOCUMENT ME!public void parse(org.xml.sax.InputSource source) throws java.io.IOException, org.xml.sax.SAXException
parse
in class org.xml.sax.helpers.XMLFilterImpl
java.io.IOException
org.xml.sax.SAXException
public void setProperty(java.lang.String name, java.lang.Object value) throws org.xml.sax.SAXNotRecognizedException, org.xml.sax.SAXNotSupportedException
setProperty
in class org.xml.sax.helpers.XMLFilterImpl
org.xml.sax.SAXNotRecognizedException
org.xml.sax.SAXNotSupportedException
public java.lang.Object getProperty(java.lang.String name) throws org.xml.sax.SAXNotRecognizedException, org.xml.sax.SAXNotSupportedException
getProperty
in class org.xml.sax.helpers.XMLFilterImpl
org.xml.sax.SAXNotRecognizedException
org.xml.sax.SAXNotSupportedException
public void setLexicalHandler(org.xml.sax.ext.LexicalHandler handler)
public org.xml.sax.ext.LexicalHandler getLexicalHandler()
public void setDocumentLocator(org.xml.sax.Locator locator)
setDocumentLocator
in class org.xml.sax.helpers.XMLFilterImpl
public void startDocument() throws org.xml.sax.SAXException
startDocument
in class org.xml.sax.helpers.XMLFilterImpl
org.xml.sax.SAXException
public void endDocument() throws org.xml.sax.SAXException
endDocument
in class org.xml.sax.helpers.XMLFilterImpl
org.xml.sax.SAXException
public void startPrefixMapping(java.lang.String prefix, java.lang.String uri) throws org.xml.sax.SAXException
startPrefixMapping
in class org.xml.sax.helpers.XMLFilterImpl
org.xml.sax.SAXException
public void endPrefixMapping(java.lang.String prefix) throws org.xml.sax.SAXException
endPrefixMapping
in class org.xml.sax.helpers.XMLFilterImpl
org.xml.sax.SAXException
public void startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attributes) throws org.xml.sax.SAXException
startElement
in class org.xml.sax.helpers.XMLFilterImpl
org.xml.sax.SAXException
public void endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName) throws org.xml.sax.SAXException
endElement
in class org.xml.sax.helpers.XMLFilterImpl
org.xml.sax.SAXException
public void characters(char[] ch, int start, int length) throws org.xml.sax.SAXException
characters
in class org.xml.sax.helpers.XMLFilterImpl
org.xml.sax.SAXException
public void ignorableWhitespace(char[] ch, int start, int length) throws org.xml.sax.SAXException
ignorableWhitespace
in class org.xml.sax.helpers.XMLFilterImpl
org.xml.sax.SAXException
public void processingInstruction(java.lang.String target, java.lang.String data) throws org.xml.sax.SAXException
processingInstruction
in class org.xml.sax.helpers.XMLFilterImpl
org.xml.sax.SAXException
public void notationDecl(java.lang.String name, java.lang.String publicID, java.lang.String systemID) throws org.xml.sax.SAXException
notationDecl
in class org.xml.sax.helpers.XMLFilterImpl
org.xml.sax.SAXException
public void unparsedEntityDecl(java.lang.String name, java.lang.String publicID, java.lang.String systemID, java.lang.String notationName) throws org.xml.sax.SAXException
unparsedEntityDecl
in class org.xml.sax.helpers.XMLFilterImpl
org.xml.sax.SAXException
public void startDTD(java.lang.String name, java.lang.String publicID, java.lang.String systemID) throws org.xml.sax.SAXException
startDTD
in interface org.xml.sax.ext.LexicalHandler
org.xml.sax.SAXException
public void endDTD() throws org.xml.sax.SAXException
endDTD
in interface org.xml.sax.ext.LexicalHandler
org.xml.sax.SAXException
public void startCDATA() throws org.xml.sax.SAXException
startCDATA
in interface org.xml.sax.ext.LexicalHandler
org.xml.sax.SAXException
public void endCDATA() throws org.xml.sax.SAXException
endCDATA
in interface org.xml.sax.ext.LexicalHandler
org.xml.sax.SAXException
public void startEntity(java.lang.String name) throws org.xml.sax.SAXException
startEntity
in interface org.xml.sax.ext.LexicalHandler
org.xml.sax.SAXException
public void endEntity(java.lang.String name) throws org.xml.sax.SAXException
endEntity
in interface org.xml.sax.ext.LexicalHandler
org.xml.sax.SAXException
public void comment(char[] ch, int start, int length) throws org.xml.sax.SAXException
comment
in interface org.xml.sax.ext.LexicalHandler
org.xml.sax.SAXException
protected void writeElement(Element element) throws java.io.IOException
java.io.IOException
protected final boolean isElementSpacePreserved(Element element)
element
- DOCUMENT ME!protected void writeElementContent(Element element) throws java.io.IOException
element
- DOCUMENT ME!java.io.IOException
- DOCUMENT ME!protected void writeCDATA(java.lang.String text) throws java.io.IOException
java.io.IOException
protected void writeDocType(DocumentType docType) throws java.io.IOException
java.io.IOException
protected void writeNamespace(Namespace namespace) throws java.io.IOException
java.io.IOException
protected void writeNamespaces() throws java.io.IOException
java.io.IOException
- DOCUMENT ME!protected void writeNamespace(java.lang.String prefix, java.lang.String uri) throws java.io.IOException
prefix
- the prefixuri
- the namespace urijava.io.IOException
- DOCUMENT ME!protected void writeNamespaces(Element element) throws java.io.IOException
java.io.IOException
protected void writeProcessingInstruction(ProcessingInstruction pi) throws java.io.IOException
java.io.IOException
protected void writeString(java.lang.String text) throws java.io.IOException
java.io.IOException
protected void writeNodeText(Node node) throws java.io.IOException
node
- DOCUMENT ME!java.io.IOException
- DOCUMENT ME!protected void writeNode(Node node) throws java.io.IOException
java.io.IOException
protected void installLexicalHandler()
protected void writeDocType(java.lang.String name, java.lang.String publicID, java.lang.String systemID) throws java.io.IOException
java.io.IOException
protected void writeEntity(Entity entity) throws java.io.IOException
java.io.IOException
protected void writeEntityRef(java.lang.String name) throws java.io.IOException
java.io.IOException
protected void writeComment(java.lang.String text) throws java.io.IOException
java.io.IOException
protected void writeAttributes(Element element) throws java.io.IOException
element
- DOCUMENT ME!java.io.IOException
- DOCUMENT ME!protected void writeAttribute(Attribute attribute) throws java.io.IOException
java.io.IOException
protected void writeAttributes(org.xml.sax.Attributes attributes) throws java.io.IOException
java.io.IOException
protected void writeAttribute(org.xml.sax.Attributes attributes, int index) throws java.io.IOException
java.io.IOException
protected void indent() throws java.io.IOException
java.io.IOException
protected void writePrintln() throws java.io.IOException
This will print a new line only if the newlines flag was set to true
java.io.IOException
- DOCUMENT ME!protected java.io.Writer createWriter(java.io.OutputStream outStream, java.lang.String encoding) throws java.io.UnsupportedEncodingException
outStream
- DOCUMENT ME!encoding
- DOCUMENT ME!java.io.UnsupportedEncodingException
- DOCUMENT ME!protected void writeDeclaration() throws java.io.IOException
This will write the declaration to the given Writer. Assumes XML version 1.0 since we don't directly know.
java.io.IOException
- DOCUMENT ME!protected void writeClose(java.lang.String qualifiedName) throws java.io.IOException
java.io.IOException
protected void writeEmptyElementClose(java.lang.String qualifiedName) throws java.io.IOException
java.io.IOException
protected boolean isExpandEmptyElements()
protected java.lang.String escapeElementEntities(java.lang.String text)
text
- DOCUMENT ME!protected void writeEscapeAttributeEntities(java.lang.String txt) throws java.io.IOException
java.io.IOException
protected java.lang.String escapeAttributeEntities(java.lang.String text)
text
- DOCUMENT ME!protected boolean shouldEncodeChar(int codepoint)
codepoint
- Unicode codepoint.
DOCUMENT ME!protected int defaultMaximumAllowedCharacter()
protected boolean isNamespaceDeclaration(Namespace ns)
protected void handleException(java.io.IOException e) throws org.xml.sax.SAXException
org.xml.sax.SAXException
protected OutputFormat getOutputFormat()
public boolean resolveEntityRefs()
public void setResolveEntityRefs(boolean resolve)