public class XMLWriter extends org.xml.sax.helpers.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 ContentHandlerand LexicalHandler interfaces. as well. This formatter performs typical document
formatting. The XML declaration and processing instructions are always on
their own lines. An OutputFormatobject 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 |
|---|---|
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 String[] |
LEXICAL_HANDLER_NAMES |
protected boolean |
preserve
Stores the xml:space attribute value of preserve for whitespace flag
|
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) |
void |
close()
Closes the underlying Writer
|
void |
comment(char[] ch,
int start,
int length) |
protected Writer |
createWriter(OutputStream outStream,
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(String namespaceURI,
String localName,
String qName) |
void |
endEntity(String name) |
void |
endPrefixMapping(String prefix) |
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() |
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.
|
Object |
getProperty(String name) |
protected void |
handleException(IOException e) |
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(String name,
String publicID,
String systemID) |
void |
parse(InputSource source) |
void |
println()
Writes the new line text to the underlying Writer
|
void |
processingInstruction(String target,
String data) |
boolean |
resolveEntityRefs() |
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 |
setOutputStream(OutputStream out) |
void |
setProperty(String name,
Object value) |
void |
setResolveEntityRefs(boolean resolve) |
void |
setWriter(Writer writer) |
protected boolean |
shouldEncodeChar(char c)
Should the given character be escaped.
|
void |
startCDATA() |
void |
startDocument() |
void |
startDTD(String name,
String publicID,
String systemID) |
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 |
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 txt) |
protected void |
writeNamespace(Namespace namespace) |
protected void |
writeNamespace(String prefix,
String uri)
Writes the SAX namepsaces
|
protected void |
writeNamespaces()
Writes the SAX namepsaces
|
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(String text) |
error, fatalError, getContentHandler, getDTDHandler, getEntityResolver, getErrorHandler, getFeature, getParent, parse, resolveEntity, setContentHandler, setDTDHandler, setEntityResolver, setErrorHandler, setFeature, setParent, skippedEntity, warningprotected static final String[] LEXICAL_HANDLER_NAMES
protected static final OutputFormat DEFAULT_FORMAT
protected int lastOutputNodeType
protected boolean preserve
protected Writer writer
public XMLWriter(Writer writer)
public XMLWriter(Writer writer, OutputFormat format)
public XMLWriter()
public XMLWriter(OutputStream out) throws UnsupportedEncodingException
UnsupportedEncodingExceptionpublic XMLWriter(OutputStream out, OutputFormat format) throws UnsupportedEncodingException
UnsupportedEncodingExceptionpublic XMLWriter(OutputFormat format) throws UnsupportedEncodingException
UnsupportedEncodingExceptionpublic void setWriter(Writer writer)
public void setOutputStream(OutputStream out) throws UnsupportedEncodingException
UnsupportedEncodingExceptionpublic 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 IOException
IOException - DOCUMENT ME!public void close()
throws IOException
IOException - DOCUMENT ME!public void println()
throws IOException
IOException - DOCUMENT ME!public void write(Attribute attribute) throws IOException
Attribute.attribute - Attribute to output.IOException - DOCUMENT ME!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.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 - DOCUMENT ME!public void write(CDATA cdata) throws IOException
CDATA.cdata - CDATA to output.IOException - DOCUMENT ME!public void write(Comment comment) throws IOException
Comment.comment - Comment to output.IOException - DOCUMENT ME!public void write(DocumentType docType) throws IOException
DocumentType.docType - DocumentType to output.IOException - DOCUMENT ME!public void write(Entity entity) throws IOException
Entity.entity - Entity to output.IOException - DOCUMENT ME!public void write(Namespace namespace) throws IOException
Namespace.namespace - Namespace to output.IOException - DOCUMENT ME!public void write(ProcessingInstruction processingInstruction) throws IOException
ProcessingInstruction.processingInstruction - ProcessingInstruction to output.IOException - DOCUMENT ME!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 - DOCUMENT ME!public void write(Text text) throws IOException
Text.text - Text to output.IOException - DOCUMENT ME!public void write(Node node) throws IOException
Node.node - Node to output.IOException - DOCUMENT ME!public void write(Object object) throws IOException
object - is the object to output.IOException - DOCUMENT ME!public void writeOpen(Element element) throws IOException
element - Element to output.IOException - DOCUMENT ME!public void writeClose(Element element) throws IOException
Writes the closing tag of an Element
element - Element to output.IOException - DOCUMENT ME!public void parse(InputSource source) throws IOException, SAXException
parse in interface XMLReaderparse in class org.xml.sax.helpers.XMLFilterImplIOExceptionSAXExceptionpublic void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException
setProperty in interface XMLReadersetProperty in class org.xml.sax.helpers.XMLFilterImplSAXNotRecognizedExceptionSAXNotSupportedExceptionpublic Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException
getProperty in interface XMLReadergetProperty in class org.xml.sax.helpers.XMLFilterImplSAXNotRecognizedExceptionSAXNotSupportedExceptionpublic void setLexicalHandler(LexicalHandler handler)
public LexicalHandler getLexicalHandler()
public void setDocumentLocator(Locator locator)
setDocumentLocator in interface ContentHandlersetDocumentLocator in class org.xml.sax.helpers.XMLFilterImplpublic void startDocument()
throws SAXException
startDocument in interface ContentHandlerstartDocument in class org.xml.sax.helpers.XMLFilterImplSAXExceptionpublic void endDocument()
throws SAXException
endDocument in interface ContentHandlerendDocument in class org.xml.sax.helpers.XMLFilterImplSAXExceptionpublic void startPrefixMapping(String prefix, String uri) throws SAXException
startPrefixMapping in interface ContentHandlerstartPrefixMapping in class org.xml.sax.helpers.XMLFilterImplSAXExceptionpublic void endPrefixMapping(String prefix) throws SAXException
endPrefixMapping in interface ContentHandlerendPrefixMapping in class org.xml.sax.helpers.XMLFilterImplSAXExceptionpublic void startElement(String namespaceURI, String localName, String qName, Attributes attributes) throws SAXException
startElement in interface ContentHandlerstartElement in class org.xml.sax.helpers.XMLFilterImplSAXExceptionpublic void endElement(String namespaceURI, String localName, String qName) throws SAXException
endElement in interface ContentHandlerendElement in class org.xml.sax.helpers.XMLFilterImplSAXExceptionpublic void characters(char[] ch,
int start,
int length)
throws SAXException
characters in interface ContentHandlercharacters in class org.xml.sax.helpers.XMLFilterImplSAXExceptionpublic void ignorableWhitespace(char[] ch,
int start,
int length)
throws SAXException
ignorableWhitespace in interface ContentHandlerignorableWhitespace in class org.xml.sax.helpers.XMLFilterImplSAXExceptionpublic void processingInstruction(String target, String data) throws SAXException
processingInstruction in interface ContentHandlerprocessingInstruction in class org.xml.sax.helpers.XMLFilterImplSAXExceptionpublic void notationDecl(String name, String publicID, String systemID) throws SAXException
notationDecl in interface DTDHandlernotationDecl in class org.xml.sax.helpers.XMLFilterImplSAXExceptionpublic void unparsedEntityDecl(String name, String publicID, String systemID, String notationName) throws SAXException
unparsedEntityDecl in interface DTDHandlerunparsedEntityDecl in class org.xml.sax.helpers.XMLFilterImplSAXExceptionpublic void startDTD(String name, String publicID, String systemID) throws SAXException
startDTD in interface LexicalHandlerSAXExceptionpublic void endDTD()
throws SAXException
endDTD in interface LexicalHandlerSAXExceptionpublic void startCDATA()
throws SAXException
startCDATA in interface LexicalHandlerSAXExceptionpublic void endCDATA()
throws SAXException
endCDATA in interface LexicalHandlerSAXExceptionpublic void startEntity(String name) throws SAXException
startEntity in interface LexicalHandlerSAXExceptionpublic void endEntity(String name) throws SAXException
endEntity in interface LexicalHandlerSAXExceptionpublic void comment(char[] ch,
int start,
int length)
throws SAXException
comment in interface LexicalHandlerSAXExceptionprotected void writeElement(Element element) throws IOException
IOExceptionprotected final boolean isElementSpacePreserved(Element element)
element - DOCUMENT ME!protected void writeElementContent(Element element) throws IOException
element - DOCUMENT ME!IOException - DOCUMENT ME!protected void writeCDATA(String text) throws IOException
IOExceptionprotected void writeDocType(DocumentType docType) throws IOException
IOExceptionprotected void writeNamespace(Namespace namespace) throws IOException
IOExceptionprotected void writeNamespaces()
throws IOException
IOException - DOCUMENT ME!protected void writeNamespace(String prefix, String uri) throws IOException
prefix - the prefixuri - the namespace uriIOExceptionprotected void writeProcessingInstruction(ProcessingInstruction pi) throws IOException
IOExceptionprotected void writeString(String text) throws IOException
IOExceptionprotected void writeNodeText(Node node) throws IOException
node - DOCUMENT ME!IOException - DOCUMENT ME!protected void writeNode(Node node) throws IOException
IOExceptionprotected void installLexicalHandler()
protected void writeDocType(String name, String publicID, String systemID) throws IOException
IOExceptionprotected void writeEntity(Entity entity) throws IOException
IOExceptionprotected void writeEntityRef(String name) throws IOException
IOExceptionprotected void writeComment(String text) throws IOException
IOExceptionprotected void writeAttributes(Element element) throws IOException
element - DOCUMENT ME!IOException - DOCUMENT ME!protected void writeAttribute(Attribute attribute) throws IOException
IOExceptionprotected void writeAttributes(Attributes attributes) throws IOException
IOExceptionprotected void writeAttribute(Attributes attributes, int index) throws IOException
IOExceptionprotected void indent()
throws IOException
IOExceptionprotected void writePrintln()
throws IOException
This will print a new line only if the newlines flag was set to true
IOException - DOCUMENT ME!protected Writer createWriter(OutputStream outStream, String encoding) throws UnsupportedEncodingException
outStream - DOCUMENT ME!encoding - DOCUMENT ME!UnsupportedEncodingException - DOCUMENT ME!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 - DOCUMENT ME!protected void writeClose(String qualifiedName) throws IOException
IOExceptionprotected void writeEmptyElementClose(String qualifiedName) throws IOException
IOExceptionprotected boolean isExpandEmptyElements()
protected String escapeElementEntities(String text)
text - DOCUMENT ME!protected void writeEscapeAttributeEntities(String txt) throws IOException
IOExceptionprotected String escapeAttributeEntities(String text)
text - DOCUMENT ME!protected boolean shouldEncodeChar(char c)
c - DOCUMENT ME!protected int defaultMaximumAllowedCharacter()
protected boolean isNamespaceDeclaration(Namespace ns)
protected void handleException(IOException e) throws SAXException
SAXExceptionprotected OutputFormat getOutputFormat()
public boolean resolveEntityRefs()
public void setResolveEntityRefs(boolean resolve)
Copyright © 2012 JBoss by Red Hat. All Rights Reserved.