KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.framework.parameters.Parameters;
19 import org.apache.cocoon.ProcessingException;
20 import org.apache.excalibur.source.Source;
21 import org.apache.excalibur.source.SourceException;
22 import org.apache.cocoon.components.source.SourceUtil;
23 import org.apache.cocoon.environment.SourceResolver;
24 import org.xml.sax.Attributes JavaDoc;
25 import org.xml.sax.Locator JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27
28 import java.io.FileWriter JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * @cocoon.sitemap.component.documentation
35  * The <code>LogTransformer</code> is a class that can be plugged into a pipeline
36  * to print the SAX events which passes thru this transformer in a readable form
37  * to a file.
38  *
39  * @cocoon.sitemap.component.name log
40  * @cocoon.sitemap.component.logger sitemap.transformer.log
41  *
42  * @cocoon.sitemap.component.pooling.max 16
43  *
44  *
45  * The <code>LogTransformer</code> is a class that can be plugged into a pipeline
46  * to print the SAX events which passes thru this transformer in a readable form
47  * to a file.
48  * <br>
49  * The file will be specified in a parameter tag in the sitemap pipeline to the
50  * transformer as follows:
51  * <p>
52  * <pre>
53  * &lt;map:transform type="log"&gt;
54  * &nbsp;&nbsp;&lt;map:parameter name="logfile" value="logfile.log"/&gt;
55  * &nbsp;&nbsp;&lt;map:parameter name="append" value="no"/&gt;
56  * &lt;/map:transform&gt;
57  * </pre>
58  * </p>
59  *
60  * Because the log file will be hardcoded into the sitemap this LOGTransformer will
61  * not be thread save!!
62  * <br>
63  * This transformations main purpose is debugging.
64  *
65  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
66  * @author <a HREF="mailto:giacomo.pati@pwr.ch">Giacomo Pati</a>
67  * (PWR Organisation &amp; Entwicklung)
68  * @version CVS $Id: LogTransformer.java 153376 2005-02-11 08:50:21Z cziegeler $
69  *
70  */

71 public class LogTransformer
72   extends AbstractTransformer {
73
74     private static String JavaDoc lf = System.getProperty("line.separator", "\n");
75
76     /** log file */
77     private FileWriter JavaDoc logfile;
78
79     /**
80      * Setup
81      */

82     public void setup(SourceResolver resolver, Map JavaDoc objectModel,
83                       String JavaDoc src, Parameters parameters)
84     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
85         final boolean append = parameters.getParameterAsBoolean("append", false);
86         final String JavaDoc logfilename = parameters.getParameter("logfile", null);
87
88         // Check for null, use System.out if logfile is not specified.
89
this.logfile = null;
90         if ( null != logfilename ) {
91             Source source = null;
92             try {
93                 source = resolver.resolveURI( logfilename );
94                 final String JavaDoc systemId = source.getURI();
95                 if ( systemId.startsWith("file:") ) {
96                     this.logfile = new FileWriter JavaDoc(systemId.substring(5), append );
97                 } else {
98                     throw new ProcessingException("The logfile parameter must point to a file: " + logfilename);
99                 }
100             } catch (SourceException se) {
101                 throw SourceUtil.handle(se);
102             } finally {
103                 resolver.release( source );
104             }
105         }
106
107         Date JavaDoc date = new Date JavaDoc();
108         StringBuffer JavaDoc logEntry = new StringBuffer JavaDoc();
109         logEntry.append ( "---------------------------- [" );
110         logEntry.append ( date.toString() );
111         logEntry.append ( "] ----------------------------" );
112         this.log("setup", logEntry.toString());
113     }
114
115     /**
116      * Recycle
117      */

118     public void recycle() {
119         super.recycle();
120         try {
121             if (this.logfile != null) logfile.close();
122         } catch (Exception JavaDoc e) {
123             this.getLogger().warn("LogTransformer.recycle()", e);
124         }
125         this.logfile = null;
126     }
127
128     /**
129      * Receive an object for locating the origin of SAX document events.
130      */

131     public void setDocumentLocator(Locator JavaDoc locator) {
132         this.log("setDocumentLocator", locator != null ? "systemid="+locator.getSystemId()+",publicid="+locator.getPublicId() : "(locator is null)");
133         if (super.contentHandler!=null) {
134             super.contentHandler.setDocumentLocator(locator);
135         }
136     }
137
138     /**
139      * Receive notification of the beginning of a document.
140      */

141     public void startDocument()
142     throws SAXException JavaDoc {
143         this.log("startDocument", "");
144         if (super.contentHandler!=null) {
145             super.contentHandler.startDocument();
146         }
147     }
148
149     /**
150      * Receive notification of the end of a document.
151      */

152     public void endDocument()
153     throws SAXException JavaDoc {
154         this.log ("endDocument", "");
155         if (super.contentHandler!=null) {
156             super.contentHandler.endDocument();
157         }
158     }
159
160     /**
161      * Begin the scope of a prefix-URI Namespace mapping.
162      */

163     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
164     throws SAXException JavaDoc {
165         this.log ("startPrefixMapping", "prefix="+prefix+",uri="+uri);
166         if (super.contentHandler!=null) {
167             super.contentHandler.startPrefixMapping(prefix,uri);
168         }
169     }
170
171     /**
172      * End the scope of a prefix-URI mapping.
173      */

174     public void endPrefixMapping(String JavaDoc prefix)
175     throws SAXException JavaDoc {
176         this.log ("endPrefixMapping", "prefix="+prefix);
177         if (super.contentHandler!=null) {
178             super.contentHandler.endPrefixMapping(prefix);
179         }
180     }
181
182     /**
183      * Receive notification of the beginning of an element.
184      */

185     public void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a)
186     throws SAXException JavaDoc {
187         this.log ("startElement", "uri="+uri+",local="+loc+",raw="+raw);
188         for (int i = 0; i < a.getLength(); i++) {
189             this.log (" ", Integer.toString(i+1)
190                  +". uri="+a.getURI(i)
191                  +",local="+a.getLocalName(i)
192                  +",qname="+a.getQName(i)
193                  +",type="+a.getType(i)
194                  +",value="+a.getValue(i));
195         }
196         if (super.contentHandler!=null) {
197             super.contentHandler.startElement(uri,loc,raw,a);
198         }
199     }
200
201
202     /**
203      * Receive notification of the end of an element.
204      */

205     public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw)
206     throws SAXException JavaDoc {
207         this.log ("endElement", "uri="+uri+",local="+loc+",raw="+raw);
208         if (super.contentHandler!=null) {
209             super.contentHandler.endElement(uri,loc,raw);
210         }
211     }
212
213     /**
214      * Receive notification of character data.
215      */

216     public void characters(char ch[], int start, int len)
217     throws SAXException JavaDoc {
218         this.log ("characters", new String JavaDoc(ch,start,len));
219         if (super.contentHandler!=null) {
220             super.contentHandler.characters(ch,start,len);
221         }
222     }
223
224     /**
225      * Receive notification of ignorable whitespace in element content.
226      */

227     public void ignorableWhitespace(char ch[], int start, int len)
228     throws SAXException JavaDoc {
229         this.log ("ignorableWhitespace", new String JavaDoc(ch,start,len));
230         if (super.contentHandler!=null) {
231             super.contentHandler.ignorableWhitespace(ch,start,len);
232         }
233     }
234
235     /**
236      * Receive notification of a processing instruction.
237      */

238     public void processingInstruction(String JavaDoc target, String JavaDoc data)
239     throws SAXException JavaDoc {
240         log ("processingInstruction", "target="+target+",data="+data);
241         if (super.contentHandler!=null) {
242             super.contentHandler.processingInstruction(target,data);
243         }
244     }
245
246     /**
247      * Receive notification of a skipped entity.
248      */

249     public void skippedEntity(String JavaDoc name)
250     throws SAXException JavaDoc {
251         this.log ("skippedEntity", "name="+name);
252         if (super.contentHandler!=null) {
253             super.contentHandler.skippedEntity(name);
254         }
255     }
256
257     /**
258      * Report the start of DTD declarations, if any.
259      */

260     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
261     throws SAXException JavaDoc {
262         this.log ("startDTD", "name="+name+",publicId="+publicId+",systemId="+systemId);
263         if (super.lexicalHandler!=null) {
264             super.lexicalHandler.startDTD(name,publicId,systemId);
265         }
266     }
267
268     /**
269      * Report the end of DTD declarations.
270      */

271     public void endDTD()
272     throws SAXException JavaDoc {
273         this.log ("endDTD", "");
274         if (super.lexicalHandler!=null) {
275             super.lexicalHandler.endDTD();
276         }
277     }
278
279     /**
280      * Report the beginning of an entity.
281      */

282     public void startEntity(String JavaDoc name)
283     throws SAXException JavaDoc {
284         this.log ("startEntity", "name="+name);
285         if (super.lexicalHandler!=null) {
286             super.lexicalHandler.startEntity(name);
287         }
288     }
289
290     /**
291      * Report the end of an entity.
292      */

293     public void endEntity(String JavaDoc name)
294     throws SAXException JavaDoc {
295         this.log ("endEntity", "name="+name);
296         if (super.lexicalHandler!=null) {
297             super.lexicalHandler.endEntity(name);
298         }
299     }
300
301     /**
302      * Report the start of a CDATA section.
303      */

304     public void startCDATA()
305     throws SAXException JavaDoc {
306         this.log ("startCDATA", "");
307         if (super.lexicalHandler!=null) {
308             super.lexicalHandler.startCDATA();
309         }
310     }
311
312     /**
313      * Report the end of a CDATA section.
314      */

315     public void endCDATA()
316     throws SAXException JavaDoc {
317         this.log ("endCDATA", "");
318         if (super.lexicalHandler!=null) {
319             super.lexicalHandler.endCDATA();
320         }
321     }
322
323     /**
324      * Report an XML comment anywhere in the document.
325      */

326     public void comment(char ch[], int start, int len)
327     throws SAXException JavaDoc {
328         this.log ("comment", new String JavaDoc(ch,start,len));
329         if (super.lexicalHandler!=null) {
330             super.lexicalHandler.comment(ch,start,len);
331         }
332     }
333
334     /**
335      * Report to logfile.
336      */

337     private void log (String JavaDoc location, String JavaDoc description) {
338         final StringBuffer JavaDoc logEntry = new StringBuffer JavaDoc();
339         logEntry.append ( "[" );
340         logEntry.append ( location );
341         logEntry.append ( "] " );
342         logEntry.append ( description );
343         logEntry.append ( lf );
344         final String JavaDoc text = logEntry.toString();
345         if ( this.getLogger().isInfoEnabled() ) {
346             this.getLogger().info( text );
347         }
348         try {
349             if ( null != this.logfile ) {
350                 this.logfile.write( text, 0, text.length());
351                 this.logfile.flush();
352             } else {
353                 System.out.println( text );
354             }
355         }
356         catch(IOException JavaDoc ioe) {
357             this.getLogger().debug("LogTransformer.log", ioe);
358         }
359     }
360
361     /**
362      * Attempt to close the log file when the class is GC'd
363      */

364     public void destroy() {
365         try {
366             if (this.logfile != null) logfile.close();
367         } catch (Exception JavaDoc e) {getLogger().debug("LogTransformer.destroy()", e);}
368     }
369 }
370
Popular Tags