001: package tools.tracesviewer;
002:
003: //ifdef J2SDK1.4
004: import javax.xml.parsers.SAXParser;
005: import javax.xml.parsers.SAXParserFactory; //endif
006:
007: import org.xml.sax.*;
008: import org.xml.sax.helpers.DefaultHandler;
009: import org.xml.sax.SAXException;
010:
011: import java.io.*;
012: import java.util.*;
013:
014: /**
015: * Parse the log files - sort them and organize by call id.
016: */
017: public class LogFileParser extends DefaultHandler {
018: protected Hashtable messageLogs;
019:
020: private XMLReader xmlReader;
021:
022: private String transactionId;
023:
024: private String from;
025:
026: private String to;
027:
028: private String time;
029:
030: private String statusMessage;
031:
032: private String firstLine;
033:
034: private String callId;
035:
036: private String isSender;
037:
038: private String messageTimeStamp;
039:
040: private StringBuffer message;
041:
042: public String logDescription;
043:
044: public String auxInfo;
045:
046: public String logName;
047:
048: private String currentTag;
049:
050: private String debugLine;
051:
052: private String previousDebug = null;
053:
054: private TracesMessage messageLog;
055:
056: public LogFileParser() {
057: messageLogs = new Hashtable();
058: try {
059: SAXParserFactory saxParserFactory = SAXParserFactory
060: .newInstance();
061: SAXParser saxParser = saxParserFactory.newSAXParser();
062: this .xmlReader = saxParser.getXMLReader();
063:
064: xmlReader.setContentHandler(this );
065:
066: xmlReader.setFeature(
067: "http://xml.org/sax/features/validation", false);
068: // parse the xml specification for the event tags.
069:
070: } catch (Exception pce) {
071: // Parser with specified options can't be built
072: pce.printStackTrace();
073: }
074:
075: }
076:
077: public Hashtable parseLogs(InputSource inputSource) {
078: try {
079: this .xmlReader.parse(inputSource);
080: return messageLogs;
081: } catch (SAXParseException spe) {
082: spe.printStackTrace();
083: } catch (SAXException sxe) {
084: sxe.printStackTrace();
085: } catch (IOException ioe) {
086: // I/O error
087: ioe.printStackTrace();
088: }
089: return messageLogs;
090: }
091:
092: // ===========================================================
093: // SAX DocumentHandler methods
094: // ===========================================================
095:
096: public void startDocument() throws SAXException {
097: }
098:
099: public void endDocument() throws SAXException {
100: }
101:
102: public void startElement(String namespaceURI, String lName, // local name
103: String qName, // qualified name
104: Attributes attrs) throws SAXException {
105: currentTag = qName;
106:
107: // System.out.println("currentTag:"+currentTag);
108: if (qName.equalsIgnoreCase("debug")) {
109:
110: }
111: if (qName.equalsIgnoreCase("message")) {
112: from = attrs.getValue("from");
113: to = attrs.getValue("to");
114: time = attrs.getValue("time");
115: statusMessage = attrs.getValue("statusMessage");
116: transactionId = attrs.getValue("transactionId");
117: firstLine = attrs.getValue("firstLine");
118: callId = attrs.getValue("callId");
119: isSender = attrs.getValue("isSender");
120: debugLine = attrs.getValue("debugLine");
121: messageTimeStamp = attrs.getValue("timeStamp");
122: message = new StringBuffer();
123:
124: }
125: if (qName.equalsIgnoreCase("description")) {
126: logName = attrs.getValue("name");
127: logDescription = attrs.getValue("logDescription");
128: auxInfo = attrs.getValue("auxInfo");
129: }
130:
131: }
132:
133: public void endElement(String namespaceURI, String sName, // simple name
134: String qName // qualified name
135: ) throws SAXException {
136: if (qName.equalsIgnoreCase("message")) {
137: boolean sflag = isSender.equals("true");
138: messageLog = new TracesMessage(from, to, time, firstLine,
139: message.toString(), statusMessage, transactionId,
140: messageTimeStamp, debugLine);
141:
142: MessageLogList messageLogList = (MessageLogList) messageLogs
143: .get(callId);
144: if (messageLogList == null) {
145: messageLogList = new MessageLogList(new LogComparator());
146: messageLogs.put(callId, messageLogList);
147: }
148: if (!messageLogList.contains(messageLog))
149: messageLogList.add(messageLog);
150: }
151: }
152:
153: public void characters(char buf[], int offset, int len)
154: throws SAXException {
155: if (buf == null)
156: return;
157:
158: if (currentTag.equalsIgnoreCase("message")) {
159: // StringBuffer s = new StringBuffer();
160: // s.append(new String(buf, offset, len));
161:
162: String str = new String(buf, offset, len); // s.toString().trim();
163:
164: if (str.equals(""))
165: return;
166: message.append(str);
167:
168: }
169:
170: if (currentTag.equalsIgnoreCase("debug")) {
171: StringBuffer s = new StringBuffer();
172: s.append(new String(buf, offset, len));
173: String str = s.toString().trim();
174:
175: if (str.equals(""))
176: return;
177:
178: // System.out.println("cdata:"+str);
179:
180: if (messageLog != null) {
181: messageLog.beforeDebug = previousDebug;
182: // System.out.println("messageLog.beforeDebug:"+messageLog.beforeDebug);
183: messageLog.afterDebug = str;
184: // System.out.println("messageLog.afterDebug:"+messageLog.afterDebug);
185: previousDebug = str;
186: } else {
187: previousDebug = str;
188: }
189: }
190: }
191:
192: /**
193: * Generate a file that can be digested by the trace viewer.
194: */
195: public Hashtable parseLogsFromDebugFile(String logFileName) {
196: try {
197:
198: // FileWriter fw=new FileWriter(logFileName);
199: // fw.write("]]></debug>");
200: File file = new File(logFileName);
201: long length = file.length();
202: char[] cbuf = new char[(int) length];
203: FileReader fr = new FileReader(file);
204: fr.read(cbuf);
205: fr.close();
206:
207: StringBuffer sb = new StringBuffer();
208: sb.append("<?xml version='1.0' encoding='us-ascii'?>\n")
209: .append("<messages>\n").append(new String(cbuf))
210: .append("]]></debug></messages>\n");
211:
212: // System.out.println(sb.toString());
213:
214: InputSource inputSource = new InputSource(
215: new ByteArrayInputStream(sb.toString().getBytes()));
216: return this .parseLogs(inputSource);
217: } catch (IOException ex) {
218: ex.printStackTrace();
219:
220: return null;
221: }
222: }
223:
224: /**
225: * Generate a file that can be digested by the trace viewer.
226: */
227: public Hashtable parseLogsFromFile(String logFileName) {
228: try {
229:
230: // FileWriter fw=new FileWriter(logFileName);
231: // fw.write("]]></debug>");
232: File file = new File(logFileName);
233: long length = file.length();
234: char[] cbuf = new char[(int) length];
235: FileReader fr = new FileReader(file);
236: fr.read(cbuf);
237: fr.close();
238:
239: StringBuffer sb = new StringBuffer();
240: sb.append("<?xml version='1.0' encoding='us-ascii'?>\n")
241: .append("<messages>\n").append(new String(cbuf))
242: .append("</messages>\n");
243:
244: System.out.println(sb.toString());
245:
246: InputSource inputSource = new InputSource(
247: new ByteArrayInputStream(sb.toString().getBytes()));
248: return this .parseLogs(inputSource);
249: } catch (IOException ex) {
250: ex.printStackTrace();
251:
252: return null;
253: }
254: }
255:
256: public Hashtable parseLogsFromString(String logString) {
257: StringBuffer sb = new StringBuffer();
258: sb.append("<?xml version='1.0' encoding='us-ascii'?>\n")
259: .append("<messages>\n").append(logString).append(
260: "</messages>\n");
261: // System.out.println(sb.toString());
262: InputSource inputSource = new InputSource(
263: new ByteArrayInputStream(sb.toString().getBytes()));
264: return this.parseLogs(inputSource);
265: }
266:
267: }
|