KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > sqlprofiler > gui > XMLFileHandler


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software
5  * License version 1.1, a copy of which has been included with this
6  * distribution in the APACHE.txt file. */

7 package org.jahia.sqlprofiler.gui;
8
9 import java.util.StringTokenizer JavaDoc;
10 import org.apache.log4j.Priority;
11 import org.xml.sax.Attributes JavaDoc;
12 import org.xml.sax.SAXException JavaDoc;
13 import org.xml.sax.helpers.DefaultHandler JavaDoc;
14
15 /**
16  * A content handler for document containing Log4J events logged using the
17  * XMLLayout class. It will create events and add them to a supplied model.
18  *
19  * @author <a HREF="mailto:oliver@puppycrawl.com">Oliver Burn</a>
20  * @version 1.0
21  */

22 class XMLFileHandler
23     extends DefaultHandler JavaDoc
24 {
25     /** represents the event tag **/
26     private static final String JavaDoc TAG_EVENT = "log4j:event";
27     /** represents the message tag **/
28     private static final String JavaDoc TAG_MESSAGE = "log4j:message";
29     /** represents the ndc tag **/
30     private static final String JavaDoc TAG_NDC = "log4j:NDC";
31     /** represents the throwable tag **/
32     private static final String JavaDoc TAG_THROWABLE = "log4j:throwable";
33     /** represents the location info tag **/
34     private static final String JavaDoc TAG_LOCATION_INFO = "log4j:locationInfo";
35
36     /** where to put the events **/
37     private final LoggerTableModel mModel;
38     /** the number of events in the document **/
39     private int mNumEvents;
40
41     /** the time of the event **/
42     private long mTimeStamp;
43     /** the priority (level) of the event **/
44     private Priority mPriority;
45     /** the category of the event **/
46     private String JavaDoc mCategoryName;
47     /** the NDC for the event **/
48     private String JavaDoc mNDC;
49     /** the thread for the event **/
50     private String JavaDoc mThreadName;
51     /** the msg for the event **/
52     private String JavaDoc mMessage;
53     /** the throwable details the event **/
54     private String JavaDoc[] mThrowableStrRep;
55     /** the location details for the event **/
56     private String JavaDoc mLocationDetails;
57     /** buffer for collecting text **/
58     private final StringBuffer JavaDoc mBuf = new StringBuffer JavaDoc();
59
60     /**
61      * Creates a new <code>XMLFileHandler</code> instance.
62      *
63      * @param aModel where to add the events
64      */

65     XMLFileHandler(LoggerTableModel aModel) {
66         mModel = aModel;
67     }
68
69     /** @see DefaultHandler **/
70     public void startDocument()
71         throws SAXException JavaDoc
72     {
73         mNumEvents = 0;
74     }
75
76     /** @see DefaultHandler **/
77     public void characters(char[] aChars, int aStart, int aLength) {
78         mBuf.append(String.valueOf(aChars, aStart, aLength));
79     }
80
81     /** @see DefaultHandler **/
82     public void endElement(String JavaDoc aNamespaceURI,
83                            String JavaDoc aLocalName,
84                            String JavaDoc aQName)
85     {
86         if (TAG_EVENT.equals(aQName)) {
87             addEvent();
88             resetData();
89         } else if (TAG_NDC.equals(aQName)) {
90             mNDC = mBuf.toString();
91         } else if (TAG_MESSAGE.equals(aQName)) {
92             mMessage = mBuf.toString();
93         } else if (TAG_THROWABLE.equals(aQName)) {
94             final StringTokenizer JavaDoc st =
95                 new StringTokenizer JavaDoc(mBuf.toString(), "\n\t");
96             mThrowableStrRep = new String JavaDoc[st.countTokens()];
97             if (mThrowableStrRep.length > 0) {
98                 mThrowableStrRep[0] = st.nextToken();
99                 for (int i = 1; i < mThrowableStrRep.length; i++) {
100                     mThrowableStrRep[i] = "\t" + st.nextToken();
101                 }
102             }
103         }
104     }
105
106     /** @see DefaultHandler **/
107     public void startElement(String JavaDoc aNamespaceURI,
108                              String JavaDoc aLocalName,
109                              String JavaDoc aQName,
110                              Attributes JavaDoc aAtts)
111     {
112         mBuf.setLength(0);
113
114         if (TAG_EVENT.equals(aQName)) {
115             mThreadName = aAtts.getValue("thread");
116             mTimeStamp = Long.parseLong(aAtts.getValue("timestamp"));
117             mCategoryName = aAtts.getValue("logger");
118             mPriority = Priority.toPriority(aAtts.getValue("level"));
119         } else if (TAG_LOCATION_INFO.equals(aQName)) {
120             mLocationDetails = aAtts.getValue("class") + "."
121                 + aAtts.getValue("method")
122                 + "(" + aAtts.getValue("file") + ":" + aAtts.getValue("line")
123                 + ")";
124         }
125     }
126
127     /** @return the number of events in the document **/
128     int getNumEvents() {
129         return mNumEvents;
130     }
131
132     ////////////////////////////////////////////////////////////////////////////
133
// Private methods
134
////////////////////////////////////////////////////////////////////////////
135

136     /** Add an event to the model **/
137     private void addEvent() {
138         mModel.addEvent(new EventDetails(mTimeStamp,
139                                          mPriority,
140                                          mCategoryName,
141                                          mNDC,
142                                          mThreadName,
143                                          mMessage,
144                                          mThrowableStrRep,
145                                          mLocationDetails));
146         mNumEvents++;
147     }
148
149     /** Reset the data for an event **/
150     private void resetData() {
151         mTimeStamp = 0;
152         mPriority = null;
153         mCategoryName = null;
154         mNDC = null;
155         mThreadName = null;
156         mMessage = null;
157         mThrowableStrRep = null;
158         mLocationDetails = null;
159     }
160 }
161
Popular Tags