1 17 package org.eclipse.emf.codegen.jmerge; 18 19 20 import java.lang.reflect.Method ; 21 import java.util.ArrayList ; 22 import java.util.List ; 23 import java.util.regex.Pattern ; 24 25 import javax.xml.parsers.DocumentBuilder ; 26 import javax.xml.parsers.DocumentBuilderFactory ; 27 28 import org.w3c.dom.Document ; 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.Node ; 31 import org.xml.sax.InputSource ; 32 33 import org.eclipse.emf.codegen.CodeGenPlugin; 34 35 36 39 public class JControlModel 40 { 41 public static class Feature 42 { 43 protected Class featureClass; 44 protected Method featureMethod; 45 46 public Feature(String path, Class [] parameterTypes) 47 { 48 int index = path.indexOf('/'); 49 String className = "org.eclipse.jdt.core.jdom.IDOM" + path.substring(0, index); 50 String methodName = path.substring(index + 1); 51 try 52 { 53 featureClass = Class.forName(className); 54 featureMethod = featureClass.getMethod(methodName, parameterTypes); 55 } 56 catch (NoSuchMethodException exception) 57 { 58 } 60 catch (ClassNotFoundException exception) 61 { 62 } 64 } 65 66 public Class getFeatureClass() 67 { 68 return featureClass; 69 } 70 71 public Method getFeatureMethod() 72 { 73 return featureMethod; 74 } 75 } 76 77 public static class DictionaryPattern 78 { 79 protected static Class [] noParameterTypes = new Class [0]; 80 protected static Class [] stringParameterType = new Class [] { String .class }; 81 protected String name; 82 protected Feature selectorFeature; 83 protected Pattern pattern; 84 85 public DictionaryPattern() 86 { 87 } 88 89 public DictionaryPattern(Element element) 90 { 91 initialize(element); 92 } 93 94 public void initialize(Element element) 95 { 96 name = element.getAttribute("name"); 97 selectorFeature = new Feature(element.getAttribute("select"), noParameterTypes); 98 pattern = Pattern.compile(element.getAttribute("match"), Pattern.MULTILINE | Pattern.DOTALL); 99 } 100 101 public String getName() 102 { 103 return name; 104 } 105 106 public void setName(String name) 107 { 108 this.name = name; 109 } 110 111 public Feature getSelectorFeature() 112 { 113 return selectorFeature; 114 } 115 116 public void setSelectorFeature(Feature selectorFeature) 117 { 118 this.selectorFeature = selectorFeature; 119 } 120 121 public Pattern getPattern() 122 { 123 return pattern; 124 } 125 126 public void setPattern(Pattern pattern) 127 { 128 this.pattern = pattern; 129 } 130 } 131 132 public static class PullRule 133 { 134 protected static Class [] noParameterTypes = new Class [0]; 135 protected String name; 136 137 protected Pattern sourceMarkup; 138 protected Feature sourceGetFeature; 139 protected Pattern sourceTransfer; 140 141 protected Pattern targetMarkup; 142 protected Feature targetPutFeature; 143 protected Pattern targetTransfer; 144 145 public PullRule() 146 { 147 } 148 149 public PullRule(Element element) 150 { 151 initialize(element); 152 } 153 154 public void initialize(Element element) 155 { 156 sourceGetFeature = new Feature(element.getAttribute("sourceGet"), noParameterTypes); 157 if (sourceGetFeature != null) 158 { 159 Class sourceReturnType = sourceGetFeature.getFeatureMethod().getReturnType(); 160 targetPutFeature = new Feature(element.getAttribute("targetPut"), new Class [] { sourceReturnType }); 161 if (targetPutFeature.getFeatureMethod() == null && sourceReturnType.isArray()) 162 { 163 targetPutFeature = new Feature(element.getAttribute("targetPut"), new Class [] { sourceReturnType.getComponentType() }); 164 } 165 } 166 if (element.hasAttribute("sourceMarkup")) 167 { 168 sourceMarkup= Pattern.compile(element.getAttribute("sourceMarkup"), Pattern.MULTILINE | Pattern.DOTALL); 169 } 170 if (element.hasAttribute("targetMarkup")) 171 { 172 targetMarkup= Pattern.compile(element.getAttribute("targetMarkup"), Pattern.MULTILINE | Pattern.DOTALL); 173 } 174 if (element.hasAttribute("sourceTransfer")) 175 { 176 sourceTransfer= Pattern.compile(element.getAttribute("sourceTransfer"), Pattern.MULTILINE | Pattern.DOTALL); 177 } 178 if (element.hasAttribute("targetTransfer")) 179 { 180 targetTransfer= Pattern.compile(element.getAttribute("targetTransfer"), Pattern.MULTILINE | Pattern.DOTALL); 181 } 182 } 183 184 public String getName() 185 { 186 return name; 187 } 188 189 public void setName(String name) 190 { 191 this.name = name; 192 } 193 194 public Feature getSourceGetFeature() 195 { 196 return sourceGetFeature; 197 } 198 199 public void setSourceGetFeature(Feature sourceGetFeature) 200 { 201 this.sourceGetFeature = sourceGetFeature; 202 } 203 204 public Feature getTargetPutFeature() 205 { 206 return targetPutFeature; 207 } 208 209 public void setTargetPutFeature(Feature targetPutFeature) 210 { 211 this.targetPutFeature = targetPutFeature; 212 } 213 214 public Pattern getSourceTransfer() 215 { 216 return sourceTransfer; 217 } 218 219 public void setSourceTransfer(Pattern sourceTransfer) 220 { 221 this.sourceTransfer = sourceTransfer; 222 } 223 224 public Pattern getTargetTransfer() 225 { 226 return targetTransfer; 227 } 228 229 public void setTargetTransfer(Pattern targetTransfer) 230 { 231 this.targetTransfer = targetTransfer; 232 } 233 234 public Pattern getSourceMarkup() 235 { 236 return sourceMarkup; 237 } 238 239 public void setSourceMarkup(Pattern sourceMarkup) 240 { 241 this.sourceMarkup = sourceMarkup; 242 } 243 244 public Pattern getTargetMarkup() 245 { 246 return targetMarkup; 247 } 248 249 public void setTargetMarkup(Pattern targetMarkup) 250 { 251 this.targetMarkup = targetMarkup; 252 } 253 } 254 255 public static class SweepRule 256 { 257 protected String name; 258 protected Class selector; 259 protected Pattern markup; 260 261 public SweepRule() 262 { 263 } 264 265 public SweepRule(Element element) 266 { 267 initialize(element); 268 } 269 270 public void initialize(Element element) 271 { 272 if (element.hasAttribute("select")) 273 { 274 selector = classForClassName(element.getAttribute("select")); 275 } 276 if (element.hasAttribute("markup")) 277 { 278 markup= Pattern.compile(element.getAttribute("markup"), Pattern.MULTILINE | Pattern.DOTALL); 279 } 280 } 281 282 public String getName() 283 { 284 return name; 285 } 286 287 public void setName(String name) 288 { 289 this.name = name; 290 } 291 292 public Class getSelector() 293 { 294 return selector; 295 } 296 297 public void setSelector(Class selector) 298 { 299 this.selector = selector; 300 } 301 302 public Pattern getMarkup() 303 { 304 return markup; 305 } 306 307 public void setMarkup(Pattern markup) 308 { 309 this.markup = markup; 310 } 311 } 312 313 public static class SortRule 314 { 315 protected String name; 316 protected Class selector; 317 protected Pattern markup; 318 319 public SortRule() 320 { 321 } 322 323 public SortRule(Element element) 324 { 325 initialize(element); 326 } 327 328 public void initialize(Element element) 329 { 330 if (element.hasAttribute("select")) 331 { 332 selector = classForClassName(element.getAttribute("select")); 333 } 334 if (element.hasAttribute("markup")) 335 { 336 markup= Pattern.compile(element.getAttribute("markup"), Pattern.MULTILINE | Pattern.DOTALL); 337 } 338 } 339 340 public String getName() 341 { 342 return name; 343 } 344 345 public void setName(String name) 346 { 347 this.name = name; 348 } 349 350 public Class getSelector() 351 { 352 return selector; 353 } 354 355 public void setSelector(Class selector) 356 { 357 this.selector = selector; 358 } 359 360 public Pattern getMarkup() 361 { 362 return markup; 363 } 364 365 public void setMarkup(Pattern markup) 366 { 367 this.markup = markup; 368 } 369 } 370 371 372 protected List dictionaryPatterns; 373 protected List pullRules; 374 protected List sweepRules; 375 protected List sortRules; 376 protected String indent; 377 protected String redirect; 378 protected boolean standardBraceStyle; 379 protected Pattern blockPattern; 380 protected Pattern noImportPattern; 381 382 385 public JControlModel(String uri) 386 { 387 initialize(uri); 388 } 389 390 public JControlModel(Element element) 391 { 392 initialize(element); 393 } 394 395 public boolean convertToStandardBraceStyle() 396 { 397 return standardBraceStyle; 398 } 399 400 public void setConvertToStandardBraceStyle(boolean standardBraceStyle) 401 { 402 this.standardBraceStyle = standardBraceStyle; 403 } 404 405 public String getLeadingTabReplacement() 406 { 407 return indent; 408 } 409 410 public void setLeadingTabReplacement(String indent) 411 { 412 this.indent = indent; 413 } 414 415 public String getRedirect() 416 { 417 return redirect; 418 } 419 420 public Pattern getBlockPattern() 421 { 422 return blockPattern; 423 } 424 425 public Pattern getNoImportPattern() 426 { 427 return noImportPattern; 428 } 429 430 public List getDictionaryPatterns() 431 { 432 if (dictionaryPatterns == null) 433 { 434 dictionaryPatterns = new ArrayList (); 435 } 436 return dictionaryPatterns; 437 } 438 439 public List getPullRules() 440 { 441 if (pullRules == null) 442 { 443 pullRules = new ArrayList (); 444 } 445 return pullRules; 446 } 447 448 public List getSweepRules() 449 { 450 if (sweepRules == null) 451 { 452 sweepRules = new ArrayList (); 453 } 454 return sweepRules; 455 } 456 457 public List getSortRules() 458 { 459 if (sortRules == null) 460 { 461 sortRules = new ArrayList (); 462 } 463 return sortRules; 464 } 465 466 protected void initialize(String uri) 467 { 468 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 469 documentBuilderFactory.setNamespaceAware(true); 470 documentBuilderFactory.setValidating(false); 471 try 472 { 473 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 474 Document document = documentBuilder.parse(new InputSource (uri)); 475 initialize(document.getDocumentElement()); 476 } 477 catch (Exception exception) 478 { 479 CodeGenPlugin.INSTANCE.log(exception); 480 } 481 } 482 483 protected void initialize(Element element) 484 { 485 if (element.getLocalName().equals("options")) 486 { 487 if ("standard".equals(element.getAttributeNS(null, "braceStyle"))) 488 { 489 standardBraceStyle = true; 490 } 491 492 if (element.hasAttributeNS(null, "indent")) 493 { 494 indent = element.getAttributeNS(null, "indent"); 495 } 496 497 if (element.hasAttributeNS(null, "redirect")) 498 { 499 redirect = element.getAttributeNS(null, "redirect"); 500 } 501 502 if (element.hasAttributeNS(null, "block")) 503 { 504 blockPattern = Pattern.compile(element.getAttributeNS(null, "block"), Pattern.MULTILINE | Pattern.DOTALL); 505 } 506 507 if (element.hasAttributeNS(null, "noImport")) 508 { 509 noImportPattern = Pattern.compile(element.getAttributeNS(null, "noImport"), Pattern.MULTILINE | Pattern.DOTALL); 510 } 511 512 for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) 513 { 514 if (child.getNodeType() == Node.ELEMENT_NODE) 515 { 516 Element elementChild = (Element )child; 517 if (elementChild.getLocalName().equals("dictionaryPattern")) 518 { 519 getDictionaryPatterns().add(new DictionaryPattern(elementChild)); 520 } 521 else if (elementChild.getLocalName().equals("pull")) 522 { 523 getPullRules().add(new PullRule(elementChild)); 524 } 525 else if (elementChild.getLocalName().equals("sweep")) 526 { 527 getSweepRules().add(new SweepRule(elementChild)); 528 } 529 else if (elementChild.getLocalName().equals("sort")) 530 { 531 getSortRules().add(new SortRule(elementChild)); 532 } 533 } 534 } 535 } 536 } 537 538 public static Class classForClassName(String className) 539 { 540 className = "org.eclipse.jdt.core.jdom.IDOM" + className; 541 try 542 { 543 Class result = Class.forName(className); 544 return result; 545 } 546 catch (ClassNotFoundException exception) 547 { 548 CodeGenPlugin.INSTANCE.log(exception); 549 } 550 return null; 551 } 552 } 553 | Popular Tags |