source: trunk/autoquest-plugin-html/src/main/java/de/ugoe/cs/autoquest/plugin/html/commands/CMDparseDirHTML.java @ 1276

Last change on this file since 1276 was 1220, checked in by pharms, 11 years ago
  • allowed parsing recursive directory structures
File size: 4.0 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.plugin.html.commands;
16
17import java.io.File;
18import java.util.Arrays;
19import java.util.Collection;
20import java.util.List;
21import java.util.logging.Level;
22
23import de.ugoe.cs.autoquest.CommandHelpers;
24import de.ugoe.cs.autoquest.eventcore.Event;
25import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
26import de.ugoe.cs.autoquest.plugin.html.HTMLLogParser;
27import de.ugoe.cs.util.console.Command;
28import de.ugoe.cs.util.console.Console;
29import de.ugoe.cs.util.console.GlobalDataContainer;
30
31/**
32 * <p>
33 * Command that tries to parse all files in a folder as if they were log files generated by the
34 * HTMLMonitor. The result is one set of sequences for all files (not one set of sequences for each
35 * file!).
36 * </p>
37 *
38 * @author Patrick Harms
39 * @version 1.0
40 */
41public class CMDparseDirHTML implements Command {
42
43    /*
44     * (non-Javadoc)
45     *
46     * @see de.ugoe.cs.util.console.Command#run(java.util.List)
47     */
48    @Override
49    public void run(List<Object> parameters) {
50        String path;
51        String sequencesName = "sequences";
52
53        try {
54            path = (String) parameters.get(0);
55            if (parameters.size() >= 2) {
56                sequencesName = (String) parameters.get(1);
57            }
58        }
59        catch (Exception e) {
60            throw new IllegalArgumentException("illegal parameters provided: " + e);
61        }
62
63        File folder = new File(path);
64        if (!folder.isDirectory()) {
65            Console.printerrln(path + " is not a directory");
66            return;
67        }
68
69        HTMLLogParser parser = new HTMLLogParser();
70
71        parseFile(folder, parser);
72
73        Collection<List<Event>> sequences = parser.getSequences();
74
75        GUIModel targets = parser.getGuiModel();
76
77        if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
78            CommandHelpers.dataOverwritten(sequencesName);
79        }
80
81        if (GlobalDataContainer.getInstance().addData(sequencesName + "_targets", targets)) {
82            CommandHelpers.dataOverwritten(sequencesName + "_targets");
83        }
84    }
85
86    /**
87     * <p>
88     * recursive method for parsing a directory structures
89     * </p>
90     *
91     * @param file   the file object to be parsed. If the file is a folder, the method calls itself
92     *               for all children
93     * @param parser the parser to use for parsing the files.
94     */
95    private void parseFile(File file, HTMLLogParser parser) {
96        if (file.isDirectory()) {
97            String[] children = file.list();
98            Arrays.sort(children);
99           
100            for (String child : children) {
101                File childFile = new File(file, child);
102                parseFile(childFile, parser);
103            }
104        }
105        else if (file.isFile()) {
106            String source = file.getAbsolutePath();
107            Console.traceln(Level.INFO, "Processing file: " + source);
108
109            try {
110                parser.parseFile(file);
111            }
112            catch (Exception e) {
113                Console.printerrln("Could not parse " + source + ": " + e.getMessage());
114            }
115        }
116    }
117
118    /*
119     * (non-Javadoc)
120     *
121     * @see de.ugoe.cs.util.console.Command#help()
122     */
123    @Override
124    public String help() {
125        return "parseDirHTML <directory> {<sequencesName>}";
126    }
127
128}
Note: See TracBrowser for help on using the repository browser.