The Filter Architecture


Figure 8.1 diagrams the normal course of XML processing. A client application instructs a parser, represented in SAX by an XMLReader object, to read the text of an XML document. As it reads, the parser calls back to the client application's ContentHandler .

Figure 8.1. The XML Parsing Process

graphics/08fig01.gif

Figure 8.2 diagrams the course of XML processing with a filter. A client application instructs the filter, represented in SAX by an XMLFilter object, to read the text of an XML document. The filter then instructs the parser to read the text of an XML document. As it reads, the parser calls back to the filter's ContentHandler . The filter's ContentHandler then calls back to the client application's ContentHandler .

Figure 8.2. XML Parsing with a Filter

graphics/08fig02.gif

Because the filter sits in the middle between the real parser and the client application, it can change the stream of events that gets passed back and forth between the two. For example, it can convert Vector Markup Language (VML) to Scalable Vector Graphics (SVG) on the fly. It can replace xinclude:include elements with the documents to which they point. It can add namespaces to elements and attributes that don't normally have them. Alternately it can work with the stream without actually changing the data. For example, it could log all SOAP requests that pass through it to a database. This wouldn't necessarily change the data passing through the filter in any way, but it could help implement transactions with rollback and/or journaling to protect against data corruption in the event of a system crash.

The XMLFilter interface is a subinterface of XMLReader . Therefore, rather than chaining a filter directly to the parser that is reading the actual document, you can chain it to another filter. And this filter can be chained to another filter, and this filter can be chained to another filter, and so on for as many filters as necessary, as diagrammed in Figure 8.3. For example, this allows you to add namespaces to a document, convert VML to SVG, and resolve XIncludesall while parsing a single document. The document the client application receives can differ greatly from the text file stored on a disk somewhere, depending on how many filters there are and how much they change the original data.

Figure 8.3. XML Parsing with Multiple Filters

graphics/08fig03.gif

As a final trick, filters can present data that is not XML to an application as if it were XML. In other words, you can write a parser for some non-XML data such as tab-delimited text files, and provide an XMLReader interface through a filter to hide the fact that what's being parsed isn't really XML!



Processing XML with Java. A Guide to SAX, DOM, JDOM, JAXP, and TrAX
Processing XML with Javaв„ў: A Guide to SAX, DOM, JDOM, JAXP, and TrAX
ISBN: 0201771861
EAN: 2147483647
Year: 2001
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net