KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > UseXMLFilters


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: UseXMLFilters.java,v 1.10 2004/02/17 19:11:45 minchau Exp $
18  */

19
20 import java.io.IOException JavaDoc;
21
22 import javax.xml.transform.TransformerConfigurationException JavaDoc;
23 import javax.xml.transform.TransformerException JavaDoc;
24 import javax.xml.transform.TransformerFactory JavaDoc;
25 import javax.xml.transform.sax.SAXResult JavaDoc;
26 import javax.xml.transform.sax.SAXSource JavaDoc;
27 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
28 import javax.xml.transform.stream.StreamSource JavaDoc;
29
30 import org.apache.xml.serializer.Serializer;
31 import org.apache.xml.serializer.SerializerFactory;
32 import org.apache.xml.serializer.OutputPropertiesFactory;
33 import org.xml.sax.InputSource JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35 import org.xml.sax.XMLFilter JavaDoc;
36 import org.xml.sax.XMLReader JavaDoc;
37 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
38
39   /**
40    * This example shows how to chain a series of transformations by
41    * piping SAX events from one Transformer to another. Each Transformer
42    * operates as a SAX2 XMLFilter/XMLReader.
43    */

44 public class UseXMLFilters
45 {
46   public static void main(String JavaDoc[] args)
47     throws TransformerException JavaDoc, TransformerConfigurationException JavaDoc,
48          SAXException JavaDoc, IOException JavaDoc
49     {
50     // Instantiate a TransformerFactory.
51
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
52     // Determine whether the TransformerFactory supports The use uf SAXSource
53
// and SAXResult
54
if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE))
55     {
56       // Cast the TransformerFactory to SAXTransformerFactory.
57
SAXTransformerFactory JavaDoc saxTFactory = ((SAXTransformerFactory JavaDoc) tFactory);
58       // Create an XMLFilter for each stylesheet.
59
XMLFilter JavaDoc xmlFilter1 = saxTFactory.newXMLFilter(new StreamSource JavaDoc("foo1.xsl"));
60       XMLFilter JavaDoc xmlFilter2 = saxTFactory.newXMLFilter(new StreamSource JavaDoc("foo2.xsl"));
61       XMLFilter JavaDoc xmlFilter3 = saxTFactory.newXMLFilter(new StreamSource JavaDoc("foo3.xsl"));
62     
63       // Create an XMLReader.
64
XMLReader JavaDoc reader = XMLReaderFactory.createXMLReader();
65     
66       // xmlFilter1 uses the XMLReader as its reader.
67
xmlFilter1.setParent(reader);
68     
69       // xmlFilter2 uses xmlFilter1 as its reader.
70
xmlFilter2.setParent(xmlFilter1);
71     
72       // xmlFilter3 uses xmlFilter2 as its reader.
73
xmlFilter3.setParent(xmlFilter2);
74     
75       // xmlFilter3 outputs SAX events to the serializer.
76
Serializer serializer = SerializerFactory.getSerializer
77                       (OutputPropertiesFactory.getDefaultMethodProperties("xml"));
78       serializer.setOutputStream(System.out);
79       xmlFilter3.setContentHandler(serializer.asContentHandler());
80
81       // Perform the series of transformations as follows:
82
// - transformer3 gets its parent (transformer2) as the XMLReader/XMLFilter
83
// and calls transformer2.parse(new InputSource("foo.xml")).
84
// - transformer2 gets its parent (transformer1) as the XMLReader/XMLFilter
85
// and calls transformer1.parse(new InputSource("foo.xml")).
86
// - transformer1 gets its parent (reader, a SAXParser) as the XMLReader
87
// and calls reader.parse(new InputSource("foo.xml")).
88
// - reader parses the XML document and sends the SAX parse events to transformer1,
89
// which performs transformation 1 and sends the output to transformer2.
90
// - transformer2 parses the transformation 1 output, performs transformation 2, and
91
// sends the output to transformer3.
92
// - transformer3 parses the transformation 2 output, performs transformation 3,
93
// and sends the output to the serializer.
94
xmlFilter3.parse(new InputSource JavaDoc("foo.xml"));
95     }
96   }
97 }
98
Popular Tags