1 17 package org.eclipse.emf.codegen.jmerge; 18 19 20 import java.lang.reflect.InvocationTargetException ; 21 import java.util.Collection ; 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.regex.Matcher ; 27 import java.util.regex.Pattern ; 28 29 import org.eclipse.jdt.core.jdom.IDOMCompilationUnit; 30 import org.eclipse.jdt.core.jdom.IDOMField; 31 import org.eclipse.jdt.core.jdom.IDOMImport; 32 import org.eclipse.jdt.core.jdom.IDOMInitializer; 33 import org.eclipse.jdt.core.jdom.IDOMMethod; 34 import org.eclipse.jdt.core.jdom.IDOMNode; 35 import org.eclipse.jdt.core.jdom.IDOMPackage; 36 import org.eclipse.jdt.core.jdom.IDOMType; 37 38 39 42 public class JPatternDictionary 43 { 44 protected IDOMCompilationUnit compilationUnit; 45 protected IDOMPackage jPackage; 46 protected JControlModel options; 47 protected Map importMap = new HashMap (); 48 protected Map typeMap = new HashMap (); 49 protected Map initializerMap = new HashMap (); 50 protected Map fieldMap = new HashMap (); 51 protected Map methodMap = new HashMap (); 52 protected Map markupMap = new HashMap (); 53 protected Collection noImportSet = new HashSet (); 54 55 58 public JPatternDictionary(IDOMCompilationUnit compilationUnit, JControlModel options) 59 { 60 this.options = options; 61 analyzeCompilationUnit(compilationUnit); 62 } 63 64 protected void analyzeCompilationUnit(IDOMCompilationUnit compilationUnit) 65 { 66 this.compilationUnit = compilationUnit; 67 68 if (options.getNoImportPattern() != null) 69 { 70 Matcher matcher = options.getNoImportPattern().matcher(compilationUnit.getContents()); 71 while (matcher.find()) 72 { 73 noImportSet.add(matcher.group(1)); 74 } 75 } 76 77 match(compilationUnit); 78 for (IDOMNode child = compilationUnit.getFirstChild(); child != null; child = child.getNextNode()) 79 { 80 switch (child.getNodeType()) 81 { 82 case IDOMNode.PACKAGE: 83 { 84 analyzePackage((IDOMPackage)child); 85 break; 86 } 87 case IDOMNode.IMPORT: 88 { 89 analyzeImport((IDOMImport)child); 90 break; 91 } 92 case IDOMNode.TYPE: 93 { 94 analyzeType((IDOMType)child); 95 break; 96 } 97 } 98 } 99 } 100 101 protected void analyzePackage(IDOMPackage jPackage) 102 { 103 this.jPackage = jPackage; 104 match(jPackage); 105 } 106 107 protected void analyzeImport(IDOMImport jImport) 108 { 109 importMap.put(getQualifiedName(jImport), jImport); 110 match(jImport); 111 } 112 113 protected void analyzeType(IDOMType type) 114 { 115 typeMap.put(getQualifiedName(type), type); 116 match(type); 117 for (IDOMNode child = type.getFirstChild(); child != null; child = child.getNextNode()) 118 { 119 switch (child.getNodeType()) 120 { 121 case IDOMNode.INITIALIZER: 122 { 123 analyzeInitializer((IDOMInitializer)child); 124 break; 125 } 126 case IDOMNode.FIELD: 127 { 128 analyzeField((IDOMField)child); 129 break; 130 } 131 case IDOMNode.METHOD: 132 { 133 analyzeMethod((IDOMMethod)child); 134 break; 135 } 136 case IDOMNode.TYPE: 137 { 138 analyzeType((IDOMType)child); 139 break; 140 } 141 } 142 } 143 } 144 145 protected void analyzeInitializer(IDOMInitializer initializer) 146 { 147 initializerMap.put(getQualifiedName(initializer), initializer); 148 match(initializer); 149 } 150 151 protected void analyzeField(IDOMField field) 152 { 153 fieldMap.put(getQualifiedName(field), field); 154 match(field); 155 } 156 157 protected void analyzeMethod(IDOMMethod method) 158 { 159 methodMap.put(getQualifiedName(method), method); 160 match(method); 161 } 162 163 public String getQualifiedName(IDOMNode jdomNode) 164 { 165 switch (jdomNode.getNodeType()) 166 { 167 case IDOMNode.COMPILATION_UNIT: 168 { 169 return jdomNode.getName(); 170 } 171 case IDOMNode.PACKAGE: 172 { 173 return jdomNode.getName(); 174 } 175 case IDOMNode.IMPORT: 176 { 177 return jdomNode.getName(); 178 } 179 case IDOMNode.TYPE: 180 { 181 return jPackage != null ? jPackage.getName() + "." + jdomNode.getName() : jdomNode.getName(); 182 } 183 case IDOMNode.FIELD: 184 { 185 return getQualifiedName(jdomNode.getParent()) + "." + jdomNode.getName(); 186 } 187 case IDOMNode.INITIALIZER: 188 { 189 String name = getQualifiedName(jdomNode.getParent()); 190 int index = 0; 191 for (jdomNode = jdomNode.getNextNode(); jdomNode != null; jdomNode = jdomNode.getNextNode()) 192 { 193 if (jdomNode.getNodeType() == IDOMNode.INITIALIZER) 194 { 195 ++index; 196 } 197 } 198 return name + "." + index; 199 } 200 case IDOMNode.METHOD: 201 { 202 IDOMMethod jdomMethod = (IDOMMethod)jdomNode; 203 StringBuffer result = new StringBuffer (getQualifiedName(jdomNode.getParent())); 204 result.append("."); 205 if (jdomMethod.isConstructor()) 206 { 207 result.append(jdomMethod.getParent().getName()); 208 } 209 else 210 { 211 result.append(jdomMethod.getName()); 212 } 213 result.append("("); String [] parameters = jdomMethod.getParameterTypes(); 215 if (parameters != null) 216 { 217 for (int i = 0; i < parameters.length; ++i) 218 { 219 if (i != 0) 220 { 221 result.append(", "); 222 } 223 result.append(parameters[i]); 224 } 225 } 226 result.append(")"); 228 return result.toString(); 229 } 230 default: 231 { 232 return ""; 233 } 234 } 235 } 236 237 public void dump() 238 { 239 System.out.println("---- imports ---------------------------------------------"); 240 dumpStringToIDOMNodeMap(importMap); 241 System.out.println("---- types -----------------------------------------------"); 242 dumpStringToIDOMNodeMap(typeMap); 243 System.out.println("---- initializers ----------------------------------------"); 244 dumpStringToIDOMNodeMap(initializerMap); 245 System.out.println("---- fields ----------------------------------------------"); 246 dumpStringToIDOMNodeMap(fieldMap); 247 System.out.println("---- methods ---------------------------------------------"); 248 dumpStringToIDOMNodeMap(methodMap); 249 250 dumpMarkup(); 251 } 252 253 public void dumpMarkup() 254 { 255 System.out.println("==== markup ============================================"); 256 for (Iterator entries = markupMap.entrySet().iterator(); entries.hasNext(); ) 257 { 258 Map.Entry entry = (Map.Entry )entries.next(); 259 System.out.println("==== " + entry.getKey() + " ============================================"); 260 for (Iterator values = ((Collection )entry.getValue()).iterator(); values.hasNext(); ) 261 { 262 IDOMNode node = (IDOMNode)values.next(); 263 System.out.println(getQualifiedName(node)); 264 } 266 } 267 } 268 269 public void dumpNodeContents(IDOMNode node) 270 { 271 System.out.println("____ " + getQualifiedName(node) + " ____________________________________________"); 272 System.out.print(node.getContents()); 273 System.out.println("_____________________________________________________________________"); 274 } 275 276 public void dumpStringToIDOMNodeMap(Map map) 277 { 278 for (Iterator entries = map.entrySet().iterator(); entries.hasNext(); ) 279 { 280 Map.Entry entry = (Map.Entry )entries.next(); 281 String key = (String )entry.getKey(); 282 IDOMNode node = (IDOMNode)entry.getValue(); 283 System.out.println(key + "->" + getQualifiedName(node)); 284 } 286 } 287 288 protected static Pattern comment = Pattern.compile("/\\*.*?\\*/", Pattern.MULTILINE | Pattern.DOTALL); 289 protected static Object [] noArguments = new Object [0]; 290 protected void match(IDOMNode node) 291 { 292 for (Iterator dictionaryPatterns = options.getDictionaryPatterns().iterator(); dictionaryPatterns.hasNext(); ) 293 { 294 JControlModel.DictionaryPattern dictionaryPattern = (JControlModel.DictionaryPattern)dictionaryPatterns.next(); 295 if (dictionaryPattern.getSelectorFeature().getFeatureClass().isInstance(node)) 296 { 297 try 298 { 299 String selection = (String )dictionaryPattern.getSelectorFeature().getFeatureMethod().invoke(node, noArguments); 300 if (dictionaryPattern.getSelectorFeature().getFeatureMethod().getName().equals("getComment")) 301 { 302 String contents = node.getContents(); 303 for (int start = 0, end = contents.length(), count = 0; start < end; ) 304 { 305 Matcher matcher = comment.matcher(contents.subSequence(start, end)); 306 if (matcher.find()) 307 { 308 if (++count > 1) 311 { 312 int braceIndex = contents.indexOf("{", start); if (braceIndex > start + matcher.start(0)) 314 { 315 selection = null; 316 } 317 318 break; 319 } 320 start += matcher.end(0) + 1; 321 } 322 else 323 { 324 break; 325 } 326 } 327 } 328 329 if (selection != null) 330 { 331 Matcher matcher = dictionaryPattern.getPattern().matcher(selection); 332 if (matcher.find()) 333 { 334 for (int i = 1; i <= matcher.groupCount(); ++i) 335 { 336 String markup = matcher.group(i); 337 Collection collection = (Collection )markupMap.get(markup); 338 if (collection == null) 339 { 340 collection = new HashSet (); 341 markupMap.put(markup, collection); 342 } 343 collection.add(node); 344 } 345 } 346 } 347 } 348 catch (IllegalAccessException exception) 349 { 350 } 352 catch (InvocationTargetException exception) 353 { 354 } 356 } 357 } 358 } 359 360 public IDOMCompilationUnit getCompilationUnit() 361 { 362 return compilationUnit; 363 } 364 365 public IDOMPackage getPackage() 366 { 367 return jPackage; 368 } 369 370 public JControlModel options() 371 { 372 return options; 373 } 374 375 public Map getImportMap() 376 { 377 return importMap; 378 } 379 380 public Map getTypeMap() 381 { 382 return typeMap; 383 } 384 385 public Map getInitializerMap() 386 { 387 return initializerMap; 388 } 389 390 public Map getFieldMap() 391 { 392 return fieldMap; 393 } 394 395 public Map getMethodMap() 396 { 397 return methodMap; 398 } 399 400 public Map getMarkupMap() 401 { 402 return markupMap; 403 } 404 405 public boolean isMarkedUp(Pattern markupPattern, IDOMNode node) 406 { 407 if (markupPattern == null) 408 { 409 return true; 410 } 411 else 412 { 413 for (Iterator markupEntries = markupMap.entrySet().iterator(); markupEntries.hasNext(); ) 414 { 415 Map.Entry markupEntry = (Map.Entry )markupEntries.next(); 416 String key = (String )markupEntry.getKey(); 417 if (key != null && markupPattern.matcher(key).find()) 418 { 419 if (((Collection )markupEntry.getValue()).contains(node)) 420 { 421 return true; 422 } 423 } 424 } 425 return false; 426 } 427 } 428 429 public boolean isNoImport(IDOMImport domImport) 430 { 431 return noImportSet.contains(getQualifiedName(domImport)); 432 } 433 } 434 | Popular Tags |