KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > trax > TrAXFilter


1 /*
2  * Copyright 2001-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: TrAXFilter.java,v 1.8 2004/02/16 22:57:21 minchau Exp $
18  */

19
20
21 package org.apache.xalan.xsltc.trax;
22
23 import java.io.IOException JavaDoc;
24
25 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27 import javax.xml.parsers.SAXParser JavaDoc;
28 import javax.xml.parsers.SAXParserFactory JavaDoc;
29 import javax.xml.transform.ErrorListener JavaDoc;
30 import javax.xml.transform.Templates JavaDoc;
31 import javax.xml.transform.Transformer JavaDoc;
32 import javax.xml.transform.TransformerConfigurationException JavaDoc;
33 import javax.xml.transform.sax.SAXResult JavaDoc;
34
35 import org.apache.xml.utils.XMLReaderManager;
36
37 import org.xml.sax.ContentHandler JavaDoc;
38 import org.xml.sax.InputSource JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40 import org.xml.sax.XMLReader JavaDoc;
41 import org.xml.sax.helpers.XMLFilterImpl JavaDoc;
42 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
43
44 /**
45  * skeleton extension of XMLFilterImpl for now.
46  * @author Santiago Pericas-Geertsen
47  * @author G. Todd Miller
48  */

49 public class TrAXFilter extends XMLFilterImpl JavaDoc {
50     private Templates JavaDoc _templates;
51     private TransformerImpl _transformer;
52     private TransformerHandlerImpl _transformerHandler;
53
54     public TrAXFilter(Templates JavaDoc templates) throws
55     TransformerConfigurationException JavaDoc
56     {
57     _templates = templates;
58     _transformer = (TransformerImpl) templates.newTransformer();
59         _transformerHandler = new TransformerHandlerImpl(_transformer);
60     }
61     
62     public Transformer JavaDoc getTransformer() {
63         return _transformer;
64     }
65
66     private void createParent() throws SAXException JavaDoc {
67     XMLReader JavaDoc parent = null;
68         try {
69             SAXParserFactory JavaDoc pfactory = SAXParserFactory.newInstance();
70             pfactory.setNamespaceAware(true);
71             SAXParser JavaDoc saxparser = pfactory.newSAXParser();
72             parent = saxparser.getXMLReader();
73         }
74         catch (ParserConfigurationException JavaDoc e) {
75             throw new SAXException JavaDoc(e);
76         }
77         catch (FactoryConfigurationError JavaDoc e) {
78             throw new SAXException JavaDoc(e.toString());
79         }
80
81         if (parent == null) {
82             parent = XMLReaderFactory.createXMLReader();
83         }
84
85         // make this XMLReader the parent of this filter
86
setParent(parent);
87     }
88
89     public void parse (InputSource JavaDoc input) throws SAXException JavaDoc, IOException JavaDoc
90     {
91         XMLReader JavaDoc managedReader = null;
92
93         try {
94             if (getParent() == null) {
95                 try {
96                     managedReader = XMLReaderManager.getInstance()
97                                                     .getXMLReader();
98                     setParent(managedReader);
99                 } catch (SAXException JavaDoc e) {
100                     throw new SAXException JavaDoc(e.toString());
101                 }
102             }
103
104             // call parse on the parent
105
getParent().parse(input);
106         } finally {
107             if (managedReader != null) {
108                 XMLReaderManager.getInstance().releaseXMLReader(managedReader);
109             }
110         }
111     }
112
113     public void parse (String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc
114     {
115         parse(new InputSource JavaDoc(systemId));
116     }
117
118     public void setContentHandler (ContentHandler JavaDoc handler)
119     {
120     _transformerHandler.setResult(new SAXResult JavaDoc(handler));
121     if (getParent() == null) {
122                 try {
123                     createParent();
124                 }
125                 catch (SAXException JavaDoc e) {
126                    return;
127                 }
128     }
129     getParent().setContentHandler(_transformerHandler);
130     }
131
132     public void setErrorListener (ErrorListener JavaDoc handler) { }
133 }
134
Popular Tags