net.sf.fikin.xml.sax
Class SAXSerializer

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by net.sf.fikin.xml.sax.SAXSerializer
All Implemented Interfaces:
SAXHandler, SAXHandlerAndResolver, org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler, org.xml.sax.ext.LexicalHandler
Direct Known Subclasses:
TemplateEngine

public class SAXSerializer
extends org.xml.sax.helpers.DefaultHandler
implements SAXHandlerAndResolver

SAX serializer

Prints received SAX events into XML document with minimum modifications.

Example:

      SAXSerializer out = new SAXSerializer( new BufferedWriter( new FileWriter( file ) ) );

      out.parse( input, XMLReaderFactory.createXMLReader() );
 
created on Aug 12, 2006

Since:
Version:
$Revision: 1.2 $
Author:
fiykov

Field Summary
protected  boolean doIndent
          whether to do newline+indent before every startElement ?
protected  int elementDepth
          Element depth.
protected  boolean inCDATA
          In CDATA section.
protected  java.lang.String indent
          what is indent string if do indenting
protected  boolean isCanonical
          Canonical output.
protected  boolean isPreviousStartElement
          indicate that previous event was startElement, used to render single tags properly
protected  boolean isXML11
          Processing XML 1.1 document.
protected  org.xml.sax.Locator locator
          Document locator.
protected  java.util.Hashtable namespaces
          parsed namespaces
protected  java.io.BufferedWriter output
          output stream where result of the transformation is written
 
Constructor Summary
SAXSerializer()
           
SAXSerializer(java.io.BufferedWriter out)
           
 
Method Summary
 void characters(char[] ch, int start, int length)
          copy the data without any processing
 void characters(java.lang.String str)
          Report an XML comment anywhere in the document.
 void comment(char[] ch, int start, int length)
          Report an XML comment anywhere in the document.
 void comment(java.lang.String str)
          Report an XML comment anywhere in the document.
 void endCDATA()
          Report the end of a CDATA section.
 void endDocument()
          flush the output writer
 void endDTD()
          Report the end of DTD declarations.
 void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
          print closing tag
 void endEntity(java.lang.String name)
          Report the end of an entity.
 void error(org.xml.sax.SAXParseException ex)
          Error.
 void fatalError(org.xml.sax.SAXParseException ex)
          Fatal error.
 org.xml.sax.Locator getDocumentLocator()
          get document locator
 boolean getDoIndent()
           
 java.lang.String getIndent()
           
 boolean getIsXML11()
          is output xml comatibility version 1.1 or 1.0
 boolean getIsXMLCanonical()
          is xml output canonical format
protected  java.util.Hashtable getNamespaces()
          get parsed namespaces
 java.io.BufferedWriter getOutput()
          get output stream
protected  java.lang.String getXMLVersion()
          Extracts the XML version from the Locator.
 void ignorableWhitespace(char[] ch, int start, int length)
          copy the data without any processing
 void ignorableWhitespace(java.lang.String str)
          Report an XML comment anywhere in the document.
protected  org.xml.sax.SAXException newLocationException(java.lang.Exception e)
          factory method for exception
protected  org.xml.sax.SAXException newLocationException(java.lang.reflect.Method method, org.xml.sax.Attributes attributes, java.lang.String msg)
          factory method for exception
protected  org.xml.sax.SAXException newLocationException(java.lang.String msg)
          factory method for exception
protected  org.xml.sax.SAXException newLocationException(java.lang.String method, org.xml.sax.Attributes attributes, java.lang.Exception e)
          factory method for exception
protected  org.xml.sax.SAXException newLocationException(java.lang.String method, org.xml.sax.Attributes attributes, java.lang.String msg)
          factory method for exception
protected  org.xml.sax.SAXException newLocationException(java.lang.String msg, java.lang.Exception e)
          factory method for exception
protected  org.xml.sax.SAXException newLocationException(java.lang.String method, int cnt, java.lang.Exception e)
          factory method for exception
protected  org.xml.sax.SAXException newLocationException(java.lang.String method, int cnt, java.lang.String msg)
          factory method for exception
protected  java.lang.String normalize(char[] ch, int offset, int length, boolean isAttValue)
          Normalizes and prints the given string.
protected  java.lang.String normalize(char c, boolean isAttValue)
          Normalizes and prints the given string.
protected  java.lang.String normalize(java.lang.String s, boolean isAttValue)
          Normalizes and prints the given string.
 void parse(org.xml.sax.InputSource input, org.xml.sax.XMLReader reader)
          process the input via the reader and serialize the events method for convenience
protected  void parse(org.xml.sax.InputSource in1, org.xml.sax.XMLReader reader, java.lang.String in2)
           
 void parse(java.lang.String systemId, org.xml.sax.XMLReader reader)
          process the input via the reader and serialize the events method for convenience
protected  void printError(java.lang.String type, org.xml.sax.SAXParseException ex)
          Prints the error message.
protected  void printIndent(int howmany)
          print indent (that many times the nestling of the element) if set so (setDoIndent(true) )
 void processingInstruction(java.lang.String target, java.lang.String data)
          copy the data
 void setDocumentLocator(org.xml.sax.Locator locator)
          Set Document Locator.
 void setDoIndent(boolean flg)
          set to false no newline+indent is to be printed before each element by default true
 void setIndent(java.lang.String val)
          set the indent string, by default it is 3 spaces
 void setIsXML11(boolean flg)
          set is xml output version 1.1 or to be threated as 1.0
 void setIsXMLCanonical(boolean flg)
          set to true if xml output to be in canonical format (i.e.
 void setOutput(java.io.BufferedWriter out)
          set the output stream
protected  java.util.Iterator sortAttributes(org.xml.sax.Attributes attrs)
          Returns a sorted list of attributes.
 void startCDATA()
          Report the start of a CDATA section.
 void startDocument()
          init the internal state
 void startDTD(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
          Report the start of DTD declarations.
 void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attributes)
          print tag
 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)
          store parsed namespace declaration
 void warning(org.xml.sax.SAXParseException ex)
          Warning.
protected  void writeClosingTag(boolean isEndElementEvent)
          writes closing tag of startElement event called from all events except startElement and if it is endElement event then prints out "/>", otherwise ">"
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endPrefixMapping, notationDecl, resolveEntity, skippedEntity, unparsedEntityDecl
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.xml.sax.EntityResolver
resolveEntity
 
Methods inherited from interface org.xml.sax.ContentHandler
endPrefixMapping, skippedEntity
 
Methods inherited from interface org.xml.sax.DTDHandler
notationDecl, unparsedEntityDecl
 

Field Detail

output

protected java.io.BufferedWriter output
output stream where result of the transformation is written


namespaces

protected java.util.Hashtable namespaces
parsed namespaces


locator

protected org.xml.sax.Locator locator
Document locator.


isCanonical

protected boolean isCanonical
Canonical output.


isXML11

protected boolean isXML11
Processing XML 1.1 document.


elementDepth

protected int elementDepth
Element depth.


inCDATA

protected boolean inCDATA
In CDATA section.


isPreviousStartElement

protected boolean isPreviousStartElement
indicate that previous event was startElement, used to render single tags properly


doIndent

protected boolean doIndent
whether to do newline+indent before every startElement ?


indent

protected java.lang.String indent
what is indent string if do indenting

Constructor Detail

SAXSerializer

public SAXSerializer()

SAXSerializer

public SAXSerializer(java.io.BufferedWriter out)
Method Detail

setOutput

public void setOutput(java.io.BufferedWriter out)
set the output stream


getOutput

public java.io.BufferedWriter getOutput()
get output stream


getNamespaces

protected java.util.Hashtable getNamespaces()
get parsed namespaces


setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
Set Document Locator.

Specified by:
setDocumentLocator in interface org.xml.sax.ContentHandler
Overrides:
setDocumentLocator in class org.xml.sax.helpers.DefaultHandler

getDocumentLocator

public org.xml.sax.Locator getDocumentLocator()
get document locator


setIsXMLCanonical

public void setIsXMLCanonical(boolean flg)
set to true if xml output to be in canonical format (i.e. no comments)


getIsXMLCanonical

public boolean getIsXMLCanonical()
is xml output canonical format


setIsXML11

public void setIsXML11(boolean flg)
set is xml output version 1.1 or to be threated as 1.0


getIsXML11

public boolean getIsXML11()
is output xml comatibility version 1.1 or 1.0


setDoIndent

public void setDoIndent(boolean flg)
set to false no newline+indent is to be printed before each element by default true


getDoIndent

public boolean getDoIndent()

setIndent

public void setIndent(java.lang.String val)
set the indent string, by default it is 3 spaces

Parameters:
val -

getIndent

public java.lang.String getIndent()

parse

public void parse(org.xml.sax.InputSource input,
                  org.xml.sax.XMLReader reader)
           throws org.xml.sax.SAXException,
                  java.io.IOException
process the input via the reader and serialize the events method for convenience

Parameters:
input - xml document
reader - used to parse the input (calling its parse(input) method)
Throws:
org.xml.sax.SAXException
java.io.IOException

parse

public void parse(java.lang.String systemId,
                  org.xml.sax.XMLReader reader)
           throws org.xml.sax.SAXException,
                  java.io.IOException
process the input via the reader and serialize the events method for convenience

Parameters:
systemId - resolving to the xml document
reader - used to parse the input (calling its parse(input) method)
Throws:
org.xml.sax.SAXException
java.io.IOException

parse

protected void parse(org.xml.sax.InputSource in1,
                     org.xml.sax.XMLReader reader,
                     java.lang.String in2)
              throws org.xml.sax.SAXException,
                     java.io.IOException
Throws:
org.xml.sax.SAXException
java.io.IOException

newLocationException

protected org.xml.sax.SAXException newLocationException(java.lang.String msg)
factory method for exception


newLocationException

protected org.xml.sax.SAXException newLocationException(java.lang.Exception e)
factory method for exception


newLocationException

protected org.xml.sax.SAXException newLocationException(java.lang.String msg,
                                                        java.lang.Exception e)
factory method for exception


newLocationException

protected org.xml.sax.SAXException newLocationException(java.lang.String method,
                                                        int cnt,
                                                        java.lang.String msg)
factory method for exception


newLocationException

protected org.xml.sax.SAXException newLocationException(java.lang.String method,
                                                        int cnt,
                                                        java.lang.Exception e)
factory method for exception


newLocationException

protected org.xml.sax.SAXException newLocationException(java.lang.reflect.Method method,
                                                        org.xml.sax.Attributes attributes,
                                                        java.lang.String msg)
factory method for exception


newLocationException

protected org.xml.sax.SAXException newLocationException(java.lang.String method,
                                                        org.xml.sax.Attributes attributes,
                                                        java.lang.String msg)
factory method for exception


newLocationException

protected org.xml.sax.SAXException newLocationException(java.lang.String method,
                                                        org.xml.sax.Attributes attributes,
                                                        java.lang.Exception e)
factory method for exception


startDocument

public void startDocument()
                   throws org.xml.sax.SAXException
init the internal state

Specified by:
startDocument in interface org.xml.sax.ContentHandler
Overrides:
startDocument in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
flush the output writer

Specified by:
endDocument in interface org.xml.sax.ContentHandler
Overrides:
endDocument in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

startPrefixMapping

public void startPrefixMapping(java.lang.String prefix,
                               java.lang.String uri)
                        throws org.xml.sax.SAXException
store parsed namespace declaration

Specified by:
startPrefixMapping in interface org.xml.sax.ContentHandler
Overrides:
startPrefixMapping in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

startElement

public void startElement(java.lang.String uri,
                         java.lang.String localName,
                         java.lang.String qName,
                         org.xml.sax.Attributes attributes)
                  throws org.xml.sax.SAXException
print tag

Specified by:
startElement in interface org.xml.sax.ContentHandler
Overrides:
startElement in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

endElement

public void endElement(java.lang.String uri,
                       java.lang.String localName,
                       java.lang.String qName)
                throws org.xml.sax.SAXException
print closing tag

Specified by:
endElement in interface org.xml.sax.ContentHandler
Overrides:
endElement in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

characters

public void characters(char[] ch,
                       int start,
                       int length)
                throws org.xml.sax.SAXException
copy the data without any processing

Specified by:
characters in interface org.xml.sax.ContentHandler
Overrides:
characters in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

characters

public void characters(java.lang.String str)
                throws org.xml.sax.SAXException
Report an XML comment anywhere in the document.

Specified by:
characters in interface SAXHandler
Throws:
org.xml.sax.SAXException

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int length)
                         throws org.xml.sax.SAXException
copy the data without any processing

Specified by:
ignorableWhitespace in interface org.xml.sax.ContentHandler
Overrides:
ignorableWhitespace in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

ignorableWhitespace

public void ignorableWhitespace(java.lang.String str)
                         throws org.xml.sax.SAXException
Report an XML comment anywhere in the document.

Specified by:
ignorableWhitespace in interface SAXHandler
Throws:
org.xml.sax.SAXException

processingInstruction

public void processingInstruction(java.lang.String target,
                                  java.lang.String data)
                           throws org.xml.sax.SAXException
copy the data

Specified by:
processingInstruction in interface org.xml.sax.ContentHandler
Overrides:
processingInstruction in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

startDTD

public void startDTD(java.lang.String name,
                     java.lang.String publicId,
                     java.lang.String systemId)
              throws org.xml.sax.SAXException
Report the start of DTD declarations.

Specified by:
startDTD in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException

endDTD

public void endDTD()
            throws org.xml.sax.SAXException
Report the end of DTD declarations.

Specified by:
endDTD in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException

startEntity

public void startEntity(java.lang.String name)
                 throws org.xml.sax.SAXException
Report the beginning of some internal and external XML entities.

Specified by:
startEntity in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException

endEntity

public void endEntity(java.lang.String name)
               throws org.xml.sax.SAXException
Report the end of an entity.

Specified by:
endEntity in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException

startCDATA

public void startCDATA()
                throws org.xml.sax.SAXException
Report the start of a CDATA section.

Specified by:
startCDATA in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException

endCDATA

public void endCDATA()
              throws org.xml.sax.SAXException
Report the end of a CDATA section.

Specified by:
endCDATA in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException

comment

public void comment(char[] ch,
                    int start,
                    int length)
             throws org.xml.sax.SAXException
Report an XML comment anywhere in the document.

Specified by:
comment in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException

comment

public void comment(java.lang.String str)
             throws org.xml.sax.SAXException
Report an XML comment anywhere in the document.

Specified by:
comment in interface SAXHandler
Throws:
org.xml.sax.SAXException

writeClosingTag

protected void writeClosingTag(boolean isEndElementEvent)
                        throws java.io.IOException
writes closing tag of startElement event called from all events except startElement and if it is endElement event then prints out "/>", otherwise ">"

Parameters:
isEndElementEvent - is true only when it is called from endElement event method, all other cases it is false
Throws:
java.io.IOException

sortAttributes

protected java.util.Iterator sortAttributes(org.xml.sax.Attributes attrs)
Returns a sorted list of attributes.


normalize

protected java.lang.String normalize(java.lang.String s,
                                     boolean isAttValue)
Normalizes and prints the given string.

Parameters:
s - is the string to normalize
isAttValue - is true if the string is an attribute value
Returns:
xml normized string

normalize

protected java.lang.String normalize(char[] ch,
                                     int offset,
                                     int length,
                                     boolean isAttValue)
Normalizes and prints the given string.

Parameters:
isAttValue - is true if the string is an attribute value
Returns:
xml normized string

normalize

protected java.lang.String normalize(char c,
                                     boolean isAttValue)
Normalizes and prints the given string.

Parameters:
c - is the character to normalize
isAttValue - is true if the string is an attribute value
Returns:
xml normized string (same char or its xml normalized format)

getXMLVersion

protected java.lang.String getXMLVersion()
Extracts the XML version from the Locator.


warning

public void warning(org.xml.sax.SAXParseException ex)
             throws org.xml.sax.SAXException
Warning.

Specified by:
warning in interface org.xml.sax.ErrorHandler
Overrides:
warning in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

error

public void error(org.xml.sax.SAXParseException ex)
           throws org.xml.sax.SAXException
Error.

Specified by:
error in interface org.xml.sax.ErrorHandler
Overrides:
error in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

fatalError

public void fatalError(org.xml.sax.SAXParseException ex)
                throws org.xml.sax.SAXException
Fatal error.

Specified by:
fatalError in interface org.xml.sax.ErrorHandler
Overrides:
fatalError in class org.xml.sax.helpers.DefaultHandler
Throws:
org.xml.sax.SAXException

printError

protected void printError(java.lang.String type,
                          org.xml.sax.SAXParseException ex)
Prints the error message.


printIndent

protected void printIndent(int howmany)
                    throws java.io.IOException
print indent (that many times the nestling of the element) if set so (setDoIndent(true) )

Throws:
java.io.IOException


Copyright © 2003-2007 Nikolay Fiykov. All Rights Reserved.