[2125] | 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 | |
---|
| 15 | package de.ugoe.cs.autoquest.plugin.html; |
---|
| 16 | |
---|
| 17 | import java.io.File; |
---|
| 18 | import java.io.FileNotFoundException; |
---|
| 19 | import java.io.FileOutputStream; |
---|
| 20 | import java.io.OutputStreamWriter; |
---|
| 21 | import java.io.PrintWriter; |
---|
| 22 | import java.io.UnsupportedEncodingException; |
---|
| 23 | import java.util.Map; |
---|
| 24 | |
---|
| 25 | import org.xml.sax.SAXException; |
---|
| 26 | |
---|
| 27 | import de.ugoe.cs.util.StringTools; |
---|
| 28 | import de.ugoe.cs.util.console.Console; |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * <p> |
---|
| 32 | * deletes queries from URLs belonging to GUI elements contained in an HTML log file. For this, it |
---|
| 33 | * parses a given file and dumps a replacement, in which all are removed. |
---|
| 34 | * </p> |
---|
| 35 | * |
---|
| 36 | * @author Patrick Harms |
---|
| 37 | * @version 1.0 |
---|
| 38 | * |
---|
| 39 | */ |
---|
| 40 | public class HTMLLogQueryDeleter extends AbstractDefaultLogParser { |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * <p> |
---|
| 44 | * The output writer into which the new variant of the log file without queries is written |
---|
| 45 | * </p> |
---|
| 46 | */ |
---|
| 47 | private PrintWriter outputWriter; |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * <p> |
---|
| 51 | * called to delete queries in the given log file. The method reuses |
---|
| 52 | * {@link #deleteQueries(File)}. |
---|
| 53 | * </p> |
---|
| 54 | * |
---|
| 55 | * @param file the log file in which the queries shall be deleted |
---|
| 56 | */ |
---|
| 57 | public void deleteQueries(String file) { |
---|
| 58 | if (file == null) { |
---|
| 59 | throw new IllegalArgumentException("file must not be null"); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | deleteQueries(new File(file)); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * <p> |
---|
| 67 | * called to delete the queries in the given log file. The given file is read |
---|
| 68 | * completely. All events are written to an output file as they are. All GUI elements are |
---|
| 69 | * written to an output file as they are, as well, as long as they do not have a query |
---|
| 70 | * parameter. If they have a query parameter, it is removed. Finally, the original log file is |
---|
| 71 | * deleted and replaced by the adapted variant. Log files, which do not contain query parameters |
---|
| 72 | * stay untouched. |
---|
| 73 | * </p> |
---|
| 74 | * |
---|
| 75 | * @param file the log file in which the query parameters shall be removed |
---|
| 76 | */ |
---|
| 77 | public void deleteQueries(File file) { |
---|
| 78 | if (file == null) { |
---|
| 79 | throw new IllegalArgumentException("file must not be null"); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | if (!file.exists()) { |
---|
| 83 | throw new IllegalArgumentException("file must denote an existing file"); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | if (!file.isFile()) { |
---|
| 87 | throw new IllegalArgumentException("file must denote a file"); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | File outFile = new File(file.getParentFile(), file.getName() + "_tmp"); |
---|
| 91 | boolean parsingFailed = false; |
---|
| 92 | |
---|
| 93 | try { |
---|
| 94 | FileOutputStream fis = new FileOutputStream(outFile); |
---|
| 95 | outputWriter = new PrintWriter(new OutputStreamWriter(fis, "UTF-8")); |
---|
| 96 | outputWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
---|
| 97 | outputWriter.println("<session>"); |
---|
| 98 | |
---|
| 99 | try { |
---|
| 100 | super.parseFile(file); |
---|
| 101 | } |
---|
| 102 | catch (SAXException e) { |
---|
| 103 | parsingFailed = true; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | outputWriter.println("</session>"); |
---|
| 107 | outputWriter.flush(); |
---|
| 108 | } |
---|
| 109 | catch (FileNotFoundException e) { |
---|
| 110 | Console.printerrln("could not create adapted file " + outFile); |
---|
| 111 | } |
---|
| 112 | catch (UnsupportedEncodingException e) { |
---|
| 113 | // this should never happen |
---|
| 114 | e.printStackTrace(); |
---|
| 115 | } |
---|
| 116 | finally { |
---|
| 117 | if (outputWriter != null) { |
---|
| 118 | outputWriter.close(); |
---|
| 119 | outputWriter = null; |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | if (!parsingFailed && outFile.exists()) { |
---|
| 124 | if (!file.delete()) { |
---|
| 125 | Console.printerrln("could not delete adapted file " + file); |
---|
| 126 | } |
---|
| 127 | else if (!outFile.renameTo(file)) { |
---|
| 128 | Console.printerrln |
---|
| 129 | ("could not rename adapted file to original file name " + file); |
---|
| 130 | } |
---|
| 131 | else { |
---|
| 132 | Console.println("removed queries from file " + file); |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | else { |
---|
| 136 | if (!outFile.delete()) { |
---|
| 137 | Console.printerrln("could not delete temporary file " + outFile); |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /* (non-Javadoc) |
---|
| 143 | * @see de.ugoe.cs.autoquest.plugin.html.AbstractDefaultLogParser#parseFile(java.lang.String) |
---|
| 144 | */ |
---|
| 145 | @Override |
---|
| 146 | public void parseFile(String filename) { |
---|
| 147 | throw new IllegalStateException("this method must not be called externally"); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /* (non-Javadoc) |
---|
| 151 | * @see de.ugoe.cs.autoquest.plugin.html.AbstractDefaultLogParser#parseFile(java.io.File) |
---|
| 152 | */ |
---|
| 153 | @Override |
---|
| 154 | public void parseFile(File file) { |
---|
| 155 | throw new IllegalStateException("this method must not be called externally"); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | /* (non-Javadoc) |
---|
| 159 | * @see de.ugoe.cs.autoquest.plugin.html.AbstractDefaultLogParser#handleGUIElement(String, Map) |
---|
| 160 | */ |
---|
| 161 | @Override |
---|
| 162 | protected boolean handleGUIElement(String id, Map<String, String> parameters) |
---|
| 163 | throws SAXException |
---|
| 164 | { |
---|
| 165 | outputWriter.print("<component id=\""); |
---|
| 166 | outputWriter.print(id); |
---|
| 167 | outputWriter.println("\">"); |
---|
| 168 | |
---|
| 169 | for (Map.Entry<String, String> param : parameters.entrySet()) { |
---|
| 170 | if (!"query".equals(param.getKey())) { |
---|
| 171 | dumpParam(param.getKey(), param.getValue()); |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | outputWriter.println("</component>"); |
---|
| 176 | |
---|
| 177 | return true; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | /* (non-Javadoc) |
---|
| 181 | * @see de.ugoe.cs.autoquest.plugin.html.AbstractDefaultLogParser#handleEvent(String,Map) |
---|
| 182 | */ |
---|
| 183 | @Override |
---|
| 184 | protected boolean handleEvent(String type, Map<String, String> parameters) throws SAXException { |
---|
| 185 | outputWriter.print("<event type=\""); |
---|
| 186 | outputWriter.print(type); |
---|
| 187 | outputWriter.println("\">"); |
---|
| 188 | |
---|
| 189 | for (Map.Entry<String, String> param : parameters.entrySet()) { |
---|
| 190 | dumpParam(param.getKey(), param.getValue()); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | outputWriter.println("</event>"); |
---|
| 194 | |
---|
| 195 | return true; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /** |
---|
| 199 | * <p> |
---|
| 200 | * dumps a parameter with the given name and value to the log file. The result is a |
---|
| 201 | * tag named param with a name attribute and a value attribute. The value is transformed |
---|
| 202 | * to a String if it is no String already. Furthermore, an XML entity replacement is performed |
---|
| 203 | * if required. |
---|
| 204 | * </p> |
---|
| 205 | * |
---|
| 206 | * @param name the name of the parameter to be dumped |
---|
| 207 | * @param value the value of the parameter to be dumped |
---|
| 208 | */ |
---|
| 209 | private void dumpParam(String name, Object value) { |
---|
| 210 | if (value == null) { |
---|
| 211 | return; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | String val; |
---|
| 215 | |
---|
| 216 | if (value instanceof String) { |
---|
| 217 | val = (String) value; |
---|
| 218 | } |
---|
| 219 | else { |
---|
| 220 | val = String.valueOf(value); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | outputWriter.print(" <param name=\""); |
---|
| 224 | outputWriter.print(name); |
---|
| 225 | outputWriter.print("\" value=\""); |
---|
| 226 | outputWriter.print(StringTools.xmlEntityReplacement(val)); |
---|
| 227 | outputWriter.println("\"/>"); |
---|
| 228 | } |
---|
| 229 | } |
---|