KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > queries > SubprojectProviderImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.project.queries;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import org.netbeans.api.project.FileOwnerQuery;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.api.project.ProjectManager;
33 import org.netbeans.api.project.ant.AntArtifact;
34 import org.netbeans.api.project.ant.AntArtifactQuery;
35 import org.netbeans.modules.apisupport.project.NbModuleProject;
36 import org.netbeans.modules.apisupport.project.NbModuleProjectType;
37 import org.netbeans.modules.apisupport.project.Util;
38 import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
39 import org.netbeans.modules.apisupport.project.universe.ModuleList;
40 import org.netbeans.spi.project.SubprojectProvider;
41 import org.netbeans.spi.project.support.ant.PropertyUtils;
42 import org.openide.ErrorManager;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileUtil;
45 import org.w3c.dom.Element JavaDoc;
46
47 /**
48  * Enumerates subprojects, defined as other modules on which this one
49  * has build-time dependencies.
50  * @author Jesse Glick
51  */

52 public final class SubprojectProviderImpl implements SubprojectProvider {
53     
54     private final NbModuleProject project;
55     
56     public SubprojectProviderImpl(NbModuleProject project) {
57         this.project = project;
58     }
59     
60     public Set JavaDoc getSubprojects() {
61         // XXX could use a special set w/ lazy isEmpty() - cf. #58639 for freeform
62
Set JavaDoc<Project> s = new HashSet JavaDoc();
63         ModuleList ml;
64         try {
65             ml = project.getModuleList();
66         } catch (IOException JavaDoc e) {
67             Util.err.notify(ErrorManager.INFORMATIONAL, e);
68             return Collections.EMPTY_SET;
69         }
70         Element JavaDoc data = project.getPrimaryConfigurationData();
71         Element JavaDoc moduleDependencies = Util.findElement(data,
72             "module-dependencies", NbModuleProjectType.NAMESPACE_SHARED); // NOI18N
73
List JavaDoc<Element JavaDoc> deps = Util.findSubElements(moduleDependencies);
74         Iterator JavaDoc it = deps.iterator();
75         while (it.hasNext()) {
76             Element JavaDoc dep = (Element JavaDoc)it.next();
77             /* Probably better to open runtime deps too. TBD.
78             if (Util.findElement(dep, "build-prerequisite", // NOI18N
79                     NbModuleProjectType.NAMESPACE_SHARED) == null) {
80                 continue;
81             }
82              */

83             Element JavaDoc cnbEl = Util.findElement(dep, "code-name-base", // NOI18N
84
NbModuleProjectType.NAMESPACE_SHARED);
85             String JavaDoc cnb = Util.findText(cnbEl);
86             ModuleEntry module = ml.getEntry(cnb);
87             if (module == null) {
88                 Util.err.log(ErrorManager.WARNING, "Warning - could not find dependent module " + cnb + " for " + project);
89                 continue;
90             }
91             File JavaDoc moduleProjectDirF = module.getSourceLocation();
92             if (moduleProjectDirF == null) {
93                 Util.err.log(ErrorManager.WARNING, "Warning - could not find sources for dependent module " + cnb + " for " + project);
94                 continue;
95             }
96             FileObject moduleProjectDir = FileUtil.toFileObject(moduleProjectDirF);
97             if (moduleProjectDir == null) {
98                 Util.err.log(ErrorManager.WARNING, "Warning - could not load sources for dependent module " + cnb + " for " + project);
99                 continue;
100             }
101             try {
102                 Project moduleProject = ProjectManager.getDefault().findProject(moduleProjectDir);
103                 if (moduleProject == null) {
104                     Util.err.log(ErrorManager.WARNING, "Warning - dependent module " + cnb + " for " + project + " is not projectized");
105                     continue;
106                 }
107                 s.add(moduleProject);
108             } catch (IOException JavaDoc e) {
109                 Util.err.notify(e);
110             }
111         }
112         // #63824: consider also artifacts found in ${cp.extra} and/or <class-path-extension>s
113
List JavaDoc<Element JavaDoc> cpexts = Util.findSubElements(data);
114         it = cpexts.iterator();
115         while (it.hasNext()) {
116             Element JavaDoc cpext = (Element JavaDoc) it.next();
117             if (!cpext.getTagName().equals("class-path-extension")) { // NOI18N
118
continue;
119             }
120             Element JavaDoc binorig = Util.findElement(cpext, "binary-origin", NbModuleProjectType.NAMESPACE_SHARED); // NOI18N
121
if (binorig == null) {
122                 continue;
123             }
124             String JavaDoc text = Util.findText(binorig);
125             String JavaDoc eval = project.evaluator().evaluate(text);
126             if (eval == null) {
127                 continue;
128             }
129             File JavaDoc jar = project.getHelper().resolveFile(eval);
130             AntArtifact aa = AntArtifactQuery.findArtifactFromFile(jar);
131             if (aa != null) {
132                 Project owner = aa.getProject();
133                 if (owner != null) {
134                     s.add(owner);
135                 }
136             }
137         }
138         String JavaDoc eval = project.evaluator().getProperty("cp.extra"); // NOI18N
139
if (eval != null) {
140             String JavaDoc[] pieces = PropertyUtils.tokenizePath(eval);
141             for (int i = 0; i < pieces.length; i++) {
142                 File JavaDoc jar = project.getHelper().resolveFile(pieces[i]);
143                 Project owner = FileOwnerQuery.getOwner(jar.toURI());
144                 if (owner != null) {
145                     s.add(owner);
146                 }
147             }
148         }
149         return s;
150     }
151     
152     public void addChangeListener(ChangeListener JavaDoc listener) {
153         // XXX no impl yet
154
}
155     
156     public void removeChangeListener(ChangeListener JavaDoc listener) {
157         // XXX
158
}
159     
160 }
161
Popular Tags