org.foray.xml
Class FOrayPretty

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by org.xml.sax.ext.DefaultHandler2
          extended by org.foray.xml.FOrayPretty
All Implemented Interfaces:
ContentHandler, DTDHandler, EntityResolver, ErrorHandler, DeclHandler, EntityResolver2, LexicalHandler

public class FOrayPretty
extends DefaultHandler2

Command-line application that reads an XML file and writes a pretty-printed version of it.


Field Summary
static byte STATUS_COMMAND_LINE_ERROR
          Command-line status constant indicating that the command line itself was not properly formed.
static byte STATUS_FILE_NOT_FOUND
          Command-line return status constant indicating that a file was not found.
static byte STATUS_PARSING_ERROR
          Command-line return status constant indicating that there was an error parsing the input file.
 
Constructor Summary
FOrayPretty(InputSource input, OutputStream output, String catalog)
          Constructor.
 
Method Summary
 void characters(char[] buffer, int offset, int length)
           
 void comment(char[] buffer, int offset, int length)
           
 XMLReader createParser()
          Creates a SAX parser.
 void elementDecl(String name, String model)
           
 void endDocument()
           
 void endDTD()
           
 void endElement(String uri, String local, String qName)
           
 void endEntity(String name)
           
 org.apache.commons.logging.Log getLogger()
          Returns the logger.
 void ignorableWhitespace(char[] buffer, int offset, int length)
           
static void main(String[] args)
          Command-line interface for pretty-printing an XML document.
 void processingInstruction(String target, String data)
           
 void setBreakPCDATA(boolean breakPCDATA)
          Set this configuration parameter to true if whitespace within PCDATA can be interchanged.
 void setDesiredLineLength(int desiredLineLength)
          Use this configuration parameter to set the desired maximum line length, in characters, for the pretty-printed document.
 void setDocumentLocator(Locator locator)
           
 void setIndent(int indent)
          Use this configuration parameter to set the number of spaces that each successive block level of the document should be indented.
 void setIndentBlocksInsideMixedContent(boolean indent)
          Set this configuration parameter to true if blocks that are inside of mixed content should be indented.
 void setLineTerminator(String lineTerminator)
          Use this configuration parameter to set the text that should be used to delimit lines in the pretty-printed document.
 void setOutputEncoding(String outputEncoding)
          Use this configuration parameter to set the output encoding for the pretty-printed document.
 void setPostRootText(String postRootText)
          Use this configuration parameter to control the text that is written right after the close of the root element.
 void setPreRootText(String preRootText)
          Use this configuration parameter to control the text that is written right before the root element.
 void setXmlDeclaration(String xmlDeclaration)
          Use this configuration parameter to set the value of the XML Declaration at the beginning of the pretty-printed document.
 void start()
          Intantiates parser and starts parsing of input.
 void startDocument()
           
 void startDTD(String name, String publicId, String systemId)
           
 void startElement(String uri, String local, String qName, Attributes attributes)
           
 void startEntity(String name)
           
 
Methods inherited from class org.xml.sax.ext.DefaultHandler2
attributeDecl, endCDATA, externalEntityDecl, getExternalSubset, internalEntityDecl, resolveEntity, resolveEntity, startCDATA
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endPrefixMapping, error, fatalError, notationDecl, skippedEntity, startPrefixMapping, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STATUS_COMMAND_LINE_ERROR

public static final byte STATUS_COMMAND_LINE_ERROR
Command-line status constant indicating that the command line itself was not properly formed.

See Also:
Constant Field Values

STATUS_FILE_NOT_FOUND

public static final byte STATUS_FILE_NOT_FOUND
Command-line return status constant indicating that a file was not found.

See Also:
Constant Field Values

STATUS_PARSING_ERROR

public static final byte STATUS_PARSING_ERROR
Command-line return status constant indicating that there was an error parsing the input file.

See Also:
Constant Field Values
Constructor Detail

FOrayPretty

public FOrayPretty(InputSource input,
                   OutputStream output,
                   String catalog)
Constructor.

Parameters:
input - The input source encapsulating the document to be pretty-printed.
output - The output stream to which the pretty-printed document should be sent.
catalog - The location of a catalog file that should be used to find the DTD for this document. This may be null.
Method Detail

start

public void start()
           throws IOException,
                  SAXException,
                  ParserConfigurationException
Intantiates parser and starts parsing of input.

Throws:
IOException - For I/O Errors.
SAXException - For parsing errors.
ParserConfigurationException - For errors configuring parser.

createParser

public XMLReader createParser()
                       throws SAXException,
                              ParserConfigurationException
Creates a SAX parser.

Returns:
The created SAX parser.
Throws:
SAXException - For error creating parser.
ParserConfigurationException - For error configuring parser.

setDocumentLocator

public void setDocumentLocator(Locator locator)
Specified by:
setDocumentLocator in interface ContentHandler
Overrides:
setDocumentLocator in class org.xml.sax.helpers.DefaultHandler

startDocument

public void startDocument()
Specified by:
startDocument in interface ContentHandler
Overrides:
startDocument in class org.xml.sax.helpers.DefaultHandler

endDocument

public void endDocument()
Specified by:
endDocument in interface ContentHandler
Overrides:
endDocument in class org.xml.sax.helpers.DefaultHandler

startElement

public void startElement(String uri,
                         String local,
                         String qName,
                         Attributes attributes)
Specified by:
startElement in interface ContentHandler
Overrides:
startElement in class org.xml.sax.helpers.DefaultHandler

endElement

public void endElement(String uri,
                       String local,
                       String qName)
Specified by:
endElement in interface ContentHandler
Overrides:
endElement in class org.xml.sax.helpers.DefaultHandler

characters

public void characters(char[] buffer,
                       int offset,
                       int length)
Specified by:
characters in interface ContentHandler
Overrides:
characters in class org.xml.sax.helpers.DefaultHandler

processingInstruction

public void processingInstruction(String target,
                                  String data)
Specified by:
processingInstruction in interface ContentHandler
Overrides:
processingInstruction in class org.xml.sax.helpers.DefaultHandler

ignorableWhitespace

public void ignorableWhitespace(char[] buffer,
                                int offset,
                                int length)
Specified by:
ignorableWhitespace in interface ContentHandler
Overrides:
ignorableWhitespace in class org.xml.sax.helpers.DefaultHandler

comment

public void comment(char[] buffer,
                    int offset,
                    int length)
Specified by:
comment in interface LexicalHandler
Overrides:
comment in class DefaultHandler2

startDTD

public void startDTD(String name,
                     String publicId,
                     String systemId)
Specified by:
startDTD in interface LexicalHandler
Overrides:
startDTD in class DefaultHandler2

endDTD

public void endDTD()
Specified by:
endDTD in interface LexicalHandler
Overrides:
endDTD in class DefaultHandler2

startEntity

public void startEntity(String name)
Specified by:
startEntity in interface LexicalHandler
Overrides:
startEntity in class DefaultHandler2

endEntity

public void endEntity(String name)
Specified by:
endEntity in interface LexicalHandler
Overrides:
endEntity in class DefaultHandler2

elementDecl

public void elementDecl(String name,
                        String model)
Specified by:
elementDecl in interface DeclHandler
Overrides:
elementDecl in class DefaultHandler2

getLogger

public org.apache.commons.logging.Log getLogger()
Returns the logger.

Returns:
The logger.

setBreakPCDATA

public void setBreakPCDATA(boolean breakPCDATA)
Set this configuration parameter to true if whitespace within PCDATA can be interchanged. Specifically, if this parameter is set, tabs will be normalized to spaces, duplicate spaces will be removed, and spaces and new-lines will be treated as interchangeable. The default value is true.

Parameters:
breakPCDATA - The new value.

setXmlDeclaration

public void setXmlDeclaration(String xmlDeclaration)
Use this configuration parameter to set the value of the XML Declaration at the beginning of the pretty-printed document. The default value is <?xml version="1.0" encoding="UTF-8">

Parameters:
xmlDeclaration - The new value.

setLineTerminator

public void setLineTerminator(String lineTerminator)
Use this configuration parameter to set the text that should be used to delimit lines in the pretty-printed document. The default is platform-specific. On Unix, for example, it is "\n". On Windows, it is "\r\n".

Parameters:
lineTerminator - The new value.

setOutputEncoding

public void setOutputEncoding(String outputEncoding)
Use this configuration parameter to set the output encoding for the pretty-printed document. The default value is "UTF-8".

Parameters:
outputEncoding - The new value.

setPreRootText

public void setPreRootText(String preRootText)
Use this configuration parameter to control the text that is written right before the root element. Any text between the DTD and the root element in the source document is considered to be insignificant and is therefore stripped out. The default value is null.

Parameters:
preRootText - The new value.

setPostRootText

public void setPostRootText(String postRootText)
Use this configuration parameter to control the text that is written right after the close of the root element. Any text after the root element in the source document is considered to be insignificant and is therefore stripped out. The default value is "\n" (one line-feed).

Parameters:
postRootText - The new value.

setIndent

public void setIndent(int indent)
Use this configuration parameter to set the number of spaces that each successive block level of the document should be indented. The default is 2.

Parameters:
indent - The new value.

setDesiredLineLength

public void setDesiredLineLength(int desiredLineLength)
Use this configuration parameter to set the desired maximum line length, in characters, for the pretty-printed document. The default is 80.

Parameters:
desiredLineLength - The new value.

setIndentBlocksInsideMixedContent

public void setIndentBlocksInsideMixedContent(boolean indent)
Set this configuration parameter to true if blocks that are inside of mixed content should be indented. The default is "false".

Parameters:
indent - The new value.

main

public static void main(String[] args)
Command-line interface for pretty-printing an XML document.

Design Note: We use files instead of URLs here to more easily integrate with desktop tools such as XML editors, which typically know how to pass parameters for local files, but not how to convert them to URLs. If URLs are needed, additional flags probably need to be added to the command-line interface to designate that.

Return status is one of:

Parameters:
args - command-line arguments. Argument 1 is the location of the input file. Argument 2 is the location of the output file. Argument 3 is an optional location of an OASIS-compliant catalog file that can be used to locate local DTDs.


Copyright © 2017. All rights reserved.