1 16 package org.apache.log4j.chainsaw; 17 18 import java.util.StringTokenizer ; 19 import org.apache.log4j.Level; 20 import org.xml.sax.Attributes ; 21 import org.xml.sax.SAXException ; 22 import org.xml.sax.helpers.DefaultHandler ; 23 24 31 class XMLFileHandler 32 extends DefaultHandler 33 { 34 35 private static final String TAG_EVENT = "log4j:event"; 36 37 private static final String TAG_MESSAGE = "log4j:message"; 38 39 private static final String TAG_NDC = "log4j:NDC"; 40 41 private static final String TAG_THROWABLE = "log4j:throwable"; 42 43 private static final String TAG_LOCATION_INFO = "log4j:locationInfo"; 44 45 46 private final MyTableModel mModel; 47 48 private int mNumEvents; 49 50 51 private long mTimeStamp; 52 53 private Level mLevel; 54 55 private String mCategoryName; 56 57 private String mNDC; 58 59 private String mThreadName; 60 61 private String mMessage; 62 63 private String [] mThrowableStrRep; 64 65 private String mLocationDetails; 66 67 private final StringBuffer mBuf = new StringBuffer (); 68 69 74 XMLFileHandler(MyTableModel aModel) { 75 mModel = aModel; 76 } 77 78 79 public void startDocument() 80 throws SAXException 81 { 82 mNumEvents = 0; 83 } 84 85 86 public void characters(char[] aChars, int aStart, int aLength) { 87 mBuf.append(String.valueOf(aChars, aStart, aLength)); 88 } 89 90 91 public void endElement(String aNamespaceURI, 92 String aLocalName, 93 String 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 st = 104 new StringTokenizer (mBuf.toString(), "\n\t"); 105 mThrowableStrRep = new String [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 116 public void startElement(String aNamespaceURI, 117 String aLocalName, 118 String aQName, 119 Attributes 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 137 int getNumEvents() { 138 return mNumEvents; 139 } 140 141 145 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 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 |