edu.iastate.utils.log
Class DataUnformatFilter

java.lang.Object
  extended byorg.xml.sax.helpers.XMLFilterImpl
      extended byedu.iastate.utils.log.XMLFilterBase
          extended byedu.iastate.utils.log.DataUnformatFilter
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler, org.xml.sax.XMLFilter, org.xml.sax.XMLReader

public class DataUnformatFilter
extends XMLFilterBase

Filter for removing formatting from data- or field-oriented XML. Code and comments adapted from DataWriter-0.2, written by David Megginson and released into the public domain, without warranty.

This filter removes leading and trailing whitespace from field-oriented XML without mixed content. Note that this class will likely not yield appropriate results for document-oriented XML like XHTML pages, which mix character data and elements together.

See Also:
DataFormatFilter

Field Summary
private static java.lang.Object SEEN_DATA
           
private static java.lang.Object SEEN_ELEMENT
           
private static java.lang.Object SEEN_NOTHING
           
private  java.lang.Object state
           
private  java.util.Stack stateStack
           
private  java.lang.StringBuffer whitespace
           
 
Fields inherited from class edu.iastate.utils.log.XMLFilterBase
EMPTY_ATTS
 
Fields inherited from class org.xml.sax.helpers.XMLFilterImpl
 
Constructor Summary
DataUnformatFilter()
          Create a new filter.
DataUnformatFilter(org.xml.sax.XMLReader xmlreader)
          Create a new filter.
 
Method Summary
 void characters(char[] ch, int start, int length)
          Filter a character data event.
protected  void clearWhitespace()
          Discards saved whitespace.
protected  void emitWhitespace()
          Passes saved whitespace down the filter chain.
 void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
          Filter an end element event.
 void ignorableWhitespace(char[] ch, int start, int length)
          Filter an ignorable whitespace event.
private  boolean isXMLWhitespace(char c)
          Returns true if character is XML whitespace.
 void processingInstruction(java.lang.String target, java.lang.String data)
          Filter a processing instruction event.
 void reset()
          Reset the filter so that it can be reused.
protected  void saveWhitespace(char[] ch, int start, int length)
          Saves trailing whitespace.
 void startDocument()
          Filter a start document event.
 void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts)
          Filter a start element event.
 
Methods inherited from class edu.iastate.utils.log.XMLFilterBase
characters, dataElement, dataElement, dataElement, emptyElement, emptyElement, emptyElement, endElement, endElement, startElement, startElement
 
Methods inherited from class org.xml.sax.helpers.XMLFilterImpl
endDocument, endPrefixMapping, error, fatalError, getContentHandler, getDTDHandler, getEntityResolver, getErrorHandler, getFeature, getParent, getProperty, notationDecl, parse, parse, resolveEntity, setContentHandler, setDocumentLocator, setDTDHandler, setEntityResolver, setErrorHandler, setFeature, setParent, setProperty, skippedEntity, startPrefixMapping, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SEEN_NOTHING

private static final java.lang.Object SEEN_NOTHING

SEEN_ELEMENT

private static final java.lang.Object SEEN_ELEMENT

SEEN_DATA

private static final java.lang.Object SEEN_DATA

state

private java.lang.Object state

stateStack

private java.util.Stack stateStack

whitespace

private java.lang.StringBuffer whitespace
Constructor Detail

DataUnformatFilter

public DataUnformatFilter()
Create a new filter.


DataUnformatFilter

public DataUnformatFilter(org.xml.sax.XMLReader xmlreader)
Create a new filter.

Use the XMLReader provided as the source of events.

Parameters:
xmlreader - The parent in the filter chain.
Method Detail

reset

public void reset()
Reset the filter so that it can be reused.

This method is especially useful if the filter failed with an exception the last time through.


startDocument

public void startDocument()
                   throws org.xml.sax.SAXException
Filter a start document event.

Reset state and pass the event on for further processing.

Throws:
com.jivesoftware.sax.SAXException - If a filter further down the chain raises an exception.
org.xml.sax.SAXException
See Also:
com.jivesoftware.sax.ContentHandler#startDocument

startElement

public void startElement(java.lang.String uri,
                         java.lang.String localName,
                         java.lang.String qName,
                         org.xml.sax.Attributes atts)
                  throws org.xml.sax.SAXException
Filter a start element event.

Parameters:
uri - The element's Namespace URI.
localName - The element's local name.
qName - The element's qualified (prefixed) name.
atts - The element's attribute list.
Throws:
com.jivesoftware.sax.SAXException - If a filter further down the chain raises an exception.
org.xml.sax.SAXException
See Also:
com.jivesoftware.sax.ContentHandler#startElement

endElement

public void endElement(java.lang.String uri,
                       java.lang.String localName,
                       java.lang.String qName)
                throws org.xml.sax.SAXException
Filter an end element event.

Parameters:
uri - The element's Namespace URI.
localName - The element's local name.
qName - The element's qualified (prefixed) name.
Throws:
com.jivesoftware.sax.SAXException - If a filter further down the chain raises an exception.
org.xml.sax.SAXException
See Also:
com.jivesoftware.sax.ContentHandler#endElement

characters

public void characters(char[] ch,
                       int start,
                       int length)
                throws org.xml.sax.SAXException
Filter a character data event.

Parameters:
ch - The characters to write.
start - The starting position in the array.
length - The number of characters to use.
Throws:
com.jivesoftware.sax.SAXException - If a filter further down the chain raises an exception.
org.xml.sax.SAXException
See Also:
com.jivesoftware.sax.ContentHandler#characters

ignorableWhitespace

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

Parameters:
ch - The array of characters to write.
start - The starting position in the array.
length - The number of characters to write.
Throws:
com.jivesoftware.sax.SAXException - If a filter further down the chain raises an exception.
org.xml.sax.SAXException
See Also:
com.jivesoftware.sax.ContentHandler#ignorableWhitespace

processingInstruction

public void processingInstruction(java.lang.String target,
                                  java.lang.String data)
                           throws org.xml.sax.SAXException
Filter a processing instruction event.

Parameters:
target - The PI target.
data - The PI data.
Throws:
com.jivesoftware.sax.SAXException - If a filter further down the chain raises an exception.
org.xml.sax.SAXException
See Also:
com.jivesoftware.sax.ContentHandler#processingInstruction

saveWhitespace

protected void saveWhitespace(char[] ch,
                              int start,
                              int length)
Saves trailing whitespace.


emitWhitespace

protected void emitWhitespace()
                       throws org.xml.sax.SAXException
Passes saved whitespace down the filter chain.

Throws:
org.xml.sax.SAXException

clearWhitespace

protected void clearWhitespace()
Discards saved whitespace.


isXMLWhitespace

private boolean isXMLWhitespace(char c)
Returns true if character is XML whitespace.