|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.topologi.diffx.xml.sax.XMLWriterSAX
public final class XMLWriterSAX
An XML writer that generates SAX2 events.
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:
true
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.
Constructor Summary | |
---|---|
XMLWriterSAX(ContentHandler handler)
Creates a new XML writer. |
Method Summary | |
---|---|
void |
attribute(String name,
int value)
Writes an attribute. |
void |
attribute(String name,
String value)
Writes an attribute. |
void |
attribute(String uri,
String name,
int value)
Writes an attribute. |
void |
attribute(String uri,
String name,
String value)
Writes an attribute. |
void |
close()
Closes the writer. |
void |
closeElement()
Write an end element tag. |
void |
element(String name,
String text)
Opens element, inserts text node and closes. |
void |
emptyElement(String element)
Same as emptyElement(null, element); . |
void |
emptyElement(String uri,
String element)
Write an empty element. |
void |
flush()
Does nothing. |
void |
openElement(String name)
Writes a start element tag correctly indented. |
void |
openElement(String name,
boolean hasChildren)
Writes a start element tag correctly indented. |
void |
openElement(String uri,
String name)
Write a start element tag correctly indented. |
void |
openElement(String uri,
String name,
boolean hasChildren)
Writes a start element tag correctly indented. |
void |
setIndentChars(String spaces)
Sets the string to use for indentation. |
void |
setPrefixMapping(String uri,
String prefix)
Sets a prefix mapping. |
void |
writeCDATA(String data)
Writes the given text as a CDATA section. |
void |
writeComment(String comment)
Does nothing as SAX content handler do not handle comments. |
void |
writePI(String target,
String data)
Writes an XML processing instruction. |
void |
writeText(char c)
Writes the given character correctly for the encoding of this document. |
void |
writeText(char[] text,
int off,
int len)
Write the given text correctly for the encoding of this document. |
void |
writeText(Object o)
Writes the string value of an object. |
void |
writeText(String text)
Writes the given text correctly for the encoding of this document. |
void |
writeXML(char[] text,
int off,
int len)
Always throw an UnsupportedOperationException exception. |
void |
writeXML(String text)
Always throw an UnsupportedOperationException exception. |
void |
xmlDecl()
Does nothing. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public XMLWriterSAX(ContentHandler handler) throws NullPointerException
Creates a new XML writer.
handler
- The SAX2 content handler to use.
NullPointerException
- If the handler is null
.Method Detail |
---|
public void xmlDecl()
xmlDecl
in interface XMLWriter
public void setIndentChars(String spaces) throws IllegalStateException, IllegalArgumentException
The string must be only composed of valid spaces characters.
If the string is null
then the indentation is turned off.
setIndentChars
in interface XMLWriter
spaces
- The indentation string to use.
IllegalStateException
- If the writer has already been used.
IllegalArgumentException
- If the indent string is not made of spaces.Character.isSpaceChar(char)
public void writeText(String text) throws IOException
Does nothing if the text is null
.
writeText
in interface XMLWriter
text
- The text to write
IOException
- If an I/O exception is thrown by the underlying writer.public void writeText(char[] text, int off, int len) throws IOException
writeText
in interface XMLWriter
text
- The text to write.off
- The offset where we should start writing the string.len
- The length of the character subarray to write.
IOException
- If an I/O exception is thrown by the underlying writer.public void writeText(char c) throws IOException
writeText
in interface XMLWriter
c
- The character to write.
IOException
- If an I/O exception is thrown by the underlying writer.public void writeText(Object o) throws IOException
Does nothing if the object is null
.
o
- The object that should be written as text.
IOException
- If thrown by the wrapped writer.Object.toString()
,
writeText(java.lang.String)
public void writeCDATA(String data) throws IOException
XMLWriter
Does nothing if the text is null
.
writeCDATA
in interface XMLWriter
data
- The data to write inside the CDATA section.
IOException
- If an I/O exception is thrown by the underlying writer.public void writeXML(String text) throws UnsupportedOperationException
UnsupportedOperationException
exception.
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
.
writeXML
in interface XMLWriter
text
- The text to write.
UnsupportedOperationException
public void writeXML(char[] text, int off, int len) throws UnsupportedOperationException
UnsupportedOperationException
exception.
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.
writeXML
in interface XMLWriter
text
- The text to write.off
- The offset where we should start writing the string.len
- The length of the character subarray to write.
UnsupportedOperationException
public void writeComment(String comment)
An XML comment is:
<!-- comment -->
Comments are not indented.
Does not write anything if the comment if null
.
writeComment
in interface XMLWriter
comment
- The comment to be writtenpublic void writePI(String target, String data) throws IOException
An XML processing intruction is:
<?target data?>
writePI
in interface XMLWriter
target
- The PI's target.data
- The PI's data.
IOException
- If an I/O exception occurs.public void attribute(String name, String value) throws IOException
attribute
in interface XMLWriter
name
- The name of the attribute.value
- The value of the attribute.
IOException
- If thrown by the wrapped writer.public void attribute(String name, int value) throws IOException
This method for number does not require escaping.
attribute
in interface XMLWriter
name
- The name of the attribute.value
- The value of the attribute.
IOException
- If thrown by the wrapped writer.public void attribute(String uri, String name, String value) throws IOException
attribute
in interface XMLWriter
uri
- The namespace URI this attribute belongs to.name
- The name of the attribute.value
- The value of the attribute.
IOException
- If thrown by the wrapped writer.
IllegalStateException
- If there is no open element or text has been written.public void attribute(String uri, String name, int value) throws IOException
This method for number does not require escaping.
attribute
in interface XMLWriter
uri
- The namespace URI this attribute belongs to.name
- The name of the attribute.value
- The value of the attribute.
IOException
- If thrown by the wrapped writer.
IllegalStateException
- If there is no open element or text has been written.public void openElement(String name) throws IOException
It is the same as openElement("", name, false)
openElement
in interface XMLWriter
name
- the name of the element
IOException
- If thrown by the wrapped writer.openElement(java.lang.String, java.lang.String, boolean)
public void openElement(String uri, String name) throws IOException
It is the same as openElement(name, false)
uri
- The namespace URI of this element.name
- The name of the element.
IOException
- If thrown by the wrapped writer.openElement(java.lang.String, boolean)
public void openElement(String name, boolean hasChildren) throws IOException
Use the hasChildren
parameter 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.
openElement
in interface XMLWriter
name
- The name of the element.hasChildren
- true
if this element has children.
IOException
- If thrown by the wrapped writer.public void openElement(String uri, String name, boolean hasChildren) throws IOException
Use the hasChildren
parameter 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.
openElement
in interface XMLWriter
uri
- The namespace URI of this element.name
- The name of the element.hasChildren
- true if this element has children.
IOException
- If thrown by the wrapped writer.public void element(String name, String text) throws IOException
This method should behave like:
this.openElement(name, false); this.writeText(text); this.closeElement();
element
in interface XMLWriter
name
- The name of the element.text
- The text of the element.
IOException
- If thrown by the wrapped writer.public void flush()
flush
in interface XMLWriter
public void closeElement() throws IOException
closeElement
in interface XMLWriter
IOException
- If thrown by the wrapped writer.public void emptyElement(String element) throws IOException
emptyElement(null, element);
.
emptyElement
in interface XMLWriter
element
- the name of the element
IOException
- If thrown by the wrapped writer.public void emptyElement(String uri, String element) throws IOException
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"/>
emptyElement
in interface XMLWriter
uri
- The namespace URI for this element.element
- The name of the element.
IOException
- If thrown by the wrapped writer.public void setPrefixMapping(String uri, String prefix) throws NullPointerException
XMLWriter
setPrefixMapping
in interface XMLWriter
uri
- The full namespace URI.prefix
- The prefix for the namespace uri.
NullPointerException
- if the prefix is null
.This implementation does not keep a history of the prefix mappings so it needs to be
reset. If a prefix is already being used it is overridden.
public void close() throws IOException, UnclosedElementException
This method only checks that it is possible to close the writer.
close
in interface XMLWriter
IOException
- If thrown by the wrapped writer.
UnclosedElementException
- If an element has been left open.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |