001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.xml;
018:
019: import org.apache.avalon.framework.logger.AbstractLogEnabled;
020: import org.xml.sax.Attributes;
021: import org.xml.sax.Locator;
022: import org.xml.sax.SAXException;
023: import org.xml.sax.ContentHandler;
024:
025: /**
026: * Logging content handler logs all events going through to the logger.
027: *
028: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
029: * @version CVS $Id: LoggingContentHandler.java 433543 2006-08-22 06:22:54Z crossley $
030: */
031: public class LoggingContentHandler extends AbstractLogEnabled implements
032: ContentHandler {
033:
034: /**
035: * All debug messages from this handler are prefixed with this id.
036: */
037: String id;
038:
039: /** The current <code>ContentHandler</code>. */
040: ContentHandler contentHandler;
041:
042: /**
043: * Creates new <code>LoggingContentHandler</code> with specified
044: * <code>id</code> and destination <code>contentHandler</code>.
045: */
046: public LoggingContentHandler(String id,
047: ContentHandler contentHandler) {
048: this .id = id;
049: this .contentHandler = contentHandler;
050: }
051:
052: public void setDocumentLocator(Locator locator) {
053: log("setDocumentLocator", "");
054: contentHandler.setDocumentLocator(locator);
055: }
056:
057: public void startDocument() throws SAXException {
058: log("startDocument", "");
059: this .contentHandler.startDocument();
060: }
061:
062: public void endDocument() throws SAXException {
063: log("endDocument", "");
064: this .contentHandler.endDocument();
065: }
066:
067: public void startPrefixMapping(String prefix, String uri)
068: throws SAXException {
069: log("startPrefixMapping", "prefix=" + prefix + ",uri=" + uri);
070: this .contentHandler.startPrefixMapping(prefix, uri);
071: }
072:
073: public void endPrefixMapping(String prefix) throws SAXException {
074: log("endPrefixMapping", "prefix=" + prefix);
075: this .contentHandler.endPrefixMapping(prefix);
076: }
077:
078: public void startElement(String uri, String loc, String raw,
079: Attributes a) throws SAXException {
080: log("startElement", "uri=" + uri + ",local=" + loc + ",raw="
081: + raw);
082: for (int i = 0; i < a.getLength(); i++) {
083: log(" ", Integer.toString(i + 1) + ". uri="
084: + a.getURI(i) + ",local=" + a.getLocalName(i)
085: + ",qname=" + a.getQName(i) + ",type="
086: + a.getType(i) + ",value=" + a.getValue(i));
087: }
088: this .contentHandler.startElement(uri, loc, raw, a);
089: }
090:
091: public void endElement(String uri, String loc, String qname)
092: throws SAXException {
093: log("endElement", "uri=" + uri + ",local=" + loc + ",qname="
094: + qname);
095: this .contentHandler.endElement(uri, loc, qname);
096: }
097:
098: public void characters(char ch[], int start, int len)
099: throws SAXException {
100: log("characters", new String(ch, start, len));
101: this .contentHandler.characters(ch, start, len);
102: }
103:
104: public void ignorableWhitespace(char ch[], int start, int len)
105: throws SAXException {
106: log("ignorableWhitespace", new String(ch, start, len));
107: this .contentHandler.ignorableWhitespace(ch, start, len);
108: }
109:
110: public void processingInstruction(String target, String data)
111: throws SAXException {
112: log("processingInstruction", "target=" + target + ",data="
113: + data);
114: this .contentHandler.processingInstruction(target, data);
115: }
116:
117: public void skippedEntity(String name) throws SAXException {
118: log("skippedEntity", "name=" + name);
119: this .contentHandler.skippedEntity(name);
120: }
121:
122: private void log(String location, String description) {
123: StringBuffer logEntry = new StringBuffer();
124: logEntry.append(id);
125: logEntry.append("[");
126: logEntry.append(location);
127: logEntry.append("] ");
128: logEntry.append(description);
129: logEntry.append("\n");
130: getLogger().debug(logEntry.toString());
131: // System.out.print(logEntry.toString());
132: }
133: }
|