source: trunk/autoquest-plugin-http/src/main/java/de/ugoe/cs/autoquest/plugin/http/commands/CMDparseDirHTTP.java @ 1367

Last change on this file since 1367 was 1367, checked in by pharms, 10 years ago

Initial import.

File size: 3.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.http.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.plugin.http.HTTPLogParser;
26import de.ugoe.cs.util.console.Command;
27import de.ugoe.cs.util.console.Console;
28import de.ugoe.cs.util.console.GlobalDataContainer;
29
30/**
31 * <p>
32 * Command that tries to parse all files in a folder as if they were log files generated by the
33 * HTMLMonitor. The result is one set of sequences for all files (not one set of sequences for each
34 * file!).
35 * </p>
36 *
37 * @author Patrick Harms
38 * @version 1.0
39 */
40public class CMDparseDirHTTP implements Command {
41
42    /*
43     * (non-Javadoc)
44     *
45     * @see de.ugoe.cs.util.console.Command#run(java.util.List)
46     */
47    @Override
48    public void run(List<Object> parameters) {
49        String directory = null;
50        String sequencesName = "sequences";
51
52        try {
53            directory = (String) parameters.get(0);
54            if (parameters.size() >= 2) {
55                sequencesName = (String) parameters.get(1);
56            }
57        }
58        catch (Exception e) {
59            throw new IllegalArgumentException("missing parameter for directory");
60        }
61
62        File folder = new File(directory);
63        if (!folder.isDirectory()) {
64            Console.printerrln(directory + " is not a directory");
65            return;
66        }
67
68        HTTPLogParser parser = new HTTPLogParser();
69
70        parseFile(folder, parser);
71
72        Collection<List<Event>> sequences = parser.getSequences();
73
74        if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
75            CommandHelpers.dataOverwritten(sequencesName);
76        }
77    }
78
79    /**
80     * <p>
81     * recursive method for parsing a directory structures
82     * </p>
83     *
84     * @param file   the file object to be parsed. If the file is a folder, the method calls itself
85     *               for all children
86     * @param parser the parser to use for parsing the files.
87     */
88    private void parseFile(File file, HTTPLogParser parser) {
89        if (file.isDirectory()) {
90            String[] children = file.list();
91            Arrays.sort(children);
92           
93            for (String child : children) {
94                File childFile = new File(file, child);
95                parseFile(childFile, parser);
96            }
97        }
98        else if (file.isFile()) {
99            String source = file.getAbsolutePath();
100            Console.traceln(Level.INFO, "Processing file: " + source);
101
102            try {
103                parser.parseFile(file);
104            }
105            catch (Exception e) {
106                Console.printerrln("Could not parse " + source + ": " + e.getMessage());
107            }
108        }
109    }
110
111    /*
112     * (non-Javadoc)
113     *
114     * @see de.ugoe.cs.util.console.Command#help()
115     */
116    @Override
117    public String help() {
118        return "parseDirHTTP <directory> [<sequencesName>]";
119    }
120
121}
Note: See TracBrowser for help on using the repository browser.