UTF-X, Unit Testing Framework - Xslt

utfx.util
Class CanonicalForm

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by utfx.util.CanonicalForm
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler, org.xml.sax.ext.LexicalHandler

public class CanonicalForm
extends org.xml.sax.helpers.DefaultHandler
implements org.xml.sax.ext.LexicalHandler

Transforms an XML document into its canonical form according to the W3C Canonical XML specification. This class also has a mode which removes whitespace outside of an element and whitespace in an element if it is only whitespace.

Copyright © 2004 - University of Southern Queensland.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License v2 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

$Source: /cvs/utf-x/framework/src/java/utfx/util/CanonicalForm.java,v $

Version:
$Revision: 1.2.2.2 $ $Date: 2006/08/23 13:57:42 $ $Name: release_0_0_8_alpha $
Author:
Jacek Radajewski, Oliver Lucido (Lulu)

Field Summary
protected  boolean accumulating
          Set true when character data is being accumulated into a buffer.
protected  java.lang.StringBuffer characterData
          Buffer where character data is accumulated
protected static java.lang.String DEFAULT_PARSER_NAME
          Default parser name.
protected  int elementDepth
          Element depth.
protected  java.io.PrintWriter fOut
          Print writer.
protected  boolean inEntity
          whether we are within an entity
protected  org.xml.sax.XMLReader parser
          SAX Parser.
protected  boolean trailingNewLine
          True to put trailing new lines, false for preceding new lines.
protected  boolean useW3CSpec
          Set true to use W3C specification.
 
Constructor Summary
CanonicalForm()
          Default constructor.
CanonicalForm(boolean useW3CSpec)
          Constructs a CanonicalForm.
 
Method Summary
 void characters(char[] ch, int start, int length)
          Characters.
 void comment(char[] ch, int start, int length)
          Comment.
 void endCDATA()
          End CDATA section.
 void endDocument()
          End document.
 void endDTD()
          End DTD.
 void endElement(java.lang.String uri, java.lang.String local, java.lang.String raw)
          End element.
 void endEntity(java.lang.String name)
          End entity.
 void error(org.xml.sax.SAXParseException ex)
          Error.
 void fatalError(org.xml.sax.SAXParseException ex)
          Fatal error.
 void ignorableWhitespace(char[] ch, int start, int length)
          Ignorable whitespace.
protected  void normalizeAndPrint(char c)
          Normalizes and print the given character.
protected  void normalizeAndPrint(char[] ch, int offset, int length)
          Normalizes and prints the given array of characters.
protected  void normalizeAndPrint(java.lang.String s)
          Normalizes and prints the given string.
protected  void normalizeAndPrintNewline(char c)
          Normalizes newline characters and prints the given character
protected  void normalizeAndPrintNewline(java.lang.String s)
          Normalizes newline characters and prints the given string
protected  void outputCharacterData()
          Outputs any character data that was being accumulated in the characters method.
protected  void printError(java.lang.String type, org.xml.sax.SAXParseException ex)
          Prints the error message.
 void processingInstruction(java.lang.String target, java.lang.String data)
          Processing instruction.
protected  void setDefaultFeatures()
          Set the XML parser features for Canonical XML.
 void setOutputStream(java.io.OutputStream stream)
          Sets the output stream for printing.
protected  void setParserAndHandlers(org.xml.sax.helpers.DefaultHandler handler)
          set the various handlers to the handler passed in
protected  org.xml.sax.Attributes sortAttributes(org.xml.sax.Attributes attrs)
          Returns a sorted list of attributes.
 void startCDATA()
          Start CDATA section.
 void startDocument()
          Start document.
 void startDTD(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
          Start DTD.
 void startElement(java.lang.String uri, java.lang.String local, java.lang.String raw, org.xml.sax.Attributes attrs)
          Default Implementation of start element.
 void startEntity(java.lang.String name)
          Start entity.
 void transform(java.io.InputStream is, java.io.OutputStream os)
          Transform an XML input stream into its canonical XML form.
 void warning(org.xml.sax.SAXParseException ex)
          Warning.
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endPrefixMapping, notationDecl, resolveEntity, setDocumentLocator, skippedEntity, startPrefixMapping, unparsedEntityDecl
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PARSER_NAME

protected static final java.lang.String DEFAULT_PARSER_NAME
Default parser name.

See Also:
Constant Field Values

fOut

protected java.io.PrintWriter fOut
Print writer.


elementDepth

protected int elementDepth
Element depth.


parser

protected org.xml.sax.XMLReader parser
SAX Parser.


useW3CSpec

protected boolean useW3CSpec
Set true to use W3C specification.


trailingNewLine

protected boolean trailingNewLine
True to put trailing new lines, false for preceding new lines. This applies to comments and processing instructions when using W3C specifications.


accumulating

protected boolean accumulating
Set true when character data is being accumulated into a buffer. This is necessary since SAX does not guarantee that contiguous text is passed in a single characters() method call.


inEntity

protected boolean inEntity
whether we are within an entity


characterData

protected java.lang.StringBuffer characterData
Buffer where character data is accumulated

Constructor Detail

CanonicalForm

public CanonicalForm()
Default constructor.


CanonicalForm

public CanonicalForm(boolean useW3CSpec)
Constructs a CanonicalForm.

Parameters:
useW3CSpec - true to use the W3C Canonical XML spec, set to false so that whitespace between elements is considered insignificant. Default is false.
Method Detail

setParserAndHandlers

protected void setParserAndHandlers(org.xml.sax.helpers.DefaultHandler handler)
                             throws java.lang.Exception
set the various handlers to the handler passed in

Throws:
java.lang.Exception

setDefaultFeatures

protected void setDefaultFeatures()
                           throws org.xml.sax.SAXException
Set the XML parser features for Canonical XML.

Throws:
org.xml.sax.SAXException

transform

public void transform(java.io.InputStream is,
                      java.io.OutputStream os)
               throws java.io.IOException,
                      org.xml.sax.SAXException
Transform an XML input stream into its canonical XML form.

Parameters:
is - input stream representing an XML document.
os - output stream which contains the canonical form
Throws:
java.io.IOException
org.xml.sax.SAXException

setOutputStream

public void setOutputStream(java.io.OutputStream stream)
Sets the output stream for printing.

Parameters:
stream - an OutputStream to which the output will be printed.

startDocument

public void startDocument()
Start document.

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

endDocument

public void endDocument()
End document.

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

processingInstruction

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

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

startElement

public void startElement(java.lang.String uri,
                         java.lang.String local,
                         java.lang.String raw,
                         org.xml.sax.Attributes attrs)
                  throws org.xml.sax.SAXException
Default Implementation of start element.

Specified by:
startElement in interface org.xml.sax.ContentHandler
Overrides:
startElement in class org.xml.sax.helpers.DefaultHandler
Parameters:
uri - a String value
local - a String value
raw - a String value
attrs - an Attributes value
Throws:
org.xml.sax.SAXException - if an error occurs

characters

public void characters(char[] ch,
                       int start,
                       int length)
                throws org.xml.sax.SAXException
Characters.

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

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int length)
                         throws org.xml.sax.SAXException
Ignorable whitespace.

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

endElement

public void endElement(java.lang.String uri,
                       java.lang.String local,
                       java.lang.String raw)
                throws org.xml.sax.SAXException
End element.

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

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

startDTD

public void startDTD(java.lang.String name,
                     java.lang.String publicId,
                     java.lang.String systemId)
              throws org.xml.sax.SAXException
Start DTD.

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

endDTD

public void endDTD()
            throws org.xml.sax.SAXException
End DTD.

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
Start entity.

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
End 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
Start 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
End 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
Comment.

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

sortAttributes

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


normalizeAndPrint

protected void normalizeAndPrint(java.lang.String s)
Normalizes and prints the given string.


normalizeAndPrint

protected void normalizeAndPrint(char[] ch,
                                 int offset,
                                 int length)
Normalizes and prints the given array of characters.


normalizeAndPrint

protected void normalizeAndPrint(char c)
Normalizes and print the given character.


normalizeAndPrintNewline

protected void normalizeAndPrintNewline(java.lang.String s)
Normalizes newline characters and prints the given string

Parameters:
s -

normalizeAndPrintNewline

protected void normalizeAndPrintNewline(char c)
Normalizes newline characters and prints the given character

Parameters:
c -

printError

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


outputCharacterData

protected void outputCharacterData()
Outputs any character data that was being accumulated in the characters method. This should be called for every SAX event or the data may not be output to its proper location.


UTF-X, Unit Testing Framework - Xslt