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

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