|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.topologi.diffx.xml.dom.DOMWriterImpl
public final class DOMWriterImpl
A simple implementation of a DOM writer
Provides methods to generate well-formed XML data easily via DOM.
Constructor Summary | |
---|---|
DOMWriterImpl()
Creates a new XML writer for DOM using the default implementation on the system. |
|
DOMWriterImpl(Document document)
Creates a new XML writer for DOM. |
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)
Not supported. |
void |
attribute(String uri,
String name,
String value)
Not supported. |
void |
close()
Does nothing. |
void |
closeElement()
Close the element automatically. |
void |
element(String name,
String text)
Opens element, inserts text node and closes. |
void |
emptyElement(String name)
Writes an empty element. |
void |
emptyElement(String uri,
String element)
Not supported. |
void |
flush()
Normalises the current element. |
Document |
getDocument()
Returns the DOM document produced by the XML 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 |
setIndentChars(String spaces)
Sets the string to use for indentation. |
void |
setPrefixMapping(String uri,
String prefix)
Not supported. |
void |
writeCDATA(String data)
Writes the CDATA section to the DOM. |
void |
writeComment(String comment)
Writes an XML comment. |
void |
writePI(String target,
String data)
Writes an XML processing instruction. |
void |
writeText(char c)
This method is expensive as the character has to be converted to a String for DOM. |
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()
Does nothing. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public DOMWriterImpl() throws ParserConfigurationException
Creates a new XML writer for DOM using the default implementation on the system.
Attempts to create the DOM document using:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance() DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument();
ParserConfigurationException
- If thrown by the document builder factory.public DOMWriterImpl(Document document)
Creates a new XML writer for DOM.
document
- The DOM provided.
NullPointerException
- If the handler is null
.Method Detail |
---|
public void xmlDecl()
xmlDecl
in interface XMLWriter
public void setIndentChars(String spaces)
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.Character.isSpaceChar(char)
public void writeText(String text)
Does nothing if the text is null
.
writeText
in interface XMLWriter
text
- The text to write
DOMException
- If thrown by method invoked on the underlying DOM documentpublic void writeText(char[] text, int off, int len)
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.
DOMException
- If thrown by method invoked on the underlying DOM documentpublic void writeText(char c)
writeText
in interface XMLWriter
c
- The character to write.
DOMException
- If thrown by method invoked on the underlying DOM documentpublic void writeText(Object o)
Does nothing if the object is null
.
o
- The object that should be written as text.
DOMException
- If thrown by method invoked on the underlying DOM documentObject.toString()
public void writeCDATA(String data)
Does nothing if the object is null
.
writeCDATA
in interface XMLWriter
data
- The data to write to the section.
DOMException
- If thrown by method invoked on the underlying DOM documentpublic void writeXML(String text)
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
- XML cannot be written to the DOMpublic void writeXML(char[] text, int off, int len) throws UnsupportedOperationException
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
- XML cannot be written to the DOMpublic void writeComment(String comment) throws DOMException
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 written
DOMException
- If thrown by method invoked on the underlying DOM documentpublic void writePI(String target, String data) throws DOMException
An XML processing intruction is:
<?target data?>
writePI
in interface XMLWriter
target
- The PI's target.data
- The PI's data.
DOMException
- If thrown by method invoked on the underlying DOM documentpublic void attribute(String name, String value) throws DOMException
attribute
in interface XMLWriter
name
- The name of the attribute.value
- The value of the attribute.
DOMException
- If thrown by method invoked on the underlying DOM documentpublic void attribute(String name, int value) throws DOMException
This method for number does not require escaping.
attribute
in interface XMLWriter
name
- The name of the attribute.value
- The value of the attribute.
DOMException
- If thrown by method invoked on the underlying DOM documentpublic void openElement(String name) throws DOMException
It is the same as openElement("", name, false)
openElement
in interface XMLWriter
name
- the name of the element
DOMException
- If thrown by method invoked on the underlying DOM documentopenElement(java.lang.String, java.lang.String, boolean)
public void openElement(String name, boolean hasChildren) throws DOMException
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.
DOMException
- If thrown by method invoked on the underlying DOM documentpublic void element(String name, String text) throws DOMException
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.
DOMException
- If thrown by method invoked on the underlying DOM documentpublic void closeElement() throws DOMException, IllegalCloseElementException
The element is closed symmetrically to the
XMLWriter.openElement(String, String, boolean)
method if the XML writer is namespace
aware or the XMLWriter.openElement(String, boolean)
method.
closeElement
in interface XMLWriter
DOMException
- If thrown by method invoked on the underlying DOM document
IllegalCloseElementException
public void emptyElement(String name) throws DOMException
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
name
- the name of the element
DOMException
- If thrown by method invoked on the underlying DOM documentpublic void close()
close
in interface XMLWriter
public void flush()
flush
in interface XMLWriter
public Document getDocument()
getDocument
in interface DOMWriter
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.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |