public class ASTWriter extends BasicWriter
ASTWriter uses a low-level XMLStreamer to stream character data to the output stream. XMLWriter always pretty-prints the XML file afresh, using the XMLStreamer's tab setting for indentation. Any XML metadata that is not part of the AST model may be retrieved from the Metadata object that originally recorded the mapping of XML names to Java names, and may be used again to reconstruct the XML names from Java. This Metadata object is typically exported by an ASTWriter and imported into this ASTWriter. If no Metadata is imported, this ASTWriter will use default settings, which assume that the AST classes all come from one package. ASTWriter implements the Closeable interface, by virtue of inheriting from BasicWriter.
Modifier and Type | Field and Description |
---|---|
private Marshaller |
marshaller
The Marshaller that is used to de-construct AST object instances.
|
private Metadata |
metadata
A collection of mappings between XML and Java.
|
private java.util.Deque<java.lang.String> |
symbolStack
The stack of processed XML symbols.
|
lexicon, streamer
Constructor and Description |
---|
ASTWriter(java.io.File file)
Creates an ASTWriter writing to a file, using the default character
encoding UTF-8.
|
ASTWriter(java.io.File file,
java.lang.String encoding)
Creates an ASTWriter writing to a file, using the specified character
encoding.
|
ASTWriter(java.io.OutputStream stream,
java.lang.String encoding)
Creates an ASTWriter writing to a basic output stream, using the
specified character encoding.
|
ASTWriter(java.io.Writer writer)
Creates an ASTWriter writing to a character writer, using the default
ISO-8859-1 (Latin 1) character encoding specified for the MIME-type
text/xml.
|
ASTWriter(java.io.Writer writer,
java.lang.String encoding)
Creates an ASTWriter 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 ASTWriter.
|
Metadata |
getMetadata()
Exports the Metadata used by this ASTWriter.
|
void |
setMetadata(Metadata metadata)
Imports the supplied Metadata into this ASTWriter.
|
void |
useDomain(java.lang.String domainName)
Instructs this ASTWriter to recognise the named DNS domain.
|
void |
usePackage(java.lang.String packageName)
Instructs this ASTWriter to use the named Java package when mapping
the default XML namespace.
|
void |
usePackage(java.lang.String packageName,
java.lang.String namespace)
Instructs this ASTWriter to use the named Java package when mapping
a specific XML namespace.
|
protected void |
writeAttribute(java.lang.String nameToken,
java.lang.Object value)
Optionally writes an attribute in key-value format, if a value exists.
|
protected void |
writeBindings()
Optionally writes a Java binding processing instructions after the XML
declaration.
|
protected void |
writeBoundNamespaces()
Writes out any declarations for XML namespaces that were bound to Java
packages.
|
protected void |
writeDeclaration()
Writes the required XML declaration at the head of the output data.
|
protected void |
writeDoctype()
Optionally writes a doctype declaration before the root element.
|
void |
writeDocument(java.lang.Object syntaxTree)
Writes a Java abstract syntax tree to an XML output file or stream.
|
protected void |
writeElement(java.lang.Object object,
int indent)
Writes an AST object as an XML element.
|
protected void |
writeOriginal(java.lang.Object object,
int indent,
java.lang.String nameToken)
Writes an original AST object.
|
protected void |
writePrintingText(java.lang.Object content,
int indent)
Writes printing textual content.
|
protected void |
writeReference(java.lang.Object object)
Writes an AST object reference.
|
protected void |
writeSchemaReference()
Writes out any declarations for an XML schema reference, if one was
supplied.
|
protected void |
writeStylesheet()
Optionally writes a stylesheet processing instruction after the XML
declaration.
|
checkEncoding, close, endOfStream, flush, getEncoding, getLexicon, getLineNumber, setLexicon
encodingError, semanticError, syntaxError
private Metadata metadata
private Marshaller marshaller
private java.util.Deque<java.lang.String> symbolStack
public ASTWriter(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 ASTWriter(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 ASTWriter(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 ASTWriter(java.io.Writer writer)
writer
- a character Writer.public ASTWriter(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 Metadata getMetadata()
public void setMetadata(Metadata metadata)
metadata
- the Metadata.public void useDomain(java.lang.String domainName)
domainName
- a DNS domain name, such as "sheffield.ac.uk"public void usePackage(java.lang.String packageName)
packageName
- the Java package name.public void usePackage(java.lang.String packageName, java.lang.String namespace)
packageName
- the Java package name.namespace
- the XML namespace attribute name.public void writeDocument(java.lang.Object syntaxTree) throws SemanticError, java.io.IOException
syntaxTree
- the root of the graph of AST objects.SemanticError
- if AST unpacking conventions are broken.java.io.IOException
- if the output stream fails.java.io.UnsupportedEncodingException
- if the declared character
encoding does not match the actual encoding used by the output stream.protected void writeDeclaration() throws java.io.IOException
java.io.IOException
- if the output stream fails.protected void writeStylesheet() throws java.io.IOException
java.io.IOException
- if the output stream fails.protected void writeBindings() throws java.io.IOException
java.io.IOException
- if the output stream fails.protected void writeDoctype() throws java.io.IOException
java.io.IOException
- if the output stream fails.protected void writeAttribute(java.lang.String nameToken, java.lang.Object value) throws java.io.IOException
nameToken
- the attribute's XML name.value
- the attribute's value.java.io.IOException
- if the output stream fails.protected void writeElement(java.lang.Object object, int indent) throws java.io.IOException
object
- the AST object.indent
- the indentation level.java.io.IOException
- if any output error occurs.protected void writeOriginal(java.lang.Object object, int indent, java.lang.String nameToken) throws java.io.IOException
object
- the AST object.indent
- the indentation level.nameToken
- the XML element identifier.java.io.IOException
- if the output stream fails.protected void writeReference(java.lang.Object object) throws java.io.IOException
object
- the AST object.java.io.IOException
- if the output stream fails.protected void writeBoundNamespaces() throws java.io.IOException
java.io.IOException
- if the output stream fails.protected void writeSchemaReference() throws java.io.IOException
java.io.IOException
- if the output stream fails.protected void writePrintingText(java.lang.Object content, int indent) throws java.io.IOException
content
- the content of an AST object.indent
- the indentation level.java.io.IOException
- if the output stream fails.