com.caucho.xml2
Class XmlPrinter

java.lang.Object
  extended by com.caucho.xml2.XmlPrinter
All Implemented Interfaces:
XMLWriter

public class XmlPrinter
extends java.lang.Object
implements XMLWriter

Controls printing of XML documents. Typical use:

 Node node = ...;

 OutputStream os = Vfs.openWrite("test.xml");
 XmlPrinter printer = new XmlPrinter(os);

 printer.printXml(node);
 


Constructor Summary
XmlPrinter()
          Create an XmlPrinter.
XmlPrinter(java.io.OutputStream os)
          Creates a new XmlPrinter writing to an output stream.
XmlPrinter(java.io.Writer writer)
          Creates a new XmlPrinter writing to a writer.
 
Method Summary
 void addCdataElement(java.lang.String elt)
           
 void attribute(java.lang.String uri, java.lang.String localName, java.lang.String qName, java.lang.String value)
          Prints an attribute
 void cdata(char[] buffer, int offset, int length)
           
 void cdata(java.lang.String text)
           
 void comment(java.lang.String data)
          Prints a comment
 void endDocument()
          Callback when the document completes
 void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
          Prints the end tag of an element
 void endPrefixMapping(java.lang.String prefix)
           
 boolean finishAttributes()
          Complete printing of the attributes when the open tag completes.
 boolean getEscapeText()
          Returns true if the text is currently being escaped
 boolean getEscaping()
          Returns the current XML escaping.
 boolean getIncludeContentType()
          Return true if the printer should automatically add the <meta content-type> to HTML.
 LineMap getLineMap()
           
 Path getPath()
           
 void init(WriteStream os)
          Initialize the XmlPrinter with the write stream.
 boolean isJSP()
          True if this is JSP special cased.
 boolean isPretty()
          Returns true if the printer is currently pretty-printing the output.
 void print(org.w3c.dom.Node node)
           
static void print(Path path, org.w3c.dom.Node node)
          Prints the node as XML.
 void printHeader(java.lang.String top)
          Prints the header, if necessary.
 void printHtml(org.w3c.dom.Node node)
          Prints the node and children as HTML
 void printNode(org.w3c.dom.Node node)
           
 void printPrettyXml(org.w3c.dom.Node node)
          Prints the node and children as XML, automatically indending
 java.lang.String printString(org.w3c.dom.Node node)
          Prints the node as XML to a string.
 void printXml(org.w3c.dom.Node node)
          Prints the node as XML.
 void processingInstruction(java.lang.String name, java.lang.String data)
          Prints a processing instruction
 void setDocumentLocator(org.xml.sax.Locator locator)
          Sets the locator.
 void setEncoding(java.lang.String encoding)
          Sets the character set encoding for the output file.
 void setEscapeText(boolean isEscaped)
          Sets true if the text should be escaped, else it will be printed verbatim.
 void setEscaping(boolean escapeText)
          Sets to true if XML entities like < should be escaped as &lt;.
 void setIncludeContentType(boolean include)
          Set true if the printer should automatically add the <meta content-type> to HTML.
 void setJSP(boolean isJsp)
          Set true if this is JSP special cased.
 void setLineMap(java.lang.String filename)
          Creates a new line map.
 void setLocation(java.lang.String filename, int line, int column)
          Sets the current location.
 void setMethod(java.lang.String method)
          Sets the output methods, like the XSL <xsl:output method='method'/>.
 void setMimeType(java.lang.String mimeType)
           
 void setPretty(boolean isPretty)
          Set to true if the printer should add whitespace to 'pretty-print' the output.
 void setPrintDeclaration(boolean printDeclaration)
           
 void setPublicId(java.lang.String id)
           
 void setStandalone(java.lang.String standalone)
           
 void setSystemId(java.lang.String id)
           
 void setVersion(java.lang.String version)
          Sets the XML/HTML version of the output file.
 void startDocument()
          Callback when the document starts printing.
 void startDocument(org.w3c.dom.Document document)
           
 void startElement(java.lang.String url, java.lang.String localName, java.lang.String qName)
          Called at the start of a new element.
 void startPrefixMapping(java.lang.String prefix, java.lang.String uri)
           
 void text(char[] buffer, int offset, int length)
          Prints text.
 void text(java.lang.String text)
          Prints text.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlPrinter

public XmlPrinter()
Create an XmlPrinter. Using this API, you'll need to use printer.init(os) to assign an output stream.


XmlPrinter

public XmlPrinter(java.io.OutputStream os)
Creates a new XmlPrinter writing to an output stream.

Parameters:
os - output stream serving as the destination

XmlPrinter

public XmlPrinter(java.io.Writer writer)
Creates a new XmlPrinter writing to a writer.

Parameters:
writer - destination of the serialized node
Method Detail

init

public void init(WriteStream os)
Initialize the XmlPrinter with the write stream.

Parameters:
os - WriteStream containing the results.

print

public static void print(Path path,
                         org.w3c.dom.Node node)
                  throws java.io.IOException
Prints the node as XML. The destination stream has already been set using init() or in the constructor.

Parameters:
node - source DOM node
Throws:
java.io.IOException

printXml

public void printXml(org.w3c.dom.Node node)
              throws java.io.IOException
Prints the node as XML. The destination stream has already been set using init() or in the constructor.

Parameters:
node - source DOM node
Throws:
java.io.IOException

printHtml

public void printHtml(org.w3c.dom.Node node)
               throws java.io.IOException
Prints the node and children as HTML

Parameters:
node - the top node to print
Throws:
java.io.IOException

printPrettyXml

public void printPrettyXml(org.w3c.dom.Node node)
                    throws java.io.IOException
Prints the node and children as XML, automatically indending

Parameters:
node - the top node to print
Throws:
java.io.IOException

printString

public java.lang.String printString(org.w3c.dom.Node node)
                             throws java.io.IOException
Prints the node as XML to a string.

Parameters:
node - the source node
Returns:
a string containing the XML.
Throws:
java.io.IOException

setEscaping

public void setEscaping(boolean escapeText)
Sets to true if XML entities like < should be escaped as &lt;. The default is true.

Parameters:
escapeText - set to true if entities should be escaped.

getEscaping

public boolean getEscaping()
Returns the current XML escaping. If true, entities like < will be escaped as &lt;.

Returns:
true if entities should be escaped.

setMethod

public void setMethod(java.lang.String method)
Sets the output methods, like the XSL <xsl:output method='method'/>.


setVersion

public void setVersion(java.lang.String version)
Sets the XML/HTML version of the output file.

Parameters:
version - the output version

setEncoding

public void setEncoding(java.lang.String encoding)
Sets the character set encoding for the output file.

Parameters:
encoding - the mime name of the output encoding

setMimeType

public void setMimeType(java.lang.String mimeType)

setJSP

public void setJSP(boolean isJsp)
Set true if this is JSP special cased.


isJSP

public boolean isJSP()
True if this is JSP special cased.


setPretty

public void setPretty(boolean isPretty)
Set to true if the printer should add whitespace to 'pretty-print' the output.

Parameters:
isPretty - if true, add spaces for printing

isPretty

public boolean isPretty()
Returns true if the printer is currently pretty-printing the output.


setPrintDeclaration

public void setPrintDeclaration(boolean printDeclaration)

setStandalone

public void setStandalone(java.lang.String standalone)

setSystemId

public void setSystemId(java.lang.String id)

setIncludeContentType

public void setIncludeContentType(boolean include)
Set true if the printer should automatically add the <meta content-type> to HTML.


getIncludeContentType

public boolean getIncludeContentType()
Return true if the printer should automatically add the <meta content-type> to HTML.


setPublicId

public void setPublicId(java.lang.String id)

getPath

public Path getPath()

setLineMap

public void setLineMap(java.lang.String filename)
Creates a new line map.


getLineMap

public LineMap getLineMap()

addCdataElement

public void addCdataElement(java.lang.String elt)

print

public void print(org.w3c.dom.Node node)
           throws java.io.IOException
Throws:
java.io.IOException

printNode

public void printNode(org.w3c.dom.Node node)
               throws java.io.IOException
Throws:
java.io.IOException

startDocument

public void startDocument(org.w3c.dom.Document document)
                   throws java.io.IOException
Throws:
java.io.IOException

startDocument

public void startDocument()
                   throws java.io.IOException
Callback when the document starts printing.

Specified by:
startDocument in interface XMLWriter
Throws:
java.io.IOException

endDocument

public void endDocument()
                 throws java.io.IOException
Callback when the document completes

Specified by:
endDocument in interface XMLWriter
Throws:
java.io.IOException

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
Sets the locator.

Specified by:
setDocumentLocator in interface XMLWriter

setLocation

public void setLocation(java.lang.String filename,
                        int line,
                        int column)
Sets the current location.

Parameters:
filename - the source filename
line - the source line
column - the source column

startElement

public void startElement(java.lang.String url,
                         java.lang.String localName,
                         java.lang.String qName)
                  throws java.io.IOException
Called at the start of a new element.

Specified by:
startElement in interface XMLWriter
Parameters:
url - the namespace url
localName - the local name
qName - the qualified name
Throws:
java.io.IOException

printHeader

public void printHeader(java.lang.String top)
                 throws java.io.IOException
Prints the header, if necessary.

Parameters:
top - name of the top element
Throws:
java.io.IOException

startPrefixMapping

public void startPrefixMapping(java.lang.String prefix,
                               java.lang.String uri)
                        throws java.io.IOException
Specified by:
startPrefixMapping in interface XMLWriter
Throws:
java.io.IOException

endPrefixMapping

public void endPrefixMapping(java.lang.String prefix)
                      throws java.io.IOException
Specified by:
endPrefixMapping in interface XMLWriter
Throws:
java.io.IOException

attribute

public void attribute(java.lang.String uri,
                      java.lang.String localName,
                      java.lang.String qName,
                      java.lang.String value)
               throws java.io.IOException
Prints an attribute

Specified by:
attribute in interface XMLWriter
Parameters:
uri - namespace uri
localName - localname of the attribute
qName - qualified name of the attribute
value - value of the attribute.
Throws:
java.io.IOException

finishAttributes

public boolean finishAttributes()
                         throws java.io.IOException
Complete printing of the attributes when the open tag completes.

Throws:
java.io.IOException

endElement

public void endElement(java.lang.String uri,
                       java.lang.String localName,
                       java.lang.String qName)
                throws java.io.IOException
Prints the end tag of an element

Specified by:
endElement in interface XMLWriter
Parameters:
uri - the namespace uri of the element
localName - the localname of the element tag
qName - qualified name of the element
Throws:
java.io.IOException

processingInstruction

public void processingInstruction(java.lang.String name,
                                  java.lang.String data)
                           throws java.io.IOException
Prints a processing instruction

Specified by:
processingInstruction in interface XMLWriter
Parameters:
name - the name of the processing instruction
data - the processing instruction data
Throws:
java.io.IOException

comment

public void comment(java.lang.String data)
             throws java.io.IOException
Prints a comment

Specified by:
comment in interface XMLWriter
Parameters:
data - the comment data
Throws:
java.io.IOException

getEscapeText

public boolean getEscapeText()
Returns true if the text is currently being escaped

Specified by:
getEscapeText in interface XMLWriter

setEscapeText

public void setEscapeText(boolean isEscaped)
Sets true if the text should be escaped, else it will be printed verbatim.

Specified by:
setEscapeText in interface XMLWriter

text

public void text(java.lang.String text)
          throws java.io.IOException
Prints text. If the text is escaped, codes like < will be printed as &lt;.

Specified by:
text in interface XMLWriter
Throws:
java.io.IOException

text

public void text(char[] buffer,
                 int offset,
                 int length)
          throws java.io.IOException
Prints text. If the text is escaped, codes like < will be printed as &lt;.

Specified by:
text in interface XMLWriter
Throws:
java.io.IOException

cdata

public void cdata(java.lang.String text)
           throws java.io.IOException
Specified by:
cdata in interface XMLWriter
Throws:
java.io.IOException

cdata

public void cdata(char[] buffer,
                  int offset,
                  int length)
           throws java.io.IOException
Specified by:
cdata in interface XMLWriter
Throws:
java.io.IOException