KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URI JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import org.netbeans.api.java.queries.SourceForBinaryQuery;
31 import org.netbeans.modules.apisupport.project.NbModuleProject;
32 import org.netbeans.modules.apisupport.project.Util;
33 import org.netbeans.modules.apisupport.project.universe.TestEntry;
34 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
35 import org.netbeans.spi.project.support.ant.PropertyUtils;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.w3c.dom.Element JavaDoc;
39
40 /**
41  * Provides sources for module build products.
42  * XXX can we also translate xtest/lib/nbjunit.jar -> xtest/nbjunit/src/?
43  */

44 public final class SourceForBinaryImpl implements SourceForBinaryQueryImplementation {
45     
46     private final NbModuleProject project;
47     private String JavaDoc clusterPath;
48     private URL JavaDoc classesUrl;
49     private URL JavaDoc testClassesUrl;
50     private Map JavaDoc<URL JavaDoc, SourceForBinaryQuery.Result> cache = new HashMap JavaDoc ();
51     
52     public SourceForBinaryImpl(NbModuleProject project) {
53         this.project = project;
54     }
55     
56     public SourceForBinaryQuery.Result findSourceRoots(URL JavaDoc binaryRoot) {
57         //System.err.println("findSourceRoot: " + binaryRoot);
58
SourceForBinaryQuery.Result res = (SourceForBinaryQuery.Result) cache.get(binaryRoot);
59         if (res == null) {
60             URL JavaDoc binaryJar = FileUtil.getArchiveFile(binaryRoot);
61             if (binaryJar != null) {
62                 File JavaDoc binaryJarF = new File JavaDoc(URI.create(binaryJar.toExternalForm()));
63                 FileObject srcDir = null;
64                 if (binaryJarF.getAbsolutePath().endsWith(getModuleJarClusterPath())) {
65                     srcDir = project.getSourceDirectory();
66                 } else {
67                     // maybe tests.jar in testdistribution
68
TestEntry entry = TestEntry.get(binaryJarF);
69                     if (entry != null && project.getCodeNameBase().equals(entry.getCodeNameBase())) {
70                         srcDir = ( entry.isUnit() ) ? project.getTestSourceDirectory() :
71                                project.getFunctionalTestSourceDirectory();
72                     }
73                 }
74                 if (srcDir != null) {
75                     res = new Result(new FileObject[] {srcDir});
76                     return res;
77                 }
78             }
79             if (binaryRoot.equals(getClassesUrl())) {
80                 FileObject srcDir = project.getSourceDirectory();
81                 if (srcDir != null) {
82                     res = new Result(new FileObject[] {srcDir});
83                 }
84             } else if (binaryRoot.equals(getTestClassesUrl())) {
85                 FileObject testSrcDir = project.getTestSourceDirectory();
86                 if (testSrcDir != null) {
87                     res = new Result (new FileObject[] {testSrcDir});
88                 }
89             } else {
90                 // Check extra compilation units.
91
Iterator JavaDoc<Map.Entry JavaDoc<FileObject,Element JavaDoc>> ecus = project.getExtraCompilationUnits().entrySet().iterator();
92                 ECUS: while (ecus.hasNext()) {
93                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) ecus.next();
94                     Element JavaDoc pkgrootEl = (Element JavaDoc) entry.getValue();
95                     Iterator JavaDoc<Element JavaDoc> pkgrootKids = Util.findSubElements(pkgrootEl).iterator();
96                     while (pkgrootKids.hasNext()) {
97                         Element JavaDoc kid = (Element JavaDoc) pkgrootKids.next();
98                         if (!kid.getLocalName().equals("built-to")) { // NOI18N
99
continue;
100                         }
101                         String JavaDoc rawtext = Util.findText(kid);
102                         assert rawtext != null : "Null content for <built-to> in " + project;
103                         String JavaDoc text = project.evaluator().evaluate(rawtext);
104                         if (text == null) {
105                             continue;
106                         }
107                         File JavaDoc loc = project.getHelper().resolveFile(text);
108                         URL JavaDoc u = Util.urlForDirOrJar(loc);
109                         if (u.equals(binaryRoot)) {
110                             res = new Result(new FileObject[] {(FileObject) entry.getKey()});
111                             break ECUS;
112                         }
113                     }
114                 }
115             }
116             if (res != null) {
117                 this.cache.put(binaryRoot,res);
118             }
119         }
120         return res;
121     }
122     
123     private String JavaDoc getModuleJarClusterPath() {
124         if (clusterPath == null) { // XXX should listen to changes on cluster property?
125
File JavaDoc cluster = project.getHelper().resolveFile(project.evaluator().evaluate("${cluster}")); // NOI18N
126
clusterPath = PropertyUtils.relativizeFile(cluster.getParentFile(),
127                    project.getModuleJarLocation()).replace('/', File.separatorChar);
128         }
129         return clusterPath;
130     }
131     
132     private URL JavaDoc getClassesUrl() {
133         if (classesUrl == null) {
134             File JavaDoc classesDir = project.getClassesDirectory();
135             classesUrl = Util.urlForDir(classesDir);
136         }
137         return classesUrl;
138     }
139     
140     private URL JavaDoc getTestClassesUrl() {
141         if (testClassesUrl == null && project.supportsUnitTests()) {
142             File JavaDoc testClassesDir = project.getTestClassesDirectory();
143             testClassesUrl = Util.urlForDir(testClassesDir);
144         }
145         return testClassesUrl;
146     }
147     
148     
149     private static class Result implements SourceForBinaryQuery.Result {
150                
151         private FileObject[] res;
152         
153         public Result (FileObject[] res) {
154             this.res = res;
155             assert res != null && !Arrays.asList(res).contains(null);
156         }
157         
158         public FileObject[] getRoots () {
159             return this.res;
160         }
161         
162         public void addChangeListener (ChangeListener JavaDoc l) {
163             //Not needed, do not suppose the source root to be changed in nbproject
164
}
165         
166         public void removeChangeListener (ChangeListener JavaDoc l) {
167             //Not needed, do not suppose the source root to be changed in nbproject
168
}
169         
170     }
171     
172 }
173
Popular Tags