2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.java.freeform;
45
import java.net.MalformedURLException;
47
import java.util.ArrayList;
48
import java.util.HashMap;
49
import java.util.Iterator;
50
import java.util.List;
52
import javax.swing.event.ChangeListener;
53
import org.netbeans.api.java.queries.SourceForBinaryQuery;
54
import org.netbeans.modules.ant.freeform.spi.support.Util;
55
import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
56
import org.netbeans.spi.project.AuxiliaryConfiguration;
57
import org.netbeans.spi.project.support.ant.AntProjectEvent;
58
import org.netbeans.spi.project.support.ant.AntProjectHelper;
59
import org.netbeans.spi.project.support.ant.AntProjectListener;
60
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
61
import org.openide.filesystems.FileObject;
62
import org.openide.filesystems.FileUtil;
63
import org.w3c.dom.Element;
66
* Report the location of source folders (compilation units)
67
* corresponding to declared build products.
70
final class SourceForBinaryQueryImpl implements SourceForBinaryQueryImplementation, AntProjectListener {
72
private AntProjectHelper helper;
73
private PropertyEvaluator evaluator;
74
private AuxiliaryConfiguration aux;
77
* Map from known binary roots to lists of source roots.
79
private Map<URL,FileObject[]> roots = null;
81
public SourceForBinaryQueryImpl(AntProjectHelper helper, PropertyEvaluator evaluator, AuxiliaryConfiguration aux) {
83
this.evaluator = evaluator;
85
helper.addAntProjectListener(this);
88
private void refresh () {
92
public synchronized SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) {
94
// Need to compute it. Easiest to compute them all at once.
95
roots = new HashMap<URL,FileObject[]>();
96
Element java = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true);
100
for (Element compilationUnit : Util.findSubElements(java)) {
101
assert compilationUnit.getLocalName().equals("compilation-unit") : compilationUnit;
102
List<URL> binaries = findBinaries(compilationUnit);
103
if (!binaries.isEmpty()) {
104
List<FileObject> packageRoots = Classpaths.findPackageRoots(helper, evaluator, compilationUnit);
105
FileObject[] sources = packageRoots.toArray(new FileObject[packageRoots.size()]);
106
for (URL u : binaries) {
107
FileObject[] orig = roots.get(u);
108
//The case when sources are in the separate compilation units but
109
//the output is built into a single archive is not very common.
110
//It is better to recreate arrays rather then to add source roots
111
//into lists which will slow down creation of Result instances.
113
FileObject[] merged = new FileObject[orig.length+sources.length];
114
System.arraycopy(orig, 0, merged, 0, orig.length);
115
System.arraycopy(sources, 0, merged, orig.length, sources.length);
118
roots.put(u, sources);
123
assert roots != null;
124
FileObject[] sources = roots.get(binaryRoot);
125
return sources == null ? null : new Result (sources); //TODO: Optimize it, resolution of sources should be done in the result
129
* Find a list of URLs of binaries which will be produced from a compilation unit.
130
* Result may be empty.
132
private List<URL> findBinaries(Element compilationUnitEl) {
133
List<URL> binaries = new ArrayList<URL>();
134
for (Element builtToEl : Util.findSubElements(compilationUnitEl)) {
135
if (!builtToEl.getLocalName().equals("built-to")) { // NOI18N
138
String text = Util.findText(builtToEl);
139
String textEval = evaluator.evaluate(text);
140
if (textEval == null) {
143
File buildProduct = helper.resolveFile(textEval);
146
buildProductURL = buildProduct.toURI().toURL();
147
} catch (MalformedURLException e) {
151
if (FileUtil.isArchiveFile(buildProductURL)) {
152
buildProductURL = FileUtil.getArchiveRoot(buildProductURL);
154
// If it is not jar then it has to be folder. Make sure folder
155
// URL ends with slash character. If buildProduct file above
156
// does not exist then created URL will not end with slash!
157
if (!buildProduct.exists() && !buildProductURL.toExternalForm().endsWith("/")) {
159
buildProductURL = new URL(buildProductURL.toExternalForm()+"/");
160
} catch (MalformedURLException e) {
165
binaries.add(buildProductURL);
170
public void configurationXmlChanged(AntProjectEvent ev) {
174
public void propertiesChanged(AntProjectEvent ev) {
178
private static class Result implements SourceForBinaryQuery.Result {
180
private FileObject[] ret;
182
public Result (FileObject[] ret) {
186
public FileObject[] getRoots () {
190
public void addChangeListener (ChangeListener l) {
194
public void removeChangeListener (ChangeListener l) {