001: /*
002: * Copyright (C) The Apache Software Foundation. All rights reserved.
003: *
004: * This software is published under the terms of the Apache Software
005: * License version 1.1, a copy of which has been included with this
006: * distribution in the APACHE.txt file. */
007: package org.jahia.sqlprofiler.gui;
008:
009: import java.util.StringTokenizer;
010: import org.apache.log4j.Priority;
011: import org.xml.sax.Attributes;
012: import org.xml.sax.SAXException;
013: import org.xml.sax.helpers.DefaultHandler;
014:
015: /**
016: * A content handler for document containing Log4J events logged using the
017: * XMLLayout class. It will create events and add them to a supplied model.
018: *
019: * @author <a href="mailto:oliver@puppycrawl.com">Oliver Burn</a>
020: * @version 1.0
021: */
022: class XMLFileHandler extends DefaultHandler {
023: /** represents the event tag **/
024: private static final String TAG_EVENT = "log4j:event";
025: /** represents the message tag **/
026: private static final String TAG_MESSAGE = "log4j:message";
027: /** represents the ndc tag **/
028: private static final String TAG_NDC = "log4j:NDC";
029: /** represents the throwable tag **/
030: private static final String TAG_THROWABLE = "log4j:throwable";
031: /** represents the location info tag **/
032: private static final String TAG_LOCATION_INFO = "log4j:locationInfo";
033:
034: /** where to put the events **/
035: private final LoggerTableModel mModel;
036: /** the number of events in the document **/
037: private int mNumEvents;
038:
039: /** the time of the event **/
040: private long mTimeStamp;
041: /** the priority (level) of the event **/
042: private Priority mPriority;
043: /** the category of the event **/
044: private String mCategoryName;
045: /** the NDC for the event **/
046: private String mNDC;
047: /** the thread for the event **/
048: private String mThreadName;
049: /** the msg for the event **/
050: private String mMessage;
051: /** the throwable details the event **/
052: private String[] mThrowableStrRep;
053: /** the location details for the event **/
054: private String mLocationDetails;
055: /** buffer for collecting text **/
056: private final StringBuffer mBuf = new StringBuffer();
057:
058: /**
059: * Creates a new <code>XMLFileHandler</code> instance.
060: *
061: * @param aModel where to add the events
062: */
063: XMLFileHandler(LoggerTableModel aModel) {
064: mModel = aModel;
065: }
066:
067: /** @see DefaultHandler **/
068: public void startDocument() throws SAXException {
069: mNumEvents = 0;
070: }
071:
072: /** @see DefaultHandler **/
073: public void characters(char[] aChars, int aStart, int aLength) {
074: mBuf.append(String.valueOf(aChars, aStart, aLength));
075: }
076:
077: /** @see DefaultHandler **/
078: public void endElement(String aNamespaceURI, String aLocalName,
079: String aQName) {
080: if (TAG_EVENT.equals(aQName)) {
081: addEvent();
082: resetData();
083: } else if (TAG_NDC.equals(aQName)) {
084: mNDC = mBuf.toString();
085: } else if (TAG_MESSAGE.equals(aQName)) {
086: mMessage = mBuf.toString();
087: } else if (TAG_THROWABLE.equals(aQName)) {
088: final StringTokenizer st = new StringTokenizer(mBuf
089: .toString(), "\n\t");
090: mThrowableStrRep = new String[st.countTokens()];
091: if (mThrowableStrRep.length > 0) {
092: mThrowableStrRep[0] = st.nextToken();
093: for (int i = 1; i < mThrowableStrRep.length; i++) {
094: mThrowableStrRep[i] = "\t" + st.nextToken();
095: }
096: }
097: }
098: }
099:
100: /** @see DefaultHandler **/
101: public void startElement(String aNamespaceURI, String aLocalName,
102: String aQName, Attributes aAtts) {
103: mBuf.setLength(0);
104:
105: if (TAG_EVENT.equals(aQName)) {
106: mThreadName = aAtts.getValue("thread");
107: mTimeStamp = Long.parseLong(aAtts.getValue("timestamp"));
108: mCategoryName = aAtts.getValue("logger");
109: mPriority = Priority.toPriority(aAtts.getValue("level"));
110: } else if (TAG_LOCATION_INFO.equals(aQName)) {
111: mLocationDetails = aAtts.getValue("class") + "."
112: + aAtts.getValue("method") + "("
113: + aAtts.getValue("file") + ":"
114: + aAtts.getValue("line") + ")";
115: }
116: }
117:
118: /** @return the number of events in the document **/
119: int getNumEvents() {
120: return mNumEvents;
121: }
122:
123: ////////////////////////////////////////////////////////////////////////////
124: // Private methods
125: ////////////////////////////////////////////////////////////////////////////
126:
127: /** Add an event to the model **/
128: private void addEvent() {
129: mModel.addEvent(new EventDetails(mTimeStamp, mPriority,
130: mCategoryName, mNDC, mThreadName, mMessage,
131: mThrowableStrRep, mLocationDetails));
132: mNumEvents++;
133: }
134:
135: /** Reset the data for an event **/
136: private void resetData() {
137: mTimeStamp = 0;
138: mPriority = null;
139: mCategoryName = null;
140: mNDC = null;
141: mThreadName = null;
142: mMessage = null;
143: mThrowableStrRep = null;
144: mLocationDetails = null;
145: }
146: }
|