KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > TeeTransformer


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 package org.apache.cocoon.transformation;
17
18 import java.io.IOException JavaDoc;
19 import java.io.OutputStream JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.xml.transform.TransformerConfigurationException JavaDoc;
23 import javax.xml.transform.TransformerFactory JavaDoc;
24 import javax.xml.transform.TransformerFactoryConfigurationError JavaDoc;
25 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
26 import javax.xml.transform.sax.TransformerHandler JavaDoc;
27 import javax.xml.transform.stream.StreamResult JavaDoc;
28
29 import org.apache.avalon.framework.CascadingRuntimeException;
30 import org.apache.avalon.framework.configuration.Configuration;
31 import org.apache.avalon.framework.configuration.ConfigurationException;
32 import org.apache.avalon.framework.parameters.Parameters;
33 import org.apache.cocoon.ProcessingException;
34 import org.apache.cocoon.components.source.SourceUtil;
35 import org.apache.cocoon.environment.SourceResolver;
36 import org.apache.cocoon.util.ClassUtils;
37 import org.apache.excalibur.source.ModifiableSource;
38 import org.apache.excalibur.source.Source;
39 import org.apache.excalibur.source.SourceException;
40 import org.xml.sax.Attributes JavaDoc;
41 import org.xml.sax.Locator JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43
44 /**
45  * @cocoon.sitemap.component.name tee
46  * @cocoon.sitemap.component.logger sitemap.transformer.tee
47  * @cocoon.sitemap.component.pooling.max 16
48  *
49  * The Teetransformer serializes SAX events as-is to the {@link org.apache.excalibur.source.ModifiableSource}
50  * specified by its <code>src</code> parameter.
51  * It does not in any way change the events.
52  * <p/>
53  * This transformer works just like the unix "tee" command and is useful for debugging
54  * received XML streams.
55  * <p/>
56  * Usage:<br>
57  * <pre>
58  * &lt;map:transform type="tee" SRC="url"/&gt;
59  * </pre>
60  *
61  * @version $Id: TeeTransformer.java 178614 2005-05-26 07:14:22Z ugo $
62  */

63 public class TeeTransformer extends AbstractSAXTransformer {
64
65     /** the serializer */
66     private TransformerHandler JavaDoc serializer;
67
68     /** the transformer factory to use */
69     private SAXTransformerFactory JavaDoc transformerFactory;
70
71     /** the resolver */
72     private SourceResolver resolver;
73     
74     private OutputStream JavaDoc os;
75
76     /*
77      * (non-Javadoc)
78      *
79      * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver,
80      * java.util.Map, java.lang.String,
81      * org.apache.avalon.framework.parameters.Parameters)
82      */

83     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters parameters)
84         throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
85
86         super.setup(resolver, objectModel, src, parameters);
87
88         Source source = null;
89         try {
90             this.resolver = resolver;
91             source = this.resolver.resolveURI(src);
92             String JavaDoc systemId = source.getURI();
93             if (!(source instanceof ModifiableSource)) {
94                 throw new ProcessingException("Source '" + systemId + "' is not writeable.");
95             }
96             this.serializer = this.transformerFactory.newTransformerHandler();
97             os = ((ModifiableSource) source).getOutputStream();
98             this.serializer.setResult(new StreamResult JavaDoc(os));
99         } catch (SourceException e) {
100             throw SourceUtil.handle(e);
101         } catch (TransformerConfigurationException JavaDoc e) {
102             throw new ProcessingException(e);
103         } catch (TransformerFactoryConfigurationError JavaDoc error) {
104             throw new ProcessingException(error.getException());
105         } finally {
106             if (source != null) {
107                 this.resolver.release(source);
108             }
109         }
110     }
111
112     /*
113      * (non-Javadoc)
114      *
115      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
116      */

117     public void configure(Configuration configuration) throws ConfigurationException JavaDoc {
118         String JavaDoc tFactoryClass = configuration.getChild("transformer-factory").getValue(null);
119         if (tFactoryClass != null) {
120             try {
121                 this.transformerFactory = (SAXTransformerFactory JavaDoc) ClassUtils
122                     .newInstance(tFactoryClass);
123                 if (getLogger().isDebugEnabled()) {
124                     getLogger().debug("Using transformer factory " + tFactoryClass);
125                 }
126             } catch (Exception JavaDoc e) {
127                 throw new ConfigurationException JavaDoc(
128                     "Cannot load transformer factory " + tFactoryClass, e);
129             }
130         } else {
131             this.transformerFactory = (SAXTransformerFactory JavaDoc) TransformerFactory.newInstance();
132
133         }
134     }
135
136     /**
137      * Receive an object for locating the origin of SAX document events.
138      */

139     public void setDocumentLocator(Locator JavaDoc locator) {
140         super.contentHandler.setDocumentLocator(locator);
141         this.serializer.setDocumentLocator(locator);
142     }
143
144     /**
145      * Receive notification of the beginning of a document.
146      */

147     public void startDocument() throws SAXException JavaDoc {
148         super.contentHandler.startDocument();
149         this.serializer.startDocument();
150     }
151
152     /**
153      * Receive notification of the end of a document.
154      */

155     public void endDocument() throws SAXException JavaDoc {
156         super.contentHandler.endDocument();
157         this.serializer.endDocument();
158         if (os != null) {
159             try {
160                 os.close();
161             } catch (IOException JavaDoc e) {
162                 throw new CascadingRuntimeException("Error closing output stream.", e);
163             }
164         }
165     }
166
167     /**
168      * Begin the scope of a prefix-URI Namespace mapping.
169      */

170     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
171         super.contentHandler.startPrefixMapping(prefix, uri);
172         this.serializer.startPrefixMapping(prefix, uri);
173     }
174
175     /**
176      * End the scope of a prefix-URI mapping.
177      */

178     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
179         super.contentHandler.endPrefixMapping(prefix);
180         this.serializer.endPrefixMapping(prefix);
181     }
182
183     /**
184      * Receive notification of the beginning of an element.
185      */

186     public void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a) throws SAXException JavaDoc {
187         super.contentHandler.startElement(uri, loc, raw, a);
188         this.serializer.startElement(uri, loc, raw, a);
189     }
190
191     /**
192      * Receive notification of the end of an element.
193      */

194     public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw) throws SAXException JavaDoc {
195         super.contentHandler.endElement(uri, loc, raw);
196         this.serializer.endElement(uri, loc, raw);
197     }
198
199     /**
200      * Receive notification of character data.
201      */

202     public void characters(char ch[], int start, int len) throws SAXException JavaDoc {
203         super.contentHandler.characters(ch, start, len);
204         this.serializer.characters(ch, start, len);
205     }
206
207     /**
208      * Receive notification of ignorable whitespace in element content.
209      */

210     public void ignorableWhitespace(char ch[], int start, int len) throws SAXException JavaDoc {
211         super.contentHandler.ignorableWhitespace(ch, start, len);
212         this.serializer.ignorableWhitespace(ch, start, len);
213     }
214
215     /**
216      * Receive notification of a processing instruction.
217      */

218     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
219         super.contentHandler.processingInstruction(target, data);
220         this.serializer.processingInstruction(target, data);
221     }
222
223     /**
224      * Receive notification of a skipped entity.
225      */

226     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
227         super.contentHandler.skippedEntity(name);
228         this.serializer.skippedEntity(name);
229     }
230
231     /**
232      * Report the start of DTD declarations, if any.
233      */

234     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
235         super.lexicalHandler.startDTD(name, publicId, systemId);
236         this.serializer.startDTD(name, publicId, systemId);
237     }
238
239     /**
240      * Report the end of DTD declarations.
241      */

242     public void endDTD() throws SAXException JavaDoc {
243         super.lexicalHandler.endDTD();
244         this.serializer.endDTD();
245     }
246
247     /**
248      * Report the beginning of an entity.
249      */

250     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
251         super.lexicalHandler.startEntity(name);
252         this.serializer.startEntity(name);
253     }
254
255     /**
256      * Report the end of an entity.
257      */

258     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
259         super.lexicalHandler.endEntity(name);
260         this.serializer.endEntity(name);
261     }
262
263     /**
264      * Report the start of a CDATA section.
265      */

266     public void startCDATA() throws SAXException JavaDoc {
267         super.lexicalHandler.startCDATA();
268         this.serializer.startCDATA();
269     }
270
271     /**
272      * Report the end of a CDATA section.
273      */

274     public void endCDATA() throws SAXException JavaDoc {
275         super.lexicalHandler.endCDATA();
276         this.serializer.endCDATA();
277     }
278
279     /**
280      * Report an XML comment anywhere in the document.
281      */

282     public void comment(char ch[], int start, int len) throws SAXException JavaDoc {
283         super.lexicalHandler.comment(ch, start, len);
284         this.serializer.comment(ch, start, len);
285     }
286
287     /*
288      * (non-Javadoc)
289      *
290      * @see org.apache.avalon.excalibur.pool.Recyclable#recycle()
291      */

292     public void recycle() {
293         super.recycle();
294         this.serializer = null;
295     }
296 }
297
Popular Tags