source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/jfc/JFCLogParser.java @ 359

Last change on this file since 359 was 359, checked in by sherbold, 12 years ago
  • adapted de.ugoe.cs.eventbench.jfc.data.JFCEvent and de.ugoe.cs.eventbench.jfc.JFCLogParser create JFCEvents with more detailed information about the message target
  • Property svn:mime-type set to text/plain
File size: 6.4 KB
Line 
1package de.ugoe.cs.eventbench.jfc;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.io.InputStreamReader;
8import java.io.UnsupportedEncodingException;
9import java.security.InvalidParameterException;
10import java.util.Collection;
11import java.util.LinkedList;
12import java.util.List;
13
14import javax.xml.parsers.ParserConfigurationException;
15import javax.xml.parsers.SAXParser;
16import javax.xml.parsers.SAXParserFactory;
17
18import org.xml.sax.Attributes;
19import org.xml.sax.InputSource;
20import org.xml.sax.SAXException;
21import org.xml.sax.SAXParseException;
22import org.xml.sax.helpers.DefaultHandler;
23
24import de.ugoe.cs.eventbench.jfc.data.JFCEvent;
25import de.ugoe.cs.util.console.Console;
26
27/**
28 * <p>
29 * This class provides functionality to parse XML log files generated by the
30 * JFCMonitor of EventBench. The result of parsing a file is a collection of
31 * event sequences.
32 * </p>
33 *
34 * @author Steffen Herbold
35 * @version 1.0
36 */
37public class JFCLogParser extends DefaultHandler {
38
39        /**
40         * <p>
41         * Collection of event sequences that is contained in the log file, which is
42         * parsed.
43         * </p>
44         */
45        private Collection<List<JFCEvent>> sequences;
46
47        /**
48         * <p>
49         * Internal handle to the event that is currently being parsed.
50         * </p>
51         */
52        private JFCEvent currentEvent;
53
54        /**
55         * <p>
56         * Internal handle to the event sequence that is currently being parsed.
57         * </p>
58         */
59        private List<JFCEvent> currentSequence = null;
60       
61        /**
62         * <p>
63         * Internally used string to build a string representing a component-node.
64         * </p>
65         */
66        private String componentString;
67
68        /**
69         * <p>
70         * Enumeration to differentiate if a parameter belongs to an event, a source
71         * or the parent of a source.
72         * </p>
73         *
74         * @author Steffen Herbold
75         * @version 1.0
76         */
77        private enum ParamSource {
78                EVENT, SOURCE, PARENT, COMPONENT
79        };
80
81        /**
82         * <p>
83         * Specifies whether the parameters that are currently being read belong the
84         * the event, the source or the parent.
85         * </p>
86         */
87        ParamSource paramSource = null;
88
89        /**
90         * <p>
91         * Constructor. Creates a new JFCLogParser.
92         * </p>
93         */
94        public JFCLogParser() {
95                sequences = new LinkedList<List<JFCEvent>>();
96                currentSequence = new LinkedList<JFCEvent>();
97        }
98
99        /**
100         * <p>
101         * Returns the collection of event sequences that is obtained from parsing
102         * log files.
103         * </p>
104         *
105         * @return collection of event sequences
106         */
107        public Collection<List<JFCEvent>> getSequences() {
108                return sequences;
109        }
110
111        /*
112         * (non-Javadoc)
113         *
114         * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
115         * java.lang.String, java.lang.String, org.xml.sax.Attributes)
116         */
117        public void startElement(String uri, String localName, String qName,
118                        Attributes atts) throws SAXException {
119                if( qName.equals("sessions") ) {
120                        currentSequence = new LinkedList<JFCEvent>();
121                }
122                if (qName.equals("newsession")) {
123                        Console.traceln("start of session");
124                        if (currentSequence != null && !currentSequence.isEmpty()) {
125                                sequences.add(currentSequence);
126                        }
127                        currentSequence = new LinkedList<JFCEvent>();
128                } else if (qName.equals("event")) {
129                        currentEvent = new JFCEvent(atts.getValue("id"));
130                        paramSource = ParamSource.EVENT;
131                } else if (qName.equals("param")) {
132                        if (paramSource == ParamSource.EVENT) {
133                                currentEvent.addParameter(atts.getValue("name"),
134                                                atts.getValue("value"));
135                        } else if (paramSource == ParamSource.SOURCE) {
136                                currentEvent.addSourceInformation(atts.getValue("name"),
137                                                atts.getValue("value"));
138                        } else if (paramSource == ParamSource.PARENT) {
139                                currentEvent.addParentInformation(atts.getValue("name"),
140                                                atts.getValue("value"));
141                        } else if (paramSource == ParamSource.COMPONENT) {
142                                componentString += "'" + atts.getValue("value") + "',";
143                        }
144                } else if (qName.equals("source")) {
145                        paramSource = ParamSource.SOURCE;
146                } else if (qName.equals("parent")) {
147                        paramSource = ParamSource.PARENT;
148                } else if (qName.equals("component")) {
149                        paramSource = ParamSource.COMPONENT;
150                        componentString = "[";
151                }
152        }
153
154        /*
155         * (non-Javadoc)
156         *
157         * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
158         * java.lang.String, java.lang.String)
159         */
160        @Override
161        public void endElement(String uri, String localName, String qName)
162                        throws SAXException {
163                if (qName.equals("event")) {
164                        currentSequence.add(currentEvent);
165                } else if (qName.equals("source")) {
166                        paramSource = ParamSource.EVENT;
167                } else if (qName.equals("parent")) {
168                        paramSource = ParamSource.SOURCE;
169                } else if (qName.equals("sessions")) {
170                        if(currentSequence!=null && !currentSequence.isEmpty() ) {
171                                sequences.add(currentSequence);
172                        }
173                        currentSequence = null;
174                } else if (qName.equals("component")) {
175                        paramSource.equals(ParamSource.SOURCE);
176                        currentEvent.extendTarget(componentString.substring(0, componentString.length()-1)+"]");
177                }
178        }
179
180        /**
181         * <p>
182         * Parses a log file written by the JFCMonitor and creates a collection of
183         * event sequences.
184         * </p>
185         *
186         * @param filename
187         *            name and path of the log file
188         */
189        public void parseFile(String filename) {
190                if (filename == null) {
191                        throw new InvalidParameterException("filename must not be null");
192                }
193
194                SAXParserFactory spf = SAXParserFactory.newInstance();
195                spf.setValidating(true);
196
197                SAXParser saxParser = null;
198                InputSource inputSource = null;
199                try {
200                        saxParser = spf.newSAXParser();
201                        inputSource = new InputSource(new InputStreamReader(
202                                        new FileInputStream(filename), "UTF-16"));
203                } catch (UnsupportedEncodingException e) {
204                        e.printStackTrace();
205                } catch (ParserConfigurationException e) {
206                        e.printStackTrace();
207                } catch (SAXException e) {
208                        e.printStackTrace();
209                } catch (FileNotFoundException e) {
210                        e.printStackTrace();
211                }
212                if (inputSource != null) {
213                        inputSource.setSystemId("file://"
214                                        + new File(filename).getAbsolutePath());
215                        try {
216                                if (saxParser == null) {
217                                        throw new RuntimeException("SAXParser creation failed");
218                                }
219                                saxParser.parse(inputSource, this);
220                        } catch (SAXParseException e) {
221                                Console.printerrln("Failure parsing file in line "
222                                                + e.getLineNumber() + ", column " + e.getColumnNumber()
223                                                + ".");
224                                e.printStackTrace();
225                        } catch (SAXException e) {
226                                e.printStackTrace();
227                        } catch (IOException e) {
228                                e.printStackTrace();
229                        }
230                }
231        }
232
233}
Note: See TracBrowser for help on using the repository browser.