1 19 20 package org.netbeans.modules.apisupport.project; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import java.util.SortedSet ; 34 import java.util.TreeSet ; 35 import org.netbeans.api.project.ProjectManager; 36 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider; 37 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider.NbModuleType; 38 import org.netbeans.modules.apisupport.project.suite.SuiteProjectType; 39 import org.netbeans.modules.apisupport.project.ui.customizer.ModuleDependency; 40 import org.netbeans.modules.apisupport.project.universe.ModuleEntry; 41 import org.openide.ErrorManager; 42 import org.openide.filesystems.FileLock; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileUtil; 45 import org.openide.xml.XMLUtil; 46 import org.w3c.dom.Document ; 47 import org.w3c.dom.Element ; 48 import org.netbeans.modules.apisupport.project.universe.ModuleList; 49 import org.netbeans.modules.apisupport.project.universe.NbPlatform; 50 import org.netbeans.modules.apisupport.project.universe.TestModuleDependency; 51 import org.w3c.dom.Node ; 52 import org.w3c.dom.NodeList ; 53 54 60 public final class ProjectXMLManager { 61 62 63 private static final String PROJECT_NS = 65 "http://www.netbeans.org/ns/project/1"; 67 private static final String BINARY_ORIGIN = "binary-origin"; private static final String BUILD_PREREQUISITE = "build-prerequisite"; private static final String CLASS_PATH_BINARY_ORIGIN = "binary-origin"; private static final String CLASS_PATH_EXTENSION = "class-path-extension"; private static final String CLASS_PATH_RUNTIME_PATH = "runtime-relative-path"; private static final String CODE_NAME_BASE = "code-name-base"; private static final String COMPILE_DEPENDENCY = "compile-dependency"; private static final String DATA = "data"; private static final String DEPENDENCY = "dependency"; private static final String EXTRA_COMPILATION_UNIT = "extra-compilation-unit"; private static final String FRIEND = "friend"; private static final String FRIEND_PACKAGES = "friend-packages"; private static final String IMPLEMENTATION_VERSION = "implementation-version"; private static final String MODULE_DEPENDENCIES = "module-dependencies"; private static final String PACKAGE = "package"; private static final String PUBLIC_PACKAGES= "public-packages"; private static final String RELEASE_VERSION = "release-version"; private static final String RUN_DEPENDENCY = "run-dependency"; private static final String SPECIFICATION_VERSION = "specification-version"; private static final String STANDALONE = "standalone"; private static final String SUBPACKAGES = "subpackages"; private static final String SUITE_COMPONENT = "suite-component"; private static final String TEST_DEPENDENCIES = "test-dependencies"; private static final String TEST_TYPE_NAME = "name"; private static final String TEST_DEPENDENCY = "test-dependency"; private static final String TEST_DEPENDENCY_CNB = "code-name-base"; private static final String TEST_DEPENDENCY_RECURSIVE = "recursive"; private static final String TEST_DEPENDENCY_COMPILE = "compile-dependency"; private static final String TEST_DEPENDENCY_TEST = "test"; private static final String TEST_TYPE = "test-type"; 99 private final NbModuleProject project; 100 private NbPlatform customPlaf; 101 102 private String cnb; 103 private SortedSet <ModuleDependency> directDeps; 104 private ManifestManager.PackageExport[] publicPackages; 105 private String [] cpExtensions; 106 private String [] friends; 107 108 private Element confData; 110 111 112 public ProjectXMLManager(final NbModuleProject project) { 113 this.project = project; 114 } 115 116 123 public static ProjectXMLManager getInstance(final File projectDir) throws IOException { 124 FileObject dir = FileUtil.toFileObject(projectDir); 125 NbModuleProject p = (NbModuleProject) ProjectManager.getDefault().findProject(dir); 126 return new ProjectXMLManager(p); 127 } 128 129 public void setModuleType(NbModuleType moduleType) { 130 Element confData = getConfData(); 131 Document doc = confData.getOwnerDocument(); 132 133 Element standaloneEl = findElement(confData, ProjectXMLManager.STANDALONE); 134 if (standaloneEl != null && moduleType == NbModuleProvider.STANDALONE) { 135 return; 137 } 138 139 Element suiteCompEl = findElement(confData, ProjectXMLManager.SUITE_COMPONENT); 140 if (suiteCompEl != null && moduleType == NbModuleProvider.SUITE_COMPONENT) { 141 return; 143 } 144 145 if (suiteCompEl == null && standaloneEl == null && moduleType == NbModuleProvider.NETBEANS_ORG) { 146 return; 148 } 149 150 if (suiteCompEl != null) { 152 confData.removeChild(suiteCompEl); 153 } 154 if (standaloneEl != null) { 155 confData.removeChild(standaloneEl); 156 } 157 158 Element newModuleType = createTypeElement(doc, moduleType); 160 if (newModuleType != null) { 161 confData.insertBefore(newModuleType, findModuleDependencies(confData)); 162 } 163 project.putPrimaryConfigurationData(confData); 164 } 165 166 171 public SortedSet <ModuleDependency> getDirectDependencies() throws IOException { 172 return getDirectDependencies(null); 173 } 174 175 181 public SortedSet <ModuleDependency> getDirectDependencies(final NbPlatform customPlaf) throws IOException { 182 if (this.customPlaf == customPlaf && this.directDeps != null) { 183 return this.directDeps; 184 } 185 this.customPlaf = customPlaf; 186 SortedSet <ModuleDependency> directDeps = new TreeSet (ModuleDependency.CNB_COMPARATOR); 187 Element moduleDependencies = findModuleDependencies(getConfData()); 188 assert moduleDependencies != null : "Cannot find <module-dependencies> for: " + project; 189 List <Element > deps = Util.findSubElements(moduleDependencies); 190 File prjDirF = project.getProjectDirectoryFile(); 191 ModuleList ml; 192 if (customPlaf != null) { 193 ml = ModuleList.getModuleList(prjDirF, customPlaf.getDestDir()); 194 } else { 195 ml = ModuleList.getModuleList(prjDirF); 196 } 197 for (Iterator it = deps.iterator(); it.hasNext(); ) { 198 Element depEl = (Element )it.next(); 199 200 Element cnbEl = findElement(depEl, ProjectXMLManager.CODE_NAME_BASE); 201 String cnb = Util.findText(cnbEl); 202 ModuleEntry me = ml.getEntry(cnb); 203 if (me == null) { 204 Util.err.log(ErrorManager.WARNING, 206 "Detected dependency on module which cannot be found in " + "the current module's universe (platform, suite): " + cnb + " (skipping)"); continue; 210 } 211 212 Element runDepEl = findElement(depEl, ProjectXMLManager.RUN_DEPENDENCY); 213 if (runDepEl == null) { 214 if (!directDeps.add(new ModuleDependency(me))) { 215 String errMessage = "Corrupted project metadata (project.xml). " + "Duplicate dependency entry found: " + me; Util.err.log(ErrorManager.WARNING, errMessage); 218 throw new IllegalStateException (errMessage); 219 } 220 continue; 221 } 222 223 Element relVerEl = findElement(runDepEl, ProjectXMLManager.RELEASE_VERSION); 224 String relVer = null; 225 if (relVerEl != null) { 226 relVer = Util.findText(relVerEl); 227 } 228 229 Element specVerEl = findElement(runDepEl, ProjectXMLManager.SPECIFICATION_VERSION); 230 String specVer = null; 231 if (specVerEl != null) { 232 specVer = Util.findText(specVerEl); 233 } 234 235 Element compDepEl = findElement(depEl, ProjectXMLManager.COMPILE_DEPENDENCY); 236 Element impleVerEl = findElement(runDepEl, ProjectXMLManager.IMPLEMENTATION_VERSION); 237 238 ModuleDependency depToAdd = new ModuleDependency( 239 me, relVer, specVer, compDepEl != null, impleVerEl != null); 240 if (!directDeps.add(depToAdd)) { 241 String errMessage = "Corrupted project metadata (project.xml). " + "Duplicate dependency entry found: " + depToAdd; Util.err.log(ErrorManager.WARNING, errMessage); 244 throw new IllegalStateException (errMessage); 245 } 246 } 247 this.directDeps = Collections.unmodifiableSortedSet(directDeps); 248 return this.directDeps; 249 } 250 251 252 public void removeDependency(String cnbToRemove) { 253 Element confData = getConfData(); 254 Element moduleDependencies = findModuleDependencies(confData); 255 List <Element > currentDeps = Util.findSubElements(moduleDependencies); 256 for (Iterator it = currentDeps.iterator(); it.hasNext(); ) { 257 Element dep = (Element )it.next(); 258 Element cnbEl = findElement(dep, ProjectXMLManager.CODE_NAME_BASE); 259 String cnb = Util.findText(cnbEl); 260 if (cnbToRemove.equals(cnb)) { 261 moduleDependencies.removeChild(dep); 262 } 263 } 264 project.putPrimaryConfigurationData(confData); 265 } 266 267 271 public void removeDependencies(Collection <ModuleDependency> depsToDelete) { 272 Set cnbsToDelete = new HashSet (depsToDelete.size()); 273 for (Iterator it = depsToDelete.iterator(); it.hasNext(); ) { 274 cnbsToDelete.add(((ModuleDependency) it.next()). 275 getModuleEntry().getCodeNameBase()); 276 } 277 removeDependenciesByCNB(cnbsToDelete); 278 } 279 280 284 public void removeDependenciesByCNB(Collection <String > cnbsToDelete) { 285 Element confData = getConfData(); 286 Element moduleDependencies = findModuleDependencies(confData); 287 List <Element > currentDeps = Util.findSubElements(moduleDependencies); 288 for (Iterator it = currentDeps.iterator(); it.hasNext(); ) { 289 Element dep = (Element )it.next(); 290 Element cnbEl = findElement(dep, ProjectXMLManager.CODE_NAME_BASE); 291 String cnb = Util.findText(cnbEl); 292 if (cnbsToDelete.remove(cnb)) { 293 moduleDependencies.removeChild(dep); 294 } 295 if (cnbsToDelete.size() == 0) { 296 break; } 298 } 299 if (cnbsToDelete.size() != 0) { 300 Util.err.log(ErrorManager.WARNING, 301 "Some modules weren't deleted: " + cnbsToDelete); } 303 project.putPrimaryConfigurationData(confData); 304 } 305 306 public void editDependency(ModuleDependency origDep, ModuleDependency newDep) { 307 Element confData = getConfData(); 308 Element moduleDependencies = findModuleDependencies(confData); 309 List <Element > currentDeps = Util.findSubElements(moduleDependencies); 310 for (Iterator it = currentDeps.iterator(); it.hasNext(); ) { 311 Element dep = (Element ) it.next(); 312 Element cnbEl = findElement(dep, ProjectXMLManager.CODE_NAME_BASE); 313 String cnb = Util.findText(cnbEl); 314 if (cnb.equals(origDep.getModuleEntry().getCodeNameBase())) { 315 moduleDependencies.removeChild(dep); 316 Element nextDep = it.hasNext() ? (Element ) it.next() : null; 317 createModuleDependencyElement(moduleDependencies, newDep, nextDep); 318 break; 319 } 320 } 321 project.putPrimaryConfigurationData(confData); 322 } 323 324 327 public void addDependency(ModuleDependency md) throws IOException { 328 addDependencies(Collections.singleton(md)); 329 } 330 331 334 public void addDependencies(final Set <ModuleDependency> toAdd) throws IOException { 335 SortedSet deps = new TreeSet (getDirectDependencies()); 336 if (deps.addAll(toAdd)) { 337 replaceDependencies(deps); 338 } 339 } 340 341 344 public void replaceDependencies(final Set <ModuleDependency> newDeps) { 345 Element confData = getConfData(); 346 Document doc = confData.getOwnerDocument(); 347 Element moduleDependencies = findModuleDependencies(confData); 348 confData.removeChild(moduleDependencies); 349 moduleDependencies = createModuleElement(doc, ProjectXMLManager.MODULE_DEPENDENCIES); 350 Element before = findTestDependenciesElement(confData); 351 if (before == null) { 352 before = findPublicPackagesElement(confData); 353 } 354 if (before == null) { 355 before = findFriendsElement(confData); 356 } 357 assert before != null : "There must be " + PUBLIC_PACKAGES + " or " + FRIEND_PACKAGES + " element according to XSD"; confData.insertBefore(moduleDependencies, before); 360 SortedSet <ModuleDependency> sortedDeps = new TreeSet (newDeps); 361 for (Iterator it = sortedDeps.iterator(); it.hasNext(); ) { 362 ModuleDependency md = (ModuleDependency) it.next(); 363 createModuleDependencyElement(moduleDependencies, md, null); 364 } 365 project.putPrimaryConfigurationData(confData); 366 this.directDeps = sortedDeps; 367 } 368 369 public void removeClassPathExtensions() { 370 Element confData = getConfData(); 371 NodeList nl = confData.getElementsByTagNameNS(NbModuleProjectType.NAMESPACE_SHARED, 372 ProjectXMLManager.CLASS_PATH_EXTENSION); 373 for (int i = 0; i < nl.getLength(); i++) { 374 confData.removeChild(nl.item(i)); 375 } 376 project.putPrimaryConfigurationData(confData); 377 } 378 379 384 public boolean removeTestDependency(String testType, String cnbToRemove){ 385 boolean wasRemoved = false; 386 Element confData = getConfData(); 387 Element testModuleDependenciesEl = findTestDependenciesElement(confData); 388 List testTypesList = Util.findSubElements(testModuleDependenciesEl); 389 Element testTypeRemoveEl = null; 390 for (Iterator it = testTypesList.iterator(); it.hasNext(); ) { 391 Element type = (Element )it.next(); 392 Element nameEl = findElement(type, TEST_TYPE_NAME); 393 String nameOfType = Util.findText(nameEl); 394 if (testType.equals(nameOfType)) { 395 testTypeRemoveEl = type; 396 } 397 } 398 if (testTypeRemoveEl != null){ 400 List testDepList = Util.findSubElements(testTypeRemoveEl); 401 for (Iterator it = testDepList.iterator(); it.hasNext();) { 402 Element el = (Element ) it.next(); 403 Element cnbEl = findElement(el, TEST_DEPENDENCY_CNB); 404 if(cnbEl == null) { 405 continue; } 407 String cnb = Util.findText(cnbEl); 408 if (cnbToRemove.equals(cnb)) { 409 testTypeRemoveEl.removeChild(el); 411 wasRemoved = true; 412 project.putPrimaryConfigurationData(confData); 413 } 414 } 415 } 416 return wasRemoved; 417 } 418 419 424 public void addTestDependency(String testType, TestModuleDependency newTestDep) throws IOException { 425 final String UNIT = TestModuleDependency.UNIT; 426 final String QA_FUNCTIONAL = TestModuleDependency.QA_FUNCTIONAL; 427 assert (UNIT.equals(testType) || QA_FUNCTIONAL.equals(testType)) : "Current impl.supports only " + QA_FUNCTIONAL + 428 " or " + UNIT + " tests"; File projectDir = FileUtil.toFile(project.getProjectDirectory()); 430 ModuleList ml = ModuleList.getModuleList(projectDir); 431 Map map = new HashMap (getTestDependencies(ml)); 432 Set testDependenciesSet = (Set ) map.get(testType); 433 if(testDependenciesSet == null) { 434 testDependenciesSet = new TreeSet (); 435 map.put(testType, testDependenciesSet); 436 } else { 437 testDependenciesSet = new TreeSet (); 438 testDependenciesSet.addAll((Set ) map.get(testType)); 439 } 440 if (!testDependenciesSet.add(newTestDep)) { 441 return; } 443 Element confData = getConfData(); 444 Document doc = confData.getOwnerDocument(); 445 Element testModuleDependenciesEl = findTestDependenciesElement(confData); 446 if (testModuleDependenciesEl == null) { Element before = findPublicPackagesElement(confData); 448 if (before == null) { 449 before = findFriendsElement(confData); 450 } 451 assert before != null : "There must be " + PUBLIC_PACKAGES + " or " + FRIEND_PACKAGES + " element according to XSD"; testModuleDependenciesEl= createModuleElement(doc, TEST_DEPENDENCIES); 454 confData.insertBefore(testModuleDependenciesEl, before); 455 } 456 Element testTypeEl = null; 457 List listOfTestTypes = Util.findSubElements(testModuleDependenciesEl); 458 for (Iterator it = listOfTestTypes.iterator(); it.hasNext();) { 460 Element tt = (Element ) it.next(); 461 Node nameNode = findElement(tt, "name"); 462 assert nameNode!=null : "should be some child with name"; 463 assert (TEST_TYPE_NAME.equals(nameNode.getLocalName())) : "name node should be first child, but was:"+nameNode.getLocalName() + 466 "or" + nameNode.getNodeName(); if(nameNode.getTextContent().equals(testType)) { 469 testTypeEl = tt; 470 } 471 } 472 if (testTypeEl == null){ 474 Element newTestTypeEl = createNewTestTypeElement(doc, testType); 476 testModuleDependenciesEl.appendChild(newTestTypeEl); 477 createTestModuleDependencyElement(newTestTypeEl, newTestDep); 478 project.putPrimaryConfigurationData(confData); 479 return; 480 } else { 481 Node beforeWhat = testTypeEl.getNextSibling(); 483 testModuleDependenciesEl.removeChild(testTypeEl); 484 Element refreshedTestTypeEl = createNewTestTypeElement(doc, testType); 485 if(beforeWhat == null) { 486 testModuleDependenciesEl.appendChild(refreshedTestTypeEl); 487 } else { 488 testModuleDependenciesEl.insertBefore(refreshedTestTypeEl, beforeWhat); 489 } 490 for (Iterator it = testDependenciesSet.iterator(); it.hasNext(); ) { 491 TestModuleDependency tmd = (TestModuleDependency) it.next(); 492 createTestModuleDependencyElement(refreshedTestTypeEl, tmd); 493 project.putPrimaryConfigurationData(confData); 494 } 495 } 496 } 497 498 private Element createNewTestTypeElement(Document doc, String testTypeName){ 499 Element newTestTypeEl = createModuleElement(doc, TEST_TYPE); 500 Element nameOfTestTypeEl = createModuleElement(doc, TEST_TYPE_NAME, testTypeName); 501 newTestTypeEl.appendChild(nameOfTestTypeEl); 502 return newTestTypeEl; 503 } 504 505 506 private void createTestModuleDependencyElement(Element testTypeElement, TestModuleDependency tmd) { 507 Document doc = testTypeElement.getOwnerDocument(); 508 Element tde = createModuleElement(doc, TEST_DEPENDENCY); 509 testTypeElement.appendChild(tde); 510 tde.appendChild(createModuleElement(doc, TEST_DEPENDENCY_CNB, tmd.getModule().getCodeNameBase())); 511 if(tmd.isRecursive()) { 512 tde.appendChild(createModuleElement(doc, TEST_DEPENDENCY_RECURSIVE)); 513 } 514 if(tmd.isCompile()) { 515 tde.appendChild(createModuleElement(doc, TEST_DEPENDENCY_COMPILE)); 516 } 517 if(tmd.isTest()) { 518 tde.appendChild(createModuleElement(doc, TEST_DEPENDENCY_TEST)); 519 } 520 } 521 522 523 527 public Map <String ,Set <TestModuleDependency>> getTestDependencies(final ModuleList ml) { 528 Element testDepsEl = findTestDependenciesElement(getConfData()); 529 530 Map <String ,Set <TestModuleDependency>> testDeps = new HashMap (); 531 532 if (testDepsEl != null) { 533 for (Iterator typesIt = Util.findSubElements(testDepsEl).iterator(); typesIt.hasNext();) { 534 Element typeEl = (Element ) typesIt.next(); 535 Element testTypeEl = findElement(typeEl, TEST_TYPE_NAME); 536 String testType = null; 537 if (testTypeEl != null) { 538 testType = Util.findText(testTypeEl); 539 } 540 if (testType == null) { 541 testType = TestModuleDependency.UNIT; } 543 Set <TestModuleDependency> directTestDeps = new HashSet (); 544 for (Iterator depsIt = Util.findSubElements(typeEl).iterator() ; depsIt.hasNext();) { 545 Element depEl = (Element ) depsIt.next(); 546 if (depEl.getTagName().equals(TEST_DEPENDENCY)) { 547 Element cnbEl = findElement(depEl, TEST_DEPENDENCY_CNB); 549 boolean test = findElement(depEl, TEST_DEPENDENCY_TEST) != null; 550 String cnb = null; 551 if (cnbEl != null) { 552 cnb = Util.findText(cnbEl); 553 } 554 boolean recursive = findElement(depEl, TEST_DEPENDENCY_RECURSIVE) != null; 555 boolean compile = findElement(depEl, TEST_DEPENDENCY_COMPILE) != null; 556 if (cnb != null) { 557 ModuleEntry me = ml.getEntry(cnb); 558 if (me != null) { 559 directTestDeps.add(new TestModuleDependency(me, test, recursive, compile)); 560 } 561 } 562 } 563 } 564 testDeps.put(testType, directTestDeps); 565 } 566 } 567 return testDeps; 568 } 569 570 574 public void replaceClassPathExtensions(final Map newValues) { 575 removeClassPathExtensions(); 576 if (newValues != null && newValues.size() > 0) { 577 Element confData = getConfData(); 578 Document doc = confData.getOwnerDocument(); 579 Iterator it = newValues.entrySet().iterator(); 580 while (it.hasNext()) { 581 Map.Entry entry = (Map.Entry )it.next(); 582 Element cpel = createModuleElement(doc, ProjectXMLManager.CLASS_PATH_EXTENSION); 583 Element runtime = createModuleElement(doc, ProjectXMLManager.CLASS_PATH_RUNTIME_PATH, 584 (String )entry.getKey()); 585 Element binary = createModuleElement(doc, ProjectXMLManager.CLASS_PATH_BINARY_ORIGIN, 586 (String )entry.getValue()); 587 cpel.appendChild(runtime); 588 cpel.appendChild(binary); 589 confData.appendChild(cpel); 590 591 } 592 project.putPrimaryConfigurationData(confData); 593 } 594 } 595 596 601 public void replacePublicPackages(String [] newPackages) { 602 removePublicAndFriends(); 603 Element confData = getConfData(); 604 Document doc = confData.getOwnerDocument(); 605 Element publicPackagesEl = createModuleElement(doc, ProjectXMLManager.PUBLIC_PACKAGES); 606 607 insertPublicOrFriend(publicPackagesEl); 608 609 for (int i = 0; i < newPackages.length; i++) { 610 publicPackagesEl.appendChild( 611 createModuleElement(doc, ProjectXMLManager.PACKAGE, newPackages[i])); 612 } 613 project.putPrimaryConfigurationData(confData); 614 publicPackages = null; } 616 617 618 private void insertPublicOrFriend(Element packagesEl) { 619 Element beforeEl = findElement(getConfData(), ProjectXMLManager.CLASS_PATH_EXTENSION); 620 if (beforeEl == null) { 621 beforeEl = findElement(getConfData(), ProjectXMLManager.EXTRA_COMPILATION_UNIT); 622 } 623 getConfData().insertBefore(packagesEl, beforeEl); 624 } 625 626 632 public void replaceFriends(String [] friends, String [] packagesToExpose) { 633 removePublicAndFriends(); 634 Element confData = getConfData(); 635 Document doc = confData.getOwnerDocument(); 636 Element friendPackages = createModuleElement(doc, ProjectXMLManager.FRIEND_PACKAGES); 637 insertPublicOrFriend(friendPackages); 638 for (int i = 0; i < friends.length; i++) { 639 friendPackages.appendChild( 640 createModuleElement(doc, ProjectXMLManager.FRIEND, friends[i])); 641 } 642 for (int i = 0; i < packagesToExpose.length; i++) { 643 friendPackages.appendChild( 644 createModuleElement(doc, ProjectXMLManager.PACKAGE, packagesToExpose[i])); 645 } 646 project.putPrimaryConfigurationData(confData); 647 publicPackages = null; 648 } 649 650 659 public ManifestManager.PackageExport[] getPublicPackages() { 660 if (publicPackages == null) { 661 publicPackages = ProjectXMLManager.findPublicPackages(getConfData()); 662 } 663 return publicPackages; 664 } 665 666 667 public String [] getFriends() { 668 if (friends == null) { 669 friends = ProjectXMLManager.findFriends(getConfData()); 670 } 671 return friends; 672 } 673 674 675 682 public String [] getBinaryOrigins() { 683 if (cpExtensions != null) { 684 return cpExtensions; 685 } 686 687 List <Element > cpExtEls = Util.findSubElements(getConfData()); 688 Set <String > binaryOrigs = new TreeSet (); 689 for (Iterator it = cpExtEls.iterator(); it.hasNext(); ) { 690 Element cpExtEl = (Element ) it.next(); 691 if (CLASS_PATH_EXTENSION.equals(cpExtEl.getTagName())) { 692 Element binOrigEl = findElement(cpExtEl, BINARY_ORIGIN); 693 if (binOrigEl != null) { 694 binaryOrigs.add(Util.findText(binOrigEl)); 695 } 696 } 697 } 698 return cpExtensions = (String []) binaryOrigs.toArray(new String [binaryOrigs.size()]); 699 } 700 701 702 public String getCodeNameBase() { 703 if (cnb == null) { 704 Element cnbEl = findElement(getConfData(), ProjectXMLManager.CODE_NAME_BASE); 705 cnb = Util.findText(cnbEl); 706 } 707 return cnb; 708 } 709 710 711 static void createModuleDependencyElement( 712 Element moduleDependencies, ModuleDependency md, Element nextSibling) { 713 714 Document doc = moduleDependencies.getOwnerDocument(); 715 Element modDepEl = createModuleElement(doc, ProjectXMLManager.DEPENDENCY); 716 moduleDependencies.insertBefore(modDepEl, nextSibling); 717 718 modDepEl.appendChild(createModuleElement(doc, ProjectXMLManager.CODE_NAME_BASE, 719 md.getModuleEntry().getCodeNameBase())); 720 if (md.hasCompileDependency()) { 721 modDepEl.appendChild(createModuleElement(doc, ProjectXMLManager.BUILD_PREREQUISITE)); 722 modDepEl.appendChild(createModuleElement(doc, ProjectXMLManager.COMPILE_DEPENDENCY)); 723 } 724 725 Element runDepEl = createModuleElement(doc, ProjectXMLManager.RUN_DEPENDENCY); 726 modDepEl.appendChild(runDepEl); 727 728 String rv = md.getReleaseVersion(); 729 if (rv != null && !rv.trim().equals("")) { 730 runDepEl.appendChild(createModuleElement( 731 doc, ProjectXMLManager.RELEASE_VERSION, rv)); 732 } 733 if (md.hasImplementationDepedendency()) { 734 runDepEl.appendChild(createModuleElement( 735 doc, ProjectXMLManager.IMPLEMENTATION_VERSION)); 736 } else { 737 String sv = md.getSpecificationVersion(); 738 if (sv != null && !"".equals(sv)) { runDepEl.appendChild(createModuleElement( 740 doc, ProjectXMLManager.SPECIFICATION_VERSION, sv)); 741 } 742 } 743 } 744 745 746 private void removePublicAndFriends() { 747 Element friendPackages = findFriendsElement(getConfData()); 748 if (friendPackages != null) { 749 getConfData().removeChild(friendPackages); 750 } 751 Element publicPackages = findPublicPackagesElement(getConfData()); 752 if (publicPackages != null) { 753 getConfData().removeChild(publicPackages); 754 } 755 } 756 757 private static Element findElement(Element parentEl, String elementName) { 758 return Util.findElement(parentEl, elementName, NbModuleProjectType.NAMESPACE_SHARED); 759 } 760 761 762 static Element findModuleDependencies(Element parentEl) { 763 return findElement(parentEl, ProjectXMLManager.MODULE_DEPENDENCIES); 764 } 765 766 private static Element findTestDependenciesElement(Element parentEl) { 767 return findElement(parentEl, ProjectXMLManager.TEST_DEPENDENCIES); 768 } 769 770 private static Element findPublicPackagesElement(Element parentEl) { 771 return findElement(parentEl, ProjectXMLManager.PUBLIC_PACKAGES); 772 } 773 774 private static Element findFriendsElement(Element parentEl) { 775 return findElement(parentEl, ProjectXMLManager.FRIEND_PACKAGES); 776 } 777 778 private static Element createModuleElement(Document doc, String name) { 779 return doc.createElementNS(NbModuleProjectType.NAMESPACE_SHARED, name); 780 } 781 782 private static Element createModuleElement(Document doc, String name, String innerText) { 783 Element el = createModuleElement(doc, name); 784 el.appendChild(doc.createTextNode(innerText)); 785 return el; 786 } 787 788 private static Element createSuiteElement(Document doc, String name) { 789 return doc.createElementNS(SuiteProjectType.NAMESPACE_SHARED, name); 790 } 791 792 private static Element createSuiteElement(Document doc, String name, String innerText) { 793 Element el = createSuiteElement(doc, name); 794 el.appendChild(doc.createTextNode(innerText)); 795 return el; 796 } 797 798 803 private static Set <ManifestManager.PackageExport> findAllPackages(Element parent) { 804 Set <ManifestManager.PackageExport> packages = new HashSet (); 805 List <Element > pkgEls = Util.findSubElements(parent); 806 for (Iterator it = pkgEls.iterator(); it.hasNext(); ) { 807 Element pkgEl = (Element ) it.next(); 808 if (PACKAGE.equals(pkgEl.getTagName())) { 809 packages.add(new ManifestManager.PackageExport(Util.findText(pkgEl), false)); 810 } else if (SUBPACKAGES.equals(pkgEl.getTagName())) { 811 packages.add(new ManifestManager.PackageExport(Util.findText(pkgEl), true)); 812 } 813 } 814 return packages; 815 } 816 817 825 public static ManifestManager.PackageExport[] findPublicPackages(final Element confData) { 826 Element ppEl = findPublicPackagesElement(confData); 827 Set <ManifestManager.PackageExport> pps = new HashSet (); 828 if (ppEl != null) { 829 pps.addAll(findAllPackages(ppEl)); 830 } 831 ppEl = findFriendsElement(confData); 832 if (ppEl != null) { 833 pps.addAll(findAllPackages(ppEl)); 834 } 835 return pps.isEmpty() ? ManifestManager.EMPTY_EXPORTED_PACKAGES : 836 (ManifestManager.PackageExport[]) pps.toArray(new ManifestManager.PackageExport[pps.size()]); 837 } 838 839 840 public static String [] findFriends(final Element confData) { 841 Element friendsEl = findFriendsElement(confData); 842 if (friendsEl != null) { 843 List <Element > friendEls = Util.findSubElements(friendsEl); 844 Set <String > friends = new TreeSet (); 845 for (Iterator it = friendEls.iterator(); it.hasNext(); ) { 846 Element friendEl = (Element ) it.next(); 847 if (FRIEND.equals(friendEl.getTagName())) { 848 friends.add(Util.findText(friendEl)); 849 } 850 } 851 String [] result = new String [friends.size()]; 852 return (String []) friends.toArray(result); 853 } 854 return null; 855 } 856 857 862 static void generateEmptyModuleTemplate(FileObject projectXml, String cnb, 863 NbModuleType moduleType) throws IOException { 864 865 Document prjDoc = XMLUtil.createDocument("project", PROJECT_NS, null, null); 867 Element typeEl = prjDoc.createElementNS(PROJECT_NS, "type"); typeEl.appendChild(prjDoc.createTextNode(NbModuleProjectType.TYPE)); 870 prjDoc.getDocumentElement().appendChild(typeEl); 871 Element confEl = prjDoc.createElementNS(PROJECT_NS, "configuration"); prjDoc.getDocumentElement().appendChild(confEl); 873 874 Element dataEl = createModuleElement(confEl.getOwnerDocument(), DATA); 876 confEl.appendChild(dataEl); 877 Document dataDoc = dataEl.getOwnerDocument(); 878 dataEl.appendChild(createModuleElement(dataDoc, CODE_NAME_BASE, cnb)); 879 Element moduleTypeEl = createTypeElement(dataDoc, moduleType); 880 if (moduleTypeEl != null) { 881 dataEl.appendChild(moduleTypeEl); 882 } 883 dataEl.appendChild(createModuleElement(dataDoc, MODULE_DEPENDENCIES)); 884 dataEl.appendChild(createModuleElement(dataDoc, PUBLIC_PACKAGES)); 885 886 ProjectXMLManager.safelyWrite(projectXml, prjDoc); 888 } 889 890 896 static void generateLibraryModuleTemplate(FileObject projectXml, String cnb, 897 NbModuleType moduleType, Set publicPackages, Map extensions) throws IOException { 898 899 Document prjDoc = XMLUtil.createDocument("project", PROJECT_NS, null, null); 901 Element typeEl = prjDoc.createElementNS(PROJECT_NS, "type"); typeEl.appendChild(prjDoc.createTextNode(NbModuleProjectType.TYPE)); 904 prjDoc.getDocumentElement().appendChild(typeEl); 905 Element confEl = prjDoc.createElementNS(PROJECT_NS, "configuration"); prjDoc.getDocumentElement().appendChild(confEl); 907 908 Element dataEl = createModuleElement(confEl.getOwnerDocument(), DATA); 910 confEl.appendChild(dataEl); 911 Document dataDoc = dataEl.getOwnerDocument(); 912 dataEl.appendChild(createModuleElement(dataDoc, CODE_NAME_BASE, cnb)); 913 Element moduleTypeEl = createTypeElement(dataDoc, moduleType); 914 if (moduleTypeEl != null) { 915 dataEl.appendChild(moduleTypeEl); 916 } 917 dataEl.appendChild(createModuleElement(dataDoc, MODULE_DEPENDENCIES)); 918 Element packages = createModuleElement(dataDoc, PUBLIC_PACKAGES); 919 dataEl.appendChild(packages); 920 Iterator it = publicPackages.iterator(); 921 while (it.hasNext()) { 922 packages.appendChild(createModuleElement(dataDoc, PACKAGE, (String )it.next())); 923 } 924 it = extensions.entrySet().iterator(); 925 while (it.hasNext()) { 926 Element cp = createModuleElement(dataDoc, CLASS_PATH_EXTENSION); 927 dataEl.appendChild(cp); 928 Map.Entry entry = (Map.Entry )it.next(); 929 cp.appendChild(createModuleElement(dataDoc, CLASS_PATH_RUNTIME_PATH, (String )entry.getKey())); 930 cp.appendChild(createModuleElement(dataDoc, CLASS_PATH_BINARY_ORIGIN, (String )entry.getValue())); 931 } 932 933 ProjectXMLManager.safelyWrite(projectXml, prjDoc); 935 } 936 937 private static Element createTypeElement(Document dataDoc, NbModuleType type) { 938 Element result = null; 939 if (type == NbModuleProvider.STANDALONE) { 940 result = createModuleElement(dataDoc, STANDALONE); 941 } else if (type == NbModuleProvider.SUITE_COMPONENT) { 942 result = createModuleElement(dataDoc, SUITE_COMPONENT); 943 } 944 return result; 945 } 946 947 951 public static void generateEmptySuiteTemplate(FileObject projectXml, String name) throws IOException { 952 Document prjDoc = XMLUtil.createDocument("project", PROJECT_NS, null, null); 956 Element typeEl = prjDoc.createElementNS(PROJECT_NS, "type"); typeEl.appendChild(prjDoc.createTextNode(SuiteProjectType.TYPE)); 959 prjDoc.getDocumentElement().appendChild(typeEl); 960 Element confEl = prjDoc.createElementNS(PROJECT_NS, "configuration"); prjDoc.getDocumentElement().appendChild(confEl); 962 963 Element dataEl = createSuiteElement(confEl.getOwnerDocument(), DATA); 965 confEl.appendChild(dataEl); 966 Document dataDoc = dataEl.getOwnerDocument(); 967 dataEl.appendChild(createSuiteElement(dataDoc, "name", name)); 969 ProjectXMLManager.safelyWrite(projectXml, prjDoc); 971 } 972 973 private static void safelyWrite(FileObject projectXml, Document prjDoc) throws IOException { 974 FileLock lock = projectXml.lock(); 975 try { 976 OutputStream os = projectXml.getOutputStream(lock); 977 try { 978 XMLUtil.write(prjDoc, os, "UTF-8"); } finally { 980 os.close(); 981 } 982 } finally { 983 lock.releaseLock(); 984 } 985 } 986 987 private Element getConfData() { 988 if (confData == null) { 989 confData = project.getPrimaryConfigurationData(); 990 } 991 return confData; 992 } 993 994 } 995 | Popular Tags |