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-2006 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.queries;
043:
044: import java.io.File;
045: import java.net.URI;
046: import java.net.URL;
047: import java.util.Map;
048: import java.util.HashMap;
049: import javax.swing.event.ChangeListener;
050: import org.netbeans.api.java.queries.SourceForBinaryQuery;
051: import org.netbeans.modules.apisupport.project.NbModuleProject;
052: import org.netbeans.modules.apisupport.project.Util;
053: import org.netbeans.modules.apisupport.project.universe.TestEntry;
054: import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057: import org.w3c.dom.Element;
058:
059: /**
060: * Provides sources for module build products.
061: * XXX can we also translate xtest/lib/nbjunit.jar -> xtest/nbjunit/src/?
062: */
063: public final class SourceForBinaryImpl implements
064: SourceForBinaryQueryImplementation {
065:
066: private final NbModuleProject project;
067: private URL classesUrl;
068: private URL testClassesUrl;
069: private Map<URL, SourceForBinaryQuery.Result> cache = new HashMap<URL, SourceForBinaryQuery.Result>();
070:
071: public SourceForBinaryImpl(NbModuleProject project) {
072: this .project = project;
073: }
074:
075: public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) {
076: //System.err.println("findSourceRoot: " + binaryRoot);
077: SourceForBinaryQuery.Result res = cache.get(binaryRoot);
078: if (res == null) {
079: URL binaryJar = FileUtil.getArchiveFile(binaryRoot);
080: if (binaryJar != null) {
081: File binaryJarF = new File(URI.create(binaryJar
082: .toExternalForm()));
083: FileObject srcDir = null;
084: if (binaryJarF.getAbsolutePath().endsWith(
085: project.evaluator().getProperty("module.jar")
086: .replace('/', File.separatorChar))) {
087: srcDir = project.getSourceDirectory();
088: } else {
089: // maybe tests.jar in testdistribution
090: TestEntry entry = TestEntry.get(binaryJarF);
091: if (entry != null
092: && project.getCodeNameBase().equals(
093: entry.getCodeNameBase())) {
094: srcDir = (entry.isUnit()) ? project
095: .getTestSourceDirectory() : project
096: .getFunctionalTestSourceDirectory();
097: }
098: }
099: if (srcDir != null) {
100: res = new Result(srcDir);
101: return res;
102: }
103: }
104: if (binaryRoot.equals(getClassesUrl())) {
105: FileObject srcDir = project.getSourceDirectory();
106: if (srcDir != null) {
107: res = new Result(srcDir);
108: }
109: } else if (binaryRoot.equals(getTestClassesUrl())) {
110: FileObject testSrcDir = project
111: .getTestSourceDirectory();
112: if (testSrcDir != null) {
113: res = new Result(testSrcDir);
114: }
115: } else {
116: // Check extra compilation units.
117: ECUS: for (Map.Entry<FileObject, Element> entry : project
118: .getExtraCompilationUnits().entrySet()) {
119: for (Element kid : Util.findSubElements(entry
120: .getValue())) {
121: if (!kid.getLocalName().equals("built-to")) { // NOI18N
122: continue;
123: }
124: String rawtext = Util.findText(kid);
125: assert rawtext != null : "Null content for <built-to> in "
126: + project;
127: String text = project.evaluator().evaluate(
128: rawtext);
129: if (text == null) {
130: continue;
131: }
132: File loc = project.getHelper()
133: .resolveFile(text);
134: URL u = Util.urlForDirOrJar(loc);
135: if (u.equals(binaryRoot)) {
136: res = new Result(entry.getKey());
137: break ECUS;
138: }
139: }
140: }
141: }
142: if (res != null) {
143: cache.put(binaryRoot, res);
144: }
145: }
146: return res;
147: }
148:
149: private URL getClassesUrl() {
150: if (classesUrl == null) {
151: File classesDir = project.getClassesDirectory();
152: classesUrl = Util.urlForDir(classesDir);
153: }
154: return classesUrl;
155: }
156:
157: private URL getTestClassesUrl() {
158: if (testClassesUrl == null && project.supportsUnitTests()) {
159: File testClassesDir = project.getTestClassesDirectory();
160: testClassesUrl = Util.urlForDir(testClassesDir);
161: }
162: return testClassesUrl;
163: }
164:
165: private static class Result implements SourceForBinaryQuery.Result {
166:
167: private FileObject res;
168:
169: public Result(FileObject res) {
170: assert res != null;
171: this .res = res;
172: }
173:
174: public FileObject[] getRoots() {
175: return new FileObject[] { res };
176: }
177:
178: public void addChangeListener(ChangeListener l) {
179: //Not needed, do not suppose the source root to be changed in nbproject
180: }
181:
182: public void removeChangeListener(ChangeListener l) {
183: //Not needed, do not suppose the source root to be changed in nbproject
184: }
185:
186: }
187:
188: }
|