Class XMLWriterImpl
- All Implemented Interfaces:
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.
- Version:
- 6 December 2008
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classA light object to keep track of the element. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List<XMLWriterImpl.Element>A stack of elements to close the elements automatically.private static final XMLWriterImpl.ElementThe root node.Fields inherited from class com.topologi.diffx.xml.XMLWriterBase
depth, encoding, indent, isNude, writer, writerEscape -
Constructor Summary
ConstructorsConstructorDescriptionXMLWriterImpl(Writer writer) Creates a new XML writer.XMLWriterImpl(Writer writer, boolean indent) Create a new XML writer. -
Method Summary
Modifier and TypeMethodDescriptionvoidNot supported.voidNot supported.voidclose()Close the writer.voidWrite the end element tag.(package private) voiddeNude()Writes the angle bracket if the element open tag is not finished.voidemptyElement(String element) Same asemptyElement(null, element);.voidemptyElement(String uri, String element) Not supported.voidopenElement(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) Not supported.voidopenElement(String uri, String name, boolean hasChildren) Not supported.private XMLWriterImpl.ElementReturns the last element in the list.private XMLWriterImpl.ElementRemoves the last element in the list.voidsetPrefixMapping(String uri, String prefix) Not supported.Methods inherited from class com.topologi.diffx.xml.XMLWriterBase
attribute, attribute, doNothing, element, flush, indent, setEncoding, setIndentChars, writeCDATA, writeComment, writePI, writeText, writeText, writeText, writeText, writeXML, writeXML, xmlDeclMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.topologi.diffx.xml.XMLWriter
attribute, attribute, element, flush, setIndentChars, writeCDATA, writeComment, writePI, writeText, writeText, writeText, writeXML, writeXML, xmlDecl
-
Field Details
-
ROOT
The root node. -
elements
A stack of elements to close the elements automatically.
-
-
Constructor Details
-
XMLWriterImpl
Creates a new XML writer.
Sets the depth attribute to 0 and the indentation to
true.- Parameters:
writer- Where this writer should write the XML data.- Throws:
NullPointerException- If the writer isnull.
-
XMLWriterImpl
Create a new XML writer.
- Parameters:
writer- Where this writer should write the XML data.indent- Set the indentation flag.- Throws:
NullPointerException- If the writer isnull.
-
-
Method Details
-
deNude
Writes the angle bracket if the element open tag is not finished.- Specified by:
deNudein classXMLWriterBase- Throws:
IOException- If thrown by the wrapped writer.
-
openElement
Writes a start element tag correctly indented.It is the same as
openElement(null, name, false)- Specified by:
openElementin interfaceXMLWriter- Parameters:
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, which affects the indenting.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.
-
closeElement
Write the end element tag.- Specified by:
closeElementin interfaceXMLWriter- Throws:
IOException- If thrown by the wrapped writer.IllegalCloseElementException- If there is no element to close
-
emptyElement
Same asemptyElement(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"/>- Specified by:
emptyElementin interfaceXMLWriter- Parameters:
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.
-
openElement
Not supported.- Parameters:
uri- This parameter is ignored.name- This parameter is ignored.- Throws:
UnsupportedOperationException- This class does not handle namespaces.
-
openElement
public void openElement(String uri, String name, boolean hasChildren) throws UnsupportedOperationException Not supported.- Specified by:
openElementin interfaceXMLWriter- Parameters:
uri- This parameter is ignored.name- This parameter is ignored.hasChildren- This parameter is ignored.- Throws:
UnsupportedOperationException- This class does not handle namespaces.
-
emptyElement
Not supported.- Specified by:
emptyElementin interfaceXMLWriter- Parameters:
uri- This parameter is ignored.element- This parameter is ignored.- Throws:
UnsupportedOperationException- This class does not handle namespaces.
-
setPrefixMapping
Not supported.- Specified by:
setPrefixMappingin interfaceXMLWriter- Parameters:
uri- This parameter is ignored.prefix- This parameter is ignored.- Throws:
UnsupportedOperationException- This class does not handle namespaces.
-
attribute
Not supported.- Specified by:
attributein interfaceXMLWriter- Parameters:
uri- This parameter is ignored.name- The name of the attribute.value- The value of the attribute.- Throws:
UnsupportedOperationException- This class does not handle namespaces.
-
attribute
Not supported.- Specified by:
attributein interfaceXMLWriter- Parameters:
uri- This parameter is ignored.name- The name of the attribute.value- The value of the attribute.- Throws:
UnsupportedOperationException- This class does not handle namespaces.
-
close
Close the writer.- Specified by:
closein interfaceXMLWriter- Throws:
IOException- If thrown by the wrapped writer.UnclosedElementException- If an element has been left open.
-