KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > transformer > TrAXFilter


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: TrAXFilter.java,v 1.15 2004/02/23 17:09:48 jycli Exp $
18  */

19 package org.apache.xalan.transformer;
20
21 import java.io.IOException JavaDoc;
22
23 import javax.xml.transform.ErrorListener JavaDoc;
24 import javax.xml.transform.Templates JavaDoc;
25 import javax.xml.transform.TransformerConfigurationException JavaDoc;
26
27 import org.apache.xalan.res.XSLMessages;
28 import org.apache.xalan.res.XSLTErrorResources;
29
30 import org.xml.sax.ContentHandler JavaDoc;
31 import org.xml.sax.DTDHandler JavaDoc;
32 import org.xml.sax.EntityResolver JavaDoc;
33 import org.xml.sax.InputSource JavaDoc;
34 import org.xml.sax.XMLReader JavaDoc;
35 import org.xml.sax.helpers.XMLFilterImpl JavaDoc;
36 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
37
38
39 public class TrAXFilter extends XMLFilterImpl JavaDoc
40 {
41   private Templates JavaDoc m_templates;
42   private TransformerImpl m_transformer;
43     
44   /**
45    * Construct an empty XML filter, with no parent.
46    *
47    * <p>This filter will have no parent: you must assign a parent
48    * before you start a parse or do any configuration with
49    * setFeature or setProperty.</p>
50    *
51    * @see org.xml.sax.XMLReader#setFeature
52    * @see org.xml.sax.XMLReader#setProperty
53    */

54   public TrAXFilter (Templates JavaDoc templates)
55     throws TransformerConfigurationException JavaDoc
56   {
57     m_templates = templates;
58     m_transformer = (TransformerImpl)templates.newTransformer();
59   }
60   
61   /**
62    * Return the Transformer object used for this XML filter.
63    */

64   public TransformerImpl getTransformer()
65   {
66     return m_transformer;
67   }
68   
69   /** Set the parent reader.
70    *
71    * <p>This is the {@link org.xml.sax.XMLReader XMLReader} from which
72    * this filter will obtain its events and to which it will pass its
73    * configuration requests. The parent may itself be another filter.</p>
74    *
75    * <p>If there is no parent reader set, any attempt to parse
76    * or to set or get a feature or property will fail.</p>
77    *
78    * @param parent The parent XML reader.
79    * @throws java.lang.NullPointerException If the parent is null.
80    */

81   public void setParent (XMLReader JavaDoc parent)
82   {
83     super.setParent(parent);
84     
85     if(null != parent.getContentHandler())
86       this.setContentHandler(parent.getContentHandler());
87
88     // Not really sure if we should do this here, but
89
// it seems safer in case someone calls parse() on
90
// the parent.
91
setupParse ();
92   }
93   
94   /**
95    * Parse a document.
96    *
97    * @param input The input source for the document entity.
98    * @throws org.xml.sax.SAXException Any SAX exception, possibly
99    * wrapping another exception.
100    * @throws java.io.IOException An IO exception from the parser,
101    * possibly from a byte stream or character stream
102    * supplied by the application.
103    * @see org.xml.sax.XMLReader#parse(org.xml.sax.InputSource)
104    */

105   public void parse (InputSource JavaDoc input)
106     throws org.xml.sax.SAXException JavaDoc, IOException JavaDoc
107   {
108     if(null == getParent())
109     {
110       XMLReader JavaDoc reader=null;
111
112       // Use JAXP1.1 ( if possible )
113
try {
114           javax.xml.parsers.SAXParserFactory JavaDoc factory=
115               javax.xml.parsers.SAXParserFactory.newInstance();
116           factory.setNamespaceAware( true );
117           javax.xml.parsers.SAXParser JavaDoc jaxpParser=
118               factory.newSAXParser();
119           reader=jaxpParser.getXMLReader();
120           
121       } catch( javax.xml.parsers.ParserConfigurationException JavaDoc ex ) {
122           throw new org.xml.sax.SAXException JavaDoc( ex );
123       } catch( javax.xml.parsers.FactoryConfigurationError JavaDoc ex1 ) {
124           throw new org.xml.sax.SAXException JavaDoc( ex1.toString() );
125       } catch( NoSuchMethodError JavaDoc ex2 ) {
126       }
127       catch (AbstractMethodError JavaDoc ame){}
128
129       XMLReader JavaDoc parent;
130       if( reader==null )
131           parent= XMLReaderFactory.createXMLReader();
132       else
133           parent=reader;
134       try
135       {
136         parent.setFeature("http://xml.org/sax/features/namespace-prefixes",
137                           true);
138       }
139       catch (org.xml.sax.SAXException JavaDoc se){}
140       // setParent calls setupParse...
141
setParent(parent);
142     }
143     else
144     {
145       // Make sure everything is set up.
146
setupParse ();
147     }
148     if(null == m_transformer.getContentHandler())
149     {
150       throw new org.xml.sax.SAXException JavaDoc(XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_CALL_PARSE, null)); //"parse can not be called if the ContentHandler has not been set!");
151
}
152
153     getParent().parse(input);
154     Exception JavaDoc e = m_transformer.getExceptionThrown();
155     if(null != e)
156     {
157       if(e instanceof org.xml.sax.SAXException JavaDoc)
158         throw (org.xml.sax.SAXException JavaDoc)e;
159       else
160         throw new org.xml.sax.SAXException JavaDoc(e);
161     }
162   }
163   
164   /**
165    * Parse a document.
166    *
167    * @param systemId The system identifier as a fully-qualified URI.
168    * @throws org.xml.sax.SAXException Any SAX exception, possibly
169    * wrapping another exception.
170    * @throws java.io.IOException An IO exception from the parser,
171    * possibly from a byte stream or character stream
172    * supplied by the application.
173    * @see org.xml.sax.XMLReader#parse(java.lang.String)
174    */

175   public void parse (String JavaDoc systemId)
176     throws org.xml.sax.SAXException JavaDoc, IOException JavaDoc
177   {
178     parse(new InputSource JavaDoc(systemId));
179   }
180
181
182   /**
183    * Set up before a parse.
184    *
185    * <p>Before every parse, check whether the parent is
186    * non-null, and re-register the filter for all of the
187    * events.</p>
188    */

189   private void setupParse ()
190   {
191     XMLReader JavaDoc p = getParent();
192     if (p == null) {
193       throw new NullPointerException JavaDoc(XSLMessages.createMessage(XSLTErrorResources.ER_NO_PARENT_FOR_FILTER, null)); //"No parent for filter");
194
}
195     
196     ContentHandler JavaDoc ch = m_transformer.getInputContentHandler();
197 // if(ch instanceof SourceTreeHandler)
198
// ((SourceTreeHandler)ch).setUseMultiThreading(true);
199
p.setContentHandler(ch);
200     p.setEntityResolver(this);
201     p.setDTDHandler(this);
202     p.setErrorHandler(this);
203   }
204
205   /**
206    * Set the content event handler.
207    *
208    * @param resolver The new content handler.
209    * @throws java.lang.NullPointerException If the handler
210    * is null.
211    * @see org.xml.sax.XMLReader#setContentHandler
212    */

213   public void setContentHandler (ContentHandler JavaDoc handler)
214   {
215     m_transformer.setContentHandler(handler);
216     // super.setContentHandler(m_transformer.getResultTreeHandler());
217
}
218   
219   public void setErrorListener (ErrorListener JavaDoc handler)
220   {
221     m_transformer.setErrorListener(handler);
222   }
223
224 }
225
Popular Tags