View Javadoc

1   package net.sf.tomp.xtcl.filter;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   import org.xml.sax.XMLFilter;
6   
7   import javax.xml.transform.Templates;
8   import javax.xml.transform.TransformerConfigurationException;
9   import javax.xml.transform.sax.SAXTransformerFactory;
10  import javax.xml.transform.sax.TransformerHandler;
11  import javax.xml.transform.stream.StreamSource;
12  
13  import java.util.HashMap;
14  import java.util.Map;
15  
16  /***
17   * DOCUMENT ME!
18   *
19   * @author tomp
20   */
21  public class XTFilterFactoryImpl implements XTFilterFactory, TemplatesCache
22  {
23      private Log log = LogFactory.getLog(XTFilterFactoryImpl.class);
24  
25      /*** DOCUMENT ME! */
26      protected SAXTransformerFactory factory;
27  
28      /*** DOCUMENT ME! */
29      protected net.sf.joost.trax.TransformerFactoryImpl jtf;
30  
31      /*** DOCUMENT ME! */
32      protected Map cache;
33  
34      /***
35       * Creates a new XTFilterFactoryImpl object.
36       *
37       * @param factory DOCUMENT ME!
38       * @param jtf DOCUMENT ME!
39       */
40      public XTFilterFactoryImpl(SAXTransformerFactory factory,
41          net.sf.joost.trax.TransformerFactoryImpl jtf)
42      {
43          this.factory = factory;
44          this.jtf = jtf;
45          cache = new HashMap();
46      }
47  
48      /***
49       * DOCUMENT ME!
50       *
51       * @param className DOCUMENT ME!
52       * @return DOCUMENT ME!
53       * @throws ClassNotFoundException DOCUMENT ME!
54       * @throws InstantiationException DOCUMENT ME!
55       * @throws IllegalAccessException DOCUMENT ME!
56       */
57      public XMLFilter newXMLFilter(String className)
58          throws ClassNotFoundException, InstantiationException, 
59              IllegalAccessException
60      {
61          Class filterClass = Class.forName(className);
62          XMLFilter f = (XMLFilter) filterClass.newInstance();
63  
64          return f;
65      }
66  
67      /***
68       * DOCUMENT ME!
69       *
70       * @param templates DOCUMENT ME!
71       * @return DOCUMENT ME!
72       * @throws TransformerConfigurationException DOCUMENT ME!
73       */
74      public XMLFilter newTransformerFilter(Templates templates)
75          throws TransformerConfigurationException
76      {
77          /*
78           * if (!(templates instanceof PreparedStyleSheet)) { throw new
79           * TransformerConfigurationException( "Supplied Templates object was not
80           * created using Saxon"); }
81           */
82          TransformerHandler handler = factory.newTransformerHandler(templates);
83  
84          //Controller controller = (Controller)templates.newTransformer();
85          return new TransformerFilterImpl(handler);
86  
87          //return factory.newXMLFilter(templates);
88      }
89  
90      /***
91       * DOCUMENT ME!
92       *
93       * @param systemId DOCUMENT ME!
94       * @return DOCUMENT ME!
95       * @throws TransformerConfigurationException DOCUMENT ME!
96       */
97      public XMLFilter newTransformerFilter(String systemId)
98          throws TransformerConfigurationException
99      {
100         TransformerHandler handler = factory.newTransformerHandler(getTemplates(
101                     systemId));
102 
103         //Controller controller = (Controller)templates.newTransformer();
104         return new TransformerFilterImpl(handler);
105 
106         //return factory.newXMLFilter(new StreamSource(systemId));
107     }
108 
109     /***
110      * DOCUMENT ME!
111      *
112      * @param systemId DOCUMENT ME!
113      * @return DOCUMENT ME!
114      * @throws TransformerConfigurationException DOCUMENT ME!
115      */
116     public Templates newTemplates(String systemId)
117         throws TransformerConfigurationException
118     {
119         //System.out.println("----- newTemplates(systemId="+systemId+")");
120         return getTemplates(systemId);
121     }
122 
123     /***
124      * DOCUMENT ME!
125      *
126      * @param systemId DOCUMENT ME!
127      * @return DOCUMENT ME!
128      * @throws TransformerConfigurationException DOCUMENT ME!
129      */
130     public Templates getTemplates(String systemId)
131         throws TransformerConfigurationException
132     {
133         //System.out.println("----- newTemplates(systemId="+systemId+")");
134         Templates t = (Templates) cache.get(systemId);
135 
136         if (t == null)
137         {
138             t = factory.newTemplates(new StreamSource(systemId));
139             cache.put(systemId, t);
140         }
141 
142         return t;
143     }
144 
145     /***
146      * DOCUMENT ME!
147      *
148      * @param templates DOCUMENT ME!
149      * @return DOCUMENT ME!
150      * @throws TransformerConfigurationException DOCUMENT ME!
151      */
152     public XMLFilter newJoostFilter(Templates templates)
153         throws TransformerConfigurationException
154     {
155         log.debug("newJoostFilter(Templates=" + templates + ")");
156 
157         TransformerHandler handler = jtf.newTransformerHandler(templates);
158 
159         return new TransformerFilterImpl(handler);
160     }
161 
162     /***
163      * DOCUMENT ME!
164      *
165      * @param systemId DOCUMENT ME!
166      * @return DOCUMENT ME!
167      * @throws TransformerConfigurationException DOCUMENT ME!
168      */
169     public Templates newJoostTemplates(String systemId)
170         throws TransformerConfigurationException
171     {
172         return jtf.newTemplates(new StreamSource(systemId));
173     }
174 
175     /***
176      * DOCUMENT ME!
177      *
178      * @param systemId DOCUMENT ME!
179      * @return DOCUMENT ME!
180      * @throws TransformerConfigurationException DOCUMENT ME!
181      */
182     public XMLFilter newJoostFilter(String systemId)
183         throws TransformerConfigurationException
184     {
185         log.debug("newJoostFilter(systemId=" + systemId + ")");
186 
187         TransformerHandler handler = jtf.newTransformerHandler(new StreamSource(
188                     systemId));
189 
190         return new TransformerFilterImpl(handler);
191     }
192 
193     /***
194      * DOCUMENT ME!
195      *
196      * @return DOCUMENT ME!
197      * @throws TransformerConfigurationException DOCUMENT ME!
198      */
199     public TeeFilter newTeeFilter()
200         throws TransformerConfigurationException
201     {
202         return new TeeFilterImpl();
203     }
204 
205     /***
206      * DOCUMENT ME!
207      *
208      * @return DOCUMENT ME!
209      */
210 
211     /*
212      * public DerivationFilter newDerivationFilter() { return new
213      * DerivationFilter(); }
214      */
215 }
216 
217 
218 /*
219  * The contents of this file are subject to the Mozilla Public License Version
220  * 1.1 (the "License"); you may not use this file except in compliance with the
221  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
222  * Software distributed under the License is distributed on an "AS IS" basis,
223  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
224  * the specific language governing rights and limitations under the License. The
225  * Original Code is: all this file. The Initial Developer of the Original Code
226  * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
227  */