KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SAX2SAX


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

17 /*
18  * $Id: SAX2SAX.java,v 1.12 2004/02/17 19:07:48 minchau Exp $
19  */

20
21 /**
22  * Replicate the SimpleTransform sample, explicitly using the SAX model to handle the
23  * stylesheet, the XML input, and the transformation.
24  */

25
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 import javax.xml.transform.Result JavaDoc;
30 import javax.xml.transform.Templates JavaDoc;
31 import javax.xml.transform.TransformerConfigurationException JavaDoc;
32 import javax.xml.transform.TransformerException JavaDoc;
33 import javax.xml.transform.TransformerFactory JavaDoc;
34 import javax.xml.transform.sax.SAXResult JavaDoc;
35 import javax.xml.transform.sax.SAXSource JavaDoc;
36 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
37 import javax.xml.transform.sax.TemplatesHandler JavaDoc;
38 import javax.xml.transform.sax.TransformerHandler JavaDoc;
39
40 import org.apache.xml.serializer.Serializer;
41 import org.apache.xml.serializer.SerializerFactory;
42 import org.apache.xml.serializer.OutputPropertiesFactory;
43 import org.xml.sax.SAXException JavaDoc;
44 import org.xml.sax.XMLReader JavaDoc;
45 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
46
47
48 public class SAX2SAX
49 {
50   public static void main(String JavaDoc[] args)
51     throws TransformerException JavaDoc, TransformerConfigurationException JavaDoc,
52          SAXException JavaDoc, IOException JavaDoc
53     {
54
55     // Instantiate a TransformerFactory.
56
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
57     // Determine whether the TransformerFactory supports The use of SAXSource
58
// and SAXResult
59
if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE))
60     {
61       // Cast the TransformerFactory.
62
SAXTransformerFactory JavaDoc saxTFactory = ((SAXTransformerFactory JavaDoc) tFactory);
63       // Create a ContentHandler to handle parsing of the stylesheet.
64
TemplatesHandler JavaDoc templatesHandler = saxTFactory.newTemplatesHandler();
65
66       // Create an XMLReader and set its ContentHandler.
67
XMLReader JavaDoc reader = XMLReaderFactory.createXMLReader();
68       reader.setContentHandler(templatesHandler);
69     
70       // Parse the stylesheet.
71
reader.parse("birds.xsl");
72
73       //Get the Templates object from the ContentHandler.
74
Templates JavaDoc templates = templatesHandler.getTemplates();
75       // Create a ContentHandler to handle parsing of the XML source.
76
TransformerHandler JavaDoc handler
77         = saxTFactory.newTransformerHandler(templates);
78       // Reset the XMLReader's ContentHandler.
79
reader.setContentHandler(handler);
80
81       // Set the ContentHandler to also function as a LexicalHandler, which
82
// includes "lexical" events (e.g., comments and CDATA).
83
reader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
84       
85       FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc("birds.out");
86       
87       Serializer serializer = SerializerFactory.getSerializer
88                               (OutputPropertiesFactory.getDefaultMethodProperties("xml"));
89       serializer.setOutputStream(fos);
90    
91       
92       // Set the result handling to be a serialization to the file output stream.
93
Result JavaDoc result = new SAXResult JavaDoc(serializer.asContentHandler());
94       handler.setResult(result);
95       
96       // Parse the XML input document.
97
reader.parse("birds.xml");
98       
99         System.out.println("************* The result is in birds.out *************");
100     }
101     else
102       System.out.println("The TransformerFactory does not support SAX input and SAX output");
103   }
104 }
105
Popular Tags