Package com.stardog.stark.query.io.xml
Class XMLWriter
- java.lang.Object
-
- com.stardog.stark.query.io.xml.XMLWriter
-
public class XMLWriter extends java.lang.Object
A utility class offering convenience methods for writing XML. This class takes care of character escaping, identation, etc. This class does not verify that the written data is legal XML. It is the callers responsibility to make sure that elements are properly nested, etc.Example:
To write the following XML:
<?xml version='1.0' encoding='UTF-8'?> <xml-doc> <foo a="1" b="2&3"/> <bar>Hello World!</bar> </xml-doc>
One can use the following code:
XMLWriter xmlWriter = new XMLWriter(myWriter); xmlWriter.setPrettyPrint(true); xmlWriter.startDocument(); xmlWriter.startTag("xml-doc"); xmlWriter.setAttribute("a", 1); xmlWriter.setAttribute("b", "2&3"); xmlWriter.simpleTag("foo"); xmlWriter.textTag("bar", "Hello World!"); xmlWriter.endTag("xml-doc"); xmlWriter.endDocument();
-
-
Field Summary
Fields Modifier and Type Field Description protected int
_indentLevel
The current indentation level, i.e.
-
Constructor Summary
Constructors Constructor Description XMLWriter(java.io.OutputStream outputStream)
Creates a new XMLWriter that will write its data to the supplied OutputStream in the default UTF-8 character encoding.XMLWriter(java.io.OutputStream outputStream, java.lang.String charEncoding)
Creates a new XMLWriter that will write its data to the supplied OutputStream in specified character encoding.XMLWriter(java.io.Writer writer)
Creates a new XMLWriter that will write its data to the supplied Writer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
_write(java.lang.String s)
Writes a string.protected void
_writeIndent()
Writes as much indentation strings as appropriate for the current indentation level.protected void
_writeLn(java.lang.String s)
Writes a string followed by a line-separator.void
comment(java.lang.String comment)
Writes a comment.void
emptyElement(java.lang.String elName)
Writes an 'empty' element, e.g.void
emptyLine()
Writes an empty line.void
endDocument()
Finishes writing and flushes the OutputStream or Writer that this XMLWriter is writing to.void
endTag(java.lang.String elName)
Writes an end tag.java.lang.String
getIndentString()
Gets the string used for indentation.boolean
prettyPrintEnabled()
Checks whether pretty-printing is enabled.void
setAttribute(java.lang.String name, boolean value)
Sets an attribute for the next start element.void
setAttribute(java.lang.String name, int value)
Sets an attribute for the next start element.void
setAttribute(java.lang.String name, java.lang.String value)
Sets an attribute for the next start tag.void
setIndentString(java.lang.String indentString)
Sets the string that should be used for indentation when pretty-printing is enabled.void
setPrettyPrint(boolean prettyPrint)
Enables or disables pretty-printing.void
startDocument()
Writes the XML header for the XML file.void
startTag(java.lang.String elName)
Writes a start tag containing the previously set attributes.void
text(java.lang.String text)
Writes a piece of text.void
textElement(java.lang.String elName, boolean value)
Writes a start and end tag with the supplied boolean value between them.void
textElement(java.lang.String elName, int value)
Writes a start and end tag with the supplied value between them.void
textElement(java.lang.String elName, java.lang.String text)
Writes a start and end tag with the supplied text between them.void
unescapedTextElement(java.lang.String elName, java.lang.String text)
Writes a start and end tag with the supplied text between them, without the usual escape rules.void
writeStylesheet(java.lang.String url)
Writes a link to an XSL stylesheet, using <?xml-stylesheet type='text/xsl' href='url'?>.
-
-
-
Constructor Detail
-
XMLWriter
public XMLWriter(java.io.Writer writer)
Creates a new XMLWriter that will write its data to the supplied Writer. Character encoding issues are left to the supplier of the Writer.- Parameters:
writer
- The Writer to write the XML to.
-
XMLWriter
public XMLWriter(java.io.OutputStream outputStream)
Creates a new XMLWriter that will write its data to the supplied OutputStream in the default UTF-8 character encoding.- Parameters:
outputStream
- The OutputStream to write the XML to.
-
XMLWriter
public XMLWriter(java.io.OutputStream outputStream, java.lang.String charEncoding) throws java.io.UnsupportedEncodingException
Creates a new XMLWriter that will write its data to the supplied OutputStream in specified character encoding.- Parameters:
outputStream
- The OutputStream to write the XML to.- Throws:
java.io.UnsupportedEncodingException
-
-
Method Detail
-
setPrettyPrint
public void setPrettyPrint(boolean prettyPrint)
Enables or disables pretty-printing. If pretty-printing is enabled, the XMLWriter will add newlines and indentation to the written data. Pretty-printing is disabled by default.- Parameters:
prettyPrint
- Flag indicating whether pretty-printing should be enabled.
-
prettyPrintEnabled
public boolean prettyPrintEnabled()
Checks whether pretty-printing is enabled.- Returns:
- true if pretty-printing is enabled, false otherwise.
-
setIndentString
public void setIndentString(java.lang.String indentString)
Sets the string that should be used for indentation when pretty-printing is enabled. The default indentation string is a tab character.- Parameters:
indentString
- The indentation string, e.g. a tab or a number of spaces.
-
getIndentString
public java.lang.String getIndentString()
Gets the string used for indentation.- Returns:
- the indentation string.
-
startDocument
public void startDocument() throws java.io.IOException
Writes the XML header for the XML file.- Throws:
java.io.IOException
- If an I/O error occurs.
-
endDocument
public void endDocument() throws java.io.IOException
Finishes writing and flushes the OutputStream or Writer that this XMLWriter is writing to.- Throws:
java.io.IOException
-
setAttribute
public void setAttribute(java.lang.String name, java.lang.String value)
Sets an attribute for the next start tag.- Parameters:
name
- The name of the attribute.value
- The value of the attribute.
-
setAttribute
public void setAttribute(java.lang.String name, int value)
Sets an attribute for the next start element.- Parameters:
name
- The name of the attribute.value
- The value of the attribute. The integer value will be transformed to a string using the method String.valueOf(int).- See Also:
String.valueOf(int)
-
setAttribute
public void setAttribute(java.lang.String name, boolean value)
Sets an attribute for the next start element.- Parameters:
name
- The name of the attribute.value
- The value of the attribute. The boolean value will be transformed to a string using the method String.valueOf(boolean).- See Also:
String.valueOf(boolean)
-
startTag
public void startTag(java.lang.String elName) throws java.io.IOException
Writes a start tag containing the previously set attributes.- Parameters:
elName
- The element name.- Throws:
java.io.IOException
- See Also:
setAttribute(java.lang.String,java.lang.String)
-
endTag
public void endTag(java.lang.String elName) throws java.io.IOException
Writes an end tag.- Parameters:
elName
- The element name.- Throws:
java.io.IOException
-
emptyElement
public void emptyElement(java.lang.String elName) throws java.io.IOException
Writes an 'empty' element, e.g. <foo/>. The tag will contain any previously set attributes.- Parameters:
elName
- The element name.- Throws:
java.io.IOException
- See Also:
setAttribute(java.lang.String,java.lang.String)
-
writeStylesheet
public void writeStylesheet(java.lang.String url) throws java.io.IOException
Writes a link to an XSL stylesheet, using <?xml-stylesheet type='text/xsl' href='url'?>.- Parameters:
url
- The URL of the stylesheet.- Throws:
java.io.IOException
-
textElement
public void textElement(java.lang.String elName, java.lang.String text) throws java.io.IOException
Writes a start and end tag with the supplied text between them. The start tag will contain any previously set attributes.- Parameters:
elName
- The element name.text
- The text.- Throws:
java.io.IOException
- See Also:
setAttribute(java.lang.String,java.lang.String)
-
unescapedTextElement
public void unescapedTextElement(java.lang.String elName, java.lang.String text) throws java.io.IOException
Writes a start and end tag with the supplied text between them, without the usual escape rules. The start tag will contain any previously set attributes.- Parameters:
elName
- The element name.text
- The text.- Throws:
java.io.IOException
- See Also:
setAttribute(java.lang.String,java.lang.String)
-
textElement
public void textElement(java.lang.String elName, int value) throws java.io.IOException
Writes a start and end tag with the supplied value between them. The start tag will contain any previously set attributes.- Parameters:
elName
- The element name.value
- The value. The integer value will be transformed to a string using the method String.valueOf(int).- Throws:
java.io.IOException
- See Also:
String.valueOf(int)
-
textElement
public void textElement(java.lang.String elName, boolean value) throws java.io.IOException
Writes a start and end tag with the supplied boolean value between them. The start tag will contain any previously set attributes.- Parameters:
elName
- The element name.value
- The boolean value. The integer value will be transformed to a string using the method String.valueOf(boolean).- Throws:
java.io.IOException
- See Also:
String.valueOf(boolean)
-
text
public void text(java.lang.String text) throws java.io.IOException
Writes a piece of text.- Parameters:
text
- The text.- Throws:
java.io.IOException
-
comment
public void comment(java.lang.String comment) throws java.io.IOException
Writes a comment.- Parameters:
comment
- The comment.- Throws:
java.io.IOException
-
emptyLine
public void emptyLine() throws java.io.IOException
Writes an empty line. A call to this method will be ignored when pretty-printing is disabled.- Throws:
java.io.IOException
- See Also:
setPrettyPrint(boolean)
-
_write
protected void _write(java.lang.String s) throws java.io.IOException
Writes a string.- Throws:
java.io.IOException
-
_writeLn
protected void _writeLn(java.lang.String s) throws java.io.IOException
Writes a string followed by a line-separator. The line-separator is not written when pretty-printing is disabled.- Throws:
java.io.IOException
-
_writeIndent
protected void _writeIndent() throws java.io.IOException
Writes as much indentation strings as appropriate for the current indentation level. A call to this method is ignored when pretty-printing is disabled.- Throws:
java.io.IOException
-
-