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