KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > xml > LoggingContentHandler


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.xml;
17
18 import org.apache.avalon.framework.logger.AbstractLogEnabled;
19 import org.xml.sax.Attributes JavaDoc;
20 import org.xml.sax.Locator JavaDoc;
21 import org.xml.sax.SAXException JavaDoc;
22 import org.xml.sax.ContentHandler JavaDoc;
23
24 /**
25  * Logging content handler logs all events going through to the logger.
26  *
27  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
28  * @version CVS $Id: LoggingContentHandler.java 30932 2004-07-29 17:35:38Z vgritsenko $
29  */

30 public class LoggingContentHandler extends AbstractLogEnabled implements ContentHandler JavaDoc {
31
32     /**
33      * All debug messages from this handler are prefixed with this id.
34      */

35     String JavaDoc id;
36
37     /** The current <code>ContentHandler</code>. */
38     ContentHandler JavaDoc contentHandler;
39
40     /**
41      * Creates new <code>LoggingContentHandler</code> with specified
42      * <code>id</code> and destination <code>contentHandler</code>.
43      */

44     public LoggingContentHandler(String JavaDoc id, ContentHandler JavaDoc contentHandler) {
45         this.id = id;
46         this.contentHandler = contentHandler;
47     }
48
49     public void setDocumentLocator(Locator JavaDoc locator) {
50         log("setDocumentLocator", "");
51         contentHandler.setDocumentLocator(locator);
52     }
53
54     public void startDocument() throws SAXException JavaDoc {
55         log("startDocument", "");
56         this.contentHandler.startDocument();
57     }
58
59     public void endDocument() throws SAXException JavaDoc {
60         log ("endDocument", "");
61         this.contentHandler.endDocument();
62     }
63
64     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
65         log ("startPrefixMapping", "prefix="+prefix+",uri="+uri);
66         this.contentHandler.startPrefixMapping(prefix,uri);
67     }
68
69     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
70         log ("endPrefixMapping", "prefix="+prefix);
71         this.contentHandler.endPrefixMapping(prefix);
72     }
73
74     public void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a)
75     throws SAXException JavaDoc {
76         log ("startElement", "uri="+uri+",local="+loc+",raw="+raw);
77         for (int i = 0; i < a.getLength(); i++) {
78             log (" ", Integer.toString(i + 1)
79                  + ". uri=" + a.getURI(i)
80                  + ",local=" + a.getLocalName(i)
81                  + ",qname=" + a.getQName(i)
82                  + ",type=" + a.getType(i)
83                  + ",value=" + a.getValue(i));
84         }
85         this.contentHandler.startElement(uri,loc,raw,a);
86     }
87
88
89     public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc qname) throws SAXException JavaDoc {
90         log ("endElement", "uri="+uri+",local="+loc+",qname="+qname);
91         this.contentHandler.endElement(uri,loc,qname);
92     }
93
94     public void characters(char ch[], int start, int len) throws SAXException JavaDoc {
95         log ("characters", new String JavaDoc(ch,start,len));
96         this.contentHandler.characters(ch,start,len);
97     }
98
99     public void ignorableWhitespace(char ch[], int start, int len) throws SAXException JavaDoc {
100         log ("ignorableWhitespace", new String JavaDoc(ch,start,len));
101         this.contentHandler.ignorableWhitespace(ch,start,len);
102     }
103
104     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
105         log ("processingInstruction", "target="+target+",data="+data);
106         this.contentHandler.processingInstruction(target,data);
107     }
108
109     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
110         log ("skippedEntity", "name="+name);
111         this.contentHandler.skippedEntity(name);
112     }
113
114     private void log(String JavaDoc location, String JavaDoc description) {
115         StringBuffer JavaDoc logEntry = new StringBuffer JavaDoc();
116         logEntry.append(id);
117         logEntry.append("[");
118         logEntry.append(location);
119         logEntry.append("] ");
120         logEntry.append(description);
121         logEntry.append("\n");
122         getLogger().debug(logEntry.toString());
123         // System.out.print(logEntry.toString());
124
}
125 }
126
Popular Tags