public final class XMLWriterImpl extends Object implements XMLWriter
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 and does not support namespaces, and will therefore throw an unsupported operation exception for each call to a method that uses namespaces.
Constructor and Description |
---|
XMLWriterImpl(Writer writer)
Creates a new XML writer.
|
XMLWriterImpl(Writer writer,
boolean indent)
Create a new XML writer.
|
Modifier and Type | Method and Description |
---|---|
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)
Not supported.
|
void |
attribute(String uri,
String name,
String value)
Not supported.
|
void |
close()
Close the writer.
|
void |
closeElement()
Write the 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)
Not supported.
|
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)
Not supported.
|
void |
openElement(String uri,
String name,
boolean hasChildren)
Not supported.
|
void |
setEncoding(String encoding)
Sets the encoding to use.
|
void |
setIndentChars(String spaces)
Sets the string to use for indentation.
|
void |
setPrefixMapping(String uri,
String prefix)
Not supported.
|
void |
writeCDATA(String data)
Writes the given text as a CDATA section.
|
void |
writeComment(String comment)
Writes an XML comment.
|
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)
Write the given XML data.
|
void |
writeXML(String text)
Writes the given XML data.
|
void |
xmlDecl()
Writes the XML declaration.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
attribute, attribute, element, flush, setIndentChars, writeCDATA, writeComment, writePI, writeText, writeText, writeText, writeXML, writeXML, xmlDecl
public XMLWriterImpl(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 XMLWriterImpl(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
.public void openElement(String name) throws IOException
It is the same as openElement(null, name, false)
openElement
in interface XMLWriter
name
- The name of the elementIOException
- If thrown by the wrapped writer.openElement(java.lang.String, 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, which affects the indenting.
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 closeElement() throws IOException, IllegalCloseElementException
closeElement
in interface XMLWriter
IOException
- If thrown by the wrapped writer.IllegalCloseElementException
- If there is no element to closepublic void emptyElement(String element) throws IOException
emptyElement(null, 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"/>
emptyElement
in interface XMLWriter
element
- the name of the elementIOException
- If thrown by the wrapped writer.public void openElement(String uri, String name) throws UnsupportedOperationException
uri
- This parameter is ignored.name
- This parameter is ignored.UnsupportedOperationException
- This class does not handle namespaces.public void openElement(String uri, String name, boolean hasChildren) throws UnsupportedOperationException
openElement
in interface XMLWriter
uri
- This parameter is ignored.name
- This parameter is ignored.hasChildren
- This parameter is ignored.UnsupportedOperationException
- This class does not handle namespaces.public void emptyElement(String uri, String element) throws UnsupportedOperationException
emptyElement
in interface XMLWriter
uri
- This parameter is ignored.element
- This parameter is ignored.UnsupportedOperationException
- This class does not handle namespaces.public void setPrefixMapping(String uri, String prefix) throws UnsupportedOperationException
setPrefixMapping
in interface XMLWriter
uri
- This parameter is ignored.prefix
- This parameter is ignored.UnsupportedOperationException
- This class does not handle namespaces.public void attribute(String uri, String name, String value) throws UnsupportedOperationException
attribute
in interface XMLWriter
uri
- This parameter is ignored.name
- The name of the attribute.value
- The value of the attribute.UnsupportedOperationException
- This class does not handle namespaces.public void attribute(String uri, String name, int value) throws UnsupportedOperationException
attribute
in interface XMLWriter
uri
- This parameter is ignored.name
- The name of the attribute.value
- The value of the attribute.UnsupportedOperationException
- This class does not handle namespaces.public void close() throws IOException, UnclosedElementException
close
in interface XMLWriter
IOException
- If thrown by the wrapped writer.UnclosedElementException
- If an element has been left open.public final void xmlDecl() throws IOException
Always:
<?xml version="1.0" encoding="encoding"?>
It is followed by a new line character if the indentation is turned on.
xmlDecl
in interface XMLWriter
IOException
- If an I/O exception is thrown by the underlying writer.public final 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 final void setEncoding(String encoding) throws IllegalStateException, IllegalArgumentException
The encoding must match the encoding used if there is an underlying
OutputStreamWriter
.
encoding
- The encoding to use.IllegalArgumentException
- If the encoding is not valid.IllegalStateException
- If the writer has already been used.public final void writeText(String text) throws IOException
Does nothing if the text is null
.
writeText
in interface XMLWriter
text
- The text to writeIOException
- If an I/O exception is thrown by the underlying writer.public final 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 final 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 final 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 final void writeXML(String text) throws IOException
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.IOException
- If an I/O exception is thrown by the underlying writer.public final void writeXML(char[] text, int off, int len) throws IOException
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.IOException
- If an I/O exception is thrown by the underlying writer.public final void writeComment(String comment) throws IOException, IllegalArgumentException
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 writtenIOException
- If thrown by the wrapped writer.IllegalArgumentException
- If the comment contains "--".public final 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 final void writeCDATA(String data) throws IOException
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 final 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 final 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 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 final void flush() throws IOException
flush
in interface XMLWriter
IOException
- If thrown by the wrapped writer.Copyright © 2007-2014. All Rights Reserved.