1 19 20 package org.netbeans.modules.java.freeform; 21 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.WeakHashMap ; 25 import org.netbeans.api.project.ProjectManager; 26 import org.netbeans.modules.ant.freeform.spi.support.Util; 27 import org.netbeans.spi.java.queries.SourceLevelQueryImplementation; 28 import org.netbeans.spi.project.AuxiliaryConfiguration; 29 import org.netbeans.spi.project.support.ant.AntProjectHelper; 30 import org.netbeans.spi.project.support.ant.AntProjectListener; 31 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.util.Mutex; 35 import org.w3c.dom.Element ; 36 37 41 final class SourceLevelQueryImpl implements SourceLevelQueryImplementation, AntProjectListener { 42 43 private AntProjectHelper helper; 44 private PropertyEvaluator evaluator; 45 private AuxiliaryConfiguration aux; 46 47 50 private final Map <FileObject,String > sourceLevels = new WeakHashMap <FileObject,String >(); 51 52 public SourceLevelQueryImpl(AntProjectHelper helper, PropertyEvaluator evaluator, AuxiliaryConfiguration aux) { 53 this.helper = helper; 54 this.evaluator = evaluator; 55 this.aux = aux; 56 this.helper.addAntProjectListener(this); 57 } 58 59 public String getSourceLevel(final FileObject file) { 60 return ProjectManager.mutex().readAccess(new Mutex.Action<String >() { 63 public String run() { 64 return getSourceLevelImpl(file); 65 } 66 }); 67 } 68 69 private synchronized String getSourceLevelImpl(FileObject file) { 70 for (Map.Entry <FileObject,String > entry : sourceLevels.entrySet()) { 72 FileObject root = entry.getKey(); 73 if (root == file || FileUtil.isParentOf(root, file)) { 74 return entry.getValue(); 76 } 77 } 78 Element java = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true); 80 if (java == null) { 81 return null; 82 } 83 for (Element compilationUnitEl : Util.findSubElements(java)) { 84 assert compilationUnitEl.getLocalName().equals("compilation-unit") : compilationUnitEl; 85 List <FileObject> packageRoots = Classpaths.findPackageRoots(helper, evaluator, compilationUnitEl); 86 for (FileObject root : packageRoots) { 87 if (root == file || FileUtil.isParentOf(root, file)) { 88 String lvl = getLevel(compilationUnitEl); 90 for (FileObject root2 : packageRoots) { 91 sourceLevels.put(root2, lvl); 92 } 93 return lvl; 94 } 95 } 96 } 97 return null; 99 } 100 101 public void propertiesChanged(org.netbeans.spi.project.support.ant.AntProjectEvent ev) { 102 } 103 104 public void configurationXmlChanged(org.netbeans.spi.project.support.ant.AntProjectEvent ev) { 105 synchronized (this) { 106 this.sourceLevels.clear(); 107 } 108 } 109 110 113 private String getLevel(Element compilationUnitEl) { 114 Element sourceLevelEl = Util.findElement(compilationUnitEl, "source-level", JavaProjectNature.NS_JAVA_2); 115 if (sourceLevelEl != null) { 116 return Util.findText(sourceLevelEl); 117 } else { 118 return null; 119 } 120 } 121 122 } 123 | Popular Tags |