Class XMLWriterSAX
- All Implemented Interfaces:
XMLWriter
Provides methods to generate well-formed XML data easily through SAX events by wrapping a content handler.
This XML writer provides an efficient way to process XML bypassing the need to create
an XML stream. Instead, this class will wrap a ContentHandler and
invoke the SAX2 methods.
ContentHandler myContentHandler = ...; XMLWriter saxWriter = new XMLWriterSAX(myContentHandler);
This SAX event writer as the following features:
- http://xml.org/sax/features/namespaces set to
true - http://xml.org/sax/features/namespace-prefixes set to
false
Consequently, the attributes will not contain attributes used for namespace declarations (xmlns* attributes).
This implementation does not provide qualified names, and will always return "".
The ContentHandler's startDocument and endDocument methods
have to be called externally.
Note that the write methods do not necessarily correspond to the content handler
methods or at least they may not be invoked at the same time. For example, the
attribute methods will not generate any event until it is possible to
invoke the ContentHandler#startElement method.
- Version:
- 15 January 2007
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classA SAX attribute list implementation.private static final classA light object to keep track of the elementsprivate static final classLight-weight class to represent a prefix mapping. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate XMLWriterSAX.AttributesImplThe attributes attached to the current open element.private static final booleanSet totrueto show debug info.private static final XMLWriterSAX.PrefixMappingThe default namespace mapped to the empty prefix.private intLevel of the depth of the xml document currently produced.private List<XMLWriterSAX.Element>A stack of elements to close the elements automatically.private final ContentHandlerWhere the XML data goes.private booleanIndicates whether the xml should be indented or not.private char[]The default indentation spaces used.private booleanFlag to indicate that the element open tag is not finished yet.private static final char[]The new line constant.The current prefix mapping.private static final XMLWriterSAX.ElementThe root node.private List<XMLWriterSAX.PrefixMapping>The list of prefix mappings to be associated with the next element. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidWrites an attribute.voidWrites an attribute.voidWrites an attribute.voidWrites an attribute.voidclose()Closes the writer.voidWrite an end element tag.private voiddeNude()Writes the angle bracket if the element open tag is not finished.voidOpens element, inserts text node and closes.voidemptyElement(String element) Same asemptyElement(null, element);.voidemptyElement(String uri, String element) Write an empty element.voidflush()Does nothing.private StringReturns the qualified name for this element using the specified namespace URI.private voidhandleEx(SAXException ex) Handles the SAX Exception.private voidindent()Insert the correct amount of space characters depending on the depth and if theindentflag is set totrue.private voidnewLine()Generates a new line as an ignorable white space eventvoidopenElement(String name) Writes a start element tag correctly indented.voidopenElement(String name, boolean hasChildren) Writes a start element tag correctly indented.voidopenElement(String uri, String name) Write a start element tag correctly indented.voidopenElement(String uri, String name, boolean hasChildren) Writes a start element tag correctly indented.private XMLWriterSAX.ElementReturns the last element in the list.private XMLWriterSAX.ElementRemoves the last element in the list.private voidremoveIfNeeded(String prefix) Removes the mapping associated to the specified prefix.private voidRestores the prefix mapping after closing an element.voidsetIndentChars(String spaces) Sets the string to use for indentation.voidsetPrefixMapping(String uri, String prefix) Sets a prefix mapping.voidwriteCDATA(String data) Writes the given text as a CDATA section.voidwriteComment(String comment) Does nothing as SAX content handler do not handle comments.voidWrites an XML processing instruction.voidwriteText(char c) Writes the given character correctly for the encoding of this document.voidwriteText(char[] text, int off, int len) Write the given text correctly for the encoding of this document.voidWrites the string value of an object.voidWrites the given text correctly for the encoding of this document.voidwriteXML(char[] text, int off, int len) Always throw anUnsupportedOperationExceptionexception.voidAlways throw anUnsupportedOperationExceptionexception.voidxmlDecl()Does nothing.
-
Field Details
-
DEBUG
private static final boolean DEBUGSet totrueto show debug info.- See Also:
-
DEFAULT_NS
The default namespace mapped to the empty prefix. -
ROOT
The root node. -
NEW_LINE
private static final char[] NEW_LINEThe new line constant. -
handler
Where the XML data goes. -
indent
private boolean indentIndicates whether the xml should be indented or not.The default is
true(indented).The indentation is 2 white-spaces.
-
indentChars
private char[] indentCharsThe default indentation spaces used. -
depth
private transient int depthLevel of the depth of the xml document currently produced.This attribute changes depending on the state of the instance.
-
isNude
private transient boolean isNudeFlag to indicate that the element open tag is not finished yet. -
prefixMapping
The current prefix mapping. -
tempMapping
The list of prefix mappings to be associated with the next element. -
elements
A stack of elements to close the elements automatically. -
attributes
The attributes attached to the current open element.This variable can be
nulland should be set tonull, after thestartElementMethodhas been invoked.
-
-
Constructor Details
-
XMLWriterSAX
Creates a new XML writer.
- Parameters:
handler- The SAX2 content handler to use.- Throws:
NullPointerException- If the handler isnull.
-
-
Method Details
-
xmlDecl
public void xmlDecl()Does nothing. -
setIndentChars
Sets the string to use for indentation.The string must be only composed of valid spaces characters.
If the string is
nullthen the indentation is turned off.- Specified by:
setIndentCharsin interfaceXMLWriter- Parameters:
spaces- The indentation string to use.- Throws:
IllegalStateException- If the writer has already been used.IllegalArgumentException- If the indent string is not made of spaces.- See Also:
-
writeText
Writes the given text correctly for the encoding of this document.Does nothing if the text is
null.- Specified by:
writeTextin interfaceXMLWriter- Parameters:
text- The text to write- Throws:
IOException- If an I/O exception is thrown by the underlying writer.
-
writeText
Write the given text correctly for the encoding of this document.- Specified by:
writeTextin interfaceXMLWriter- Parameters:
text- The text to write.off- The offset where we should start writing the string.len- The length of the character subarray to write.- Throws:
IOException- If an I/O exception is thrown by the underlying writer.
-
writeText
Writes the given character correctly for the encoding of this document.- Specified by:
writeTextin interfaceXMLWriter- Parameters:
c- The character to write.- Throws:
IOException- If an I/O exception is thrown by the underlying writer.
-
writeText
Writes the string value of an object.Does nothing if the object is
null.- Parameters:
o- The object that should be written as text.- Throws:
IOException- If thrown by the wrapped writer.- See Also:
-
writeCDATA
Description copied from interface:XMLWriterWrites the given text as a CDATA section.Does nothing if the text is
null.- Specified by:
writeCDATAin interfaceXMLWriter- Parameters:
data- The data to write inside the CDATA section.- Throws:
IOException- If an I/O exception is thrown by the underlying writer.
-
writeXML
Always throw anUnsupportedOperationExceptionexception. Writes the given XML data.The text is appended as is, therefore it should be escaped properly for the encoding used by the underlying stream writer.
Does nothing if the text is
null.- Specified by:
writeXMLin interfaceXMLWriter- Parameters:
text- The text to write.- Throws:
UnsupportedOperationException
-
writeXML
Always throw anUnsupportedOperationExceptionexception. Write the given XML data.The text is appended as is, therefore it should be escaped properly for the encoding used by the underlying stream writer.
- Specified by:
writeXMLin interfaceXMLWriter- Parameters:
text- The text to write.off- The offset where we should start writing the string.len- The length of the character subarray to write.- Throws:
UnsupportedOperationException
-
writeComment
Does nothing as SAX content handler do not handle comments. Writes an XML comment.An XML comment is:
<!-- comment -->
Comments are not indented.
Does not write anything if the comment if
null.- Specified by:
writeCommentin interfaceXMLWriter- Parameters:
comment- The comment to be written
-
writePI
Writes an XML processing instruction.An XML processing intruction is:
<?target data?>
- Specified by:
writePIin interfaceXMLWriter- Parameters:
target- The PI's target.data- The PI's data.- Throws:
IOException- If an I/O exception occurs.
-
attribute
Writes an attribute.- Specified by:
attributein interfaceXMLWriter- Parameters:
name- The name of the attribute.value- The value of the attribute.- Throws:
IOException- If thrown by the wrapped writer.
-
attribute
Writes an attribute.This method for number does not require escaping.
- Specified by:
attributein interfaceXMLWriter- Parameters:
name- The name of the attribute.value- The value of the attribute.- Throws:
IOException- If thrown by the wrapped writer.
-
attribute
Writes an attribute.- Specified by:
attributein interfaceXMLWriter- Parameters:
uri- The namespace URI this attribute belongs to.name- The name of the attribute.value- The value of the attribute.- Throws:
IOException- If thrown by the wrapped writer.IllegalStateException- If there is no open element or text has been written.
-
attribute
Writes an attribute.This method for number does not require escaping.
- Specified by:
attributein interfaceXMLWriter- Parameters:
uri- The namespace URI this attribute belongs to.name- The name of the attribute.value- The value of the attribute.- Throws:
IOException- If thrown by the wrapped writer.IllegalStateException- If there is no open element or text has been written.
-
deNude
Writes the angle bracket if the element open tag is not finished.- Throws:
SAXException- If thrown by the content handler
-
openElement
Writes a start element tag correctly indented.It is the same as
openElement("", name, false)- Specified by:
openElementin interfaceXMLWriter- Parameters:
name- the name of the element- Throws:
IOException- If thrown by the wrapped writer.- See Also:
-
openElement
Write a start element tag correctly indented.It is the same as
openElement(name, false)- Parameters:
uri- The namespace URI of this element.name- The name of the element.- Throws:
IOException- If thrown by the wrapped writer.- See Also:
-
openElement
Writes a start element tag correctly indented.Use the
hasChildrenparameter to specify whether this element is terminal node or not, note: this affects the indenting. To produce correctly indented XML, you should use the same value for this flag when closing the element.The name can contain attributes and should be a valid xml name.
- Specified by:
openElementin interfaceXMLWriter- Parameters:
name- The name of the element.hasChildren-trueif this element has children.- Throws:
IOException- If thrown by the wrapped writer.
-
openElement
Writes a start element tag correctly indented.Use the
hasChildrenparameter to specify whether this element is terminal node or not, note: this affects the indenting. To produce correctly indented XML, you should use the same value for this flag when closing the element.The name can contain attributes and should be a valid xml name.
- Specified by:
openElementin interfaceXMLWriter- Parameters:
uri- The namespace URI of this element.name- The name of the element.hasChildren- true if this element has children.- Throws:
IOException- If thrown by the wrapped writer.
-
element
Opens element, inserts text node and closes.This method should behave like:
this.openElement(name, false); this.writeText(text); this.closeElement();
- Specified by:
elementin interfaceXMLWriter- Parameters:
name- The name of the element.text- The text of the element.- Throws:
IOException- If thrown by the wrapped writer.
-
flush
public void flush()Does nothing. -
indent
Insert the correct amount of space characters depending on the depth and if theindentflag is set totrue.- Throws:
SAXException- If thrown by the SAX handler.
-
closeElement
Write an end element tag.- Specified by:
closeElementin interfaceXMLWriter- Throws:
IOException- If thrown by the wrapped writer.
-
emptyElement
Same asemptyElement(null, element);.- Specified by:
emptyElementin interfaceXMLWriter- Parameters:
element- the name of the element- Throws:
IOException- If thrown by the wrapped writer.
-
emptyElement
Write an empty element.It is possible for the element to contain attributes, however, since there is no character escaping, great care must be taken not to introduce invalid characters. For example:
<example test="yes"/>- Specified by:
emptyElementin interfaceXMLWriter- Parameters:
uri- The namespace URI for this element.element- The name of the element.- Throws:
IOException- If thrown by the wrapped writer.
-
peekElement
Returns the last element in the list.- Returns:
- The current element.
-
popElement
Removes the last element in the list.- Returns:
- The current element.
-
setPrefixMapping
Description copied from interface:XMLWriterSets a prefix mapping.- Specified by:
setPrefixMappingin interfaceXMLWriter- Parameters:
uri- The full namespace URI.prefix- The prefix for the namespace uri.- Throws:
NullPointerException- if the prefix isnull.- See Also:
-
getQName
Returns the qualified name for this element using the specified namespace URI.- Parameters:
uri- The namespace URI for the element.name- The name of the element or attribute.- Returns:
- The qualified element name.
- Throws:
UndeclaredNamespaceException- If the uri has not being previously declared.
-
restorePrefixMapping
Restores the prefix mapping after closing an element.This costly operation need only to be done if the method
XMLWriterNSImpl.setPrefixMapping(String, String)have been used immediately before, therefore it should not happen often.- Parameters:
elt- The element that had some new mappings.
-
removeIfNeeded
Removes the mapping associated to the specified prefix.- Parameters:
prefix- The prefix which mapping should be removed.
-
close
Closes the writer.This method only checks that it is possible to close the writer.
- Specified by:
closein interfaceXMLWriter- Throws:
IOException- If thrown by the wrapped writer.UnclosedElementException- If an element has been left open.
-
newLine
Generates a new line as an ignorable white space event- Throws:
SAXException- If thrown by the handler.
-
handleEx
Handles the SAX Exception.- Parameters:
ex- A SAXException thrown by the content handler.- Throws:
IOException- If thrown by the handler.
-