public class XMLWriter extends BasicWriter
uk.ac.sheffield.jast.xml
.
XMLWriter uses a low-level XMLStreamer to stream character data to the output stream. By default, XMLWriter pretty-prints the XML file afresh, discarding any formatting whitespace nodes surrounding elements in the document tree. XMLWriter may be requested to preserve the actual layout of the document tree. The output will then depend on whether the tree was originally constructed preserving all whitespace, in which case the output file mirrors the input file; otherwise the output file will be compact, without any extra formatting whitespace. XMLWriter implements the Closeable interface by virtue of inheriting from BasicWriter.
Modifier and Type | Field and Description |
---|---|
private Content |
current
The current content being unpacked.
|
private boolean |
prettyPrint
Flag describing whether to pretty print the next XML content.
|
private boolean |
rawLayout
Flag describing whether to preserve the layout of the Document.
|
lexicon, streamer
Constructor and Description |
---|
XMLWriter(java.io.File file)
Creates an XMLWriter writing to a file, using the default character
encoding UTF-8.
|
XMLWriter(java.io.File file,
java.lang.String encoding)
Creates an XMLWriter writing to a file, using the specified character
encoding.
|
XMLWriter(java.io.OutputStream stream,
java.lang.String encoding)
Creates an XMLWriter writing to a basic output stream, using the
specified character encoding.
|
XMLWriter(java.io.Writer writer)
Creates an XMLWriter writing to a character writer, using the default
ISO-8859-1 (Latin 1) character encoding specified for the MIME-type
text/xml.
|
XMLWriter(java.io.Writer writer,
java.lang.String encoding)
Creates an XMLWriter writing to a character writer, using the
specified character encoding.
|
Modifier and Type | Method and Description |
---|---|
protected java.lang.String |
getContext()
Reports the current syntax tree context of this XMLWriter.
|
void |
preserveLayout(boolean value)
Instructs this XMLWriter whether to preserve, or ignore, the layout of
the Document.
|
protected void |
writeAttribute(Attribute attribute)
Writes an Attribute.
|
protected void |
writeComment(Comment comment)
Writes a Comment.
|
protected void |
writeContent(Content content,
int indent)
Writes one kind of Content.
|
protected void |
writeData(Data data)
Writes an escaped character Data node.
|
protected void |
writeDeclaration(Declaration directive)
Writes out the XML Declaration.
|
protected void |
writeDoctype(Doctype doctype)
Writes out a Doctype declaration.
|
void |
writeDocument(Document document)
Writes a Document to an XML output file or stream.
|
protected void |
writeElement(Element element,
int indent)
Writes an Element.
|
protected void |
writeInstruction(Instruction directive)
Writes a processing Instruction.
|
protected void |
writeText(Text text)
Writes a Text node.
|
checkEncoding, close, endOfStream, flush, getEncoding, getLexicon, getLineNumber, setLexicon
encodingError, semanticError, syntaxError
private Content current
private boolean prettyPrint
private boolean rawLayout
public XMLWriter(java.io.File file) throws java.io.FileNotFoundException, java.io.UnsupportedEncodingException
file
- the output text file.java.io.FileNotFoundException
- if the file cannot be found or opened.java.io.UnsupportedEncodingException
- if the character encoding is not
supported.public XMLWriter(java.io.File file, java.lang.String encoding) throws java.io.FileNotFoundException, java.io.UnsupportedEncodingException
file
- the output text file.encoding
- the name of the character encoding.java.io.FileNotFoundException
- if the file cannot be found or opened.java.io.UnsupportedEncodingException
- if the character encoding is not
supported.public XMLWriter(java.io.OutputStream stream, java.lang.String encoding) throws java.io.UnsupportedEncodingException
stream
- the basic OutputStream.encoding
- the name of the character encoding.java.io.UnsupportedEncodingException
- if the character encoding is not
supported.public XMLWriter(java.io.Writer writer)
writer
- a character Writer.public XMLWriter(java.io.Writer writer, java.lang.String encoding)
writer
- a character Writer.encoding
- the name of the character encoding.protected java.lang.String getContext()
getContext
in class BasicWriter
public void preserveLayout(boolean value)
value
- true, to write a Document with all original layout.public void writeDocument(Document document) throws java.io.UnsupportedEncodingException, SemanticError, java.io.IOException
document
- the Document comment.java.io.UnsupportedEncodingException
- if the declared encoding does not
match the output stream encoding.SemanticError
- if the Document is not well-formed.java.io.IOException
- if the output stream fails.protected void writeContent(Content content, int indent) throws SemanticError, java.io.IOException
content
- one kind of XML content.indent
- the indentation level.java.io.IOException
- if the output stream fails.SemanticError
protected void writeElement(Element element, int indent) throws java.io.IOException
element
- the Element.indent
- the indentation level.java.io.IOException
- if the output stream fails.protected void writeDeclaration(Declaration directive) throws java.io.IOException
directive
- an XML Declaration.java.io.IOException
- if the output stream fails.protected void writeInstruction(Instruction directive) throws java.io.IOException
directive
- a Declaration or an Instruction.java.io.IOException
- if the output stream fails.protected void writeDoctype(Doctype doctype) throws java.io.IOException
doctype
- the Doctype.java.io.IOException
- if the output stream fails.protected void writeComment(Comment comment) throws java.io.IOException
comment
- the Comment.java.io.IOException
- if the output stream fails.protected void writeData(Data data) throws java.io.IOException
data
- the Data.java.io.IOException
- if the output stream fails.protected void writeAttribute(Attribute attribute) throws java.io.IOException
attribute
- the Attribute.java.io.IOException
- if the output stream fails.protected void writeText(Text text) throws java.io.IOException
text
- the Text.java.io.IOException
- if the output stream fails.