KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > freeform > SourceForBinaryQueryImpl


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.java.freeform;
21
22 import java.io.File JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31 import org.netbeans.api.java.queries.SourceForBinaryQuery;
32 import org.netbeans.modules.ant.freeform.spi.support.Util;
33 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
34 import org.netbeans.spi.project.AuxiliaryConfiguration;
35 import org.netbeans.spi.project.support.ant.AntProjectEvent;
36 import org.netbeans.spi.project.support.ant.AntProjectHelper;
37 import org.netbeans.spi.project.support.ant.AntProjectListener;
38 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileUtil;
41 import org.w3c.dom.Element JavaDoc;
42
43 /**
44  * Report the location of source folders (compilation units)
45  * corresponding to declared build products.
46  * @author Jesse Glick
47  */

48 final class SourceForBinaryQueryImpl implements SourceForBinaryQueryImplementation, AntProjectListener {
49     
50     private AntProjectHelper helper;
51     private PropertyEvaluator evaluator;
52     private AuxiliaryConfiguration aux;
53     
54     /**
55      * Map from known binary roots to lists of source roots.
56      */

57     private Map JavaDoc<URL JavaDoc,FileObject[]> roots = null;
58     
59     public SourceForBinaryQueryImpl(AntProjectHelper helper, PropertyEvaluator evaluator, AuxiliaryConfiguration aux) {
60         this.helper = helper;
61         this.evaluator = evaluator;
62         this.aux = aux;
63         helper.addAntProjectListener(this);
64     }
65     
66     private void refresh () {
67         roots = null;
68     }
69     
70     public synchronized SourceForBinaryQuery.Result findSourceRoots(URL JavaDoc binaryRoot) {
71         if (roots == null) {
72             // Need to compute it. Easiest to compute them all at once.
73
roots = new HashMap JavaDoc<URL JavaDoc,FileObject[]>();
74             Element JavaDoc java = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true);
75             if (java == null) {
76                 return null;
77             }
78             for (Element JavaDoc compilationUnit : Util.findSubElements(java)) {
79                 assert compilationUnit.getLocalName().equals("compilation-unit") : compilationUnit;
80                 List JavaDoc<URL JavaDoc> binaries = findBinaries(compilationUnit);
81                 if (!binaries.isEmpty()) {
82                     List JavaDoc<FileObject> packageRoots = Classpaths.findPackageRoots(helper, evaluator, compilationUnit);
83                     FileObject[] sources = packageRoots.toArray(new FileObject[packageRoots.size()]);
84                     for (URL JavaDoc u : binaries) {
85                         FileObject[] orig = roots.get(u);
86                         //The case when sources are in the separate compilation units but
87
//the output is built into a single archive is not very common.
88
//It is better to recreate arrays rather then to add source roots
89
//into lists which will slow down creation of Result instances.
90
if (orig != null) {
91                             FileObject[] merged = new FileObject[orig.length+sources.length];
92                             System.arraycopy(orig, 0, merged, 0, orig.length);
93                             System.arraycopy(sources, 0, merged, orig.length, sources.length);
94                             sources = merged;
95                         }
96                         roots.put(u, sources);
97                     }
98                 }
99             }
100         }
101         assert roots != null;
102         FileObject[] sources = roots.get(binaryRoot);
103         return sources == null ? null : new Result (sources); //TODO: Optimize it, resolution of sources should be done in the result
104
}
105     
106     /**
107      * Find a list of URLs of binaries which will be produced from a compilation unit.
108      * Result may be empty.
109      */

110     private List JavaDoc<URL JavaDoc> findBinaries(Element JavaDoc compilationUnitEl) {
111         List JavaDoc<URL JavaDoc> binaries = new ArrayList JavaDoc<URL JavaDoc>();
112         for (Element JavaDoc builtToEl : Util.findSubElements(compilationUnitEl)) {
113             if (!builtToEl.getLocalName().equals("built-to")) { // NOI18N
114
continue;
115             }
116             String JavaDoc text = Util.findText(builtToEl);
117             String JavaDoc textEval = evaluator.evaluate(text);
118             if (textEval == null) {
119                 continue;
120             }
121             File JavaDoc buildProduct = helper.resolveFile(textEval);
122             URL JavaDoc buildProductURL;
123             try {
124                 buildProductURL = buildProduct.toURI().toURL();
125             } catch (MalformedURLException JavaDoc e) {
126                 assert false : e;
127                 continue;
128             }
129             if (FileUtil.isArchiveFile(buildProductURL)) {
130                 buildProductURL = FileUtil.getArchiveRoot(buildProductURL);
131             } else {
132                 // If it is not jar then it has to be folder. Make sure folder
133
// URL ends with slash character. If buildProduct file above
134
// does not exist then created URL will not end with slash!
135
if (!buildProduct.exists() && !buildProductURL.toExternalForm().endsWith("/")) {
136                     try {
137                         buildProductURL = new URL JavaDoc(buildProductURL.toExternalForm()+"/");
138                     } catch (MalformedURLException JavaDoc e) {
139                         assert false : e;
140                     }
141                 }
142             }
143             binaries.add(buildProductURL);
144         }
145         return binaries;
146     }
147
148     public void configurationXmlChanged(AntProjectEvent ev) {
149         refresh();
150     }
151
152     public void propertiesChanged(AntProjectEvent ev) {
153         // ignore
154
}
155     
156     private static class Result implements SourceForBinaryQuery.Result {
157         
158         private FileObject[] ret;
159         
160         public Result (FileObject[] ret) {
161             this.ret = ret;
162         }
163         
164         public FileObject[] getRoots () {
165             return ret;
166         }
167         
168         public void addChangeListener (ChangeListener JavaDoc l) {
169             // XXX
170
}
171         
172         public void removeChangeListener (ChangeListener JavaDoc l) {
173             // XXX
174
}
175         
176     }
177     
178 }
179
Popular Tags