|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--gnu.xml.util.XMLWriter
This class is a SAX handler which writes all its input as a well formed XML or XHTML document. If driven using SAX2 events, this output may include a recreated document type declaration, subject to limitations of SAX (no internal subset exposed) or DOM (the important declarations, with their documentation, are discarded).
By default, text is generated "as-is", but some optional modes are supported. Pretty-printing is supported, to make life easier for people reading the output. XHTML (1.0) output has can be made particularly pretty; all the built-in character entities are known. Canonical XML can also be generated, assuming the input is properly formed.
Some of the methods on this class are intended for applications to use directly, rather than as pure SAX2 event callbacks. Some of those methods access the JavaBeans properties (used to tweak output formats, for example canonicalization and pretty printing). Subclasses are expected to add new behaviors, not to modify current behavior, so many such methods are final.
The write*() methods may be slightly simpler for some applications to use than direct callbacks. For example, they support a simple policy for encoding data items as the content of a single element.
To reuse an XMLWriter you must provide it with a new Writer, since this handler closes the writer it was given as part of its endDocument() handling. (XML documents have an end of input, and the way to encode that on a stream is to close it.)
Note that any relative URIs in the source document, as found in entity and notation declarations, ought to have been fully resolved by the parser providing events to this handler. This means that the output text should only have fully resolved URIs, which may not be the desired behavior in cases where later binding is desired.
Note that due to SAX2 defaults, you may need to manually
ensure that the input events are XML-conformant with respect to namespace
prefixes and declarations. NSFilter
is
one solution to this problem, in the context of processing pipelines.
Something as simple as connecting this handler to a parser might not
generate the correct output. Another workaround is to ensure that the
namespace-prefixes feature is always set to true, if you're
hooking this directly up to some XMLReader implementation.
TextConsumer
Constructor Summary | |
XMLWriter()
Constructs this handler with System.out used to write SAX events using the UTF-8 encoding. |
|
XMLWriter(java.io.OutputStream out)
Constructs a handler which writes all input to the output stream in the UTF-8 encoding, and closes it when endDocument is called. |
|
XMLWriter(java.io.Writer writer)
Constructs a handler which writes all input to the writer, and then closes the writer when the document ends. |
|
XMLWriter(java.io.Writer writer,
java.lang.String encoding)
Constructs a handler which writes all input to the writer, and then closes the writer when the document ends. |
Method Summary | |
void |
attributeDecl(java.lang.String element,
java.lang.String name,
java.lang.String type,
java.lang.String defaultType,
java.lang.String defaultValue)
SAX2: called on attribute declarations |
void |
characters(char[] buf,
int off,
int len)
SAX1: reports content characters |
void |
comment(char[] buf,
int off,
int len)
SAX2: called when comments are parsed. |
void |
elementDecl(java.lang.String name,
java.lang.String model)
SAX2: called on element declarations |
void |
endCDATA()
SAX2: called after parsing CDATA characters |
void |
endDocument()
SAX1: indicates the completion of a parse |
void |
endDTD()
SAX2: called after the doctype is parsed |
void |
endElement(java.lang.String uri,
java.lang.String local,
java.lang.String name)
SAX2: indicates the end of an element |
void |
endEntity(java.lang.String name)
SAX2: called after parsing a general entity in content |
void |
endPrefixMapping(java.lang.String prefix)
SAX2: ignored. |
void |
externalEntityDecl(java.lang.String name,
java.lang.String pubid,
java.lang.String sysid)
SAX2: called on external entity declarations |
protected void |
fatal(java.lang.String message,
java.lang.Exception e)
Used internally and by subclasses, this encapsulates the logic involved in reporting fatal errors. |
void |
flush()
Flushes the output stream. |
void |
ignorableWhitespace(char[] buf,
int off,
int len)
SAX1: reports ignorable whitespace |
void |
internalEntityDecl(java.lang.String name,
java.lang.String value)
SAX2: called on internal entity declarations |
boolean |
isCanonical()
Returns value of flag controlling canonical output. |
boolean |
isExpandingEntities()
Returns true if the output will have no entity references; returns false (the default) otherwise. |
boolean |
isPrettyPrinting()
Returns value of flag controlling pretty printing. |
boolean |
isXhtml()
Returns true if the output attempts to echo the input following "transitional" XHTML rules and matching the "HTML Compatibility Guidelines" so that an HTML version 3 browser can read the output as HTML; returns false (the default) othewise. |
void |
notationDecl(java.lang.String name,
java.lang.String pubid,
java.lang.String sysid)
SAX1: called on notation declarations |
void |
processingInstruction(java.lang.String name,
java.lang.String value)
SAX1: reports a PI |
void |
setCanonical(boolean value)
Sets the output style to be canonicalized. |
void |
setDocumentLocator(Locator l)
SAX1: provides parser status information |
void |
setEOL(java.lang.String eolString)
Assigns the line ending style to be used on output. |
void |
setErrorHandler(ErrorHandler handler)
Assigns the error handler to be used to present most fatal errors. |
void |
setExpandingEntities(boolean value)
Controls whether the output text contains references to entities (the default), or instead contains the expanded values of those entities. |
void |
setPrettyPrinting(boolean value)
Controls pretty-printing, which by default is not enabled (and currently is most useful for XHTML output). |
void |
setWriter(java.io.Writer writer,
java.lang.String encoding)
Resets the handler to write a new text document. |
void |
setXhtml(boolean value)
Controls whether the output should attempt to follow the "transitional" XHTML rules so that it meets the "HTML Compatibility Guidelines" appendix in the XHTML specification. |
void |
skippedEntity(java.lang.String name)
SAX1: indicates a non-expanded entity reference |
void |
startCDATA()
SAX2: called before parsing CDATA characters |
void |
startDocument()
SAX1: indicates the beginning of a document parse. |
void |
startDTD(java.lang.String root,
java.lang.String pubid,
java.lang.String sysid)
SAX2: called when the doctype is partially parsed Note that this, like other doctype related calls, is ignored when XHTML is in use. |
void |
startElement(java.lang.String uri,
java.lang.String local,
java.lang.String name,
Attributes atts)
SAX2: indicates the start of an element. |
void |
startEntity(java.lang.String name)
SAX2: called before parsing a general entity in content |
void |
startPrefixMapping(java.lang.String prefix,
java.lang.String uri)
SAX2: ignored. |
void |
unparsedEntityDecl(java.lang.String name,
java.lang.String pubid,
java.lang.String sysid,
java.lang.String notation)
SAX1: called on unparsed entity declarations |
void |
write(java.lang.String data)
Writes the string as if characters() had been called on the contents of the string. |
void |
writeElement(java.lang.String uri,
java.lang.String local,
java.lang.String name,
Attributes atts,
int content)
Writes an element that has content consisting of a single integer, encoded as a decimal string. |
void |
writeElement(java.lang.String uri,
java.lang.String local,
java.lang.String name,
Attributes atts,
java.lang.String content)
Writes an element that has content consisting of a single string. |
void |
writeEmptyElement(java.lang.String uri,
java.lang.String local,
java.lang.String name,
Attributes atts)
Writes an empty element. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public XMLWriter() throws java.io.IOException
public XMLWriter(java.io.OutputStream out) throws java.io.IOException
public XMLWriter(java.io.Writer writer)
See the description of the constructor which takes an encoding name for imporant information about selection of encodings.
writer
- XML text is written to this writer.public XMLWriter(java.io.Writer writer, java.lang.String encoding)
At this time, only the UTF-8 ("UTF8") and UTF-16 ("Unicode") output encodings are fully lossless with respect to XML data. If you use any other encoding you risk having your data be silently mangled on output, as the standard Java character encoding subsystem silently maps non-encodable characters to a question mark ("?") and will not report such errors to applications.
For a few other encodings the risk can be reduced. If the writer is a java.io.OutputStreamWriter, and uses either the ISO-8859-1 ("8859_1", "ISO8859_1", etc) or US-ASCII ("ASCII") encodings, content which can't be encoded in those encodings will be written safely. Where relevant, the XHTML entity names will be used; otherwise, numeric character references will be emitted.
However, there remain a number of cases where substituting such entity or character references is not an option. Such references are not usable within a DTD, comment, PI, or CDATA section. Neither may they be used when element, attribute, entity, or notation names have the problematic characters.
writer
- XML text is written to this writer.encoding
- if non-null, and an XML declaration is written,
this is the name that will be used for the character encoding.Method Detail |
public final void setWriter(java.io.Writer writer, java.lang.String encoding)
writer
- XML text is written to this writer.encoding
- if non-null, and an XML declaration is written,
this is the name that will be used for the character encoding.IllegalStateException
- if the current
document hasn't yet ended (with endDocument()
)public final void setEOL(java.lang.String eolString)
eolString
- null to use the system default; else
"\n", "\r", or "\r\n".public void setErrorHandler(ErrorHandler handler)
protected void fatal(java.lang.String message, java.lang.Exception e) throws SAXException
public final void setXhtml(boolean value)
When this option is enabled, it is the caller's responsibility to ensure that the input is otherwise valid as XHTML. Things to be careful of in all cases, as described in the appendix referenced above, include:
Additionally, some of the oldest browsers have additional quirks, to address with guidelines such as:
Also, some characteristics of the resulting output may be a function of whether the document is later given a MIME content type of text/html rather than one indicating XML (application/xml or text/xml). Worse, some browsers ignore MIME content types and prefer to rely URI name suffixes -- so an "index.xml" could always be XML, never XHTML, no matter its MIME type.
public final boolean isXhtml()
public final void setExpandingEntities(boolean value)
public final boolean isExpandingEntities()
public final void setPrettyPrinting(boolean value)
At this writing, structural indentation and line wrapping are enabled when pretty printing is enabled and the xml:space attribute has the value default (its other legal value is preserve, as defined in the XML specification). The three XHTML element types which use another value are recognized by their names (namespaces are ignored).
Also, for the record, the "pretty" aspect of printing here is more to provide basic structure on outputs that would otherwise risk being a single long line of text. For now, expect the structure to be ragged ... unless you'd like to submit a patch to make this be more strictly formatted!
IllegalStateException
- thrown if this method is invoked
after output has begun.public final boolean isPrettyPrinting()
public final void setCanonical(boolean value)
Note that fragments of XML documents, as specified by an XPath node set, may be canonicalized. In such cases, elements may need some fixup (for xml:* attributes and application-specific context).
java.lang.IllegalArgumentException
- if the output encoding
is anything other than UTF-8.public final boolean isCanonical()
public final void flush() throws java.io.IOException
public final void write(java.lang.String data) throws SAXException
public void writeElement(java.lang.String uri, java.lang.String local, java.lang.String name, Attributes atts, java.lang.String content) throws SAXException
startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
public void writeElement(java.lang.String uri, java.lang.String local, java.lang.String name, Attributes atts, int content) throws SAXException
startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
public final void setDocumentLocator(Locator l)
setDocumentLocator
in interface ContentHandler
org.xml.sax.ContentHandler
locator
- An object that can return the location of
any SAX document event.Locator
public void startDocument() throws SAXException
startDocument
in interface ContentHandler
org.xml.sax.ContentHandler
SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.endDocument()
public void endDocument() throws SAXException
endDocument
in interface ContentHandler
org.xml.sax.ContentHandler
SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.startDocument()
public final void startPrefixMapping(java.lang.String prefix, java.lang.String uri)
startPrefixMapping
in interface ContentHandler
org.xml.sax.ContentHandler
prefix
- The Namespace prefix being declared.uri
- The Namespace URI the prefix is mapped to.SAXException
- The client may throw
an exception during processing.ContentHandler.endPrefixMapping(java.lang.String)
,
ContentHandler.startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
public final void endPrefixMapping(java.lang.String prefix)
endPrefixMapping
in interface ContentHandler
org.xml.sax.ContentHandler
prefix
- The prefix that was being mapping.SAXException
- The client may throw
an exception during processing.ContentHandler.startPrefixMapping(java.lang.String, java.lang.String)
,
ContentHandler.endElement(java.lang.String, java.lang.String, java.lang.String)
public final void startElement(java.lang.String uri, java.lang.String local, java.lang.String name, Attributes atts) throws SAXException
startElement
in interface ContentHandler
org.xml.sax.ContentHandler
uri
- The Namespace URI, or the empty string if the
element has no Namespace URI or if Namespace
processing is not being performed.localName
- The local name (without prefix), or the
empty string if Namespace processing is not being
performed.qName
- The qualified name (with prefix), or the
empty string if qualified names are not available.atts
- The attributes attached to the element. If
there are no attributes, it shall be an empty
Attributes object.SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.endElement(java.lang.String, java.lang.String, java.lang.String)
,
Attributes
public void writeEmptyElement(java.lang.String uri, java.lang.String local, java.lang.String name, Attributes atts) throws SAXException
startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
public final void endElement(java.lang.String uri, java.lang.String local, java.lang.String name) throws SAXException
endElement
in interface ContentHandler
org.xml.sax.ContentHandler
uri
- The Namespace URI, or the empty string if the
element has no Namespace URI or if Namespace
processing is not being performed.localName
- The local name (without prefix), or the
empty string if Namespace processing is not being
performed.qName
- The qualified XML 1.0 name (with prefix), or the
empty string if qualified names are not available.SAXException
- Any SAX exception, possibly
wrapping another exception.public final void characters(char[] buf, int off, int len) throws SAXException
characters
in interface ContentHandler
org.xml.sax.ContentHandler
ch
- The characters from the XML document.start
- The start position in the array.length
- The number of characters to read from the array.SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.ignorableWhitespace(char[], int, int)
,
Locator
public final void ignorableWhitespace(char[] buf, int off, int len) throws SAXException
ignorableWhitespace
in interface ContentHandler
org.xml.sax.ContentHandler
ch
- The characters from the XML document.start
- The start position in the array.length
- The number of characters to read from the array.SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.characters(char[], int, int)
public final void processingInstruction(java.lang.String name, java.lang.String value) throws SAXException
processingInstruction
in interface ContentHandler
org.xml.sax.ContentHandler
target
- The processing instruction target.data
- The processing instruction data, or null if
none was supplied. The data does not include any
whitespace separating it from the target.SAXException
- Any SAX exception, possibly
wrapping another exception.public void skippedEntity(java.lang.String name) throws SAXException
skippedEntity
in interface ContentHandler
org.xml.sax.ContentHandler
name
- The name of the skipped entity. If it is a
parameter entity, the name will begin with '%', and if
it is the external DTD subset, it will be the string
"[dtd]".SAXException
- Any SAX exception, possibly
wrapping another exception.public final void startCDATA() throws SAXException
startCDATA
in interface LexicalHandler
org.xml.sax.ext.LexicalHandler
SAXException
- The application may raise an exception.LexicalHandler.endCDATA()
public final void endCDATA() throws SAXException
endCDATA
in interface LexicalHandler
org.xml.sax.ext.LexicalHandler
SAXException
- The application may raise an exception.LexicalHandler.startCDATA()
public final void startDTD(java.lang.String root, java.lang.String pubid, java.lang.String sysid) throws SAXException
startDTD
in interface LexicalHandler
org.xml.sax.ext.LexicalHandler
name
- The document type name.publicId
- The declared public identifier for the
external DTD subset, or null if none was declared.systemId
- The declared system identifier for the
external DTD subset, or null if none was declared.SAXException
- The application may raise an
exception.LexicalHandler.endDTD()
,
LexicalHandler.startEntity(java.lang.String)
public final void endDTD() throws SAXException
endDTD
in interface LexicalHandler
org.xml.sax.ext.LexicalHandler
SAXException
- The application may raise an exception.LexicalHandler.startDTD(java.lang.String, java.lang.String, java.lang.String)
public final void startEntity(java.lang.String name) throws SAXException
startEntity
in interface LexicalHandler
org.xml.sax.ext.LexicalHandler
name
- The name of the entity. If it is a parameter
entity, the name will begin with '%', and if it is the
external DTD subset, it will be "[dtd]".SAXException
- The application may raise an exception.LexicalHandler.endEntity(java.lang.String)
,
DeclHandler.internalEntityDecl(java.lang.String, java.lang.String)
,
DeclHandler.externalEntityDecl(java.lang.String, java.lang.String, java.lang.String)
public final void endEntity(java.lang.String name) throws SAXException
endEntity
in interface LexicalHandler
org.xml.sax.ext.LexicalHandler
name
- The name of the entity that is ending.SAXException
- The application may raise an exception.LexicalHandler.startEntity(java.lang.String)
public final void comment(char[] buf, int off, int len) throws SAXException
comment
in interface LexicalHandler
org.xml.sax.ext.LexicalHandler
ch
- An array holding the characters in the comment.start
- The starting position in the array.length
- The number of characters to use from the array.SAXException
- The application may raise an exception.public final void notationDecl(java.lang.String name, java.lang.String pubid, java.lang.String sysid) throws SAXException
notationDecl
in interface DTDHandler
org.xml.sax.DTDHandler
name
- The notation name.publicId
- The notation's public identifier, or null if
none was given.systemId
- The notation's system identifier, or null if
none was given.SAXException
- Any SAX exception, possibly
wrapping another exception.DTDHandler.unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
,
AttributeList
public final void unparsedEntityDecl(java.lang.String name, java.lang.String pubid, java.lang.String sysid, java.lang.String notation) throws SAXException
unparsedEntityDecl
in interface DTDHandler
org.xml.sax.DTDHandler
name
- The unparsed entity's name.publicId
- The entity's public identifier, or null if none
was given.systemId
- The entity's system identifier.notation
- name The name of the associated notation.SAXException
- Any SAX exception, possibly
wrapping another exception.DTDHandler.notationDecl(java.lang.String, java.lang.String, java.lang.String)
,
AttributeList
public final void attributeDecl(java.lang.String element, java.lang.String name, java.lang.String type, java.lang.String defaultType, java.lang.String defaultValue) throws SAXException
attributeDecl
in interface DeclHandler
org.xml.sax.ext.DeclHandler
eName
- The name of the associated element.aName
- The name of the attribute.type
- A string representing the attribute type.valueDefault
- A string representing the attribute default
("#IMPLIED", "#REQUIRED", or "#FIXED") or null if
none of these applies.value
- A string representing the attribute's default value,
or null if there is none.SAXException
- The application may raise an exception.public final void elementDecl(java.lang.String name, java.lang.String model) throws SAXException
elementDecl
in interface DeclHandler
org.xml.sax.ext.DeclHandler
name
- The element type name.model
- The content model as a normalized string.SAXException
- The application may raise an exception.public final void externalEntityDecl(java.lang.String name, java.lang.String pubid, java.lang.String sysid) throws SAXException
externalEntityDecl
in interface DeclHandler
org.xml.sax.ext.DeclHandler
name
- The name of the entity. If it is a parameter
entity, the name will begin with '%'.publicId
- The declared public identifier of the entity, or
null if none was declared.systemId
- The declared system identifier of the entity.SAXException
- The application may raise an exception.DeclHandler.internalEntityDecl(java.lang.String, java.lang.String)
,
DTDHandler.unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
public final void internalEntityDecl(java.lang.String name, java.lang.String value) throws SAXException
internalEntityDecl
in interface DeclHandler
org.xml.sax.ext.DeclHandler
name
- The name of the entity. If it is a parameter
entity, the name will begin with '%'.value
- The replacement text of the entity.SAXException
- The application may raise an exception.DeclHandler.externalEntityDecl(java.lang.String, java.lang.String, java.lang.String)
,
DTDHandler.unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
|
Source code is GPL'd in the JAXP subproject at http://savannah.gnu.org/projects/classpathx |
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |