KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > chainsaw > XMLFileHandler


1 /*
2  * Copyright 1999-2005 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.log4j.chainsaw;
17
18 import java.util.StringTokenizer JavaDoc;
19 import org.apache.log4j.Level;
20 import org.xml.sax.Attributes JavaDoc;
21 import org.xml.sax.SAXException JavaDoc;
22 import org.xml.sax.helpers.DefaultHandler JavaDoc;
23
24 /**
25  * A content handler for document containing Log4J events logged using the
26  * XMLLayout class. It will create events and add them to a supplied model.
27  *
28  * @author <a HREF="mailto:oliver@puppycrawl.com">Oliver Burn</a>
29  * @version 1.0
30  */

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

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

145     /** Add an event to the model **/
146     private void addEvent() {
147         mModel.addEvent(new EventDetails(mTimeStamp,
148                                          mLevel,
149                                          mCategoryName,
150                                          mNDC,
151                                          mThreadName,
152                                          mMessage,
153                                          mThrowableStrRep,
154                                          mLocationDetails));
155         mNumEvents++;
156     }
157
158     /** Reset the data for an event **/
159     private void resetData() {
160         mTimeStamp = 0;
161         mLevel = null;
162         mCategoryName = null;
163         mNDC = null;
164         mThreadName = null;
165         mMessage = null;
166         mThrowableStrRep = null;
167         mLocationDetails = null;
168     }
169 }
170
Popular Tags