Class XMLWriterNSImpl
- 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.
- Version:
- 11 December 2011
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate 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 static final booleanSet totrueto show debug info.private static final XMLWriterNSImpl.PrefixMappingThe default namespace mapped to the empty prefix.private final List<XMLWriterNSImpl.Element>A stack of elements to close the elements automatically.The current prefix mapping.private static final XMLWriterNSImpl.ElementThe root node.private List<XMLWriterNSImpl.PrefixMapping>The list of prefix mappings to be associated with the next element.Fields inherited from class com.topologi.diffx.xml.XMLWriterBase
depth, encoding, indent, isNude, writer, writerEscape -
Constructor Summary
ConstructorsConstructorDescriptionXMLWriterNSImpl(Writer writer) Creates a new XML writer.XMLWriterNSImpl(Writer writer, boolean indent) Create a new XML writer. -
Method Summary
Modifier and TypeMethodDescriptionvoidWrites an attribute.voidWrites an attribute.voidclose()Close the writer.voidWrite an 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) Write an empty element.private StringReturns the qualified name for this element using the specified namespace URI.private voidHandles the namespace declaration and updates the prefix mappings.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) Write a start element tag correctly indented.voidopenElement(String uri, String name, boolean hasChildren) Writes a start element tag correctly indented.private XMLWriterNSImpl.ElementReturns the last element in the list.private XMLWriterNSImpl.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.voidsetPrefixMapping(String uri, String prefix) Sets a prefix mapping.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
-
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. -
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.
-
-
Constructor Details
-
XMLWriterNSImpl
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.
-
XMLWriterNSImpl
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
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.
-
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.
-
attribute
public void attribute(String uri, String name, String value) throws IOException, IllegalStateException Writes an attribute.- Specified by:
attributein interfaceXMLWriter- Parameters:
uri- The namespcae 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 namespcae 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.
-
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.
-
handleNamespaceDeclaration
Handles the namespace declaration and updates the prefix mappings.- Throws:
IOException- If thrown by the wrapped writer.
-
restorePrefixMapping
Restores the prefix mapping after closing an element.This costly operation need only to be done if the method
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
Close the writer.- Specified by:
closein interfaceXMLWriter- Throws:
IOException- If thrown by the wrapped writer.UnclosedElementException- If an element has been left open.
-