// Copyright 2012 Georg-August-Universität Göttingen, Germany // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package de.ugoe.cs.autoquest.plugin.html; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.text.DecimalFormat; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import org.xml.sax.SAXException; import de.ugoe.cs.util.StringTools; import de.ugoe.cs.util.console.Console; /** *
* This class can be used to compress a bunch of HTML log files to one file, that contains only * one full GUI model. *
* * @author Patrick Harms * @version 1.0 * */ public class HTMLLogCompressor extends AbstractDefaultLogParser { /** ** The output writer into which the compressed variant of the log files is written *
*/ private PrintWriter outputWriter; /** ** the GUI elements, that were already logged and need therefore not be logged again into * the same file *
*/ private Set* the events that were read *
*/ private List* called to compress all log files in the provided directory. The method reuses * {@link #compressFilesInDirectory(File)}. *
* * @param directory the directory containing the log files to be compressed */ public void compressFilesInDirectory(String directory) { if (directory == null) { throw new IllegalArgumentException("directory must not be null"); } compressFilesInDirectory(new File(directory)); } /** ** called to compress all log files in the provided directory. The directory if processed * recursively. For each directory containing at least one log file the log files are * replaced by one large log file containing the GUI model as well as all events contained in * the compressed log file. If one log file could not be parsed, it is ignored and not * compressed with the other ones. *
* * @param directory the directory containing the log files to be compressed */ public void compressFilesInDirectory(File directory) { if (directory == null) { throw new IllegalArgumentException("directory must not be null"); } if (!directory.exists()) { throw new IllegalArgumentException("directory must denote an existing directory"); } if (!directory.isDirectory()) { throw new IllegalArgumentException("directory must denote a directory"); } File[] files = directory.listFiles(); if ((files == null) || (files.length == 0)) { Console.println("directory is empty"); return; } File outFile = null; int logFileIndex = -1; do { outFile = new File(directory, getLogFileName(directory.getName(), logFileIndex)); logFileIndex++; } while (outFile.exists()); List* dumps a parameter with the given name and value to the log file. The result is a * tag named param with a name attribute and a value attribute. The value is transformed * to a String if it is no String already. Furthermore, an XML entity replacement is performed * if required. *
* * @param name the name of the parameter to be dumped * @param value the value of the parameter to be dumped */ private void dumpParam(String name, Object value) { if (value == null) { return; } String val; if (value instanceof String) { val = (String) value; } else { val = String.valueOf(value); } outputWriter.print(" "); } /** ** used to calculate a log file name. If the provided index is smaller 0, then no index * is added to the file name. A filename is e.g. "htmlmonitor_12345_001.log". *
* * @param index the index of the log file or -1 one, if no index shall be added * * @return the file name as described */ private String getLogFileName(String clientId, int index) { String result = "htmlmonitor_" + clientId; if (index >= 0) { result += "_" + new DecimalFormat("000" ).format(index); } result += ".log"; return result; } /** ** this class is used internally for storing events in a sorted list together with the * timestamps, being the sort criteria. *
*/ private class EventEntry { /** ** the type of the event *
*/ private String type; /** ** the parameters of the event *
*/ private Map* the timestamp of the event *
*/ private long timestamp; /** ** creates a new event entry with event type, parameters and the timestamp *
*/ private EventEntry(String type, Map* convenience method for dumping the event into the compressed log file *
*/ private void dump() { outputWriter.print("