| sax.XMLFilterBase sax.DataFormatFilter
DataFormatFilter | public class DataFormatFilter extends XMLFilterBase (Code) | | 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>
See Also: DataUnformatFilter |
DataFormatFilter | public DataFormatFilter()(Code) | | Create a new filter.
|
DataFormatFilter | public DataFormatFilter(XMLReader xmlreader)(Code) | | Create a new filter.
Use the XMLReader provided as the source of events.
Parameters: xmlreader - The parent in the filter chain. |
characters | public void characters(char ch, int start, int length) throws SAXException(Code) | | Filter a character data event.
Parameters: ch - The characters to write. Parameters: start - The starting position in the array. Parameters: length - The number of characters to use. exception: org.xml.sax.SAXException - If a filterfurther down the chain raises an exception. See Also: org.xml.sax.ContentHandler.characters |
endElement | public void endElement(String uri, String localName, String qName) throws SAXException(Code) | | 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.
Parameters: uri - The element's Namespace URI. Parameters: localName - The element's local name. Parameters: qName - The element's qualified (prefixed) name. exception: org.xml.sax.SAXException - If a filterfurther down the chain raises an exception. See Also: org.xml.sax.ContentHandler.endElement |
getIndentStep | public int getIndentStep()(Code) | | 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. See Also: DataFormatFilter.setIndentStep |
reset | public void reset()(Code) | | 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.
|
setIndentStep | public void setIndentStep(int indentStep)(Code) | | Set the current indent step.
Parameters: indentStep - The new indent step (0 or less for noindentation). See Also: DataFormatFilter.getIndentStep |
startElement | public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException(Code) | | 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.
Parameters: uri - The element's Namespace URI. Parameters: localName - The element's local name. Parameters: qName - The element's qualified (prefixed) name. Parameters: atts - The element's attribute list. exception: org.xml.sax.SAXException - If a filterfurther down the chain raises an exception. See Also: org.xml.sax.ContentHandler.startElement |
Methods inherited from sax.XMLFilterBase | public void characters(String data) throws SAXException(Code)(Java Doc) public void comment(char[] ch, int start, int length) throws SAXException(Code)(Java Doc) public void dataElement(String uri, String localName, String qName, Attributes atts, String content) throws SAXException(Code)(Java Doc) public void dataElement(String uri, String localName, String content) throws SAXException(Code)(Java Doc) public void dataElement(String localName, Attributes atts, String content) throws SAXException(Code)(Java Doc) public void dataElement(String localName, String content) throws SAXException(Code)(Java Doc) public void emptyElement(String uri, String localName, String qName, Attributes atts) throws SAXException(Code)(Java Doc) public void emptyElement(String uri, String localName) throws SAXException(Code)(Java Doc) public void emptyElement(String localName, Attributes atts) throws SAXException(Code)(Java Doc) public void emptyElement(String localName) throws SAXException(Code)(Java Doc) public void endCDATA() throws SAXException(Code)(Java Doc) public void endDTD() throws SAXException(Code)(Java Doc) public void endElement(String uri, String localName) throws SAXException(Code)(Java Doc) public void endElement(String localName) throws SAXException(Code)(Java Doc) public void endEntity(String name) throws SAXException(Code)(Java Doc) public LexicalHandler getLexicalHandler()(Code)(Java Doc) public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException(Code)(Java Doc) public void parse(InputSource input) throws SAXException, IOException(Code)(Java Doc) public void setLexicalHandler(LexicalHandler handler)(Code)(Java Doc) public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException(Code)(Java Doc) public void startCDATA() throws SAXException(Code)(Java Doc) public void startDTD(String name, String publicId, String systemId) throws SAXException(Code)(Java Doc) public void startElement(String uri, String localName) throws SAXException(Code)(Java Doc) public void startElement(String localName, Attributes atts) throws SAXException(Code)(Java Doc) public void startElement(String localName) throws SAXException(Code)(Java Doc) public void startEntity(String name) throws SAXException(Code)(Java Doc)
|
|
|