001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.project.universe;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.HashSet;
047: import java.util.Set;
048: import java.util.TreeSet;
049: import org.netbeans.api.project.ProjectManager;
050: import org.netbeans.modules.apisupport.project.ManifestManager;
051: import org.netbeans.modules.apisupport.project.NbModuleProject;
052: import org.netbeans.modules.apisupport.project.NbModuleProjectType;
053: import org.netbeans.modules.apisupport.project.Util;
054: import org.openide.ErrorManager;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057: import org.w3c.dom.Element;
058:
059: abstract class AbstractEntryWithSources extends AbstractEntry {
060:
061: private LocalizedBundleInfo bundleInfo;
062: private Set<String> allPackageNames;
063: private final String src;
064:
065: protected AbstractEntryWithSources(final String src) {
066: this .src = src;
067: }
068:
069: protected LocalizedBundleInfo getBundleInfo() {
070: if (bundleInfo == null) {
071: bundleInfo = ModuleList.loadBundleInfo(getSourceLocation());
072: }
073: return bundleInfo;
074: }
075:
076: protected Set<String> computePublicClassNamesInMainModule()
077: throws IOException {
078: Set<String> result = new HashSet<String>();
079: File srcF = new File(getSourceLocation(), src);
080: for (ManifestManager.PackageExport p : getPublicPackages()) {
081: String pkg = p.getPackage();
082: scanForClasses(result, pkg, new File(srcF, pkg.replace('.',
083: File.separatorChar)), p.isRecursive());
084: }
085: return result;
086: }
087:
088: public synchronized Set<String> getAllPackageNames() {
089: if (allPackageNames == null) {
090: allPackageNames = Util
091: .scanProjectForPackageNames(getSourceLocation());
092: }
093: return allPackageNames;
094: }
095:
096: private void scanForClasses(Set<String> result, String pkg,
097: File dir, boolean recurse) throws IOException {
098: if (!dir.isDirectory()) {
099: return;
100: }
101: File[] kids = dir.listFiles();
102: if (kids == null) {
103: throw new IOException(dir.getAbsolutePath());
104: }
105: for (File kid : kids) {
106: String name = kid.getName();
107: if (name.endsWith(".java")) { // NOI18N
108: String basename = name.substring(0, name.length() - 5);
109: result.add(pkg + '.' + basename);
110: // no inner classes scanned, too slow
111: }
112: if (recurse && kid.isDirectory()) {
113: scanForClasses(result, pkg + '.' + name, kid, true);
114: }
115: }
116: }
117:
118: public String[] getRunDependencies() {
119: Set<String> deps = new TreeSet<String>();
120: FileObject source = FileUtil.toFileObject(getSourceLocation());
121: if (source == null) { // ??
122: return new String[0];
123: }
124: NbModuleProject project;
125: try {
126: project = (NbModuleProject) ProjectManager.getDefault()
127: .findProject(source);
128: } catch (IOException e) {
129: Util.err.notify(ErrorManager.INFORMATIONAL, e);
130: return new String[0];
131: }
132: if (project == null) { // #106351
133: return new String[0];
134: }
135: Element data = project.getPrimaryConfigurationData();
136: Element moduleDependencies = Util.findElement(data,
137: "module-dependencies",
138: NbModuleProjectType.NAMESPACE_SHARED); // NOI18N
139: assert moduleDependencies != null : "Malformed metadata in "
140: + project;
141: for (Element dep : Util.findSubElements(moduleDependencies)) {
142: if (Util.findElement(dep, "run-dependency", // NOI18N
143: NbModuleProjectType.NAMESPACE_SHARED) == null) {
144: continue;
145: }
146: Element cnbEl = Util.findElement(dep, "code-name-base", // NOI18N
147: NbModuleProjectType.NAMESPACE_SHARED);
148: String cnb = Util.findText(cnbEl);
149: deps.add(cnb);
150: }
151: return deps.toArray(new String[deps.size()]);
152: }
153:
154: public String getSpecificationVersion() {
155: FileObject source = FileUtil.toFileObject(getSourceLocation());
156: if (source != null) {
157: NbModuleProject project;
158: try {
159: project = (NbModuleProject) ProjectManager.getDefault()
160: .findProject(source);
161: if (project != null) {
162: return project.getSpecVersion();
163: }
164: } catch (IOException e) {
165: Util.err.notify(ErrorManager.INFORMATIONAL, e);
166: }
167: }
168: return null;
169: }
170:
171: }
|