org.dom4j.io
Class XMLWriter

java.lang.Object
  extended byorg.xml.sax.helpers.XMLFilterImpl
      extended byorg.dom4j.io.XMLWriter
All Implemented Interfaces:
ContentHandler, DTDHandler, EntityResolver, ErrorHandler, LexicalHandler, XMLFilter, XMLReader
Direct Known Subclasses:
HTMLWriter

public class XMLWriter
extends XMLFilterImpl
implements LexicalHandler

XMLWriter takes a DOM4J tree and formats it to a stream as XML. It can also take SAX events too so can be used by SAX clients as this object implements the ContentHandler and LexicalHandler interfaces. as well. This formatter performs typical document formatting. The XML declaration and processing instructions are always on their own lines. An OutputFormat object can be used to define how whitespace is handled when printing and allows various configuration options, such as to allow suppression of the XML declaration, the encoding declaration or whether empty documents are collapsed.

There are write(...) methods to print any of the standard DOM4J classes, including Document and Element, to either a Writer or an OutputStream. Warning: using your own Writer may cause the writer's preferred character encoding to be ignored. If you use encodings other than UTF8, we recommend using the method that takes an OutputStream instead.

Version:
$Revision: 1.76 $
Author:
James Strachan, Joseph Bowbeer

Field Summary
protected static OutputFormat DEFAULT_FORMAT
           
protected  int lastOutputNodeType
          Stores the last type of node written so algorithms can refer to the previous node type
protected static java.lang.String[] LEXICAL_HANDLER_NAMES
           
protected  boolean preserve
          Stores the xml:space attribute value of preserve for whitespace flag
protected  java.io.Writer writer
          The Writer used to output to
 
Constructor Summary
XMLWriter()
           
XMLWriter(OutputFormat format)
           
XMLWriter(java.io.OutputStream out)
           
XMLWriter(java.io.OutputStream out, OutputFormat format)
           
XMLWriter(java.io.Writer writer)
           
XMLWriter(java.io.Writer writer, OutputFormat format)
           
 
Method Summary
 void characters(char[] ch, int start, int length)
          Filter a character data event.
 void close()
          Closes the underlying Writer
 void comment(char[] ch, int start, int length)
          Report an XML comment anywhere in the document.
protected  java.io.Writer createWriter(java.io.OutputStream outStream, java.lang.String encoding)
          Get an OutputStreamWriter, use preferred encoding.
protected  int defaultMaximumAllowedCharacter()
          Returns the maximum allowed character code that should be allowed unescaped which defaults to 127 in US-ASCII (7 bit) or 255 in ISO-* (8 bit).
 void endCDATA()
          Report the end of a CDATA section.
 void endDocument()
          Filter an end document event.
 void endDTD()
          Report the end of DTD declarations.
 void endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName)
          Filter an end element event.
 void endEntity(java.lang.String name)
          Report the end of an entity.
 void endPrefixMapping(java.lang.String prefix)
          Filter an end Namespace prefix mapping event.
protected  java.lang.String escapeAttributeEntities(java.lang.String text)
          This will take the pre-defined entities in XML 1.0 and convert their character representation to the appropriate entity reference, suitable for XML attributes.
protected  java.lang.String escapeElementEntities(java.lang.String text)
          This will take the pre-defined entities in XML 1.0 and convert their character representation to the appropriate entity reference, suitable for XML attributes.
 void flush()
          Flushes the underlying Writer
 LexicalHandler getLexicalHandler()
           
 int getMaximumAllowedCharacter()
          Returns the maximum allowed character code that should be allowed unescaped which defaults to 127 in US-ASCII (7 bit) or 255 in ISO-* (8 bit).
protected  OutputFormat getOutputFormat()
          Lets subclasses get at the current format object, so they can call setTrimText, setNewLines, etc.
 java.lang.Object getProperty(java.lang.String name)
          Look up the value of a property.
protected  void handleException(java.io.IOException e)
           
 void ignorableWhitespace(char[] ch, int start, int length)
          Filter an ignorable whitespace event.
protected  void indent()
           
protected  void installLexicalHandler()
           
protected  boolean isElementSpacePreserved(Element element)
          Determines if element is a special case of XML elements where it contains an xml:space attribute of "preserve".
 boolean isEscapeText()
           
protected  boolean isExpandEmptyElements()
           
protected  boolean isNamespaceDeclaration(Namespace ns)
           
 void notationDecl(java.lang.String name, java.lang.String publicID, java.lang.String systemID)
          Filter a notation declaration event.
 void parse(InputSource source)
          Parse a document.
 void println()
          Writes the new line text to the underlying Writer
 void processingInstruction(java.lang.String target, java.lang.String data)
          Filter a processing instruction event.
 boolean resolveEntityRefs()
           
 void setDocumentLocator(Locator locator)
          Filter a new document locator event.
 void setEscapeText(boolean escapeText)
          Sets whether text output should be escaped or not.
 void setIndentLevel(int indentLevel)
          Set the initial indentation level.
 void setLexicalHandler(LexicalHandler handler)
           
 void setMaximumAllowedCharacter(int maximumAllowedCharacter)
          Sets the maximum allowed character code that should be allowed unescaped such as 127 in US-ASCII (7 bit) or 255 in ISO-* (8 bit) or -1 to not escape any characters (other than the special XML characters like < > &) If this is not explicitly set then it is defaulted from the encoding.
 void setOutputStream(java.io.OutputStream out)
           
 void setProperty(java.lang.String name, java.lang.Object value)
          Set the value of a property.
 void setResolveEntityRefs(boolean resolve)
           
 void setWriter(java.io.Writer writer)
           
protected  boolean shouldEncodeChar(char c)
          Should the given character be escaped.
 void startCDATA()
          Report the start of a CDATA section.
 void startDocument()
          Filter a start document event.
 void startDTD(java.lang.String name, java.lang.String publicID, java.lang.String systemID)
          Report the start of DTD declarations, if any.
 void startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, Attributes attributes)
          Filter a start element event.
 void startEntity(java.lang.String name)
          Report the beginning of some internal and external XML entities.
 void startPrefixMapping(java.lang.String prefix, java.lang.String uri)
          Filter a start Namespace prefix mapping event.
 void unparsedEntityDecl(java.lang.String name, java.lang.String publicID, java.lang.String systemID, java.lang.String notationName)
          Filter an unparsed entity declaration event.
 void write(Attribute attribute)
          Writes the given Attribute.
 void write(CDATA cdata)
          Writes the given CDATA.
 void write(Comment comment)
          Writes the given Comment.
 void write(Document doc)
          This will print the Document to the current Writer.
 void write(DocumentType docType)
          Writes the given DocumentType.
 void write(Element element)
          Writes the Element, including its Attributes, and its value, and all its content (child nodes) to the current Writer.
 void write(Entity entity)
          Writes the given Entity.
 void write(Namespace namespace)
          Writes the given Namespace.
 void write(Node node)
          Writes the given Node.
 void write(java.lang.Object object)
          Writes the given object which should be a String, a Node or a List of Nodes.
 void write(ProcessingInstruction processingInstruction)
          Writes the given ProcessingInstruction.
 void write(java.lang.String text)
          Print out a String, Perfoms the necessary entity escaping and whitespace stripping.
 void write(Text text)
          Writes the given Text.
protected  void writeAttribute(Attribute attribute)
           
protected  void writeAttribute(Attributes attributes, int index)
           
protected  void writeAttributes(Attributes attributes)
           
protected  void writeAttributes(Element element)
          Writes the attributes of the given element
protected  void writeCDATA(java.lang.String text)
           
 void writeClose(Element element)
          Writes the closing tag of an Element
protected  void writeClose(java.lang.String qualifiedName)
           
protected  void writeComment(java.lang.String text)
           
protected  void writeDeclaration()
           This will write the declaration to the given Writer.
protected  void writeDocType(DocumentType docType)
           
protected  void writeDocType(java.lang.String name, java.lang.String publicID, java.lang.String systemID)
           
protected  void writeElement(Element element)
           
protected  void writeElementContent(Element element)
          Outputs the content of the given element.
protected  void writeEmptyElementClose(java.lang.String qualifiedName)
           
protected  void writeEntity(Entity entity)
           
protected  void writeEntityRef(java.lang.String name)
           
protected  void writeEscapeAttributeEntities(java.lang.String text)
           
protected  void writeNamespace(Namespace namespace)
           
protected  void writeNamespace(java.lang.String prefix, java.lang.String uri)
          Writes the SAX namepsaces
protected  void writeNamespaces()
          Writes the SAX namepsaces
protected  void writeNode(Node node)
           
protected  void writeNodeText(Node node)
          This method is used to write out Nodes that contain text and still allow for xml:space to be handled properly.
 void writeOpen(Element element)
          Writes the opening tag of an Element, including its Attributes but without its content.
protected  void writePrintln()
           This will print a new line only if the newlines flag was set to true
protected  void writeProcessingInstruction(ProcessingInstruction processingInstruction)
           
protected  void writeString(java.lang.String text)
           
 
Methods inherited from class org.xml.sax.helpers.XMLFilterImpl
error, fatalError, getContentHandler, getDTDHandler, getEntityResolver, getErrorHandler, getFeature, getParent, parse, resolveEntity, setContentHandler, setDTDHandler, setEntityResolver, setErrorHandler, setFeature, setParent, skippedEntity, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LEXICAL_HANDLER_NAMES

protected static final java.lang.String[] LEXICAL_HANDLER_NAMES

DEFAULT_FORMAT

protected static final OutputFormat DEFAULT_FORMAT

lastOutputNodeType

protected int lastOutputNodeType
Stores the last type of node written so algorithms can refer to the previous node type


preserve

protected boolean preserve
Stores the xml:space attribute value of preserve for whitespace flag


writer

protected java.io.Writer writer
The Writer used to output to

Constructor Detail

XMLWriter

public XMLWriter(java.io.Writer writer)

XMLWriter

public XMLWriter(java.io.Writer writer,
                 OutputFormat format)

XMLWriter

public XMLWriter()

XMLWriter

public XMLWriter(java.io.OutputStream out)
          throws java.io.UnsupportedEncodingException

XMLWriter

public XMLWriter(java.io.OutputStream out,
                 OutputFormat format)
          throws java.io.UnsupportedEncodingException

XMLWriter

public XMLWriter(OutputFormat format)
          throws java.io.UnsupportedEncodingException
Method Detail

setWriter

public void setWriter(java.io.Writer writer)

setOutputStream

public void setOutputStream(java.io.OutputStream out)
                     throws java.io.UnsupportedEncodingException
Throws:
java.io.UnsupportedEncodingException

isEscapeText

public boolean isEscapeText()
Returns:
true if text thats output should be escaped. This is enabled by default. It could be disabled if the output format is textual, like in XSLT where we can have xml, html or text output.

setEscapeText

public void setEscapeText(boolean escapeText)
Sets whether text output should be escaped or not. This is enabled by default. It could be disabled if the output format is textual, like in XSLT where we can have xml, html or text output.


setIndentLevel

public void setIndentLevel(int indentLevel)
Set the initial indentation level. This can be used to output a document (or, more likely, an element) starting at a given indent level, so it's not always flush against the left margin. Default: 0

Parameters:
indentLevel - the number of indents to start with

getMaximumAllowedCharacter

public int getMaximumAllowedCharacter()
Returns the maximum allowed character code that should be allowed unescaped which defaults to 127 in US-ASCII (7 bit) or 255 in ISO-* (8 bit).


setMaximumAllowedCharacter

public void setMaximumAllowedCharacter(int maximumAllowedCharacter)
Sets the maximum allowed character code that should be allowed unescaped such as 127 in US-ASCII (7 bit) or 255 in ISO-* (8 bit) or -1 to not escape any characters (other than the special XML characters like < > &) If this is not explicitly set then it is defaulted from the encoding.

Parameters:
maximumAllowedCharacter - The maximumAllowedCharacter to set

flush

public void flush()
           throws java.io.IOException
Flushes the underlying Writer

Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Closes the underlying Writer

Throws:
java.io.IOException

println

public void println()
             throws java.io.IOException
Writes the new line text to the underlying Writer

Throws:
java.io.IOException

write

public void write(Attribute attribute)
           throws java.io.IOException
Writes the given Attribute.

Parameters:
attribute - Attribute to output.
Throws:
java.io.IOException

write

public void write(Document doc)
           throws java.io.IOException

This will print the Document to the current Writer.

Warning: using your own Writer may cause the writer's preferred character encoding to be ignored. If you use encodings other than UTF8, we recommend using the method that takes an OutputStream instead.

Note: as with all Writers, you may need to flush() yours after this method returns.

Parameters:
doc - Document to format.
Throws:
IOException - - if there's any problem writing.
java.io.IOException

write

public void write(Element element)
           throws java.io.IOException

Writes the Element, including its Attributes, and its value, and all its content (child nodes) to the current Writer.

Parameters:
element - Element to output.
Throws:
java.io.IOException

write

public void write(CDATA cdata)
           throws java.io.IOException
Writes the given CDATA.

Parameters:
cdata - CDATA to output.
Throws:
java.io.IOException

write

public void write(Comment comment)
           throws java.io.IOException
Writes the given Comment.

Parameters:
comment - Comment to output.
Throws:
java.io.IOException

write

public void write(DocumentType docType)
           throws java.io.IOException
Writes the given DocumentType.

Parameters:
docType - DocumentType to output.
Throws:
java.io.IOException

write

public void write(Entity entity)
           throws java.io.IOException
Writes the given Entity.

Parameters:
entity - Entity to output.
Throws:
java.io.IOException

write

public void write(Namespace namespace)
           throws java.io.IOException
Writes the given Namespace.

Parameters:
namespace - Namespace to output.
Throws:
java.io.IOException

write

public void write(ProcessingInstruction processingInstruction)
           throws java.io.IOException
Writes the given ProcessingInstruction.

Parameters:
processingInstruction - ProcessingInstruction to output.
Throws:
java.io.IOException

write

public void write(java.lang.String text)
           throws java.io.IOException

Print out a String, Perfoms the necessary entity escaping and whitespace stripping.

Parameters:
text - is the text to output
Throws:
java.io.IOException

write

public void write(Text text)
           throws java.io.IOException
Writes the given Text.

Parameters:
text - Text to output.
Throws:
java.io.IOException

write

public void write(Node node)
           throws java.io.IOException
Writes the given Node.

Parameters:
node - Node to output.
Throws:
java.io.IOException

write

public void write(java.lang.Object object)
           throws java.io.IOException
Writes the given object which should be a String, a Node or a List of Nodes.

Parameters:
object - is the object to output.
Throws:
java.io.IOException

writeOpen

public void writeOpen(Element element)
               throws java.io.IOException

Writes the opening tag of an Element, including its Attributes but without its content.

Parameters:
element - Element to output.
Throws:
java.io.IOException

writeClose

public void writeClose(Element element)
                throws java.io.IOException

Writes the closing tag of an Element

Parameters:
element - Element to output.
Throws:
java.io.IOException

parse

public void parse(InputSource source)
           throws java.io.IOException,
                  SAXException
Description copied from class: XMLFilterImpl
Parse a document.

Specified by:
parse in interface XMLReader
Overrides:
parse in class XMLFilterImpl
Parameters:
source - The input source for the document entity.
Throws:
SAXException - Any SAX exception, possibly wrapping another exception.
java.io.IOException - An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.

setProperty

public void setProperty(java.lang.String name,
                        java.lang.Object value)
                 throws SAXNotRecognizedException,
                        SAXNotSupportedException
Description copied from class: XMLFilterImpl
Set the value of a property.

This will always fail if the parent is null.

Specified by:
setProperty in interface XMLReader
Overrides:
setProperty in class XMLFilterImpl
Parameters:
name - The property name.
value - The requested property value.
Throws:
SAXNotSupportedException - When the parent recognizes the property name but cannot set the requested value.
SAXNotRecognizedException - If the property value can't be assigned or retrieved from the parent.

getProperty

public java.lang.Object getProperty(java.lang.String name)
                             throws SAXNotRecognizedException,
                                    SAXNotSupportedException
Description copied from class: XMLFilterImpl
Look up the value of a property.

Specified by:
getProperty in interface XMLReader
Overrides:
getProperty in class XMLFilterImpl
Parameters:
name - The property name.
Returns:
The current value of the property.
Throws:
SAXNotSupportedException - When the parent recognizes the property name but cannot determine its value at this time.
SAXNotRecognizedException - If the property value can't be assigned or retrieved from the parent.

setLexicalHandler

public void setLexicalHandler(LexicalHandler handler)

getLexicalHandler

public LexicalHandler getLexicalHandler()

setDocumentLocator

public void setDocumentLocator(Locator locator)
Description copied from class: XMLFilterImpl
Filter a new document locator event.

Specified by:
setDocumentLocator in interface ContentHandler
Overrides:
setDocumentLocator in class XMLFilterImpl
Parameters:
locator - The document locator.

startDocument

public void startDocument()
                   throws SAXException
Description copied from class: XMLFilterImpl
Filter a start document event.

Specified by:
startDocument in interface ContentHandler
Overrides:
startDocument in class XMLFilterImpl
Throws:
SAXException - The client may throw an exception during processing.

endDocument

public void endDocument()
                 throws SAXException
Description copied from class: XMLFilterImpl
Filter an end document event.

Specified by:
endDocument in interface ContentHandler
Overrides:
endDocument in class XMLFilterImpl
Throws:
SAXException - The client may throw an exception during processing.

startPrefixMapping

public void startPrefixMapping(java.lang.String prefix,
                               java.lang.String uri)
                        throws SAXException
Description copied from class: XMLFilterImpl
Filter a start Namespace prefix mapping event.

Specified by:
startPrefixMapping in interface ContentHandler
Overrides:
startPrefixMapping in class XMLFilterImpl
Parameters:
prefix - The Namespace prefix.
uri - The Namespace URI.
Throws:
SAXException - The client may throw an exception during processing.

endPrefixMapping

public void endPrefixMapping(java.lang.String prefix)
                      throws SAXException
Description copied from class: XMLFilterImpl
Filter an end Namespace prefix mapping event.

Specified by:
endPrefixMapping in interface ContentHandler
Overrides:
endPrefixMapping in class XMLFilterImpl
Parameters:
prefix - The Namespace prefix.
Throws:
SAXException - The client may throw an exception during processing.

startElement

public void startElement(java.lang.String namespaceURI,
                         java.lang.String localName,
                         java.lang.String qName,
                         Attributes attributes)
                  throws SAXException
Description copied from class: XMLFilterImpl
Filter a start element event.

Specified by:
startElement in interface ContentHandler
Overrides:
startElement in class XMLFilterImpl
Parameters:
namespaceURI - The element's Namespace URI, or the empty string.
localName - The element's local name, or the empty string.
qName - The element's qualified (prefixed) name, or the empty string.
attributes - The element's attributes.
Throws:
SAXException - The client may throw an exception during processing.

endElement

public void endElement(java.lang.String namespaceURI,
                       java.lang.String localName,
                       java.lang.String qName)
                throws SAXException
Description copied from class: XMLFilterImpl
Filter an end element event.

Specified by:
endElement in interface ContentHandler
Overrides:
endElement in class XMLFilterImpl
Parameters:
namespaceURI - The element's Namespace URI, or the empty string.
localName - The element's local name, or the empty string.
qName - The element's qualified (prefixed) name, or the empty string.
Throws:
SAXException - The client may throw an exception during processing.

characters

public void characters(char[] ch,
                       int start,
                       int length)
                throws SAXException
Description copied from class: XMLFilterImpl
Filter a character data event.

Specified by:
characters in interface ContentHandler
Overrides:
characters in class XMLFilterImpl
Parameters:
ch - An array of characters.
start - The starting position in the array.
length - The number of characters to use from the array.
Throws:
SAXException - The client may throw an exception during processing.

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int length)
                         throws SAXException
Description copied from class: XMLFilterImpl
Filter an ignorable whitespace event.

Specified by:
ignorableWhitespace in interface ContentHandler
Overrides:
ignorableWhitespace in class XMLFilterImpl
Parameters:
ch - An array of characters.
start - The starting position in the array.
length - The number of characters to use from the array.
Throws:
SAXException - The client may throw an exception during processing.

processingInstruction

public void processingInstruction(java.lang.String target,
                                  java.lang.String data)
                           throws SAXException
Description copied from class: XMLFilterImpl
Filter a processing instruction event.

Specified by:
processingInstruction in interface ContentHandler
Overrides:
processingInstruction in class XMLFilterImpl
Parameters:
target - The processing instruction target.
data - The text following the target.
Throws:
SAXException - The client may throw an exception during processing.

notationDecl

public void notationDecl(java.lang.String name,
                         java.lang.String publicID,
                         java.lang.String systemID)
                  throws SAXException
Description copied from class: XMLFilterImpl
Filter a notation declaration event.

Specified by:
notationDecl in interface DTDHandler
Overrides:
notationDecl in class XMLFilterImpl
Parameters:
name - The notation name.
publicID - The notation's public identifier, or null.
systemID - The notation's system identifier, or null.
Throws:
SAXException - The client may throw an exception during processing.

unparsedEntityDecl

public void unparsedEntityDecl(java.lang.String name,
                               java.lang.String publicID,
                               java.lang.String systemID,
                               java.lang.String notationName)
                        throws SAXException
Description copied from class: XMLFilterImpl
Filter an unparsed entity declaration event.

Specified by:
unparsedEntityDecl in interface DTDHandler
Overrides:
unparsedEntityDecl in class XMLFilterImpl
Parameters:
name - The entity name.
publicID - The entity's public identifier, or null.
systemID - The entity's system identifier, or null.
notationName - The name of the associated notation.
Throws:
SAXException - The client may throw an exception during processing.

startDTD

public void startDTD(java.lang.String name,
                     java.lang.String publicID,
                     java.lang.String systemID)
              throws SAXException
Description copied from interface: LexicalHandler
Report the start of DTD declarations, if any.

This method is intended to report the beginning of the DOCTYPE declaration; if the document has no DOCTYPE declaration, this method will not be invoked.

All declarations reported through DTDHandler or DeclHandler events must appear between the startDTD and endDTD events. Declarations are assumed to belong to the internal DTD subset unless they appear between startEntity and endEntity events. Comments and processing instructions from the DTD should also be reported between the startDTD and endDTD events, in their original order of (logical) occurrence; they are not required to appear in their correct locations relative to DTDHandler or DeclHandler events, however.

Note that the start/endDTD events will appear within the start/endDocument events from ContentHandler and before the first startElement event.

Specified by:
startDTD in interface LexicalHandler
Parameters:
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. (Note that this is not resolved against the document base URI.)
Throws:
SAXException - The application may raise an exception.
See Also:
LexicalHandler.endDTD(), LexicalHandler.startEntity(java.lang.String)

endDTD

public void endDTD()
            throws SAXException
Description copied from interface: LexicalHandler
Report the end of DTD declarations.

This method is intended to report the end of the DOCTYPE declaration; if the document has no DOCTYPE declaration, this method will not be invoked.

Specified by:
endDTD in interface LexicalHandler
Throws:
SAXException - The application may raise an exception.
See Also:
LexicalHandler.startDTD(java.lang.String, java.lang.String, java.lang.String)

startCDATA

public void startCDATA()
                throws SAXException
Description copied from interface: LexicalHandler
Report the start of a CDATA section.

The contents of the CDATA section will be reported through the regular characters event; this event is intended only to report the boundary.

Specified by:
startCDATA in interface LexicalHandler
Throws:
SAXException - The application may raise an exception.
See Also:
LexicalHandler.endCDATA()

endCDATA

public void endCDATA()
              throws SAXException
Description copied from interface: LexicalHandler
Report the end of a CDATA section.

Specified by:
endCDATA in interface LexicalHandler
Throws:
SAXException - The application may raise an exception.
See Also:
LexicalHandler.startCDATA()

startEntity

public void startEntity(java.lang.String name)
                 throws SAXException
Description copied from interface: LexicalHandler
Report the beginning of some internal and external XML entities.

The reporting of parameter entities (including the external DTD subset) is optional, and SAX2 drivers that report LexicalHandler events may not implement it; you can use the http://xml.org/sax/features/lexical-handler/parameter-entities feature to query or control the reporting of parameter entities.

General entities are reported with their regular names, parameter entities have '%' prepended to their names, and the external DTD subset has the pseudo-entity name "[dtd]".

When a SAX2 driver is providing these events, all other events must be properly nested within start/end entity events. There is no additional requirement that events from DeclHandler or DTDHandler be properly ordered.

Note that skipped entities will be reported through the skippedEntity event, which is part of the ContentHandler interface.

Because of the streaming event model that SAX uses, some entity boundaries cannot be reported under any circumstances:

These will be silently expanded, with no indication of where the original entity boundaries were.

Note also that the boundaries of character references (which are not really entities anyway) are not reported.

All start/endEntity events must be properly nested.

Specified by:
startEntity in interface LexicalHandler
Parameters:
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]".
Throws:
SAXException - The application may raise an exception.
See Also:
LexicalHandler.endEntity(java.lang.String), DeclHandler.internalEntityDecl(java.lang.String, java.lang.String), DeclHandler.externalEntityDecl(java.lang.String, java.lang.String, java.lang.String)

endEntity

public void endEntity(java.lang.String name)
               throws SAXException
Description copied from interface: LexicalHandler
Report the end of an entity.

Specified by:
endEntity in interface LexicalHandler
Parameters:
name - The name of the entity that is ending.
Throws:
SAXException - The application may raise an exception.
See Also:
LexicalHandler.startEntity(java.lang.String)

comment

public void comment(char[] ch,
                    int start,
                    int length)
             throws SAXException
Description copied from interface: LexicalHandler
Report an XML comment anywhere in the document.

This callback will be used for comments inside or outside the document element, including comments in the external DTD subset (if read). Comments in the DTD must be properly nested inside start/endDTD and start/endEntity events (if used).

Specified by:
comment in interface LexicalHandler
Parameters:
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.
Throws:
SAXException - The application may raise an exception.

writeElement

protected void writeElement(Element element)
                     throws java.io.IOException
Throws:
java.io.IOException

isElementSpacePreserved

protected final boolean isElementSpacePreserved(Element element)
Determines if element is a special case of XML elements where it contains an xml:space attribute of "preserve". If it does, then retain whitespace.


writeElementContent

protected void writeElementContent(Element element)
                            throws java.io.IOException
Outputs the content of the given element. If whitespace trimming is enabled then all adjacent text nodes are appended together before the whitespace trimming occurs to avoid problems with multiple text nodes being created due to text content that spans parser buffers in a SAX parser.

Throws:
java.io.IOException

writeCDATA

protected void writeCDATA(java.lang.String text)
                   throws java.io.IOException
Throws:
java.io.IOException

writeDocType

protected void writeDocType(DocumentType docType)
                     throws java.io.IOException
Throws:
java.io.IOException

writeNamespace

protected void writeNamespace(Namespace namespace)
                       throws java.io.IOException
Throws:
java.io.IOException

writeNamespaces

protected void writeNamespaces()
                        throws java.io.IOException
Writes the SAX namepsaces

Throws:
java.io.IOException

writeNamespace

protected void writeNamespace(java.lang.String prefix,
                              java.lang.String uri)
                       throws java.io.IOException
Writes the SAX namepsaces

Throws:
java.io.IOException

writeProcessingInstruction

protected void writeProcessingInstruction(ProcessingInstruction processingInstruction)
                                   throws java.io.IOException
Throws:
java.io.IOException

writeString

protected void writeString(java.lang.String text)
                    throws java.io.IOException
Throws:
java.io.IOException

writeNodeText

protected void writeNodeText(Node node)
                      throws java.io.IOException
This method is used to write out Nodes that contain text and still allow for xml:space to be handled properly.

Throws:
java.io.IOException

writeNode

protected void writeNode(Node node)
                  throws java.io.IOException
Throws:
java.io.IOException

installLexicalHandler

protected void installLexicalHandler()

writeDocType

protected void writeDocType(java.lang.String name,
                            java.lang.String publicID,
                            java.lang.String systemID)
                     throws java.io.IOException
Throws:
java.io.IOException

writeEntity

protected void writeEntity(Entity entity)
                    throws java.io.IOException
Throws:
java.io.IOException

writeEntityRef

protected void writeEntityRef(java.lang.String name)
                       throws java.io.IOException
Throws:
java.io.IOException

writeComment

protected void writeComment(java.lang.String text)
                     throws java.io.IOException
Throws:
java.io.IOException

writeAttributes

protected void writeAttributes(Element element)
                        throws java.io.IOException
Writes the attributes of the given element

Throws:
java.io.IOException

writeAttribute

protected void writeAttribute(Attribute attribute)
                       throws java.io.IOException
Throws:
java.io.IOException

writeAttributes

protected void writeAttributes(Attributes attributes)
                        throws java.io.IOException
Throws:
java.io.IOException

writeAttribute

protected void writeAttribute(Attributes attributes,
                              int index)
                       throws java.io.IOException
Throws:
java.io.IOException

indent

protected void indent()
               throws java.io.IOException
Throws:
java.io.IOException

writePrintln

protected void writePrintln()
                     throws java.io.IOException

This will print a new line only if the newlines flag was set to true

Throws:
java.io.IOException

createWriter

protected java.io.Writer createWriter(java.io.OutputStream outStream,
                                      java.lang.String encoding)
                               throws java.io.UnsupportedEncodingException
Get an OutputStreamWriter, use preferred encoding.

Throws:
java.io.UnsupportedEncodingException

writeDeclaration

protected void writeDeclaration()
                         throws java.io.IOException

This will write the declaration to the given Writer. Assumes XML version 1.0 since we don't directly know.

Throws:
java.io.IOException

writeClose

protected void writeClose(java.lang.String qualifiedName)
                   throws java.io.IOException
Throws:
java.io.IOException

writeEmptyElementClose

protected void writeEmptyElementClose(java.lang.String qualifiedName)
                               throws java.io.IOException
Throws:
java.io.IOException

isExpandEmptyElements

protected boolean isExpandEmptyElements()

escapeElementEntities

protected java.lang.String escapeElementEntities(java.lang.String text)
This will take the pre-defined entities in XML 1.0 and convert their character representation to the appropriate entity reference, suitable for XML attributes.


writeEscapeAttributeEntities

protected void writeEscapeAttributeEntities(java.lang.String text)
                                     throws java.io.IOException
Throws:
java.io.IOException

escapeAttributeEntities

protected java.lang.String escapeAttributeEntities(java.lang.String text)
This will take the pre-defined entities in XML 1.0 and convert their character representation to the appropriate entity reference, suitable for XML attributes.


shouldEncodeChar

protected boolean shouldEncodeChar(char c)
Should the given character be escaped. This depends on the encoding of the document.

Returns:
boolean

defaultMaximumAllowedCharacter

protected int defaultMaximumAllowedCharacter()
Returns the maximum allowed character code that should be allowed unescaped which defaults to 127 in US-ASCII (7 bit) or 255 in ISO-* (8 bit).


isNamespaceDeclaration

protected boolean isNamespaceDeclaration(Namespace ns)

handleException

protected void handleException(java.io.IOException e)
                        throws SAXException
Throws:
SAXException

getOutputFormat

protected OutputFormat getOutputFormat()
Lets subclasses get at the current format object, so they can call setTrimText, setNewLines, etc. Put in to support the HTMLWriter, in the way that it pushes the current newline/trim state onto a stack and overrides the state within preformatted tags.


resolveEntityRefs

public boolean resolveEntityRefs()

setResolveEntityRefs

public void setResolveEntityRefs(boolean resolve)


Copyright © 2004 MetaStuff Ltd. All Rights Reserved. Hosted by

SourceForge Logo