KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Pipe


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: Pipe.java,v 1.11 2004/02/17 19:07:09 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.sax.TransformerHandler JavaDoc;
29 import javax.xml.transform.stream.StreamSource JavaDoc;
30
31 import org.apache.xml.serializer.Serializer;
32 import org.apache.xml.serializer.SerializerFactory;
33 import org.apache.xml.serializer.OutputPropertiesFactory;
34 import org.xml.sax.SAXException JavaDoc;
35 import org.xml.sax.XMLReader JavaDoc;
36 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
37
38   /**
39    * This example shows how to chain a series of transformations by
40    * piping SAX events from one Transformer to another. Each Transformer
41    * operates as a SAX2 XMLFilter/XMLReader.
42    */

43 public class Pipe
44 {
45     public static void main(String JavaDoc[] args)
46     throws TransformerException JavaDoc, TransformerConfigurationException JavaDoc,
47          SAXException JavaDoc, IOException JavaDoc
48     {
49     // Instantiate a TransformerFactory.
50
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
51     // Determine whether the TransformerFactory supports The use uf SAXSource
52
// and SAXResult
53
if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE))
54     {
55       // Cast the TransformerFactory to SAXTransformerFactory.
56
SAXTransformerFactory JavaDoc saxTFactory = ((SAXTransformerFactory JavaDoc) tFactory);
57       // Create a TransformerHandler for each stylesheet.
58
TransformerHandler JavaDoc tHandler1 = saxTFactory.newTransformerHandler(new StreamSource JavaDoc("foo1.xsl"));
59       TransformerHandler JavaDoc tHandler2 = saxTFactory.newTransformerHandler(new StreamSource JavaDoc("foo2.xsl"));
60       TransformerHandler JavaDoc tHandler3 = saxTFactory.newTransformerHandler(new StreamSource JavaDoc("foo3.xsl"));
61     
62       // Create an XMLReader.
63
XMLReader JavaDoc reader = XMLReaderFactory.createXMLReader();
64       reader.setContentHandler(tHandler1);
65       reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHandler1);
66
67       tHandler1.setResult(new SAXResult JavaDoc(tHandler2));
68       tHandler2.setResult(new SAXResult JavaDoc(tHandler3));
69
70       // transformer3 outputs SAX events to the serializer.
71
Serializer serializer = SerializerFactory.getSerializer
72                           (OutputPropertiesFactory.getDefaultMethodProperties("xml"));
73       serializer.setOutputStream(System.out);
74       tHandler3.setResult(new SAXResult JavaDoc(serializer.asContentHandler()));
75
76         // Parse the XML input document. The input ContentHandler and output ContentHandler
77
// work in separate threads to optimize performance.
78
reader.parse("foo.xml");
79     }
80   }
81 }
82
Free Books   Free Magazines  
Popular Tags