|
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.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import org.netbeans.spi.java.queries.SourceLevelQueryImplementation;
import org.netbeans.spi.project.AuxiliaryConfiguration;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.w3c.dom.Element;
/**
* Specifies the Java source level (for example 1.4) to use for freeform sources.
* @author Jesse Glick
*/
final class SourceLevelQueryImpl implements SourceLevelQueryImplementation {
private final FreeformProject project;
/**
* Map from package roots to source levels.
*/
private final Map/**/ sourceLevels = new WeakHashMap();
public SourceLevelQueryImpl(FreeformProject project) {
this.project = project;
}
public synchronized String getSourceLevel(FileObject file) {
// Check for cached value.
Iterator it = sourceLevels.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
FileObject root = (FileObject)entry.getKey();
if (root == file || FileUtil.isParentOf(root, file)) {
// Already have it.
return (String)entry.getValue();
}
}
// Need to compute it.
AuxiliaryConfiguration aux = (AuxiliaryConfiguration)project.getLookup().lookup(AuxiliaryConfiguration.class);
assert aux != null;
Element java = aux.getConfigurationFragment("java-data", FreeformProjectType.NS_JAVA, true); // NOI18N
if (java == null) {
return null;
}
List/**/ compilationUnits = Util.findSubElements(java);
it = compilationUnits.iterator();
while (it.hasNext()) {
Element compilationUnitEl = (Element)it.next();
assert compilationUnitEl.getLocalName().equals("compilation-unit") : compilationUnitEl;
List/**/ packageRoots = Classpaths.findPackageRoots(project, compilationUnitEl);
Iterator it2 = packageRoots.iterator();
while (it2.hasNext()) {
FileObject root = (FileObject)it2.next();
if (root == file || FileUtil.isParentOf(root, file)) {
// Got it. Retrieve source level and cache it (for each root).
String lvl = getLevel(compilationUnitEl);
it2 = packageRoots.iterator();
while (it2.hasNext()) {
FileObject root2 = (FileObject)it2.next();
sourceLevels.put(root2, lvl);
}
return lvl;
}
}
}
// Didn't find anything.
return null;
}
/**
* Get the source level indicated in a compilation unit (or null if none is indicated).
*/
private String getLevel(Element compilationUnitEl) {
Element sourceLevelEl = Util.findElement(compilationUnitEl, "source-level", FreeformProjectType.NS_JAVA);
if (sourceLevelEl != null) {
return Util.findText(sourceLevelEl);
} else {
return null;
}
}
}
|