[927] | 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 | |
---|
[922] | 15 | package de.ugoe.cs.autoquest.plugin.jfc; |
---|
[675] | 16 | |
---|
[796] | 17 | import java.util.ArrayList; |
---|
[675] | 18 | import java.util.Arrays; |
---|
| 19 | import java.util.LinkedHashMap; |
---|
| 20 | import java.util.List; |
---|
[796] | 21 | import java.util.ListIterator; |
---|
[675] | 22 | import java.util.Map; |
---|
| 23 | import java.util.regex.Matcher; |
---|
| 24 | import java.util.regex.Pattern; |
---|
| 25 | |
---|
[922] | 26 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
| 27 | import de.ugoe.cs.autoquest.eventcore.IEventTarget; |
---|
| 28 | import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCGUIElement; |
---|
| 29 | import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCGUIElementSpec; |
---|
[675] | 30 | |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * <p> |
---|
| 34 | * This class provides the functionality to calculate the unique GUITAR replayIDs |
---|
| 35 | * for {@link JFCEvent}s. This code is mainly based on guitar source code: edu.umd.cs.guitar. |
---|
| 36 | * model.JFCDefaultIDGeneratorSimple |
---|
| 37 | * |
---|
| 38 | * </p> |
---|
| 39 | * @author fabian.glaser |
---|
| 40 | * |
---|
| 41 | */ |
---|
| 42 | |
---|
| 43 | public class JFCReplayIDCalculator { |
---|
| 44 | |
---|
[785] | 45 | static final int prime = 31; |
---|
[806] | 46 | private JFCReplayIDValidator validator; |
---|
[675] | 47 | |
---|
[806] | 48 | public JFCReplayIDCalculator() { |
---|
| 49 | this.validator = null; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | public JFCReplayIDCalculator(JFCReplayIDValidator validator){ |
---|
| 53 | this.validator = validator; |
---|
| 54 | } |
---|
| 55 | |
---|
[675] | 56 | /** |
---|
| 57 | * Properties that are used to identify widgets |
---|
| 58 | */ |
---|
| 59 | private static List<String> ID_PROPERTIES = Arrays.asList( |
---|
| 60 | "Class","Title","Icon"); |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * Those classes are invisible widgets but cause false-positive when |
---|
| 64 | * calculating ID (compare guitar source code: edu.umd.cs.guitar. |
---|
| 65 | * model.JFCDefaultIDGeneratorSimple) |
---|
| 66 | */ |
---|
| 67 | private static List<String> IGNORED_CLASSES = Arrays.asList("javax.swing.JPanel", |
---|
| 68 | "javax.swing.JTabbedPane", "javax.swing.JScrollPane", |
---|
| 69 | "javax.swing.JSplitPane", "javax.swing.Box", |
---|
| 70 | "javax.swing.JViewport", "javax.swing.JScrollBar", |
---|
| 71 | "javax.swing.JLayeredPane", |
---|
| 72 | "javax.swing.JList$AccessibleJList$AccessibleJListChild", |
---|
| 73 | "javax.swing.JList$AccessibleJList", "javax.swing.JList", |
---|
| 74 | "javax.swing.JScrollPane$ScrollBar", |
---|
| 75 | "javax.swing.plaf.metal.MetalScrollButton"); |
---|
[796] | 76 | |
---|
| 77 | /** |
---|
| 78 | * Calculates the replayID of a JFCEvent needed for compatibility with guitar suite |
---|
[1009] | 79 | * @param List of {@link JFCGUIElementSpec}s that represent the path of an event target |
---|
[796] | 80 | * for which the replayID should be calculated. |
---|
| 81 | * @return replayID |
---|
| 82 | */ |
---|
[675] | 83 | |
---|
[796] | 84 | public String calculateReplayID(List<JFCGUIElementSpec> guiElementPath){ |
---|
| 85 | String replayID = ""; |
---|
| 86 | long hashCode = 1; |
---|
| 87 | |
---|
| 88 | ListIterator<JFCGUIElementSpec> iterator = guiElementPath.listIterator(); |
---|
| 89 | |
---|
| 90 | JFCGUIElementSpec currentSpec = iterator.next(); |
---|
| 91 | String title = currentSpec.getName(); |
---|
| 92 | String fuzzyTitle = getFuzzyTitle(title); |
---|
[1009] | 93 | long topLevelHashCode = fuzzyTitle.hashCode(); |
---|
| 94 | topLevelHashCode = (topLevelHashCode * 2) & 0xffffffffL; |
---|
[796] | 95 | |
---|
[1009] | 96 | long propagatedHashCode = topLevelHashCode; |
---|
[796] | 97 | |
---|
[1009] | 98 | // added validator to check if generated GUI element ids are known |
---|
[806] | 99 | if (validator != null){ |
---|
[1009] | 100 | if (validator.validateReplayID("w" + topLevelHashCode)){ |
---|
| 101 | System.out.println("ID w" + topLevelHashCode + " is valid."); |
---|
[806] | 102 | } |
---|
| 103 | else{ |
---|
[1009] | 104 | System.err.println(currentSpec + " describes an unknown GUI element."); |
---|
| 105 | System.err.println("ID w" + topLevelHashCode + " is unknown." ); |
---|
[806] | 106 | System.err.println(); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | // construct looks complicated but avoids going back and forth through path |
---|
[796] | 112 | if (iterator.hasNext()) |
---|
| 113 | currentSpec = iterator.next(); |
---|
[806] | 114 | else{ |
---|
[796] | 115 | currentSpec = null; |
---|
[1009] | 116 | // there are no children of current GUI element, so we use topLevelHashCode as hashCode |
---|
| 117 | hashCode = topLevelHashCode; |
---|
[806] | 118 | } |
---|
[796] | 119 | |
---|
[1009] | 120 | // walk through GUI element path and calculate hashcode |
---|
[796] | 121 | while(currentSpec != null){ |
---|
| 122 | long localHashCode = getLocalHashCode(currentSpec); |
---|
| 123 | hashCode = propagatedHashCode * prime + localHashCode; |
---|
| 124 | hashCode = (hashCode * 2) & 0xffffffffL; |
---|
[806] | 125 | |
---|
[1009] | 126 | // added validator to check if generated GUI element ids are known |
---|
[806] | 127 | if (validator != null){ |
---|
| 128 | if (validator.validateReplayID("w" + hashCode)){ |
---|
| 129 | System.out.println("ID w" + hashCode + " is valid."); |
---|
[1009] | 130 | System.out.println("==> " + currentSpec + " describes a known GUI element."); |
---|
[806] | 131 | } |
---|
| 132 | else{ |
---|
[836] | 133 | System.err.println("ID w" + hashCode + " is unknown." ); |
---|
[1009] | 134 | System.err.println("==> " + currentSpec + " describes an unknown GUI element."); |
---|
[836] | 135 | System.err.println(); |
---|
[806] | 136 | } |
---|
| 137 | } |
---|
[796] | 138 | |
---|
| 139 | if (iterator.hasNext()){ |
---|
| 140 | currentSpec = iterator.next(); |
---|
| 141 | Integer index = currentSpec.getIndex(); |
---|
| 142 | propagatedHashCode = prime * propagatedHashCode |
---|
| 143 | + index.hashCode(); |
---|
| 144 | } |
---|
| 145 | else |
---|
| 146 | currentSpec = null; |
---|
| 147 | |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | replayID = "e" + hashCode; |
---|
| 151 | |
---|
| 152 | return replayID; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | |
---|
[675] | 156 | /** |
---|
[796] | 157 | * Calculates the replayID of a JFCEvent needed for compatibility with guitar suite |
---|
[675] | 158 | * @param event for which the ID should be calculated |
---|
| 159 | * @return replayID |
---|
| 160 | */ |
---|
| 161 | public String calculateReplayID(Event event){ |
---|
[796] | 162 | List<JFCGUIElementSpec> guiElementPath = new ArrayList<JFCGUIElementSpec>(); |
---|
[675] | 163 | |
---|
| 164 | IEventTarget target = event.getTarget(); |
---|
[1097] | 165 | if (!"JFC".equals(target.getPlatform())) { |
---|
[806] | 166 | throw new IllegalArgumentException("Event target must be of type JFC."); |
---|
| 167 | } |
---|
[675] | 168 | |
---|
[806] | 169 | JFCGUIElement currentTarget = (JFCGUIElement) target; |
---|
| 170 | |
---|
[796] | 171 | // extract element path |
---|
[675] | 172 | while (currentTarget != null){ |
---|
[796] | 173 | JFCGUIElementSpec currentSpec = (JFCGUIElementSpec) currentTarget.getSpecification(); |
---|
[806] | 174 | |
---|
| 175 | // new specification must be inserted at the beginning of the list |
---|
[796] | 176 | guiElementPath.add(0, currentSpec); |
---|
[675] | 177 | currentTarget = (JFCGUIElement) currentTarget.getParent(); |
---|
| 178 | } |
---|
| 179 | |
---|
[796] | 180 | // calculation is delegated to other calculateReplayID method |
---|
| 181 | return this.calculateReplayID(guiElementPath); |
---|
[675] | 182 | } |
---|
| 183 | |
---|
| 184 | /** |
---|
[1009] | 185 | * Calculates the hashcode part of a GUI element. |
---|
[675] | 186 | * @param spec The {@link JFCGUIElementSpec} for which the hashcode should be calculated. |
---|
| 187 | * @return the local hashcode |
---|
| 188 | */ |
---|
| 189 | |
---|
| 190 | private long getLocalHashCode(JFCGUIElementSpec spec){ |
---|
| 191 | long hashcode = 1; |
---|
| 192 | String wClass = spec.getType(); |
---|
| 193 | if (IGNORED_CLASSES.contains(wClass)) { |
---|
| 194 | hashcode = (prime * hashcode + (wClass.equals("null") ? 0 : (wClass |
---|
| 195 | .hashCode()))); |
---|
| 196 | return hashcode; |
---|
| 197 | } |
---|
| 198 | else{ |
---|
| 199 | Map<String, String> idProperties = extractIDProperties(spec); |
---|
[1079] | 200 | for (Map.Entry<String, String> property : idProperties.entrySet()) { |
---|
| 201 | String value = property.getValue(); |
---|
[675] | 202 | if (!value.equals("null")){ |
---|
[1079] | 203 | hashcode = prime * hashcode + property.getKey().hashCode(); |
---|
[675] | 204 | hashcode = prime * hashcode + value.hashCode(); |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | hashcode = (hashcode * 2) & 0xffffffffL; |
---|
| 210 | |
---|
| 211 | return hashcode; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | /** |
---|
| 215 | * Extracts the IDProperties from a given {@link JFCGUIElementSpec}. |
---|
| 216 | * @param spec |
---|
| 217 | * @return LinkedHashMap that contains the IDProperties and its values. |
---|
| 218 | */ |
---|
| 219 | |
---|
| 220 | private Map<String, String> extractIDProperties(JFCGUIElementSpec spec){ |
---|
| 221 | LinkedHashMap<String, String> idProperties = new LinkedHashMap<String, String>(); |
---|
| 222 | if (ID_PROPERTIES.contains("Class")){ |
---|
| 223 | idProperties.put("Class", spec.getType()); |
---|
| 224 | } |
---|
| 225 | if (ID_PROPERTIES.contains("Title")){ |
---|
[768] | 226 | String name = spec.getName(); |
---|
| 227 | // spec returns extra "" that need to be removed |
---|
| 228 | idProperties.put("Title", name.substring(1, name.length() - 1)); |
---|
[675] | 229 | } |
---|
| 230 | if (ID_PROPERTIES.contains("Icon")){ |
---|
| 231 | idProperties.put("Icon", spec.getIcon()); |
---|
| 232 | } |
---|
| 233 | if (ID_PROPERTIES.contains("Index")){ |
---|
| 234 | idProperties.put("Index", Integer.toString(spec.getIndex())); |
---|
| 235 | } |
---|
| 236 | return idProperties; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | /** |
---|
[1009] | 240 | * Guitar has a special way to deal with top level GUI elements (window) |
---|
| 241 | * titles when calculating unique widget IDs. This method mimics Guitar's |
---|
[675] | 242 | * behavior (compare guitar source code: edu.umd.cs.guitar. |
---|
| 243 | * model.JFCDefaultIDGeneratorSimple). |
---|
| 244 | * @param title |
---|
| 245 | * @return fuzzyTitle |
---|
| 246 | */ |
---|
| 247 | |
---|
| 248 | private String getFuzzyTitle(String title){ |
---|
| 249 | final List<String> PATTERNS = |
---|
| 250 | Arrays.asList("Rachota .*", |
---|
| 251 | "OmegaT-.*", |
---|
| 252 | "Buddi.*", |
---|
| 253 | "Open:.*", |
---|
| 254 | "JabRef.*", |
---|
| 255 | "GanttProject.*", |
---|
| 256 | ".*Pauker.*", |
---|
| 257 | ".*FreeMind.*", |
---|
| 258 | ".* - ArgoUML.*", |
---|
| 259 | "Save Project .*"); |
---|
| 260 | |
---|
| 261 | |
---|
| 262 | for (String sPattern : PATTERNS) { |
---|
| 263 | if (matchRegex(title, sPattern)) { |
---|
| 264 | return sPattern; |
---|
| 265 | } |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | return title; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | /** |
---|
| 272 | * Determine if the input string matches the input regex pattern. |
---|
| 273 | * This method mimics Guitars behavior. |
---|
| 274 | * Attempt to match the pattern 'sPattern' with the string 'sInputString'. |
---|
| 275 | |
---|
| 276 | * @param sInputString Input string to match with pattern |
---|
| 277 | * @param sPattern Regex pattern to match with string |
---|
| 278 | * @return True if match, false otherwise |
---|
| 279 | */ |
---|
| 280 | |
---|
| 281 | private static boolean |
---|
| 282 | matchRegex(String sInputString, |
---|
| 283 | String sPattern) |
---|
| 284 | { |
---|
| 285 | Pattern pattern; |
---|
| 286 | Matcher matcher; |
---|
| 287 | |
---|
| 288 | pattern = Pattern.compile(sPattern); |
---|
| 289 | matcher = pattern.matcher(sInputString); |
---|
| 290 | if (matcher.matches()) { |
---|
| 291 | return true; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | return false; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | }; |
---|