Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package tools.tracesviewer;
  
  //ifdef J2SDK1.4
  //endif
  
  import org.xml.sax.*;
 
 import java.io.*;
 import java.util.*;

Parse the log files - sort them and organize by call id.
 
 public class LogFileParser extends DefaultHandler {
     protected Hashtable messageLogs;
 
     private XMLReader xmlReader;
 
     private String transactionId;
 
     private String from;
 
     private String to;
 
     private String time;
 
     private String statusMessage;
 
     private String firstLine;
 
     private String callId;
 
     private String isSender;
 
     private String messageTimeStamp;
 
     private StringBuffer message;
 
     public String logDescription;
 
     public String auxInfo;
 
     public String logName;
 
     private String currentTag;
 
     private String debugLine;
 
     private String previousDebug = null;
 
     private TracesMessage messageLog;
 
     public LogFileParser() {
          = new Hashtable();
         try {
             SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
             SAXParser saxParser = saxParserFactory.newSAXParser();
             this. = saxParser.getXMLReader();
 
             .setContentHandler(this);
 
             
                     .setFeature("http://xml.org/sax/features/validation"false);
             // parse the xml specification for the event tags.
 
         } catch (Exception pce) {
             // Parser with specified options can't be built
             pce.printStackTrace();
         }
 
     }
 
     public Hashtable parseLogs(InputSource inputSource) {
         try {
             this..parse(inputSource);
             return ;
         } catch (SAXParseException spe) {
             spe.printStackTrace();
         } catch (SAXException sxe) {
             sxe.printStackTrace();
         } catch (IOException ioe) {
             // I/O error
             ioe.printStackTrace();
         }
         return ;
     }
 
     // ===========================================================
     // SAX DocumentHandler methods
     // ===========================================================
 
     public void startDocument() throws SAXException {
     }
 
     public void endDocument() throws SAXException {
    }
    public void startElement(String namespaceURIString lName// local name
            String qName// qualified name
            Attributes attrsthrows SAXException {
         = qName;
        // System.out.println("currentTag:"+currentTag);
        if (qName.equalsIgnoreCase("debug")) {
        }
        if (qName.equalsIgnoreCase("message")) {
             = attrs.getValue("from");
             = attrs.getValue("to");
             = attrs.getValue("time");
             = attrs.getValue("statusMessage");
             = attrs.getValue("transactionId");
             = attrs.getValue("firstLine");
             = attrs.getValue("callId");
             = attrs.getValue("isSender");
             = attrs.getValue("debugLine");
             = attrs.getValue("timeStamp");
             = new StringBuffer();
        }
        if (qName.equalsIgnoreCase("description")) {
             = attrs.getValue("name");
             = attrs.getValue("logDescription");
             = attrs.getValue("auxInfo");
        }
    }
    public void endElement(String namespaceURIString sName// simple name
            String qName // qualified name
    ) throws SAXException {
        if (qName.equalsIgnoreCase("message")) {
            boolean sflag = .equals("true");
             = new TracesMessage(
                    .toString(), ,
                    );
            MessageLogList messageLogList = (MessageLogList
                    .get();
            if (messageLogList == null) {
                messageLogList = new MessageLogList(new LogComparator());
                .put(messageLogList);
            }
            if ( !messageLogList.contains())
                messageLogList.add();
        }
    }
    public void characters(char buf[], int offsetint lenthrows SAXException {
        if (buf == null)
            return;
        if (.equalsIgnoreCase("message")) {
            // StringBuffer s = new StringBuffer();
            // s.append(new String(buf, offset, len));
            String str = new String(bufoffsetlen); // s.toString().trim();
            if (str.equals(""))
                return;
            .append(str);
        }
        if (.equalsIgnoreCase("debug")) {
            StringBuffer s = new StringBuffer();
            s.append(new String(bufoffsetlen));
            String str = s.toString().trim();
            if (str.equals(""))
                return;
            // System.out.println("cdata:"+str);
            if ( != null) {
                . = ;
                // System.out.println("messageLog.beforeDebug:"+messageLog.beforeDebug);
                . = str;
                // System.out.println("messageLog.afterDebug:"+messageLog.afterDebug);
                 = str;
            } else {
                 = str;
            }
        }
    }

    
Generate a file that can be digested by the trace viewer.
    public Hashtable parseLogsFromDebugFile(String logFileName) {
        try {
            // FileWriter fw=new FileWriter(logFileName);
            // fw.write("]]></debug>");
            File file = new File(logFileName);
            long length = file.length();
            char[] cbuf = new char[(intlength];
            FileReader fr = new FileReader(file);
            fr.read(cbuf);
            fr.close();
            StringBuffer sb = new StringBuffer();
            sb.append("<?xml version='1.0' encoding='us-ascii'?>\n").append(
                    "<messages>\n").append(new String(cbuf)).append(
                    "]]></debug></messages>\n");
            // System.out.println(sb.toString());
            InputSource inputSource = new InputSource(new ByteArrayInputStream(
                    sb.toString().getBytes()));
            return this.parseLogs(inputSource);
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    
Generate a file that can be digested by the trace viewer.
    public Hashtable parseLogsFromFile(String logFileName) {
        try {
            // FileWriter fw=new FileWriter(logFileName);
            // fw.write("]]></debug>");
            File file = new File(logFileName);
            long length = file.length();
            char[] cbuf = new char[(intlength];
            FileReader fr = new FileReader(file);
            fr.read(cbuf);
            fr.close();
            StringBuffer sb = new StringBuffer();
            sb.append("<?xml version='1.0' encoding='us-ascii'?>\n").append(
                    "<messages>\n").append(new String(cbuf)).append(
                    "</messages>\n");
            ..println(sb.toString());
            InputSource inputSource = new InputSource(new ByteArrayInputStream(
                    sb.toString().getBytes()));
            return this.parseLogs(inputSource);
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }
    public Hashtable parseLogsFromString(String logString) {
        StringBuffer sb = new StringBuffer();
        sb.append("<?xml version='1.0' encoding='us-ascii'?>\n").append(
                "<messages>\n").append(logString).append("</messages>\n");
        // System.out.println(sb.toString());
        InputSource inputSource = new InputSource(new ByteArrayInputStream(sb
                .toString().getBytes()));
        return this.parseLogs(inputSource);
    }
New to GrepCode? Check out our FAQ X