com.caucho.xml
Class XmlPrinter

java.lang.Object
  |
  +--com.caucho.xml.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
 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.
 com.caucho.java.LineMap getLineMap()
           
 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(Node node)
           
 void printHtml(Node node)
          Prints the node and children as HTML
static void printHtml(java.io.OutputStream os, Node node)
          Prints the node as HTML
 void printNode(Node node)
           
 void printPrettyXml(Node node)
          Prints the node and children as XML, automatically indending
static void printPrettyXml(java.io.OutputStream os, Node node)
          Prints the node as indented HTML
 java.lang.String printString(Node node)
          Prints the node as XML to a string.
 void printXml(Node node)
          Prints the node as XML.
static void printXml(java.io.OutputStream os, Node node)
          Prints the node as XML.
 void processingInstruction(java.lang.String name, java.lang.String data)
          Prints a processing instruction
 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 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(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 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.

printXml

public static void printXml(java.io.OutputStream os,
                            Node node)
                     throws java.io.IOException
Prints the node as XML.
Parameters:
os - destination output stream
node - source DOM node

printHtml

public static void printHtml(java.io.OutputStream os,
                             Node node)
                      throws java.io.IOException
Prints the node as HTML
Parameters:
os - destination output stream
node - source DOM node

printPrettyXml

public static void printPrettyXml(java.io.OutputStream os,
                                  Node node)
                           throws java.io.IOException
Prints the node as indented HTML
Parameters:
os - destination output stream
node - source DOM node

printXml

public void printXml(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

printHtml

public void printHtml(Node node)
               throws java.io.IOException
Prints the node and children as HTML
Parameters:
node - the top node to print

printPrettyXml

public void printPrettyXml(Node node)
                    throws java.io.IOException
Prints the node and children as XML, automatically indending
Parameters:
node - the top node to print

printString

public java.lang.String printString(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.

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)

setPublicId

public void setPublicId(java.lang.String id)

setLineMap

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

getLineMap

public com.caucho.java.LineMap getLineMap()

addCdataElement

public void addCdataElement(java.lang.String elt)

printNode

public void printNode(Node node)
               throws java.io.IOException

print

public void print(Node node)
           throws java.io.IOException

startDocument

public void startDocument(Document document)
                   throws java.io.IOException

startDocument

public void startDocument()
                   throws java.io.IOException
Callback when the document starts printing.
Specified by:
startDocument in interface XMLWriter

endDocument

public void endDocument()
                 throws java.io.IOException
Callback when the document completes
Specified by:
endDocument in interface XMLWriter

setLocation

public void setLocation(java.lang.String filename,
                        int line,
                        int column)
Sets the current location.
Specified by:
setLocation in interface XMLWriter
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

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.

finishAttributes

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

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

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

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

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

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

cdata

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

cdata

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