1 19 20 package org.netbeans.modules.java.freeform; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Set ; 28 import org.netbeans.api.java.project.JavaProjectConstants; 29 import org.netbeans.api.project.ant.AntArtifact; 30 import org.netbeans.api.project.ant.AntArtifactQuery; 31 import org.netbeans.modules.ant.freeform.spi.support.Util; 32 import org.netbeans.spi.project.AuxiliaryConfiguration; 33 import org.netbeans.spi.project.support.ant.AntProjectHelper; 34 import org.netbeans.spi.project.support.ant.EditableProperties; 35 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 36 import org.netbeans.spi.project.support.ant.PropertyUtils; 37 import org.openide.filesystems.FileUtil; 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.Element ; 40 41 48 public class JavaProjectGenerator { 49 50 51 private static final String [] rootElementsOrder = new String []{"name", "properties", "folders", "ide-actions", "export", "view", "subprojects"}; private static final String [] viewElementsOrder = new String []{"items", "context-menu"}; 54 private static final String [] folderElementsOrder = new String []{"source-folder", "build-folder"}; private static final String [] viewItemElementsOrder = new String []{"source-folder", "source-file"}; 58 65 public static final class SourceFolder { 66 public SourceFolder() {} 67 public String label; 68 public String type; 69 public String location; 70 public String style; 71 public String includes; 72 public String excludes; 73 public String toString() { 74 return "FPG.SF[label=" + label + ",type=" + type + ",location=" + location + ",style=" + style + ",includes=" + includes + ",excludes=" + excludes + "]"; } 76 } 77 78 86 public static List <SourceFolder> getSourceFolders(AntProjectHelper helper, String type) { 87 List <SourceFolder> list = new ArrayList <SourceFolder>(); 89 Element data = Util.getPrimaryConfigurationData(helper); 90 Element foldersEl = Util.findElement(data, "folders", Util.NAMESPACE); if (foldersEl == null) { 92 return list; 93 } 94 for (Element sourceFolderEl : Util.findSubElements(foldersEl)) { 95 if (!sourceFolderEl.getLocalName().equals("source-folder")) { continue; 97 } 98 SourceFolder sf = new SourceFolder(); 99 Element el = Util.findElement(sourceFolderEl, "label", Util.NAMESPACE); if (el != null) { 101 sf.label = Util.findText(el); 102 } 103 el = Util.findElement(sourceFolderEl, "type", Util.NAMESPACE); if (el != null) { 105 sf.type = Util.findText(el); 106 } 107 el = Util.findElement(sourceFolderEl, "location", Util.NAMESPACE); if (el != null) { 109 sf.location = Util.findText(el); 110 } 111 el = Util.findElement(sourceFolderEl, "includes", Util.NAMESPACE); if (el != null) { 113 sf.includes = Util.findText(el); 114 } 115 el = Util.findElement(sourceFolderEl, "excludes", Util.NAMESPACE); if (el != null) { 117 sf.excludes = Util.findText(el); 118 } 119 if (type == null || type.equals(sf.type)) { 120 if (sf.label == null || sf.label.length() == 0) { 121 throw new IllegalArgumentException ("label element is empty or not specified. "+helper.getProjectDirectory()); } 123 if (sf.location == null || sf.location.length() == 0) { 124 throw new IllegalArgumentException ("location element is empty or not specified. "+helper.getProjectDirectory()); } 126 list.add(sf); 127 } 128 } 129 return list; 130 } 131 132 142 public static void putSourceFolders(AntProjectHelper helper, List <SourceFolder> sources, String type) { 143 Element data = Util.getPrimaryConfigurationData(helper); 145 Document doc = data.getOwnerDocument(); 146 Element foldersEl = Util.findElement(data, "folders", Util.NAMESPACE); if (foldersEl == null) { 148 foldersEl = doc.createElementNS(Util.NAMESPACE, "folders"); Util.appendChildElement(data, foldersEl, rootElementsOrder); 150 } else { 151 for (Element sourceFolderEl : Util.findSubElements(foldersEl)) { 152 if (!sourceFolderEl.getLocalName().equals("source-folder")) { continue; 154 } 155 if (type == null) { 156 foldersEl.removeChild(sourceFolderEl); 157 } else { 158 Element typeEl = Util.findElement(sourceFolderEl, "type", Util.NAMESPACE); if (typeEl != null) { 160 String typeElValue = Util.findText(typeEl); 161 if (type.equals(typeElValue)) { 162 foldersEl.removeChild(sourceFolderEl); 163 } 164 } 165 } 166 } 167 } 168 for (SourceFolder sf : sources) { 169 Element sourceFolderEl = doc.createElementNS(Util.NAMESPACE, "source-folder"); Element el; 171 if (sf.label != null && sf.label.length() > 0) { 172 el = doc.createElementNS(Util.NAMESPACE, "label"); el.appendChild(doc.createTextNode(sf.label)); sourceFolderEl.appendChild(el); 175 } else { 176 throw new IllegalArgumentException ("label cannot be empty. "+helper.getProjectDirectory()); } 178 if (sf.type != null) { 179 el = doc.createElementNS(Util.NAMESPACE, "type"); el.appendChild(doc.createTextNode(sf.type)); sourceFolderEl.appendChild(el); 182 } 183 if (sf.location != null && sf.location.length() > 0) { 184 el = doc.createElementNS(Util.NAMESPACE, "location"); el.appendChild(doc.createTextNode(sf.location)); sourceFolderEl.appendChild(el); 187 } else { 188 throw new IllegalArgumentException ("location cannot be empty. "+helper.getProjectDirectory()); } 190 if (sf.includes != null) { 191 el = doc.createElementNS(Util.NAMESPACE, "includes"); el.appendChild(doc.createTextNode(sf.includes)); sourceFolderEl.appendChild(el); 194 } 195 if (sf.excludes != null) { 196 el = doc.createElementNS(Util.NAMESPACE, "excludes"); el.appendChild(doc.createTextNode(sf.excludes)); sourceFolderEl.appendChild(el); 199 } 200 Util.appendChildElement(foldersEl, sourceFolderEl, folderElementsOrder); 201 } 202 Util.putPrimaryConfigurationData(helper, data); 203 } 204 205 213 public static List getSourceViews(AntProjectHelper helper, String style) { 214 List <SourceFolder> list = new ArrayList <SourceFolder>(); 216 Element data = Util.getPrimaryConfigurationData(helper); 217 Element viewEl = Util.findElement(data, "view", Util.NAMESPACE); if (viewEl == null) { 219 return list; 220 } 221 Element itemsEl = Util.findElement(viewEl, "items", Util.NAMESPACE); if (itemsEl == null) { 223 return list; 224 } 225 for (Element sourceFolderEl : Util.findSubElements(itemsEl)) { 226 if (!sourceFolderEl.getLocalName().equals("source-folder")) { continue; 228 } 229 SourceFolder sf = new SourceFolder(); 230 sf.style = sourceFolderEl.getAttribute("style"); assert sf.style != null && sf.style.length() > 0 : "Bad style attr on <source-folder> in " + helper; Element el = Util.findElement(sourceFolderEl, "label", Util.NAMESPACE); if (el != null) { 234 sf.label = Util.findText(el); 235 } 236 el = Util.findElement(sourceFolderEl, "location", Util.NAMESPACE); if (el != null) { 238 sf.location = Util.findText(el); 239 } 240 el = Util.findElement(sourceFolderEl, "includes", Util.NAMESPACE); if (el != null) { 242 sf.includes = Util.findText(el); 243 } 244 el = Util.findElement(sourceFolderEl, "excludes", Util.NAMESPACE); if (el != null) { 246 sf.excludes = Util.findText(el); 247 } 248 if (style == null || style.equals(sf.style)) { 249 list.add(sf); 250 } 251 } 252 return list; 253 } 254 255 266 public static void putSourceViews(AntProjectHelper helper, List <SourceFolder> sources, String style) { 267 ArrayList list = new ArrayList (); 269 Element data = Util.getPrimaryConfigurationData(helper); 270 Document doc = data.getOwnerDocument(); 271 Element viewEl = Util.findElement(data, "view", Util.NAMESPACE); if (viewEl == null) { 273 viewEl = doc.createElementNS(Util.NAMESPACE, "view"); Util.appendChildElement(data, viewEl, rootElementsOrder); 275 } 276 Element itemsEl = Util.findElement(viewEl, "items", Util.NAMESPACE); if (itemsEl == null) { 278 itemsEl = doc.createElementNS(Util.NAMESPACE, "items"); Util.appendChildElement(viewEl, itemsEl, viewElementsOrder); 280 } 281 List <Element > sourceViews = Util.findSubElements(itemsEl); 282 Iterator it = sourceViews.iterator(); 283 while (it.hasNext()) { 284 Element sourceViewEl = (Element )it.next(); 285 if (!sourceViewEl.getLocalName().equals("source-folder")) { continue; 287 } 288 String sourceStyle = sourceViewEl.getAttribute("style"); if (style == null || style.equals(sourceStyle)) { 290 itemsEl.removeChild(sourceViewEl); 291 } 292 } 293 Iterator it2 = sources.iterator(); 294 while (it2.hasNext()) { 295 SourceFolder sf = (SourceFolder)it2.next(); 296 if (sf.style == null || sf.style.length() == 0) { 297 continue; 299 } 300 Element sourceFolderEl = doc.createElementNS(Util.NAMESPACE, "source-folder"); sourceFolderEl.setAttribute("style", sf.style); Element el; 303 if (sf.label != null) { 304 el = doc.createElementNS(Util.NAMESPACE, "label"); el.appendChild(doc.createTextNode(sf.label)); sourceFolderEl.appendChild(el); 307 } 308 if (sf.location != null) { 309 el = doc.createElementNS(Util.NAMESPACE, "location"); el.appendChild(doc.createTextNode(sf.location)); sourceFolderEl.appendChild(el); 312 } 313 if (sf.includes != null) { 314 el = doc.createElementNS(Util.NAMESPACE, "includes"); el.appendChild(doc.createTextNode(sf.includes)); sourceFolderEl.appendChild(el); 317 } 318 if (sf.excludes != null) { 319 el = doc.createElementNS(Util.NAMESPACE, "excludes"); el.appendChild(doc.createTextNode(sf.excludes)); sourceFolderEl.appendChild(el); 322 } 323 Util.appendChildElement(itemsEl, sourceFolderEl, viewItemElementsOrder); 324 } 325 Util.putPrimaryConfigurationData(helper, data); 326 } 327 328 329 335 public static List <JavaCompilationUnit> getJavaCompilationUnits( 336 AntProjectHelper helper, AuxiliaryConfiguration aux) { 337 List <JavaCompilationUnit> list = new ArrayList <JavaCompilationUnit>(); 339 Element data = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true); 340 if (data == null) { 341 data = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true); 342 } 343 if (data == null) { 344 return list; 345 } 346 for (Element cuEl : Util.findSubElements(data)) { 347 JavaCompilationUnit cu = new JavaCompilationUnit(); 348 List <String > outputs = new ArrayList <String >(); 349 List <String > javadoc = new ArrayList <String >(); 350 List <JavaCompilationUnit.CP> cps = new ArrayList <JavaCompilationUnit.CP>(); 351 List <String > packageRoots = new ArrayList <String >(); 352 for (Element el : Util.findSubElements(cuEl)) { 353 if (el.getLocalName().equals("package-root")) { packageRoots.add(Util.findText(el)); 355 continue; 356 } 357 if (el.getLocalName().equals("classpath")) { JavaCompilationUnit.CP cp = new JavaCompilationUnit.CP(); 359 cp.classpath = Util.findText(el); 360 cp.mode = el.getAttribute("mode"); if (cp.mode != null && cp.classpath != null) { 362 cps.add(cp); 363 } 364 continue; 365 } 366 if (el.getLocalName().equals("built-to")) { outputs.add(Util.findText(el)); 368 continue; 369 } 370 if (el.getLocalName().equals("javadoc-built-to")) { javadoc.add(Util.findText(el)); 372 continue; 373 } 374 if (el.getLocalName().equals("source-level")) { cu.sourceLevel = Util.findText(el); 376 } 377 if (el.getLocalName().equals("unit-tests")) { cu.isTests = true; 379 } 380 } 381 cu.output = outputs.size() > 0 ? outputs : null; 382 cu.javadoc = javadoc.size() > 0 ? javadoc : null; 383 cu.classpath = cps.size() > 0 ? cps: null; 384 cu.packageRoots = packageRoots.size() > 0 ? packageRoots: null; 385 list.add(cu); 386 } 387 return list; 388 } 389 390 397 public static void putJavaCompilationUnits(AntProjectHelper helper, 398 AuxiliaryConfiguration aux, List <JavaCompilationUnit> compUnits) { 399 boolean need2 = false; 402 for (JavaCompilationUnit unit : compUnits) { 403 if (unit.isTests || (unit.javadoc != null && !unit.javadoc.isEmpty())) { 404 need2 = true; 405 break; 406 } 407 } 408 String namespace; 409 Element data = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true); 411 if (data != null) { 412 namespace = JavaProjectNature.NS_JAVA_2; 414 } else { 415 namespace = need2 ? JavaProjectNature.NS_JAVA_2 : JavaProjectNature.NS_JAVA_1; 417 data = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true); 418 if (data != null) { 419 if (need2) { 420 aux.removeConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true); 422 data = Util.getPrimaryConfigurationData(helper).getOwnerDocument(). 423 createElementNS(JavaProjectNature.NS_JAVA_2, JavaProjectNature.EL_JAVA); 424 } } else { 426 data = Util.getPrimaryConfigurationData(helper).getOwnerDocument(). 428 createElementNS(namespace, JavaProjectNature.EL_JAVA); 429 } 430 } 431 Document doc = data.getOwnerDocument(); 432 for (Element cuEl : Util.findSubElements(data)) { 433 data.removeChild(cuEl); 434 } 435 for (JavaCompilationUnit cu : compUnits) { 436 Element cuEl = doc.createElementNS(namespace, "compilation-unit"); data.appendChild(cuEl); 438 Element el; 439 if (cu.packageRoots != null) { 440 for (String packageRoot : cu.packageRoots) { 441 el = doc.createElementNS(namespace, "package-root"); el.appendChild(doc.createTextNode(packageRoot)); 443 cuEl.appendChild(el); 444 } 445 } 446 if (cu.isTests) { 447 assert namespace.equals(JavaProjectNature.NS_JAVA_2); 448 cuEl.appendChild(doc.createElementNS(namespace, "unit-tests")); } 450 if (cu.classpath != null) { 451 for (JavaCompilationUnit.CP cp : cu.classpath) { 452 el = doc.createElementNS(namespace, "classpath"); el.appendChild(doc.createTextNode(cp.classpath)); 454 el.setAttribute("mode", cp.mode); cuEl.appendChild(el); 456 } 457 } 458 if (cu.output != null) { 459 Iterator it3 = cu.output.iterator(); 460 while (it3.hasNext()) { 461 String output = (String )it3.next(); 462 el = doc.createElementNS(namespace, "built-to"); el.appendChild(doc.createTextNode(output)); 464 cuEl.appendChild(el); 465 } 466 } 467 if (cu.javadoc != null) { 468 Iterator it3 = cu.javadoc.iterator(); 469 while (it3.hasNext()) { 470 String javadoc = (String ) it3.next(); 471 assert namespace.equals(JavaProjectNature.NS_JAVA_2); 472 el = doc.createElementNS(namespace, "javadoc-built-to"); el.appendChild(doc.createTextNode(javadoc)); 474 cuEl.appendChild(el); 475 } 476 } 477 if (cu.sourceLevel != null) { 478 el = doc.createElementNS(namespace, "source-level"); el.appendChild(doc.createTextNode(cu.sourceLevel)); 480 cuEl.appendChild(el); 481 } 482 } 483 aux.putConfigurationFragment(data, true); 484 } 485 486 487 491 public static final class JavaCompilationUnit { 492 public List <String > packageRoots; 493 public List <CP> classpath; 494 public List <String > output; 495 public List <String > javadoc; 496 public String sourceLevel; 497 public boolean isTests; 498 499 public String toString() { 500 return "FPG.JCU[packageRoots=" + packageRoots + ", classpath=" + classpath + ", output=" + output + ", javadoc=" + javadoc + ", sourceLevel=" + sourceLevel + ",isTests=" + isTests + "]"; } 502 503 public static final class CP { 504 public String classpath; 505 public String mode; 506 507 public String toString() { 508 return "FPG.JCU.CP:[classpath="+classpath+", mode="+mode+", this="+super.toString()+"]"; } 510 511 } 512 513 } 514 515 519 public static final class Export { 520 public String type; 521 public String location; 522 public String script; public String buildTarget; 524 public String cleanTarget; } 526 527 530 public static List <Export> guessExports(PropertyEvaluator evaluator, File baseFolder, 531 List <TargetMapping> targetMappings, List <JavaCompilationUnit> javaCompilationUnits) { 532 List <Export> exports = new ArrayList <Export>(); 534 String targetName = null; 535 String scriptName = null; 536 for (TargetMapping tm : targetMappings) { 537 if (tm.name.equals("build")) { if (tm.targets.size() == 1) { 539 targetName = tm.targets.get(0); 540 scriptName = tm.script; 541 } else { 542 return new ArrayList <Export>(); 543 } 544 } 545 } 546 if (targetName == null) { 547 return new ArrayList <Export>(); 548 } 549 for (JavaCompilationUnit cu : javaCompilationUnits) { 550 if (cu.output != null) { 551 for (String output : cu.output) { 552 String output2 = evaluator.evaluate(output); 553 if (output2.endsWith(".jar")) { Export e = new Export(); 555 e.type = JavaProjectConstants.ARTIFACT_TYPE_JAR; 556 e.location = output; 557 e.script = scriptName; 558 e.buildTarget = targetName; 559 exports.add(e); 560 } 561 else if (isFolder(evaluator, baseFolder, output2)) { 562 Export e = new Export(); 563 e.type = JavaProjectConstants.ARTIFACT_TYPE_FOLDER; 564 e.location = output; 565 e.script = scriptName; 566 e.buildTarget = targetName; 567 exports.add(e); 568 } 569 } 570 } 571 } 572 return exports; 573 } 574 575 581 public static void putExports(AntProjectHelper helper, List <Export> exports) { 582 ArrayList list = new ArrayList (); 584 Element data = Util.getPrimaryConfigurationData(helper); 585 Document doc = data.getOwnerDocument(); 586 Iterator it = Util.findSubElements(data).iterator(); 587 while (it.hasNext()) { 588 Element exportEl = (Element )it.next(); 589 if (!exportEl.getLocalName().equals("export")) { continue; 591 } 592 data.removeChild(exportEl); 593 } 594 Iterator it2 = exports.iterator(); 595 while (it2.hasNext()) { 596 Export export = (Export)it2.next(); 597 Element exportEl = doc.createElementNS(Util.NAMESPACE, "export"); Element el; 599 el = doc.createElementNS(Util.NAMESPACE, "type"); el.appendChild(doc.createTextNode(export.type)); exportEl.appendChild(el); 602 el = doc.createElementNS(Util.NAMESPACE, "location"); el.appendChild(doc.createTextNode(export.location)); exportEl.appendChild(el); 605 if (export.script != null) { 606 el = doc.createElementNS(Util.NAMESPACE, "script"); el.appendChild(doc.createTextNode(export.script)); exportEl.appendChild(el); 609 } 610 el = doc.createElementNS(Util.NAMESPACE, "build-target"); el.appendChild(doc.createTextNode(export.buildTarget)); exportEl.appendChild(el); 613 if (export.cleanTarget != null) { 614 el = doc.createElementNS(Util.NAMESPACE, "clean-target"); el.appendChild(doc.createTextNode(export.cleanTarget)); exportEl.appendChild(el); 617 } 618 Util.appendChildElement(data, exportEl, rootElementsOrder); 619 } 620 Util.putPrimaryConfigurationData(helper, data); 621 } 622 623 626 public static List <String > guessSubprojects(PropertyEvaluator evaluator, 627 List <JavaCompilationUnit> javaCompilationUnits, File projectBase, File freeformBase) { 628 Set <String > subprojs = new HashSet <String >(); 630 for (JavaCompilationUnit cu : javaCompilationUnits) { 631 if (cu.classpath != null) { 632 for (JavaCompilationUnit.CP cp : cu.classpath) { 633 if (!"compile".equals(cp.mode)) { continue; 635 } 636 String classpath = evaluator.evaluate(cp.classpath); 637 if (classpath == null) { 638 continue; 639 } 640 for (String s : PropertyUtils.tokenizePath(classpath)) { 641 File file = FileUtil.normalizeFile(new File (s)); 642 AntArtifact aa = AntArtifactQuery.findArtifactFromFile(file); 643 if (aa != null) { 644 File proj = FileUtil.toFile(aa.getProject().getProjectDirectory()); 645 String p = Util.relativizeLocation(projectBase, freeformBase, proj); 646 subprojs.add(p); 647 } 648 } 649 } 650 } 651 } 652 return new ArrayList <String >(subprojs); 653 } 654 655 661 public static void putSubprojects(AntProjectHelper helper, List <String > subprojects) { 662 ArrayList list = new ArrayList (); 664 Element data = Util.getPrimaryConfigurationData(helper); 665 Document doc = data.getOwnerDocument(); 666 Element subproject = Util.findElement(data, "subprojects", Util.NAMESPACE); if (subproject != null) { 668 data.removeChild(subproject); 669 } 670 subproject = doc.createElementNS(Util.NAMESPACE, "subprojects"); Util.appendChildElement(data, subproject, rootElementsOrder); 672 673 Iterator it = subprojects.iterator(); 674 while (it.hasNext()) { 675 String proj = (String )it.next(); 676 Element projEl = doc.createElementNS(Util.NAMESPACE, "project"); projEl.appendChild(doc.createTextNode(proj)); 678 subproject.appendChild(projEl); 679 } 680 Util.putPrimaryConfigurationData(helper, data); 681 } 682 683 686 public static List <String > guessBuildFolders(PropertyEvaluator evaluator, 687 List <JavaCompilationUnit> javaCompilationUnits, File projectBase, File freeformBase) { 688 List <String > buildFolders = new ArrayList <String >(); 690 for (JavaCompilationUnit cu : javaCompilationUnits) { 691 if (cu.output != null) { 692 for (String output : cu.output) { 693 File f = Util.resolveFile(evaluator, freeformBase, output); 694 if (f.exists()) { 695 if (f.isFile()) { 696 f = f.getParentFile(); 697 } 698 } else { 699 if (f.getName().indexOf('.') != -1) { 701 f = f.getParentFile(); 702 } 703 } 704 output = f.getAbsolutePath(); 705 if (!output.endsWith(File.separator)) { 706 output += File.separatorChar; 707 } 708 709 if (output.startsWith(projectBase.getAbsolutePath()+File.separatorChar) || 710 output.startsWith(freeformBase.getAbsolutePath()+File.separatorChar)) { 711 continue; 713 } 714 boolean add = true; 715 Iterator <String > it = buildFolders.iterator(); 716 while (it.hasNext()) { 717 String path = it.next(); 718 if (!path.endsWith(File.separator)) { 719 path += File.separatorChar; 720 } 721 if (path.equals(output)) { 722 add = false; 724 break; 725 } else if (output.startsWith(path)) { 726 add = false; 728 break; 729 } else if (path.startsWith(output)) { 730 it.remove(); 731 } 732 } 733 if (add) { 734 buildFolders.add(f.getAbsolutePath()); 735 } 736 } 737 } 738 } 739 return buildFolders; 740 } 741 742 748 public static void putBuildFolders(AntProjectHelper helper, List <String > buildFolders) { 749 ArrayList list = new ArrayList (); 751 Element data = Util.getPrimaryConfigurationData(helper); 752 Document doc = data.getOwnerDocument(); 753 Element foldersEl = Util.findElement(data, "folders", Util.NAMESPACE); if (foldersEl == null) { 755 foldersEl = doc.createElementNS(Util.NAMESPACE, "folders"); Util.appendChildElement(data, foldersEl, rootElementsOrder); 757 } else { 758 List <Element > folders = Util.findSubElements(foldersEl); 759 Iterator it = folders.iterator(); 760 while (it.hasNext()) { 761 Element buildFolderEl = (Element )it.next(); 762 if (!buildFolderEl.getLocalName().equals("build-folder")) { continue; 764 } 765 foldersEl.removeChild(buildFolderEl); 766 } 767 } 768 Iterator it = buildFolders.iterator(); 769 while (it.hasNext()) { 770 String location = (String )it.next(); 771 Element buildFolderEl = doc.createElementNS(Util.NAMESPACE, "build-folder"); Element locationEl = doc.createElementNS(Util.NAMESPACE, "location"); locationEl.appendChild(doc.createTextNode(location)); 774 buildFolderEl.appendChild(locationEl); 775 Util.appendChildElement(foldersEl, buildFolderEl, folderElementsOrder); 776 } 777 Util.putPrimaryConfigurationData(helper, data); 778 } 779 780 786 public static List <TargetMapping> getTargetMappings(AntProjectHelper helper) { 787 List <TargetMapping> list = new ArrayList <TargetMapping>(); 789 Element genldata = Util.getPrimaryConfigurationData(helper); 790 Element actionsEl = Util.findElement(genldata, "ide-actions", Util.NAMESPACE); if (actionsEl == null) { 792 return list; 793 } 794 for (Element actionEl : Util.findSubElements(actionsEl)) { 795 TargetMapping tm = new TargetMapping(); 796 tm.name = actionEl.getAttribute("name"); List <String > targetNames = new ArrayList <String >(); 798 EditableProperties props = new EditableProperties(false); 799 for (Element subEl : Util.findSubElements(actionEl)) { 800 if (subEl.getLocalName().equals("target")) { targetNames.add(Util.findText(subEl)); 802 continue; 803 } 804 if (subEl.getLocalName().equals("script")) { tm.script = Util.findText(subEl); 806 continue; 807 } 808 if (subEl.getLocalName().equals("context")) { TargetMapping.Context ctx = new TargetMapping.Context(); 810 for (Element contextSubEl : Util.findSubElements(subEl)) { 811 if (contextSubEl.getLocalName().equals("property")) { ctx.property = Util.findText(contextSubEl); 813 continue; 814 } 815 if (contextSubEl.getLocalName().equals("format")) { ctx.format = Util.findText(contextSubEl); 817 continue; 818 } 819 if (contextSubEl.getLocalName().equals("folder")) { ctx.folder = Util.findText(contextSubEl); 821 continue; 822 } 823 if (contextSubEl.getLocalName().equals("pattern")) { ctx.pattern = Util.findText(contextSubEl); 825 continue; 826 } 827 if (contextSubEl.getLocalName().equals("arity")) { Element sepFilesEl = Util.findElement(contextSubEl, "separated-files", Util.NAMESPACE); if (sepFilesEl != null) { 830 ctx.separator = Util.findText(sepFilesEl); 831 } 832 continue; 833 } 834 } 835 tm.context = ctx; 836 } 837 if (subEl.getLocalName().equals("property")) { readProperty(subEl, props); 839 continue; 840 } 841 } 842 tm.targets = targetNames; 843 if (props.keySet().size() > 0) { 844 tm.properties = props; 845 } 846 list.add(tm); 847 } 848 return list; 849 } 850 851 852 static boolean isFolder (PropertyEvaluator eval, File baseFolder, String folder) { 853 File f = Util.resolveFile(eval, baseFolder, folder); 854 if (f != null && f.isDirectory()) { 855 return true; 856 } 857 int dotIndex = folder.lastIndexOf('.'); int slashIndex = folder.lastIndexOf('/'); return dotIndex == -1 || (dotIndex < slashIndex) ; 860 } 861 862 866 public static final class TargetMapping { 867 public String script; 868 public List <String > targets; 869 public String name; 870 public EditableProperties properties; 871 public Context context; 873 public static final class Context { 874 public String property; 875 public String format; 876 public String folder; 877 public String pattern; public String separator; } 880 } 881 882 private static void readProperty(Element propertyElement, EditableProperties props) { 883 String key = propertyElement.getAttribute("name"); String value = Util.findText(propertyElement); 885 props.setProperty(key, value); 886 } 887 888 } 889 | Popular Tags |