|
What this is
This file is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.modules.ant.freeform;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.event.ChangeListener;
import org.netbeans.api.java.queries.SourceForBinaryQuery;
import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
import org.netbeans.spi.project.AuxiliaryConfiguration;
import org.netbeans.spi.project.support.ant.AntProjectEvent;
import org.netbeans.spi.project.support.ant.AntProjectListener;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.w3c.dom.Element;
/**
* Report the location of source folders (compilation units)
* corresponding to declared build products.
* @author Jesse Glick
*/
final class SourceForBinaryQueryImpl implements SourceForBinaryQueryImplementation, AntProjectListener {
private final FreeformProject project;
/**
* Map from known binary roots to lists of source roots.
*/
private Map/**/ compilationUnits = Util.findSubElements(java);
Iterator it = compilationUnits.iterator();
while (it.hasNext()) {
Element compilationUnitEl = (Element)it.next();
assert compilationUnitEl.getLocalName().equals("compilation-unit") : compilationUnitEl;
List/**/ binaries = findBinaries(compilationUnitEl);
if (!binaries.isEmpty()) {
List/**/ packageRoots = Classpaths.findPackageRoots(project, compilationUnitEl);
FileObject[] sources = (FileObject[])packageRoots.toArray(new FileObject[packageRoots.size()]);
Iterator it2 = binaries.iterator();
while (it2.hasNext()) {
URL u = (URL)it2.next();
roots.put(u, sources);
}
}
}
}
assert roots != null;
FileObject[] sources = (FileObject[])roots.get(binaryRoot);
return sources == null ? null : new Result (sources); //TODO: Optimize it, resolution of sources should be done in the result
}
/**
* Find a list of URLs of binaries which will be produced from a compilation unit.
* Result may be empty.
*/
private List/**/ findBinaries(Element compilationUnitEl) {
List/**/ builtToEls = Util.findSubElements(compilationUnitEl);
List/**/ binaries = new ArrayList(builtToEls.size());
Iterator it = builtToEls.iterator();
while (it.hasNext()) {
Element builtToEl = (Element)it.next();
if (!builtToEl.getLocalName().equals("built-to")) { // NOI18N
continue;
}
String text = Util.findText(builtToEl);
String textEval = project.evaluator().evaluate(text);
if (textEval == null) {
continue;
}
File buildProduct = project.helper().resolveFile(textEval);
URL buildProductURL;
try {
buildProductURL = buildProduct.toURI().toURL();
} catch (MalformedURLException e) {
assert false : e;
continue;
}
if (FileUtil.isArchiveFile(buildProductURL)) {
buildProductURL = FileUtil.getArchiveRoot(buildProductURL);
} else {
// If it is not jar then it has to be folder. Make sure folder
// URL ends with slash character. If buildProduct file above
// does not exist then created URL will not end with slash!
if (!buildProduct.exists() && !buildProductURL.toExternalForm().endsWith("/")) {
try {
buildProductURL = new URL(buildProductURL.toExternalForm()+"/");
} catch (MalformedURLException e) {
assert false : e;
}
}
}
binaries.add(buildProductURL);
}
return binaries;
}
public void configurationXmlChanged(AntProjectEvent ev) {
refresh();
}
public void propertiesChanged(AntProjectEvent ev) {
// ignore
}
private static class Result implements SourceForBinaryQuery.Result {
private FileObject[] ret;
public Result (FileObject[] ret) {
this.ret = ret;
}
public FileObject[] getRoots () {
return ret;
}
public void addChangeListener (ChangeListener l) {
// XXX
}
public void removeChangeListener (ChangeListener l) {
// XXX
}
}
}
|