source: trunk/autoquest-plugin-html/src/main/java/de/ugoe/cs/autoquest/plugin/html/NewHTMLLogParser.java @ 1047

Last change on this file since 1047 was 1047, checked in by fglaser, 11 years ago
  • First version of new HTMLLogParser added
  • Property svn:mime-type set to text/plain
File size: 10.3 KB
Line 
1
2package de.ugoe.cs.autoquest.plugin.html;
3
4import java.io.File;
5import java.io.FileInputStream;
6import java.io.FileNotFoundException;
7import java.io.IOException;
8import java.io.InputStreamReader;
9import java.io.UnsupportedEncodingException;
10import java.util.Collection;
11import java.util.HashMap;
12import java.util.LinkedList;
13import java.util.List;
14import java.util.Map;
15
16import javax.xml.parsers.ParserConfigurationException;
17import javax.xml.parsers.SAXParser;
18import javax.xml.parsers.SAXParserFactory;
19
20import org.xml.sax.Attributes;
21import org.xml.sax.InputSource;
22import org.xml.sax.SAXException;
23import org.xml.sax.SAXParseException;
24import org.xml.sax.helpers.DefaultHandler;
25
26import de.ugoe.cs.autoquest.eventcore.Event;
27import de.ugoe.cs.autoquest.eventcore.guimodel.GUIElementTree;
28import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
29import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
30import de.ugoe.cs.autoquest.plugin.html.eventcore.HTMLEventTypeFactory;
31import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLGUIElement;
32import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLGUIElementSpec;
33import de.ugoe.cs.util.console.Console;
34
35/**
36 * <p>
37 * This class provides the functionality to parse XML log files generated by the HTMLMonitor of
38 * autoquest. The result of parsing a file is a collection of event sequences.
39 * </p>
40 *
41 * @author Fabian Glaser
42 * @version 1.0
43 *
44 */
45public class NewHTMLLogParser extends DefaultHandler {
46    /**
47     * <p>
48     * Constructor. Creates a new HTMLLogParser.
49     * </p>
50     */
51    public NewHTMLLogParser() {
52        sequences = new LinkedList<List<Event>>();
53    }
54
55    /**
56     * <p>
57     * Collection of event sequences that is contained in the parsed log file.
58     * </p>
59     */
60    private Collection<List<Event>> sequences;
61   
62    /**
63     * <p>
64     * Internal handle to the parsed GUI structure, stored in a GUIElementTree
65     * </p>
66     */
67    private GUIElementTree<String> currentGUIElementTree;
68   
69    /**
70     * <p>
71     * Path of the GUI element currently being parsed.
72     * </p>
73     */
74    private String currentGUIElementPath;
75   
76    /**
77     * <p>
78     * Path of the parent of the GUI element currently being parsed.
79     * </p>
80     */
81    private String currentParentPath;
82   
83    /**
84     * <p>
85     * Source of the GUI element currently being parsed.
86     * </p>
87     */
88    private String currentEventSource;
89   
90    /**
91     * <p>
92     * Timestamp of the event currently being parsed.
93     * </p>
94     */
95    private Long currentEventTimestamp;
96   
97    /**
98     * <p>
99     * Internal handle to the parameters of the event currently being parsed.
100     * </p>
101     */
102    private Map<String, String> currentEventParameters;
103   
104    /**
105     * <p>
106     * Internal handle to the sequence currently being parsed.
107     * </p>
108     */
109    private List<Event> currentSequence;
110   
111    /**
112     * <p>
113     * Internal handle to type of the event currently being parsed.
114     * </p>
115     */
116    private String currentEventType;
117   
118    /**
119     * <p>
120     * Class of the GUI element currently being parsed.
121     * </p>
122     */
123    private String currentGUIElementClass;
124   
125    /**
126     * <p>
127     * Index of the GUI element currently being parsed.
128     * </p>
129     */
130    private String currentGUIElementIndex;
131   
132    /**
133     * Internal handle to the specification of the GUI element currently being parsed.
134     */
135    private HTMLGUIElementSpec currentGUIElementSpec;
136   
137    /**
138     * <p>
139     * internal handle to the GUI element of the previous event to be potentially reused for the
140     * current
141     * </p>
142     */
143    private IGUIElement lastGUIElement;
144   
145    /**
146     * <p>
147     * Parses a log file written by the HTMLMonitor and creates a collection of event sequences.
148     * </p>
149     *
150     * @param filename
151     *          name and path of the log file
152     */
153    public void parseFile(String filename) {
154        if (filename == null){
155            throw new IllegalArgumentException("filename must not be null");
156        }
157       
158        parseFile(new File(filename));
159    }
160    /**
161     * <p>
162     * Parses a log file written by the HTMLMonitor and creates a collection of event sequences.
163     * </p>
164     * @param file
165     *          file to be parsed
166     */
167    public void parseFile(File file) {
168        if (file == null){
169            throw new IllegalArgumentException("file must not be null");
170        }
171        SAXParserFactory spf = SAXParserFactory.newInstance();
172        spf.setValidating(true);
173        SAXParser saxParser = null;
174        InputSource inputSource = null;
175        try{
176            saxParser = spf.newSAXParser();
177            inputSource =
178                    new InputSource(new InputStreamReader(new FileInputStream(file), "UTF-8"));
179        }
180        catch (UnsupportedEncodingException e){
181            Console.printerr("Error parsing file " + file.getName());
182            Console.logException(e);
183            return;
184        }
185        catch (ParserConfigurationException e){
186            Console.printerr("Error parsing file " + file.getName());
187            Console.logException(e);
188            return;
189        }
190        catch (SAXException e){
191            Console.printerr("Error parsing file " + file.getName());
192            Console.logException(e);
193        }
194        catch (FileNotFoundException e){
195            Console.printerr("Error parsing file " + file.getName());
196            Console.logException(e);
197        }
198        if (inputSource != null){
199           inputSource.setSystemId("file://" + file.getAbsolutePath());
200           try{
201               if (saxParser == null){
202                   throw new RuntimeException("SaxParser creation failed");
203               }
204               saxParser.parse(inputSource, this);
205           }
206           catch (SAXParseException e){
207               Console.printerrln("Failure parsing file in line " + e.getLineNumber() +
208                                  ", column " + e.getColumnNumber() + ".");
209               Console.logException(e);
210           }
211           catch (SAXException e){
212               Console.printerr("Error parsing file " + file.getName());
213               Console.logException(e);
214               return;
215           }
216           catch (IOException e){
217               Console.printerr("Error parsing file " + file.getName());
218               Console.logException(e);
219               return;
220           }
221        } 
222    }
223
224    @Override
225    public void startElement(String uri, String localName, String qName, Attributes atts)
226        throws SAXException
227    {
228        if (qName.equals("session")) {
229             currentSequence = new LinkedList<Event>();
230             if (currentGUIElementTree == null)
231                     currentGUIElementTree = new GUIElementTree<String>();
232        }
233        else if (qName.equals("component")) {
234            currentGUIElementPath = atts.getValue("path");
235        }
236        else if (qName.equals("event")) {
237            currentEventType = atts.getValue("type");
238            currentEventParameters = new HashMap<String, String>();
239        }
240        else if (qName.equals("param")){
241            String paramName = atts.getValue("name");
242            if (currentGUIElementPath != null){
243                if ("parent".equals(paramName)){
244                    currentParentPath = atts.getValue("value");
245                }
246                if ("class".equals(paramName)){
247                    currentGUIElementClass = atts.getValue("value");
248                }
249                if ("index".equals(paramName)){
250                    currentGUIElementIndex = atts.getValue("value");
251                }
252            }
253            else if (currentEventType != null){
254                if ("target".equals(paramName)){
255                    currentEventSource = atts.getValue("value");
256                }
257                if ("timestamp".equals(paramName)){
258                    currentEventTimestamp = Long.parseLong(atts.getValue("value"));
259                }
260                currentEventParameters.put(paramName, atts.getValue("value"));
261            }
262            else{
263                throw new SAXException("param tag found where it should not be.");
264            }
265        }
266        else{
267            throw new SAXException("unknown tag found: " + qName);
268        }
269
270    }
271
272    @Override
273    public void endElement(String uri, String localName, String qName) throws SAXException {
274        if (qName.equals("session")) {
275            if (currentSequence != null && !currentSequence.isEmpty()){
276                sequences.add(currentSequence);
277            }
278            currentSequence = null;
279        }
280        else if (qName.equals("component") && currentGUIElementPath != null) {
281            HTMLGUIElementSpec guiElementSpec = new HTMLGUIElementSpec(currentGUIElementClass);
282            currentGUIElementTree.add(currentGUIElementPath, currentParentPath, guiElementSpec);
283           
284            currentParentPath = null;
285            currentGUIElementPath = null;
286        }
287        else if (qName.equals("event")) {
288            IGUIElement currentGUIElement;
289            currentGUIElement = currentGUIElementTree.find(currentEventSource);
290           
291            Event event = new Event(HTMLEventTypeFactory.
292                                    getInstance().getEventType(currentEventType, currentEventParameters),
293                                    (currentGUIElement == null ? lastGUIElement : currentGUIElement));
294           
295            event.setTimestamp(currentEventTimestamp);
296            HTMLGUIElement currentEventTarget = (HTMLGUIElement) event.getTarget();
297            currentEventTarget.markAsUsed();
298            currentSequence.add(event);
299           
300            currentEventSource = null;
301            currentEventTimestamp = -1l;
302            currentEventParameters = null;
303            currentEventType = null;
304           
305            if (currentGUIElement != null){
306                lastGUIElement = currentGUIElement;
307            }
308           
309            currentGUIElement = null;
310        }
311    }
312   
313    /**
314     * <p>
315     * Returns a collection of event sequences that was obtained from parsing log files.
316     * </p>
317     * @return
318     */
319    public Collection<List<Event>> getSequences(){
320        return sequences;
321    }
322   
323    /**
324     * <p>
325     * Returns the GUI model that is obtained from parsing log files.
326     * </p>
327     *
328     * @return GUIModel
329     */
330    public GUIModel getGuiModel(){
331        return currentGUIElementTree.getGUIModel();
332    }
333
334}
Note: See TracBrowser for help on using the repository browser.