com.jdon.util.jdom
类 DataFormatFilter

java.lang.Object
  继承者 org.xml.sax.helpers.XMLFilterImpl
      继承者 com.jdon.util.jdom.XMLFilterBase
          继承者 com.jdon.util.jdom.DataFormatFilter
所有已实现的接口:
ContentHandler, DTDHandler, EntityResolver, ErrorHandler, LexicalHandler, XMLFilter, XMLReader

public class DataFormatFilter
extends XMLFilterBase

Filter for 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 adds indentation and newlines to field-oriented XML without mixed content. All added indentation and newlines will be passed on down the filter chain.

In general, all whitespace in an XML document is potentially significant. There is, however, a large class of XML documents where information is strictly fielded: each element contains either character data or other elements, but not both. For this special case, it is possible for a filter to provide automatic indentation and newlines. Note that this class will likely not yield appropriate results for document-oriented XML like XHTML pages, which mix character data and elements together.

This filter will automatically place each start tag on a new line, optionally indented if an indent step is provided (by default, there is no indentation). If an element contains other elements, the end tag will also appear on a new line with leading indentation. Consider, for example, the following code:

 DataFormatFilter df = new DataFormatFilter();
 df.setContentHandler(new XMLWriter());

 df.setIndentStep(2);
 df.startDocument();
 df.startElement("Person");
 df.dataElement("name", "Jane Smith");
 df.dataElement("date-of-birth", "1965-05-23");
 df.dataElement("citizenship", "US");
 df.endElement("Person");
 df.endDocument();
 

This code will produce the following document:

 <?xml version="1.0"?>

 <Person>
   <name>Jane Smith</name>
   <date-of-birth>1965-05-23</date-of-birth>
   <citizenship>US</citizenship>
 </Person>
 

另请参见:
DataUnformatFilter

字段摘要
 
从类 com.jdon.util.jdom.XMLFilterBase 继承的字段
EMPTY_ATTS, LEXICAL_HANDLER_NAMES
 
构造方法摘要
DataFormatFilter()
          Create a new filter.
DataFormatFilter(XMLReader xmlreader)
          Create a new filter.
 
方法摘要
 void characters(char[] ch, int start, int length)
          Filter a character data event.
 void endElement(String uri, String localName, String qName)
          Add newline and indentation prior to end tag.
 int getIndentStep()
          Return the current indent step.
 void reset()
          Reset the filter so that it can be reused.
 void setIndentStep(int indentStep)
          Set the current indent step.
 void startDocument()
          Filter a start document event.
 void startElement(String uri, String localName, String qName, Attributes atts)
          Add newline and indentation prior to start tag.
 
从类 com.jdon.util.jdom.XMLFilterBase 继承的方法
characters, comment, dataElement, dataElement, dataElement, dataElement, emptyElement, emptyElement, emptyElement, emptyElement, endCDATA, endDTD, endElement, endElement, endEntity, getLexicalHandler, getProperty, parse, setLexicalHandler, setProperty, startCDATA, startDTD, startElement, startElement, startElement, startEntity
 
从类 org.xml.sax.helpers.XMLFilterImpl 继承的方法
endDocument, endPrefixMapping, error, fatalError, getContentHandler, getDTDHandler, getEntityResolver, getErrorHandler, getFeature, getParent, ignorableWhitespace, notationDecl, parse, processingInstruction, resolveEntity, setContentHandler, setDocumentLocator, setDTDHandler, setEntityResolver, setErrorHandler, setFeature, setParent, skippedEntity, startPrefixMapping, unparsedEntityDecl, warning
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

DataFormatFilter

public DataFormatFilter()
Create a new filter.


DataFormatFilter

public DataFormatFilter(XMLReader xmlreader)
Create a new filter.

Use the XMLReader provided as the source of events.

参数:
xmlreader - The parent in the filter chain.
方法详细信息

getIndentStep

public int getIndentStep()
Return the current indent step.

Return the current indent step: each start tag will be indented by this number of spaces times the number of ancestors that the element has.

返回:
The number of spaces in each indentation step, or 0 or less for no indentation.
另请参见:
setIndentStep(int)

setIndentStep

public void setIndentStep(int indentStep)
Set the current indent step.

参数:
indentStep - The new indent step (0 or less for no indentation).
另请参见:
getIndentStep()

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 SAXException
Filter a start document event.

Reset state and pass the event on for further processing.

指定者:
接口 ContentHandler 中的 startDocument
覆盖:
XMLFilterImpl 中的 startDocument
抛出:
SAXException - If a filter further down the chain raises an exception.
另请参见:
ContentHandler.startDocument()

startElement

public void startElement(String uri,
                         String localName,
                         String qName,
                         Attributes atts)
                  throws SAXException
Add newline and indentation prior to start tag.

Each tag will begin on a new line, and will be indented by the current indent step times the number of ancestors that the element has.

The newline and indentation will be passed on down the filter chain through regular characters events.

指定者:
接口 ContentHandler 中的 startElement
覆盖:
XMLFilterImpl 中的 startElement
参数:
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.
抛出:
SAXException - If a filter further down the chain raises an exception.
另请参见:
ContentHandler.startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)

endElement

public void endElement(String uri,
                       String localName,
                       String qName)
                throws SAXException
Add newline and indentation prior to end tag.

If the element has contained other elements, the tag will appear indented on a new line; otherwise, it will appear immediately following whatever came before.

The newline and indentation will be passed on down the filter chain through regular characters events.

指定者:
接口 ContentHandler 中的 endElement
覆盖:
XMLFilterImpl 中的 endElement
参数:
uri - The element's Namespace URI.
localName - The element's local name.
qName - The element's qualified (prefixed) name.
抛出:
SAXException - If a filter further down the chain raises an exception.
另请参见:
ContentHandler.endElement(java.lang.String, java.lang.String, java.lang.String)

characters

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

指定者:
接口 ContentHandler 中的 characters
覆盖:
XMLFilterImpl 中的 characters
参数:
ch - The characters to write.
start - The starting position in the array.
length - The number of characters to use.
抛出:
SAXException - If a filter further down the chain raises an exception.
另请参见:
ContentHandler.characters(char[], int, int)


Copyright © 2013. All Rights Reserved.