Class XMLWriterBase
- All Implemented Interfaces:
XMLWriter
- Direct Known Subclasses:
XMLWriterImpl,XMLWriterNSImpl
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"));
- Version:
- 17 May 2005
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) intLevel of the depth of the xml document currently produced.(package private) StringEncoding of the output xml.(package private) booleanIndicates whether the xml should be indented or not.private StringThe default indentation spaces used.(package private) booleanFlag to indicate that the element open tag is not finished yet.(package private) final WriterWhere the XML data goes.(package private) XMLEscapeWriterEncoding of the output xml. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidWrites an attribute.final voidWrites an attribute.(package private) abstract voiddeNude()Writes the end of the open element tag.(package private) static final voidDoes nothing.voidOpens element, inserts text node and closes.final voidflush()Flush the writer.(package private) voidindent()Insert the correct amount of space characterss depending on the depth and if theindentflag is set totrue.final voidsetEncoding(String encoding) Sets the encoding to use.final voidsetIndentChars(String spaces) Sets the string to use for indentation.final voidwriteCDATA(String data) Writes the given text as a CDATA section.final voidwriteComment(String comment) Writes an XML comment.final voidWrites an XML processing instruction.final voidwriteText(char c) Writes the given character correctly for the encoding of this document.final voidwriteText(char[] text, int off, int len) Write the given text correctly for the encoding of this document.final voidWrites the string value of an object.final voidWrites the given text correctly for the encoding of this document.final voidwriteXML(char[] text, int off, int len) Write the given XML data.final voidWrites the given XML data.final voidxmlDecl()Writes the XML declaration.Methods 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, close, closeElement, emptyElement, emptyElement, openElement, openElement, openElement, setPrefixMapping
-
Field Details
-
writer
Where the XML data goes. -
encoding
String encodingEncoding of the output xml. -
writerEscape
XMLEscapeWriter writerEscapeEncoding of the output xml. -
depth
int depthLevel of the depth of the xml document currently produced.This attribute changes depending on the state of the instance.
-
indent
boolean indentIndicates whether the xml should be indented or not.The default is
true(indented).The indentation is 2 white-spaces.
-
indentChars
The default indentation spaces used. -
isNude
boolean isNudeFlag to indicate that the element open tag is not finished yet.
-
-
Constructor Details
-
XMLWriterBase
Creates 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
-
xmlDecl
Writes the XML declaration.Always:
<?xml version="1.0" encoding="encoding"?>
It is followed by a new line character if the indentation is turned on.
- Specified by:
xmlDeclin interfaceXMLWriter- Throws:
IOException- If an I/O exception is thrown by the underlying writer.
-
setIndentChars
public final void setIndentChars(String spaces) throws IllegalStateException, IllegalArgumentException Sets the string to use for indentation.The string must be only composed of valid spaces characters.
If the string is
nullthen the indentation is turned off.- Specified by:
setIndentCharsin interfaceXMLWriter- Parameters:
spaces- The indentation string to use.- Throws:
IllegalStateException- If the writer has already been used.IllegalArgumentException- If the indent string is not made of spaces.- See Also:
-
setEncoding
public final void setEncoding(String encoding) throws IllegalStateException, IllegalArgumentException Sets the encoding to use.The encoding must match the encoding used if there is an underlying
OutputStreamWriter.- Parameters:
encoding- The encoding to use.- Throws:
IllegalArgumentException- If the encoding is not valid.IllegalStateException- If the writer has already been used.
-
writeText
Writes the given text correctly for the encoding of this document.Does nothing if the text is
null.- Specified by:
writeTextin interfaceXMLWriter- Parameters:
text- The text to write- Throws:
IOException- If an I/O exception is thrown by the underlying writer.
-
writeText
Write the given text correctly for the encoding of this document.- Specified by:
writeTextin interfaceXMLWriter- Parameters:
text- The text to write.off- The offset where we should start writing the string.len- The length of the character subarray to write.- Throws:
IOException- If an I/O exception is thrown by the underlying writer.
-
writeText
Writes the given character correctly for the encoding of this document.- Specified by:
writeTextin interfaceXMLWriter- Parameters:
c- The character to write.- Throws:
IOException- If an I/O exception is thrown by the underlying writer.
-
writeText
Writes the string value of an object.Does nothing if the object is
null.- Parameters:
o- The object that should be written as text.- Throws:
IOException- If thrown by the wrapped writer.- See Also:
-
writeXML
Writes the given XML data.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.- Specified by:
writeXMLin interfaceXMLWriter- Parameters:
text- The text to write.- Throws:
IOException- If an I/O exception is thrown by the underlying writer.
-
writeXML
Write the given XML data.The text is appended as is, therefore it should be escaped properly for the encoding used by the underlying stream writer.
- Specified by:
writeXMLin interfaceXMLWriter- Parameters:
text- The text to write.off- The offset where we should start writing the string.len- The length of the character subarray to write.- Throws:
IOException- If an I/O exception is thrown by the underlying writer.
-
writeComment
Writes an XML comment.An XML comment is:
<!-- comment -->
Comments are not indented.
Does not write anything if the comment if
null.- Specified by:
writeCommentin interfaceXMLWriter- Parameters:
comment- The comment to be written- Throws:
IOException- If thrown by the wrapped writer.IllegalArgumentException- If the comment contains "--".
-
writePI
Writes an XML processing instruction.An XML processing intruction is:
<?target data?>
- Specified by:
writePIin interfaceXMLWriter- Parameters:
target- The PI's target.data- The PI's data.- Throws:
IOException- If an I/O exception occurs.
-
writeCDATA
Writes the given text as a CDATA section.Does nothing if the text is
null.- Specified by:
writeCDATAin interfaceXMLWriter- Parameters:
data- The data to write inside the CDATA section.- Throws:
IOException- If an I/O exception is thrown by the underlying writer.
-
attribute
Writes an attribute.- Specified by:
attributein interfaceXMLWriter- Parameters:
name- The name of the attribute.value- The value of the attribute.- Throws:
IOException- If thrown by the wrapped writer.
-
attribute
Writes an attribute.This method for number does not require escaping.
- Specified by:
attributein interfaceXMLWriter- Parameters:
name- The name of the attribute.value- The value of the attribute.- Throws:
IOException- If thrown by the wrapped writer.
-
element
Opens element, inserts text node and closes.This method should behave like:
this.openElement(name, false); this.writeText(text); this.closeElement();
- Specified by:
elementin interfaceXMLWriter- Parameters:
name- The name of the element.text- The text of the element.- Throws:
IOException- If thrown by the wrapped writer.
-
flush
Flush the writer.- Specified by:
flushin interfaceXMLWriter- Throws:
IOException- If thrown by the wrapped writer.
-
deNude
Writes the end of the open element tag.After this method is invoked it is not possible to write attributes for an element.
- Throws:
IOException- If thrown by the wrapped writer.
-
indent
Insert the correct amount of space characterss depending on the depth and if theindentflag is set totrue.- Throws:
IOException- If thrown by the wrapped writer.
-
doNothing
static final void doNothing()Does nothing.This method exists so that we can explicitly say that we should do nothing in certain conditions.
-