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

Last change on this file since 1354 was 1354, checked in by pharms, 10 years ago
  • added support for id replacements
File size: 5.8 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.HashMap;
21import java.util.LinkedList;
22import java.util.List;
23import java.util.Map;
24import java.util.logging.Level;
25import java.util.regex.Matcher;
26import java.util.regex.Pattern;
27
28import de.ugoe.cs.autoquest.CommandHelpers;
29import de.ugoe.cs.autoquest.eventcore.Event;
30import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
31import de.ugoe.cs.autoquest.plugin.html.HTMLLogParser;
32import de.ugoe.cs.util.console.Command;
33import de.ugoe.cs.util.console.Console;
34import de.ugoe.cs.util.console.GlobalDataContainer;
35
36/**
37 * <p>
38 * Command that tries to parse all files in a folder as if they were log files generated by the
39 * HTMLMonitor. The result is one set of sequences for all files (not one set of sequences for each
40 * file!).
41 * </p>
42 *
43 * @author Patrick Harms
44 * @version 1.0
45 */
46public class CMDparseDirHTML implements Command {
47
48    /*
49     * (non-Javadoc)
50     *
51     * @see de.ugoe.cs.util.console.Command#run(java.util.List)
52     */
53    @Override
54    public void run(List<Object> parameters) {
55        String path = null;
56        String sequencesName = null;
57        Map<String, List<String>> parseParams = new HashMap<String, List<String>>();
58
59        try {
60            for (int i = 0; i < parameters.size(); i++) {
61                String param = (String) parameters.get(i);
62                if (!param.startsWith("-")) {
63                    if (path == null) {
64                        path = param;
65                    }
66                    else if (sequencesName == null) {
67                        sequencesName = param;
68                    }
69                }
70                else {
71                    Pattern parseParamPattern = Pattern.compile("-(\\w*)=([\\w=\\[\\]\\(\\)/\\.]*)");
72                    Matcher matcher = parseParamPattern.matcher(param);
73                   
74                    if (matcher.matches()) {
75                        String key = matcher.group(1);
76                        List<String> values = parseParams.get(key);
77                       
78                        if (values == null) {
79                            values = new LinkedList<String>();
80                            parseParams.put(key, values);
81                        }
82                       
83                        values.add(matcher.group(2));
84                    }
85                    else {
86                        String message = "parse parameter does not follow format: -<key>=<value>";
87                        Console.printerrln(message);
88                        throw new IllegalArgumentException(message);
89                    }
90                }
91            }
92        }
93        catch (Exception e) {
94            throw new IllegalArgumentException("illegal parameters provided: " + e);
95        }
96       
97        if (sequencesName == null) {
98            sequencesName = "sequences";
99        }
100
101        File folder = new File(path);
102        if (!folder.isDirectory()) {
103            Console.printerrln(path + " is not a directory");
104            return;
105        }
106
107        HTMLLogParser parser = new HTMLLogParser(parseParams);
108
109        parseFile(folder, parser);
110
111        Collection<List<Event>> sequences = parser.getSequences();
112
113        GUIModel targets = parser.getGuiModel();
114
115        if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
116            CommandHelpers.dataOverwritten(sequencesName);
117        }
118
119        if (GlobalDataContainer.getInstance().addData(sequencesName + "_targets", targets)) {
120            CommandHelpers.dataOverwritten(sequencesName + "_targets");
121        }
122    }
123
124    /**
125     * <p>
126     * recursive method for parsing a directory structures
127     * </p>
128     *
129     * @param file   the file object to be parsed. If the file is a folder, the method calls itself
130     *               for all children
131     * @param parser the parser to use for parsing the files.
132     */
133    private void parseFile(File file, HTMLLogParser parser) {
134        if (file.isDirectory()) {
135            String[] children = file.list();
136            Arrays.sort(children);
137           
138            for (String child : children) {
139                File childFile = new File(file, child);
140                parseFile(childFile, parser);
141            }
142        }
143        else if (file.isFile()) {
144            String source = file.getAbsolutePath();
145            Console.traceln(Level.INFO, "Processing file: " + source);
146
147            try {
148                parser.parseFile(file);
149            }
150            catch (Exception e) {
151                Console.printerrln("Could not parse " + source + ": " + e.getMessage());
152            }
153        }
154    }
155
156    /*
157     * (non-Javadoc)
158     *
159     * @see de.ugoe.cs.util.console.Command#help()
160     */
161    @Override
162    public String help() {
163        return "parseDirHTML <directory> [<sequencesName>] " +
164            "{-idReplacements=path/to/replacementfile} {-clearId=path/to[0]/gui(htmlId=element)} " +
165            "{-clearIndex=path/to[0]/gui(htmlId=element)}";
166    }
167
168}
Note: See TracBrowser for help on using the repository browser.