Expresso 5-6

com.jcorporate.expresso.ext.report
Class XMLPrinter

java.lang.Object
  extended bycom.jcorporate.expresso.ext.report.XMLPrinter
All Implemented Interfaces:
DOMWriter, org.xml.sax.ErrorHandler
Direct Known Subclasses:
DocBookPrinter

public class XMLPrinter
extends Object
implements org.xml.sax.ErrorHandler, DOMWriter

XMLPrinter is a simple (DOM) parser that outputs XML to a Writer stream.

Author:
David Lloyd

Field Summary
protected  int _column
          The current indention level.
protected  String _indent
          The indention prefix string.
protected  char _indentChar
          The character to use for indenting.
protected  int _indentLength
          The virtual area occupied by an indention.
protected  String _newline
          The character sequence for a newline.
protected  boolean _omitXmlDecl
          Set to keep the <?
protected  Writer _out
          The writer that will contain the XML written.
static String DEFAULT_INDENT
           
protected static org.apache.log4j.Logger log
           
static OutputStreamWriter SYSTEM_OUT
           
 
Constructor Summary
XMLPrinter()
           
 
Method Summary
 void closeWriter()
          Closes the output writer ignoring any errors recieved.
protected  String convertNewline(String text)
          Convert newlines to what we want.
protected static void copyFile(File in, File out)
           
 void error(org.xml.sax.SAXParseException ex)
          Issue an error
 void fatalError(org.xml.sax.SAXParseException ex)
          Fatal error.
protected  String getIndent(int col)
          Get the normal indentation for a level.
 String getRequiredClass()
          Retrieve a class that must exist in the classpath for this to work.
 Writer getWriter()
          Get the output writer.
static void main(String[] args)
           
static String nodeToString(org.w3c.dom.Node node)
          Return the given node as a string.
 void outputDocument(org.w3c.dom.Node node)
          Output the document whose root element is the specified node.
 void print(String string)
          Prints the string to the output.
 void print(String string, String indent)
          Prints the string to the output at an indentation.
 void println(String string)
          Prints the string to the output following it with a newline.
 void println(String string, String indent)
          Prints the string to the output at an indentation following it with a newline.
protected  void printTree(org.w3c.dom.Node node)
          Output the node (and children) at the current indentation level.
protected  void printTree(org.w3c.dom.Node node, int col)
          Output the node (and children) at the specified indentation level.
static int run(String[] args)
           
static int run(String[] args, XMLPrinter printer)
           
 void saveDocument(OutputStream os, org.w3c.dom.Document document)
          Override to actually save a DOM document to the output stream via whatever method you desire
 void setFile(File file)
          Creates an output writer that will create the file specified and any parent directories needed to do so.
 void setFile(String filename)
          Creates an output writer that will create the file specified and any parent directories needed to do so.
 void setIndent(char indentChar, int size)
          Set the indent semantics.
 void setIndentSize(int indentSize)
          Set the indentation size to the number of characters specified using the current indent character.
 void setOmitXmlDecl(boolean b)
          Set whether the xml declaration should be output at the start of a document.
 void setWriter(Writer out)
          Set the output writer.
static String toXML(String html)
          Parse an string and convert it to xml style
static String toXML(String html, boolean escapeAll)
          Parse an string and convert it to xml style
 void warning(org.xml.sax.SAXParseException ex)
          Issue a warning on parsing errors
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static org.apache.log4j.Logger log

SYSTEM_OUT

public static final OutputStreamWriter SYSTEM_OUT

DEFAULT_INDENT

public static final String DEFAULT_INDENT
See Also:
Constant Field Values

_out

protected Writer _out
The writer that will contain the XML written.


_indent

protected String _indent
The indention prefix string.


_indentChar

protected char _indentChar
The character to use for indenting.


_indentLength

protected int _indentLength
The virtual area occupied by an indention.


_column

protected int _column
The current indention level.


_omitXmlDecl

protected boolean _omitXmlDecl
Set to keep the <?xml ... header from printing.


_newline

protected String _newline
The character sequence for a newline.

Constructor Detail

XMLPrinter

public XMLPrinter()
Method Detail

saveDocument

public void saveDocument(OutputStream os,
                         org.w3c.dom.Document document)
                  throws ExpressoRuntimeException
Override to actually save a DOM document to the output stream via whatever method you desire

Specified by:
saveDocument in interface DOMWriter
Parameters:
os - The output stream to save to
document - the DOM document representing the config.
Throws:
ExpressoRuntimeException - if there's an error saving the file.

getRequiredClass

public String getRequiredClass()
Retrieve a class that must exist in the classpath for this to work. Used as a sanity check to make sure the appropriate jars are installed.

Specified by:
getRequiredClass in interface DOMWriter
Returns:
java.lang.String a name of a class

outputDocument

public void outputDocument(org.w3c.dom.Node node)
                    throws IOException
Output the document whose root element is the specified node.

Throws:
IOException

setWriter

public void setWriter(Writer out)
Set the output writer.


getWriter

public Writer getWriter()
Get the output writer.


setIndentSize

public void setIndentSize(int indentSize)
Set the indentation size to the number of characters specified using the current indent character.


setIndent

public void setIndent(char indentChar,
                      int size)
Set the indent semantics.

Parameters:
indentChar - The character to use for indentions.
size - The number of characters to use for each indention level.

setOmitXmlDecl

public void setOmitXmlDecl(boolean b)
Set whether the xml declaration should be output at the start of a document.


closeWriter

public void closeWriter()
Closes the output writer ignoring any errors recieved. You are encouraged to close the writer yourself to recieve any errors encountered.


setFile

public void setFile(File file)
             throws IOException
Creates an output writer that will create the file specified and any parent directories needed to do so.

Throws:
IOException

setFile

public void setFile(String filename)
             throws IOException
Creates an output writer that will create the file specified and any parent directories needed to do so.

Throws:
IOException

nodeToString

public static String nodeToString(org.w3c.dom.Node node)
Return the given node as a string. If any error occurs in processing (likely an io exception from outputDocument()), null will be returned.


toXML

public static String toXML(String html)
Parse an string and convert it to xml style

Parameters:
html - The string to be parsed
Returns:
The resulting xml string

toXML

public static String toXML(String html,
                           boolean escapeAll)
Parse an string and convert it to xml style

Parameters:
html - The string to be parsed
escapeAll - If false, the semicolon, apostrophe, and quote are left alone (useful for text nodes).
Returns:
The resulting xml string

print

public void print(String string,
                  String indent)
           throws IOException
Prints the string to the output at an indentation.

Parameters:
string - The string to print.
indent - The indentation to prefix the string.
Throws:
IOException

println

public void println(String string,
                    String indent)
             throws IOException
Prints the string to the output at an indentation following it with a newline.

Parameters:
string - The string to print.
indent - The indentation to prefix the string.
Throws:
IOException

print

public void print(String string)
           throws IOException
Prints the string to the output.

Parameters:
string - The string to print.
Throws:
IOException

println

public void println(String string)
             throws IOException
Prints the string to the output following it with a newline.

Parameters:
string - The string to print.
Throws:
IOException

getIndent

protected String getIndent(int col)
Get the normal indentation for a level.

Parameters:
col - The level of indentation.

printTree

protected void printTree(org.w3c.dom.Node node,
                         int col)
                  throws IOException
Output the node (and children) at the specified indentation level.

Throws:
IOException

printTree

protected void printTree(org.w3c.dom.Node node)
                  throws IOException
Output the node (and children) at the current indentation level.

Throws:
IOException

convertNewline

protected String convertNewline(String text)
Convert newlines to what we want.


run

public static int run(String[] args,
                      XMLPrinter printer)

run

public static int run(String[] args)

main

public static void main(String[] args)

copyFile

protected static void copyFile(File in,
                               File out)
                        throws IOException
Throws:
IOException

warning

public void warning(org.xml.sax.SAXParseException ex)
Issue a warning on parsing errors

Specified by:
warning in interface org.xml.sax.ErrorHandler
Parameters:
ex - A Sax Parse Exception event

error

public void error(org.xml.sax.SAXParseException ex)
Issue an error

Specified by:
error in interface org.xml.sax.ErrorHandler
Parameters:
ex - A Sax Parse Exception event

fatalError

public void fatalError(org.xml.sax.SAXParseException ex)
                throws org.xml.sax.SAXException
Fatal error. Used Internally for parsing only

Specified by:
fatalError in interface org.xml.sax.ErrorHandler
Parameters:
ex - A Sax Parse Exception event
Throws:
org.xml.sax.SAXException - after logging the Parsing Exception

Expresso 5-6

Please see www.jcorporate.com for information about new Expresso releases.