1 19 20 package org.netbeans.modules.apisupport.project; 21 22 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.text.Collator ; 32 import java.util.ArrayList ; 33 import java.util.Collection ; 34 import java.util.Collections ; 35 import java.util.Comparator ; 36 import java.util.Enumeration ; 37 import java.util.HashMap ; 38 import java.util.HashSet ; 39 import java.util.Iterator ; 40 import java.util.LinkedHashSet ; 41 import java.util.List ; 42 import java.util.Locale ; 43 import java.util.Map ; 44 import java.util.Set ; 45 import java.util.SortedSet ; 46 import java.util.StringTokenizer ; 47 import java.util.TreeSet ; 48 import java.util.jar.JarFile ; 49 import java.util.jar.Manifest ; 50 import java.util.zip.ZipEntry ; 51 import javax.swing.event.ChangeEvent ; 52 import javax.swing.event.ChangeListener ; 53 import org.netbeans.api.java.project.JavaProjectConstants; 54 import org.netbeans.api.project.Project; 55 import org.netbeans.api.project.ProjectInformation; 56 import org.netbeans.api.project.ProjectManager; 57 import org.netbeans.api.project.ProjectUtils; 58 import org.netbeans.api.project.SourceGroup; 59 import org.netbeans.api.project.Sources; 60 import org.netbeans.api.project.Sources; 61 import org.netbeans.api.queries.VisibilityQuery; 62 import org.netbeans.modules.apisupport.project.ui.customizer.ModuleDependency; 63 import org.netbeans.modules.apisupport.project.universe.LocalizedBundleInfo; 64 import org.netbeans.modules.apisupport.project.universe.ModuleEntry; 65 import org.netbeans.modules.apisupport.project.universe.NbPlatform; 66 import org.netbeans.spi.project.support.ant.EditableProperties; 67 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 68 import org.netbeans.spi.project.support.ant.PropertyProvider; 69 import org.netbeans.spi.project.support.ant.PropertyUtils; 70 import org.openide.ErrorManager; 71 import org.openide.filesystems.FileLock; 72 import org.openide.filesystems.FileObject; 73 import org.openide.filesystems.FileUtil; 74 import org.openide.filesystems.URLMapper; 75 import org.openide.modules.SpecificationVersion; 76 import org.openide.util.NbBundle; 77 import org.openide.util.Utilities; 78 import org.openide.util.WeakListeners; 79 import org.w3c.dom.Element ; 80 import org.w3c.dom.NamedNodeMap ; 81 import org.w3c.dom.Node ; 82 import org.w3c.dom.NodeList ; 83 import org.w3c.dom.Text ; 84 85 90 public final class Util { 91 92 private Util() {} 93 94 public static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.apisupport.project"); 96 private static final String SFS_VALID_PATH_RE = "(\\p{Alnum}|\\/|_)+"; 98 102 112 public static Element findElement(Element parent, String name, String namespace) { 113 Element result = null; 114 NodeList l = parent.getChildNodes(); 115 for (int i = 0; i < l.getLength(); i++) { 116 if (l.item(i).getNodeType() == Node.ELEMENT_NODE) { 117 Element el = (Element )l.item(i); 118 if ((namespace == null && name.equals(el.getTagName())) || 119 (namespace != null && name.equals(el.getLocalName()) && 120 namespace.equals(el.getNamespaceURI()))) { 121 if (result == null) { 122 result = el; 123 } else { 124 return null; 125 } 126 } 127 } 128 } 129 return result; 130 } 131 132 138 public static String findText(Element parent) { 139 NodeList l = parent.getChildNodes(); 140 for (int i = 0; i < l.getLength(); i++) { 141 if (l.item(i).getNodeType() == Node.TEXT_NODE) { 142 Text text = (Text )l.item(i); 143 return text.getNodeValue(); 144 } 145 } 146 return null; 147 } 148 149 159 public static List <Element > findSubElements(Element parent) throws IllegalArgumentException { 160 NodeList l = parent.getChildNodes(); 161 List <Element > elements = new ArrayList <Element >(l.getLength()); 162 for (int i = 0; i < l.getLength(); i++) { 163 Node n = l.item(i); 164 if (n.getNodeType() == Node.ELEMENT_NODE) { 165 elements.add((Element )n); 166 } else if (n.getNodeType() == Node.TEXT_NODE) { 167 String text = ((Text )n).getNodeValue(); 168 if (text.trim().length() > 0) { 169 throw new IllegalArgumentException ("non-ws text encountered in " + parent + ": " + text); } 171 } else if (n.getNodeType() == Node.COMMENT_NODE) { 172 } else { 174 throw new IllegalArgumentException ("unexpected non-element child of " + parent + ": " + n); } 176 } 177 return elements; 178 } 179 180 183 public static Element translateXML(Element from, String namespace) { 184 Element to = from.getOwnerDocument().createElementNS(namespace, from.getLocalName()); 185 NodeList nl = from.getChildNodes(); 186 int length = nl.getLength(); 187 for (int i = 0; i < length; i++) { 188 Node node = nl.item(i); 189 Node newNode; 190 if (node.getNodeType() == Node.ELEMENT_NODE) { 191 newNode = translateXML((Element ) node, namespace); 192 } else { 193 newNode = node.cloneNode(true); 194 } 195 to.appendChild(newNode); 196 } 197 NamedNodeMap m = from.getAttributes(); 198 for (int i = 0; i < m.getLength(); i++) { 199 Node attr = m.item(i); 200 to.setAttribute(attr.getNodeName(), attr.getNodeValue()); 201 } 202 return to; 203 } 204 205 207 211 public static URL urlForDir(File dir) { 212 try { 213 URL u = FileUtil.normalizeFile(dir).toURI().toURL(); 214 String s = u.toExternalForm(); 215 if (s.endsWith("/")) { return u; 217 } else { 218 return new URL (s + "/"); } 220 } catch (MalformedURLException e) { 221 throw new AssertionError (e); 222 } 223 } 224 225 228 public static URL urlForJar(File jar) { 229 try { 230 return FileUtil.getArchiveRoot(FileUtil.normalizeFile(jar).toURI().toURL()); 231 } catch (MalformedURLException e) { 232 throw new AssertionError (e); 233 } 234 } 235 236 241 public static URL urlForDirOrJar(File location) { 242 try { 243 URL u = FileUtil.normalizeFile(location).toURI().toURL(); 244 if (FileUtil.isArchiveFile(u)) { 245 u = FileUtil.getArchiveRoot(u); 246 } else { 247 String us = u.toExternalForm(); 248 if (!us.endsWith("/")) { u = new URL (us + "/"); } 251 } 252 return u; 253 } catch (MalformedURLException e) { 254 throw new AssertionError (e); 255 } 256 } 257 258 263 public static String getDisplayName(FileObject projectDir) { 264 if (projectDir.isFolder()) { 265 try { 266 Project p = ProjectManager.getDefault().findProject(projectDir); 267 if (p != null) { 268 return ProjectUtils.getInformation(p).getDisplayName(); 269 } 270 } catch (IOException e) { 271 } 273 } 274 return FileUtil.getFileDisplayName(projectDir); 275 } 276 277 281 public static String normalizeCNB(String value) { 282 StringTokenizer tk = new StringTokenizer (value.toLowerCase(Locale.ENGLISH), ".", true); StringBuffer normalizedCNB = new StringBuffer (); 284 boolean delimExpected = false; 285 while (tk.hasMoreTokens()) { 286 String namePart = tk.nextToken(); 287 if (!delimExpected) { 288 if (namePart.equals(".")) { continue; 290 } 291 for (int i = 0; i < namePart.length(); i++) { 292 char c = namePart.charAt(i); 293 if (i == 0) { 294 if (!Character.isJavaIdentifierStart(c)) { 295 continue; 296 } 297 } else { 298 if (!Character.isJavaIdentifierPart(c)) { 299 continue; 300 } 301 } 302 normalizedCNB.append(c); 303 } 304 } else { 305 if (namePart.equals(".")) { normalizedCNB.append(namePart); 307 } 308 } 309 delimExpected = !delimExpected; 310 } 311 return normalizedCNB.toString().replaceAll("\\.$", ""); } 314 315 322 public static boolean isValidJavaFQN(String name) { 323 if (name.length() == 0) { 324 return false; 325 } 326 StringTokenizer tk = new StringTokenizer (name,".",true); boolean delimExpected = false; 328 while (tk.hasMoreTokens()) { 329 String namePart = tk.nextToken(); 330 if (delimExpected ^ namePart.equals(".")) { return false; 332 } 333 if (!delimExpected && !Utilities.isJavaIdentifier(namePart)) { 334 return false; 335 } 336 delimExpected = !delimExpected; 337 } 338 return delimExpected; 339 } 340 341 356 public static LocalizedBundleInfo findLocalizedBundleInfo(FileObject sourceDir, Manifest manifest) { 357 String locBundleResource = 358 ManifestManager.getInstance(manifest, false).getLocalizingBundle(); 359 try { 360 if (locBundleResource != null) { 361 List <FileObject> bundleFOs = new ArrayList <FileObject>(); 362 for (Iterator it = getPossibleResources(locBundleResource); it.hasNext(); ) { 363 String resource = (String ) it.next(); 364 FileObject bundleFO = sourceDir.getFileObject(resource); 365 if (bundleFO != null) { 366 bundleFOs.add(bundleFO); 367 } 368 } 369 if (!bundleFOs.isEmpty()) { 370 Collections.reverse(bundleFOs); 371 return LocalizedBundleInfo.load(bundleFOs.toArray(new FileObject[bundleFOs.size()])); 372 } 373 } 374 } catch (IOException e) { 375 Util.err.notify(ErrorManager.INFORMATIONAL, e); 376 } 377 return null; 378 } 379 380 383 public static LocalizedBundleInfo findLocalizedBundleInfo(File projectDir) { 384 FileObject projectDirFO = FileUtil.toFileObject(projectDir); 385 if (projectDirFO == null) { 386 return null; 387 } 388 NbModuleProject p; 389 try { 390 p = (NbModuleProject) ProjectManager.getDefault().findProject(projectDirFO); 391 } catch (IOException e) { 392 return null; 393 } 394 if (p == null) { 395 return null; 396 } 397 String src = p.evaluator().getProperty("src.dir"); assert src != null : "Cannot evaluate src.dir property for " + p; 399 File srcF = FileUtil.normalizeFile(new File (projectDir, src)); 400 FileObject sourceDir = FileUtil.toFileObject(srcF); 401 FileObject manifestFO = FileUtil.toFileObject(new File (projectDir, "manifest.mf")); 403 LocalizedBundleInfo locInfo = null; 404 Manifest mf = getManifest(manifestFO); 405 if (sourceDir != null && mf != null) { 406 locInfo = findLocalizedBundleInfo(sourceDir, mf); 407 } 408 return locInfo; 409 } 410 411 415 public static LocalizedBundleInfo findLocalizedBundleInfoFromJAR(File binaryProject) { 416 try { 417 JarFile main = new JarFile (binaryProject); 418 try { 419 Manifest mf = main.getManifest(); 420 String locBundleResource = 421 ManifestManager.getInstance(mf, false).getLocalizingBundle(); 422 if (locBundleResource != null) { 423 List <InputStream > bundleISs = new ArrayList <InputStream >(); 424 Collection <JarFile > extraJarFiles = new ArrayList <JarFile >(); 425 try { 426 String name = binaryProject.getName(); 429 int dot = name.lastIndexOf('.'); 430 if (dot == -1) { 431 dot = name.length(); 432 } 433 String base = name.substring(0, dot); 434 String suffix = name.substring(dot); 435 Iterator <String > it = NbBundle.getLocalizingSuffixes(); 436 while (it.hasNext()) { 437 String infix = it.next(); 438 File variant = new File (binaryProject.getParentFile(), "locale" + File.separatorChar + base + infix + suffix); if (variant.isFile()) { 440 JarFile jf = new JarFile (variant); 441 extraJarFiles.add(jf); 442 addBundlesFromJar(jf, bundleISs, locBundleResource); 443 } 444 } 445 addBundlesFromJar(main, bundleISs, locBundleResource); 447 if (!bundleISs.isEmpty()) { 448 Collections.reverse(bundleISs); 449 return LocalizedBundleInfo.load(bundleISs.toArray(new InputStream [bundleISs.size()])); 450 } 451 } finally { 452 for (InputStream bundleIS : bundleISs) { 453 bundleIS.close(); 454 } 455 for (JarFile jarFile : extraJarFiles) { 456 jarFile.close(); 457 } 458 } 459 } 460 } finally { 461 main.close(); 462 } 463 } catch (IOException e) { 464 Util.err.notify(ErrorManager.INFORMATIONAL, e); 465 } 466 return null; 467 } 468 469 private static void addBundlesFromJar(JarFile jf, List <InputStream > bundleISs, String locBundleResource) throws IOException { 470 for (Iterator it = getPossibleResources(locBundleResource); it.hasNext(); ) { 471 String resource = (String ) it.next(); 472 ZipEntry entry = jf.getEntry(resource); 473 if (entry != null) { 474 InputStream bundleIS = jf.getInputStream(entry); 475 bundleISs.add(bundleIS); 476 } 477 } 478 } 479 480 490 public static EditableProperties loadProperties(FileObject propsFO) throws IOException { 491 InputStream propsIS = propsFO.getInputStream(); 492 EditableProperties props = new EditableProperties(true); 493 try { 494 props.load(propsIS); 495 } finally { 496 propsIS.close(); 497 } 498 return props; 499 } 500 501 509 public static void storeProperties(FileObject propsFO, EditableProperties props) throws IOException { 510 FileLock lock = propsFO.lock(); 511 try { 512 OutputStream os = propsFO.getOutputStream(lock); 513 try { 514 props.store(os); 515 } finally { 516 os.close(); 517 } 518 } finally { 519 lock.releaseLock(); 520 } 521 } 522 523 533 public static EditableManifest loadManifest(FileObject manifestFO) throws IOException { 534 InputStream mfIS = manifestFO.getInputStream(); 535 try { 536 return new EditableManifest(mfIS); 537 } finally { 538 mfIS.close(); 539 } 540 } 541 542 550 public static void storeManifest(FileObject manifestFO, EditableManifest em) throws IOException { 551 FileLock lock = manifestFO.lock(); 552 try { 553 OutputStream os = manifestFO.getOutputStream(lock); 554 try { 555 em.write(os); 556 } finally { 557 os.close(); 558 } 559 } finally { 560 lock.releaseLock(); 561 } 562 } 563 564 567 public static URL findJavadocForNetBeansOrgModules(final ModuleDependency dep) { 568 ModuleEntry entry = dep.getModuleEntry(); 569 File destDir = entry.getDestDir(); 570 File nbOrg = null; 571 if (destDir.getParent() != null) { 572 nbOrg = destDir.getParentFile().getParentFile(); 573 } 574 if (nbOrg == null) { 575 throw new IllegalArgumentException ("ModuleDependency " + dep + " doesn't represent nb.org module"); } 578 File builtJavadoc = new File (nbOrg, "nbbuild/build/javadoc"); URL [] javadocURLs = null; 580 if (builtJavadoc.exists()) { 581 File [] javadocs = builtJavadoc.listFiles(); 582 javadocURLs = new URL [javadocs.length]; 583 for (int i = 0; i < javadocs.length; i++) { 584 javadocURLs[i] = Util.urlForDirOrJar(javadocs[i]); 585 } 586 } 587 return javadocURLs == null ? null : findJavadocURL( 588 dep.getModuleEntry().getCodeNameBase().replace('.', '-'), javadocURLs); 589 } 590 591 595 public static URL findJavadoc(final ModuleDependency dep, final NbPlatform platform) { 596 String cnbdashes = dep.getModuleEntry().getCodeNameBase().replace('.', '-'); 597 URL [] roots = platform.getJavadocRoots(); 598 return roots == null ? null : findJavadocURL(cnbdashes, roots); 599 } 600 601 public static boolean isValidSFSPath(final String path) { 602 return path.matches(SFS_VALID_PATH_RE); 603 } 604 605 608 public static boolean addDependency(final NbModuleProject target, 609 final NbModuleProject dependency) throws IOException { 610 return addDependency(target, dependency.getCodeNameBase()); 611 } 612 613 617 public static boolean addDependency(final NbModuleProject target, 618 final String codeNameBase) throws IOException { 619 return Util.addDependency(target, codeNameBase, null, null, true); 620 } 621 622 645 public static boolean addDependency(final NbModuleProject target, 646 final String codeNameBase, final String releaseVersion, 647 final SpecificationVersion version, final boolean useInCompiler) throws IOException { 648 ModuleEntry me = target.getModuleList().getEntry(codeNameBase); 649 if (me == null) { Util.err.log(ErrorManager.INFORMATIONAL, "Trying to add " + codeNameBase + " which cannot be found in the module's universe."); return false; 653 } 654 655 ProjectXMLManager pxm = new ProjectXMLManager(target); 656 657 Set currentDeps = pxm.getDirectDependencies(); 659 for (Iterator it = currentDeps.iterator(); it.hasNext(); ) { 660 ModuleDependency md = (ModuleDependency) it.next(); 661 if (codeNameBase.equals(md.getModuleEntry().getCodeNameBase())) { 662 Util.err.log(ErrorManager.INFORMATIONAL, codeNameBase + " already added"); return false; 664 } 665 } 666 667 ModuleDependency md = new ModuleDependency(me, 668 (releaseVersion == null) ? me.getReleaseVersion() : releaseVersion, 669 version == null ? me.getSpecificationVersion() : version.toString(), 670 useInCompiler, false); 671 pxm.addDependency(md); 672 return true; 673 } 674 675 private static URL findJavadocURL(final String cnbdashes, final URL [] roots) { 676 URL indexURL = null; 677 for (int i = 0; i < roots.length; i++) { 678 URL root = roots[i]; 679 try { 680 indexURL = Util.normalizeURL(new URL (root, cnbdashes + "/index.html")); if (indexURL == null && (root.toExternalForm().indexOf(cnbdashes) != -1)) { 682 indexURL = Util.normalizeURL(new URL (root, "index.html")); } 684 } catch (MalformedURLException ex) { 685 } 687 if (indexURL != null) { 688 break; 689 } 690 } 691 return indexURL; 692 } 693 694 private static URL normalizeURL(URL url) { 695 return URLMapper.findFileObject(url) == null ? null : url; 704 } 705 706 private static Iterator getPossibleResources(String locBundleResource) { 707 String locBundleResourceBase, locBundleResourceExt; 708 int idx = locBundleResource.lastIndexOf('.'); 709 if (idx != -1 && idx > locBundleResource.lastIndexOf('/')) { 710 locBundleResourceBase = locBundleResource.substring(0, idx); 711 locBundleResourceExt = locBundleResource.substring(idx); 712 } else { 713 locBundleResourceBase = locBundleResource; 714 locBundleResourceExt = ""; 715 } 716 Collection <String > resources = new LinkedHashSet <String >(); 717 for (Iterator <String > it = NbBundle.getLocalizingSuffixes(); it.hasNext(); ) { 718 String suffix = it.next(); 719 String resource = locBundleResourceBase + suffix + locBundleResourceExt; 720 resources.add(resource); 721 resources.add(resource); 722 } 723 return resources.iterator(); 724 } 725 726 public static Manifest getManifest(FileObject manifestFO) { 727 if (manifestFO != null) { 728 try { 729 InputStream is = manifestFO.getInputStream(); 730 try { 731 return new Manifest (is); 732 } finally { 733 is.close(); 734 } 735 } catch (IOException e) { 736 Util.err.notify(ErrorManager.INFORMATIONAL, e); 737 } 738 } 739 return null; 740 } 741 742 746 public static abstract class ComputedPropertyProvider implements PropertyProvider, PropertyChangeListener { 747 private final PropertyEvaluator eval; 748 private final List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 749 protected ComputedPropertyProvider(PropertyEvaluator eval) { 750 this.eval = eval; 751 eval.addPropertyChangeListener(WeakListeners.propertyChange(this, eval)); 752 } 753 754 protected abstract Map <String ,String > getProperties(Map <String ,String > inputPropertyValues); 755 756 protected abstract Set <String > inputProperties(); 757 public final Map <String ,String > getProperties() { 758 Map <String ,String > vals = new HashMap <String , String >(); 759 for (String k : inputProperties()) { 760 vals.put(k, eval.getProperty(k)); 761 } 762 return getProperties(vals); 763 } 764 public final void addChangeListener(ChangeListener l) { 765 synchronized (listeners) { 766 listeners.add(l); 767 } 768 } 769 public final void removeChangeListener(ChangeListener l) { 770 synchronized (listeners) { 771 listeners.remove(l); 772 } 773 } 774 public final void propertyChange(PropertyChangeEvent evt) { 775 String p = evt.getPropertyName(); 776 if (p != null && !inputProperties().contains(p)) { 777 return; 778 } 779 ChangeEvent ev = new ChangeEvent (this); 780 Iterator <ChangeListener > it; 781 synchronized (listeners) { 782 if (listeners.isEmpty()) { 783 return; 784 } 785 it = new HashSet <ChangeListener >(listeners).iterator(); 786 } 787 while (it.hasNext()) { 788 it.next().stateChanged(ev); 789 } 790 } 791 } 792 793 public static final class UserPropertiesFileProvider implements PropertyProvider, PropertyChangeListener , ChangeListener { 794 private final PropertyEvaluator eval; 795 private final File basedir; 796 private final List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 797 private PropertyProvider delegate; 798 public UserPropertiesFileProvider(PropertyEvaluator eval, File basedir) { 799 this.eval = eval; 800 this.basedir = basedir; 801 eval.addPropertyChangeListener(WeakListeners.propertyChange(this, eval)); 802 computeDelegate(); 803 } 804 private void computeDelegate() { 805 if (delegate != null) { 806 delegate.removeChangeListener(this); 807 } 808 String buildS = eval.getProperty("user.properties.file"); if (buildS != null) { 810 delegate = PropertyUtils.propertiesFilePropertyProvider(PropertyUtils.resolveFile(basedir, buildS)); 811 delegate.addChangeListener(this); 812 } else { 813 816 delegate = PropertyUtils.globalPropertyProvider(); 817 delegate.addChangeListener(this); 818 } 819 } 820 public Map <String ,String > getProperties() { 821 if (delegate != null) { 822 return delegate.getProperties(); 823 } else { 824 return Collections.emptyMap(); 825 } 826 } 827 public void addChangeListener(ChangeListener l) { 828 synchronized (listeners) { 829 listeners.add(l); 830 } 831 } 832 public void removeChangeListener(ChangeListener l) { 833 synchronized (listeners) { 834 listeners.remove(l); 835 } 836 } 837 public void propertyChange(PropertyChangeEvent evt) { 838 String p = evt.getPropertyName(); 839 if (p == null || p.equals("user.properties.file")) { computeDelegate(); 841 fireChange(); 842 } 843 } 844 public void stateChanged(ChangeEvent e) { 845 fireChange(); 846 } 847 private void fireChange() { 848 ChangeEvent ev = new ChangeEvent (this); 849 Iterator <ChangeListener > it; 850 synchronized (listeners) { 851 if (listeners.isEmpty()) { 852 return; 853 } 854 it = new HashSet <ChangeListener >(listeners).iterator(); 855 } 856 while (it.hasNext()) { 857 it.next().stateChanged(ev); 858 } 859 } 860 } 861 862 865 public static Comparator <Project> projectDisplayNameComparator() { 866 return new Comparator <Project>() { 867 private final Collator LOC_COLLATOR = Collator.getInstance(); 868 public int compare(Project o1, Project o2) { 869 ProjectInformation i1 = ProjectUtils.getInformation(o1); 870 ProjectInformation i2 = ProjectUtils.getInformation(o2); 871 int result = LOC_COLLATOR.compare(i1.getDisplayName(), i2.getDisplayName()); 872 if (result != 0) { 873 return result; 874 } else { 875 result = i1.getName().compareTo(i2.getName()); 876 if (result != 0) { 877 return result; 878 } else { 879 return System.identityHashCode(o1) - System.identityHashCode(o2); 880 } 881 } 882 } 883 }; 884 } 885 886 889 public static NbModuleProvider.NbModuleType getModuleType(final Project project) { 890 NbModuleProvider provider = project.getLookup().lookup(NbModuleProvider.class); 891 assert provider != null : "has NbModuleProvider in the lookup"; 892 return provider.getModuleType(); 893 } 894 895 902 public static SortedSet <String > scanProjectForPackageNames(final File prjDir) { 903 NbModuleProject project = null; 904 FileObject source = FileUtil.toFileObject(prjDir); 906 if (source != null) { try { 908 project = (NbModuleProject) ProjectManager.getDefault().findProject(source); 909 } catch (IOException e) { 910 Util.err.notify(ErrorManager.INFORMATIONAL, e); 911 } 912 } 913 914 if (project == null) { 915 return new TreeSet <String >(Collections.<String >emptySet()); 916 } 917 918 SortedSet <String > availablePublicPackages = new TreeSet <String >(); 919 Set <FileObject> pkgs = new HashSet <FileObject>(); 921 FileObject srcDirFO = project.getSourceDirectory(); 922 Util.scanForPackages(pkgs, srcDirFO, "java"); for (FileObject pkg : pkgs) { 924 if (srcDirFO.equals(pkg)) { continue; 926 } 927 String pkgS = PropertyUtils.relativizeFile(FileUtil.toFile(srcDirFO), FileUtil.toFile(pkg)); 928 availablePublicPackages.add(pkgS.replace('/', '.')); 929 } 930 931 String [] libsPaths = new ProjectXMLManager(project).getBinaryOrigins(); 932 for (int i = 0; i < libsPaths.length; i++) { 933 scanJarForPackageNames(availablePublicPackages, project.getHelper().resolveFile(libsPaths[i])); 934 } 935 936 Iterator it = availablePublicPackages.iterator(); 938 while (it.hasNext()) { 939 String pkg = (String ) it.next(); 940 if (!Util.isValidJavaFQN(pkg)) { 941 it.remove(); 942 } 943 } 944 return availablePublicPackages; 945 } 946 947 955 public static void scanJarForPackageNames(final Set <String > packages, final File jarFile) { 956 FileObject jarFileFO = FileUtil.toFileObject(jarFile); 957 if (jarFileFO == null) { 958 return; 960 } 961 FileObject root = FileUtil.getArchiveRoot(jarFileFO); 962 if (root == null) { 963 return; 965 } 966 Set <FileObject> pkgs = new HashSet <FileObject>(); 967 Util.scanForPackages(pkgs, root, "class"); for (FileObject pkg : pkgs) { 969 if (root.equals(pkg)) { continue; 971 } 972 String pkgS = pkg.getPath(); 973 packages.add(pkgS.replace('/', '.')); 974 } 975 } 976 977 984 private static void scanForPackages(final Set <FileObject> validPkgs, final FileObject dir, final String ext) { 985 if (dir == null) { 986 return; 987 } 988 for (Enumeration en1 = dir.getFolders(false); en1.hasMoreElements(); ) { 989 FileObject subDir = (FileObject) en1.nextElement(); 990 if (VisibilityQuery.getDefault().isVisible(subDir)) { 991 scanForPackages(validPkgs, subDir, ext); 992 } 993 } 994 for (Enumeration en2 = dir.getData(false); en2.hasMoreElements(); ) { 995 FileObject kid = (FileObject) en2.nextElement(); 996 if (kid.hasExt(ext) && Utilities.isJavaIdentifier(kid.getName())) { 997 validPkgs.add(dir); 999 break; 1000 } 1001 } 1002 } 1003 1004 1009 public static FileObject getResourceDirectory(Project prj) { 1010 Sources srcs = ProjectUtils.getSources(prj); 1011 SourceGroup[] grps = srcs.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_RESOURCES); 1012 if (grps != null && grps.length > 0) { 1013 return grps[0].getRootFolder(); 1014 } 1015 NbModuleProvider prov = prj.getLookup().lookup(NbModuleProvider.class); 1017 assert prov != null; 1018 return prov.getSourceDirectory(); 1019 } 1020 1021} 1022 | Popular Tags |