1 19 20 package org.netbeans.modules.web.freeform; 21 22 import java.io.File ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Set ; 31 import java.util.Collections ; 32 import org.netbeans.spi.java.classpath.ClassPathProvider; 33 import org.openide.filesystems.FileStateInvalidException; 34 import org.w3c.dom.Element ; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.filesystems.FileObject; 37 import org.netbeans.spi.project.AuxiliaryConfiguration; 38 import org.netbeans.api.java.classpath.ClassPath; 39 import org.netbeans.api.java.project.JavaProjectConstants; 40 import org.netbeans.api.java.queries.SourceForBinaryQuery; 41 import org.netbeans.api.project.FileOwnerQuery; 42 import org.netbeans.api.project.Project; 43 import org.netbeans.api.project.ProjectUtils; 44 import org.netbeans.api.project.SourceGroup; 45 import org.netbeans.modules.ant.freeform.spi.support.Util; 46 import org.netbeans.modules.web.api.webmodule.WebModule; 47 import org.netbeans.modules.web.spi.webmodule.WebModuleFactory; 48 import org.netbeans.modules.web.spi.webmodule.WebModuleProvider; 49 import org.netbeans.modules.web.spi.webmodule.WebModuleImplementation; 50 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 51 import org.netbeans.spi.project.support.ant.AntProjectEvent; 52 import org.netbeans.spi.project.support.ant.AntProjectHelper; 53 import org.netbeans.spi.project.support.ant.AntProjectListener; 54 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 55 import org.netbeans.spi.project.support.ant.PropertyUtils; 56 57 62 public class WebModules implements WebModuleProvider, AntProjectListener, ClassPathProvider { 63 64 private ArrayList modules = new ArrayList (); 65 private HashMap cache = new HashMap (); 66 private Project project; 67 private AntProjectHelper helper; 68 private PropertyEvaluator evaluator; 69 70 public WebModules (Project project, AntProjectHelper helper, PropertyEvaluator evaluator) { 71 assert project != null; 72 this.project = project; 73 this.helper = helper; 74 this.evaluator = evaluator; 75 helper.addAntProjectListener(this); 76 } 77 78 public synchronized WebModule findWebModule (FileObject file) { 79 Project owner = FileOwnerQuery.getOwner (file); 80 if (project.equals (owner)) { 81 if (modules.isEmpty()) { 82 readAuxData (); 83 } 84 for (Iterator iter = modules.iterator (); iter.hasNext ();) { 85 FFWebModule wm = (FFWebModule) iter.next (); 86 if (wm.contais (file)) { 87 if (cache.get (wm) == null) { 88 cache.put (wm, WebModuleFactory.createWebModule (wm)); 89 } 90 return (WebModule) cache.get (wm); 91 } 92 } 93 } 94 return null; 95 } 96 97 public ClassPath findClassPath (FileObject file, String type) { 98 Project owner = FileOwnerQuery.getOwner (file); 99 if (owner != null && owner.equals (project)) { 100 if (modules == null) { 101 readAuxData (); 102 } 103 for (Iterator iter = modules.iterator (); iter.hasNext ();) { 104 FFWebModule wm = (FFWebModule) iter.next (); 105 if (wm.contais (file)) { 106 return wm.findClassPath (file, type); 107 } 108 } 109 } 110 return null; 111 } 112 113 public synchronized void readAuxData () { 114 modules.clear(); 115 cache.clear(); 116 AuxiliaryConfiguration aux = (AuxiliaryConfiguration)project.getLookup().lookup(AuxiliaryConfiguration.class); 117 assert aux != null; 118 Element web = aux.getConfigurationFragment("web-data", WebProjectNature.NS_WEB, true); 119 if (web == null) { 120 return; 121 } 122 List webModules = Util.findSubElements(web); 123 Iterator it = webModules.iterator(); 124 while (it.hasNext()) { 125 Element webModulesEl = (Element )it.next(); 126 assert webModulesEl.getLocalName().equals("web-module") : webModulesEl; 127 FileObject docRootFO = getFile (webModulesEl, "doc-root"); Element j2eeSpecEl = Util.findElement (webModulesEl, "j2ee-spec-level", WebProjectNature.NS_WEB); 129 String j2eeSpec = j2eeSpecEl == null ? null : evaluator.evaluate (Util.findText (j2eeSpecEl)); 130 Element contextPathEl = Util.findElement (webModulesEl, "context-path", WebProjectNature.NS_WEB); 131 String contextPathText = contextPathEl == null ? null : Util.findText (contextPathEl); 132 String contextPath = contextPathText == null ? null : evaluator.evaluate (contextPathText); 133 Element classpathEl = Util.findElement (webModulesEl, "classpath", WebProjectNature.NS_WEB); 134 FileObject [] sources = getSources (); 135 ClassPath cp = classpathEl == null ? null : createClasspath (classpathEl, sources); 136 modules.add (new FFWebModule (docRootFO, j2eeSpec, contextPath, sources, cp)); 137 } 138 } 139 140 private FileObject getFile (Element parent, String fileElName) { 141 Element el = Util.findElement (parent, fileElName, WebProjectNature.NS_WEB); 142 String fname = Util.findText (el); 143 String locationEval = evaluator.evaluate(fname); 144 if (locationEval != null) { 145 File locationFile = helper.resolveFile(locationEval); 146 return FileUtil.toFileObject(locationFile); 147 } 148 return null; 149 } 150 151 private FileObject [] getSources () { 152 SourceGroup sg [] = ProjectUtils.getSources (project).getSourceGroups (JavaProjectConstants.SOURCES_TYPE_JAVA); 153 Set srcRootSet = new HashSet (); 154 for (int i = 0; i < sg.length; i++) { 155 URL entry; 156 try { 157 entry = sg[i].getRootFolder().getURL(); 158 } catch (FileStateInvalidException x) { 159 throw new AssertionError (x); 160 } 161 SourceForBinaryQuery.Result res = SourceForBinaryQuery.findSourceRoots (entry); 163 FileObject srcForBin [] = res.getRoots (); 164 for (int j = 0; j < srcForBin.length; j++) { 165 srcRootSet.add (srcForBin [j]); 166 } 167 } 168 169 FileObject[] roots = new FileObject [sg.length]; 170 for (int i = 0; i < sg.length; i++) { 171 roots[i] = sg[i].getRootFolder(); 172 } 173 return roots; 174 } 175 176 179 private ClassPath createClasspath(Element classpathEl, FileObject[] sources) { 180 String cp = Util.findText(classpathEl); 182 if (cp == null) { 183 cp = ""; 184 } 185 String cpEval = evaluator.evaluate(cp); 186 if (cpEval == null) { 187 return null; 188 } 189 String [] path = PropertyUtils.tokenizePath(cpEval); 190 Set entries = new HashSet (); 191 for (int i = 0; i < path.length; i++) { 192 entries.add(helper.resolveFile(path[i])); 193 } 194 if (entries.size() == 0) { 195 for (int i = 0; i < sources.length; i++) { 198 entries.add(FileUtil.toFile(sources[i])); 199 } 200 } 201 URL [] pathURL = new URL [entries.size()]; 202 int i = 0; 203 for (Iterator it = entries.iterator(); it.hasNext();) { 204 File entryFile = (File )it.next(); 205 URL entry; 206 try { 207 entry = entryFile.toURI().toURL(); 208 if (FileUtil.isArchiveFile(entry)) { 209 entry = FileUtil.getArchiveRoot(entry); 210 } else { 211 String s = entry.toExternalForm(); 212 if (!s.endsWith("/")) { entry = new URL (s + '/'); 215 } 216 } 217 } catch (MalformedURLException x) { 218 throw new AssertionError (x); 219 } 220 pathURL[i++] = entry; 221 } 222 return ClassPathSupport.createClassPath(pathURL); 223 } 224 225 public void configurationXmlChanged(AntProjectEvent ev) { 226 readAuxData(); 227 } 228 229 public void propertiesChanged(AntProjectEvent ev) { 230 } 232 233 private final class FFWebModule implements WebModuleImplementation { 234 235 public static final String FOLDER_WEB_INF = "WEB-INF"; public static final String FILE_DD = "web.xml"; 238 private FileObject docRootFO; 239 private FileObject [] sourcesFOs; 240 private ClassPath webClassPath; 241 private ClassPath javaSourcesClassPath; 242 private ClassPath composedClassPath = null; 243 private String j2eeSpec; 244 private String contextPath; 245 246 FFWebModule (FileObject docRootFO, String j2eeSpec, String contextPath, FileObject sourcesFOs[], ClassPath classPath) { 247 this.docRootFO = docRootFO; 248 this.j2eeSpec = j2eeSpec; 249 this.contextPath = (contextPath == null ? "" : contextPath); 250 this.sourcesFOs = sourcesFOs; 251 this.webClassPath = (classPath == null ? ClassPathSupport.createClassPath(Collections.EMPTY_LIST) : classPath); 252 javaSourcesClassPath = (sourcesFOs == null ? ClassPathSupport.createClassPath(Collections.EMPTY_LIST): ClassPathSupport.createClassPath(sourcesFOs)); 253 } 254 255 boolean contais (FileObject fo) { 256 if (docRootFO == fo || FileUtil.isParentOf (docRootFO , fo)) 257 return true; 258 for (int i = 0; i < sourcesFOs.length; i++) { 259 if (sourcesFOs [i] == fo || FileUtil.isParentOf (sourcesFOs [i], fo)) 260 return true; 261 } 262 return false; 263 } 264 265 public FileObject getDocumentBase () { 266 return docRootFO; 267 } 268 269 public ClassPath findClassPath (FileObject file, String type) { 270 int fileType = getType(file); 271 272 if (fileType == 0) { 273 if (!type.equals(ClassPath.SOURCE)) 274 return null; 275 else 276 return javaSourcesClassPath; 277 } else 278 if (fileType == 1){ 279 if (composedClassPath == null) { 280 HashSet all = new HashSet (); 281 FileObject[] javaRoots = null; 282 for (int i = 0; i < sourcesFOs.length; i++){ 283 javaRoots = ClassPath.getClassPath(sourcesFOs[i], type).getRoots(); 284 for (int j = 0; j < javaRoots.length; j++) 285 if (!all.contains(javaRoots[j])) 286 all.add(javaRoots[j]); 287 } 288 289 for (int i = 0; i < webClassPath.getRoots().length; i++) 290 if (!all.contains(webClassPath.getRoots()[i])) 291 all.add(webClassPath.getRoots()[i]); 292 293 FileObject[] roots = new FileObject[all.size()]; 294 int i = 0; 295 for (Iterator it = all.iterator(); it.hasNext();) 296 roots[i++] = (FileObject)it.next(); 297 298 composedClassPath = ClassPathSupport.createClassPath(roots); 299 } 300 return composedClassPath; 301 } 302 return webClassPath; 303 } 304 305 public String getJ2eePlatformVersion () { 306 return j2eeSpec; 307 } 308 309 public String getContextPath () { 310 return contextPath; 311 } 312 313 public String toString () { 314 StringBuffer sb = new StringBuffer ("web module in freeform project" + 315 "\n\tdoc root:" + docRootFO.getPath () + 316 "\n\tcontext path:" + contextPath + 317 "\n\tj2ee version:" + j2eeSpec); 318 for (int i = 0; i < sourcesFOs.length; i++) { 319 sb.append ("\n\tsource root:" + sourcesFOs [i].getPath ()); 320 } 321 return sb.toString (); 322 } 323 324 public FileObject getDeploymentDescriptor () { 325 FileObject winf = getWebInf (); 326 if (winf == null) { 327 return null; 328 } 329 return winf.getFileObject (FILE_DD); 330 } 331 332 public FileObject getWebInf () { 333 return getDocumentBase ().getFileObject (FOLDER_WEB_INF); 334 } 335 336 public FileObject[] getJavaSources() { 337 return sourcesFOs; 338 } 339 340 349 private int getType(FileObject file) { 350 for (int i=0; i < sourcesFOs.length; i++) { 352 FileObject root = sourcesFOs[i]; 353 if (root.equals(file) || FileUtil.isParentOf(root, file)) { 354 return 0; 355 } 356 } 357 358 FileObject dir = getDocumentBase(); 360 if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir,file))) { 361 return 1; 362 } 363 364 return -1; 365 } 366 } 367 } 368 | Popular Tags |