|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectcom.topologi.diffx.xml.XMLWriterBase
com.topologi.diffx.xml.NSAwareXMLWriter
A Namespace-aware writer for XML data.
Provides methods to generate well-formed XML data easily. wrapping a writer.
This version only supports utf-8 encoding, if writing to a file make sure that the encoding of the file output stream is "utf-8".
The recommended implementation is to use a BufferedWriter to write.
Writer writer =
new BufferedWriter(new OutputStreamWriter(new FileOutputStream("foo.out"),"utf-8"));
This class is not synchronised.
| Constructor Summary | |
NSAwareXMLWriter(Writer writer)
Creates a new XML writer. |
|
NSAwareXMLWriter(Writer writer,
boolean indent)
Create 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()
Close 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()
Flush the writer. |
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 |
setPrefixMapping(String uri,
String prefix)
Sets a prefix mapping. |
void |
write(char c)
Write the given character directly. |
void |
write(String s)
Write the given string directly. |
void |
writeChar(char c)
Write the given character correctly. |
void |
writeComment(String comment)
Write an XML comment. |
void |
writePI(String target,
String data)
Writes an XML processing instruction. |
void |
writeText(char c)
Writes the given text correctly for the encoding of this document. |
void |
writeText(char[] text,
int off,
int len)
Writes the given text correctly for the encoding of this document. |
void |
writeText(Object o)
Write 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)
Write the given XML data. |
void |
writeXML(String text)
Writes the given XML data. |
void |
xmlDecl()
Write the XML declaration. |
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Methods inherited from interface com.topologi.diffx.xml.XMLWriter |
close, element, flush, xmlDecl |
| Constructor Detail |
public NSAwareXMLWriter(Writer writer)
throws NullPointerException
Creates a new XML writer.
Sets the depth attribute to 0 and the indentation to true.
writer - Where this writer should write the XML data.
NullPointerException - If the writer is null.
public NSAwareXMLWriter(Writer writer,
boolean indent)
throws NullPointerException
Create a new XML writer.
writer - Where this writer should write the XML data.indent - Set the indentation flag.
NullPointerException - If the writer is null.| Method Detail |
public void writeComment(String comment)
throws IOException
An XML comment is:
<!-- comment -->
Comments are not indented.
writeComment in interface XMLWritercomment - the comment to be written
IOException - If thrown by the wrapped writer.
public void writePI(String target,
String data)
throws IOException
An XML processing intruction is:
<?target data?>
writePI in interface XMLWritertarget - The PI's target.data - The PI's data.
IOException - If an I/O exception is occurs.
public void writeText(char c)
throws IOException
XMLWriterDoes nothing if the text is null.
writeText in interface XMLWriterc - The character to write.
IOException - If an I/O exception occurs.XMLWriter.writeText(char)
public void writeText(String text)
throws IOException
XMLWriterThis method turns the string into an array of chars.
Does nothing if the text is null.
writeText in interface XMLWritertext - The text to write
IOException - If thrown by the wrapped writer.XMLWriter.writeText(String)
public void writeText(char[] text,
int off,
int len)
throws IOException
writeText in interface XMLWritertext - The text to writeoff - The offsetlen - The length to be written
IOException - If thrown by the wrapped writer.XMLWriter.writeText(char[], int, int)
public void writeXML(String text)
throws IOException
XMLWriterThe 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 XMLWriterIOExceptionXMLWriter.writeXML(java.lang.String)
public void writeXML(char[] text,
int off,
int len)
throws IOException
XMLWriterThe text is appended as is, therefore it should be escaped properly for the encoding used by the underlying stream writer.
writeXML in interface XMLWriterIOExceptionXMLWriter.writeXML(char[], int, int)
public void openElement(String name)
throws IOException
It is the same as openElement(null, name, false)
openElement in interface XMLWritername - 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 XMLWritername - 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 XMLWriteruri - 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 closeElement()
throws IOException
closeElement in interface XMLWriterIOException - If thrown by the wrapped writer.
public void emptyElement(String element)
throws IOException
emptyElement(null, element);
emptyElement in interface XMLWriterelement - 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 XMLWriteruri - The namespace URI for this element.element - The name of the element.
IOException - If thrown by the wrapped writer.
public void attribute(String name,
String value)
throws IOException,
IllegalStateException
attribute in interface XMLWritername - 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 name,
int value)
throws IOException,
IllegalStateException
This method for number does not require escaping.
attribute in interface XMLWritername - 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,
String value)
throws IOException,
IllegalStateException
attribute in interface XMLWriteruri - The namespcae 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,
IllegalStateException
This method for number does not require escaping.
attribute in interface XMLWriteruri - The namespcae 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 setPrefixMapping(String uri,
String prefix)
throws NullPointerException
XMLWriter
setPrefixMapping in interface XMLWriteruri - 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 final void xmlDecl()
throws IOException
Always:
<?xml version="1.0" encoding="encoding"?>followed by a new line character.
xmlDecl in interface XMLWriterIOException - If thrown by the wrapped writer.
public void writeChar(char c)
throws IOException
c - the character to write
IOException - If thrown by the wrapped writer.
public final void writeText(Object o)
throws IOException
Do nothing if the object is null.
o - The object that should be written as text.
IOException - If thrown by the wrapped writer.Object.toString(),
XMLWriterBase.writeText(java.lang.String)
public void element(String name,
String text)
throws IOException
The same as:
openElement(name); writeText(text); closeElement();
element in interface XMLWritername - The name of the elementtext - The text of the element
IOException - If thrown by the wrapped writer.
public final void write(String s)
throws IOException
s - the string to write
IOException - If thrown by the wrapped writer.
public final void write(char c)
throws IOException
c - the character to write
IOException - If thrown by the wrapped writer.
public final void flush()
throws IOException
flush in interface XMLWriterIOException - If thrown by the wrapped writer.
public final void close()
throws IOException
close in interface XMLWriterIOException - If thrown by the wrapped writer.
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||