[jdom-interest] SAXBuilder with XMLFilter feature
Joseph Bowbeer
jozart at csi.com
Sun Oct 22 02:44:40 PDT 2000
Below, please fine a small patch that will add an XMLFilter property to
SAXBuilder.
First, here's the output from a simple test showing a DataFormatFilter
and a DataUnformatFilter in operation:
-- test1.xml unfiltered --
<?xml version="1.0" standalone="yes"?>
<Person><name>Jane Smith</name></Person>
-- test1.xml filtered by DataFormatFilter --
<?xml version="1.0" encoding="UTF-8"?><Person>
<name>Jane Smith</name>
</Person>
-- test2.xml unfiltered --
<?xml version="1.0" standalone="yes"?>
<Person>
<name>Jane Smith</name>
</Person>
-- test2.xml filtered by DataUnformatFilter --
<?xml version="1.0" encoding="UTF-8"?><Person><name>Jane
Smith</name></Person>
The source for the first part of the test (i.e., test1) looks like:
XMLWriter echo = new XMLWriter();
DataFormatFilter format = new DataFormatFilter(echo);
format.setIndentStep(4);
SAXBuilder builder = new SAXBuilder();
builder.setXMLFilter(format);
Document doc = builder.build(in);
new XMLOutputter().output(doc, System.out);
Notes:
1. Two-stage filters are used in the tests above. The first stage is an
XMLWriter that outputs the unfiltered XML to stdout before passing it to
the next stage for the "real" filtering. The filtered output is written
to stdout by JDOM's XMLOutputter.
2. XMLOutputter does not currently have a separate setting for the
newline(s) after the header, so I can't tell it to insert a couple
newlines after the header without also having it change the document's
content in a potentially significant way.
3. The filters used in these tests are based on David Megginson's
XMLWriter and DataWriter.
Index: jdom/src/java/org/jdom/input/SAXBuilder.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/input/SAXBuilder.java,v
retrieving revision 1.24
diff -c -r1.24 SAXBuilder.java
*** jdom/src/java/org/jdom/input/SAXBuilder.java 2000/10/18
15:53:45
1.24
--- jdom/src/java/org/jdom/input/SAXBuilder.java 2000/10/22
08:49:10
***************
*** 88,93 ****
--- 88,94 ----
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXParseException;
+ import org.xml.sax.XMLFilter;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;
***************
*** 122,127 ****
--- 123,131 ----
/** DTDHandler class to use */
private DTDHandler saxDTDHandler = null;
+ /** XMLFilter instance to use */
+ private XMLFilter saxXMLFilter = null;
+
/**
* <p>
* This allows the validation features to be turned on/off
***************
*** 220,225 ****
--- 224,240 ----
/**
* <p>
+ * This sets custom XMLFilter for the <code>Builder</code>.
+ * </p>
+ *
+ * @param xmlFilter <code>XMLFilter</code>
+ */
+ public void setXMLFilter(XMLFilter xmlFilter) {
+ saxXMLFilter = xmlFilter;
+ }
+
+ /**
+ * <p>
* This builds a document from the supplied
* input source.
* </p>
***************
*** 237,242 ****
--- 252,271 ----
XMLReader parser =
XMLReaderFactory.createXMLReader(saxDriverClass);
+ // Install optional filter
+ if (saxXMLFilter != null) {
+
+ // Connect filter chain to parser
+ XMLFilter root = saxXMLFilter;
+ while (root.getParent() instanceof XMLFilter) {
+ root = (XMLFilter) root.getParent();
+ }
+ root.setParent(parser);
+
+ // Read from filter
+ parser = saxXMLFilter;
+ }
+
DefaultHandler contentHandler =
new SAXHandler(doc);
--
Enjoy
More information about the jdom-interest
mailing list