1 19 20 package org.netbeans.modules.java.freeform; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import org.netbeans.api.project.Project; 26 import org.netbeans.api.queries.FileBuiltQuery.Status; 27 import org.netbeans.modules.ant.freeform.spi.support.Util; 28 import org.netbeans.spi.project.AuxiliaryConfiguration; 29 import org.netbeans.spi.project.support.ant.AntProjectEvent; 30 import org.netbeans.spi.project.support.ant.AntProjectHelper; 31 import org.netbeans.spi.project.support.ant.AntProjectListener; 32 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 33 import org.netbeans.spi.queries.FileBuiltQueryImplementation; 34 import org.openide.ErrorManager; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileUtil; 37 import org.w3c.dom.Element ; 38 39 46 final class JavaFreeformFileBuiltQuery implements FileBuiltQueryImplementation, AntProjectListener { 47 48 private static final ErrorManager ERR = ErrorManager.getDefault().getInstance(JavaFreeformFileBuiltQuery.class.getName()); 49 50 private Project project; 51 private AntProjectHelper projectHelper; 52 private PropertyEvaluator projectEvaluator; 53 private AuxiliaryConfiguration aux; 54 55 public JavaFreeformFileBuiltQuery(Project project, AntProjectHelper projectHelper, PropertyEvaluator projectEvaluator, AuxiliaryConfiguration aux) { 56 this.project = project; 57 this.projectHelper = projectHelper; 58 this.projectEvaluator = projectEvaluator; 59 this.aux = aux; 60 61 this.delegateTo = null; 62 63 projectHelper.addAntProjectListener(this); 64 } 65 66 private FileBuiltQueryImplementation delegateTo; 67 68 private FileBuiltQueryImplementation createDelegateTo() { 69 if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) { 70 ERR.log(ErrorManager.INFORMATIONAL, "JavaFreeformFileBuiltQuery.createDelegateTo start"); } 72 73 Element java = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true); 74 List <String > from = new ArrayList <String >(); 75 List <String > to = new ArrayList <String >(); 76 77 if (java != null) { 78 List <Element > compilationUnits = Util.findSubElements(java); 79 Iterator it = compilationUnits.iterator(); 80 while (it.hasNext()) { 81 Element compilationUnitEl = (Element )it.next(); 82 assert compilationUnitEl.getLocalName().equals("compilation-unit") : compilationUnitEl; 83 List <String > rootNames = Classpaths.findPackageRootNames(compilationUnitEl); 84 List <String > builtToNames = findBuiltToNames(compilationUnitEl); 85 86 List <String > rootPatterns = new ArrayList <String >(); 87 String builtToPattern = null; 88 89 for (String n : rootNames) { 90 rootPatterns.add(projectEvaluator.evaluate(n) + "/*.java"); } 92 93 if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) { 94 ERR.log(ErrorManager.INFORMATIONAL, "Looking for a suitable built-to:"); } 96 97 for (String n : builtToNames) { 98 String builtTo = projectEvaluator.evaluate(n); 99 if (builtTo == null) { 100 continue; 101 } 102 boolean isFolder = JavaProjectGenerator.isFolder(projectEvaluator, FileUtil.toFile(project.getProjectDirectory()), builtTo); 103 104 if (isFolder && builtToPattern == null) { 105 builtToPattern = builtTo + "/*.class"; break; 107 } 108 } 109 110 if (builtToPattern != null) { 111 if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) { 112 ERR.log(ErrorManager.INFORMATIONAL, "Found built to pattern=" + builtToPattern + ", rootPatterns=" + rootPatterns); } 114 for (String p : rootPatterns) { 115 from.add(p); 116 to.add(builtToPattern); 117 } 118 } else { 119 if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) { 120 ERR.log(ErrorManager.INFORMATIONAL, "No built to pattern found, rootPatterns=" + rootPatterns); } 122 } 123 } 124 } 125 126 if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) { 127 ERR.log(ErrorManager.INFORMATIONAL, "JavaFreeformFileBuiltQuery from=" + from + " to=" + to); } 129 130 String [] fromStrings = from.toArray(new String [from.size()]); 131 String [] toStrings = to.toArray(new String [to.size()]); 132 133 FileBuiltQueryImplementation fbqi = projectHelper.createGlobFileBuiltQuery(projectEvaluator, fromStrings, toStrings); 134 135 if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) { 136 ERR.log(ErrorManager.INFORMATIONAL, "JavaFreeformFileBuiltQuery.createDelegateTo end"); } 138 139 return fbqi; 140 } 141 142 public void propertiesChanged(AntProjectEvent evt) { 143 } 145 146 public void configurationXmlChanged(AntProjectEvent evt) { 147 synchronized (this) { 148 delegateTo = null; 149 } 150 } 151 152 public synchronized Status getStatus(FileObject fo) { 153 if (delegateTo == null) 154 delegateTo = createDelegateTo(); 155 156 return delegateTo.getStatus(fo); 157 } 158 159 static List <String > findBuiltToNames(Element compilationUnitEl) { 160 List <String > names = new ArrayList <String >(); 161 for (Element e : Util.findSubElements(compilationUnitEl)) { 162 if (!e.getLocalName().equals("built-to")) { continue; 164 } 165 String location = Util.findText(e); 166 names.add(location); 167 } 168 return names; 169 } 170 171 } 172 | Popular Tags |