1 19 package org.netbeans.modules.javacore.parser; 20 21 import java.io.FileNotFoundException ; 22 import java.io.Reader ; 23 import org.netbeans.lib.java.parser.CompilerException; 24 import org.netbeans.lib.java.parser.ECRequestDesc; 25 import org.netbeans.lib.java.parser.ErrConsumer; 26 import org.openide.ErrorManager; 27 import org.openide.filesystems.FileObject; 28 import org.openide.filesystems.FileUtil; 29 import java.util.*; 30 import java.net.URL ; 31 import java.io.File ; 32 import org.netbeans.api.java.classpath.ClassPath; 33 import org.netbeans.api.java.queries.SourceForBinaryQuery; 34 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 35 import org.openide.loaders.DataObject; 36 37 38 42 public class ECRequestDescImpl implements ECRequestDesc { 43 private final String sourcePath; 44 private final String classPath; 45 private final String bootPath; 46 private final String fileName; 47 private final ASTProvider provider; 48 private final ErrConsumer consumer; 49 private Map modifiedMap; 50 51 52 public ECRequestDescImpl(String name, ASTProvider provider, ErrConsumer consumer) throws CompilerException { 53 this.provider = provider; 54 this.consumer = consumer; 55 fileName = name; 56 57 StringBuffer compileRoots=new StringBuffer (241); 58 StringBuffer sourceRoots=new StringBuffer (246); 59 FileObject fo=provider.getFileObject(); 60 ClassPath ccp = ClassPath.getClassPath (fo, ClassPath.COMPILE); 61 ClassPath ecp = ClassPath.getClassPath (fo, ClassPath.EXECUTE); 62 ClassPath scp = ClassPath.getClassPath (fo, ClassPath.SOURCE); 63 if (!fo.isValid()) { 64 throw new CompilerException(new RuntimeException ("FileObject " + fo.getPath() + " is no more valid. EC request cancelled.")); } 66 ClassPath extCcp = getCompileClassPathWithCompiledSrc (ccp, ecp, scp); 67 Set cprootsSet = new HashSet(); 68 getCompileAndSourcePath(extCcp,sourceRoots,compileRoots, cprootsSet); 69 getSourcePath(scp, sourceRoots, cprootsSet); 70 sourcePath=sourceRoots.toString(); 71 classPath=compileRoots.toString(); 72 bootPath=getClassPathString(ClassPath.getClassPath(fo, ClassPath.BOOT)); 73 } 74 75 private Map getModifiedMap() { 76 if (modifiedMap == null) { 78 modifiedMap = new HashMap(); 79 DataObject[] dobjs = DataObject.getRegistry().getModified(); 80 for (int i = 0; i < dobjs.length; i++) { 81 FileObject fobj = dobjs[i].getPrimaryFile(); 82 if (fobj != null && fobj.isValid()) { 83 String filename = fobj.getPath(); 84 modifiedMap.put(filename, dobjs[i]); 85 } 86 } 87 } 88 return modifiedMap; 89 } 90 91 public String getBootClassPath() { 92 return bootPath; 93 } 94 95 public String getClassPath() { 96 return classPath; 97 } 98 99 public ErrConsumer getErrConsumer() { 100 return consumer; 101 } 102 103 public String getFileName() { 104 return fileName; 105 } 106 107 public Reader getReader() { 108 try { 109 return provider.getFileReader(false); 110 } catch (FileNotFoundException fnfe) { 111 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, fnfe); 113 return null; 114 } catch (Exception ex) { 115 ErrorManager.getDefault().notify(ex); 116 return null; 117 } 118 } 119 120 public String getSourceClassPath() { 121 return sourcePath; 122 } 123 124 public String getSourceLevel() { 125 return provider.getSourceLevel(); 126 } 127 128 private void getCompileAndSourcePath(ClassPath classPath, StringBuffer sourceRoots, StringBuffer compileRoots, Set cprootsSet) { 129 if (classPath != null) { 130 for (Iterator it = classPath.entries().iterator(); it.hasNext(); ) { 131 ClassPath.Entry entry = (ClassPath.Entry)it.next(); 132 FileObject root = entry.getRoot(); 133 if (root != null) { 134 cprootsSet.add(root); 135 getFileName(root,compileRoots); 136 } 137 FileObject[] sRoots = SourceForBinaryQuery.findSourceRoots(entry.getURL()).getRoots(); 138 for (int x = 0; x < sRoots.length; x++) { 139 FileObject sRoot=sRoots[x]; 140 141 if (!cprootsSet.contains(sRoot)) { 142 cprootsSet.add(sRoot); 143 getFileName(sRoot,sourceRoots); 144 } } } } } 149 150 private void getSourcePath(ClassPath classPath, StringBuffer sourceRoots, Set cprootsSet) { 151 if (classPath != null) { 152 for (Iterator it = classPath.entries().iterator(); it.hasNext(); ) { 153 ClassPath.Entry entry = (ClassPath.Entry)it.next(); 154 FileObject root = entry.getRoot(); 155 if (root != null && cprootsSet.add(root)) { 156 getFileName(root,sourceRoots); 157 } 158 } } } 161 162 void getFileName(FileObject fo,StringBuffer buf) { 163 try { 164 URL url=fo.getURL(); 165 if (url.getProtocol().equals("jar")) { fo = FileUtil.getArchiveFile(fo); 167 } 168 File f = FileUtil.toFile(fo); 169 170 if (buf.length()>0) 171 buf.append(File.pathSeparatorChar); 172 buf.append(f.getAbsolutePath()); 173 } catch (Exception ex) { 174 ex.printStackTrace(); 175 } 176 } 177 178 String getClassPathString(ClassPath cp) { 179 FileObject[] roots = cp.getRoots(); 180 if (roots.length == 0) 181 return ""; 182 StringBuffer buf = new StringBuffer (237); 183 for (int i = 0; i < roots.length; i++) { 184 getFileName(roots[i],buf); 185 } 186 return buf.toString(); 187 } 188 189 public Reader getReader(String filename) { 190 filename = filename.replace('\\', '/'); 191 if (filename.startsWith("/")) { filename = filename.substring(1); 193 } 194 Reader reader = null; 195 FileObject fo; 196 DataObject dobj = (DataObject) getModifiedMap().get(filename); 197 if (dobj == null) { 198 fo=provider.getFileObject(); 200 ClassPath scp = ClassPath.getClassPath (fo, ClassPath.SOURCE); 201 FileObject[] roots = scp.getRoots(); 202 for (int i = 0; i < roots.length; i++) { 203 String root = roots[i].getPath(); 204 if (filename.startsWith(root)) { 205 filename = filename.substring(root.length() + 1); 206 break; 207 } 208 } 209 fo = scp.findResource(filename); 210 } else { 211 fo = dobj.getPrimaryFile(); 212 } 213 if (fo != null) { 214 try { 215 ASTProvider provider = new ASTProvider(null, fo); 216 reader = provider.getFileReader(false); 217 } catch (Exception e) { 218 } 220 } 221 return reader; 222 } 223 224 public boolean isModified(String filename) { 225 filename = filename.replace('\\', '/'); 226 if (filename.startsWith("/")) { filename = filename.substring(1); 228 } 229 return getModifiedMap().containsKey(filename); 230 } 231 232 240 private static ClassPath getCompileClassPathWithCompiledSrc (final ClassPath ccp, final ClassPath ecp, final ClassPath scp) { 241 assert scp != null : "Source path can not be null"; List buildRoots; 243 if (ecp == null) { 244 buildRoots = Collections.EMPTY_LIST; 245 } else { 246 buildRoots = new ArrayList (5); for (Iterator it = ecp.entries().iterator(); it.hasNext();) { 248 URL url = ((ClassPath.Entry)it.next()).getURL(); 249 FileObject[] sourceRoots = SourceForBinaryQuery.findSourceRoots(url).getRoots(); 250 for (int i=0; i<sourceRoots.length; i++) { 251 if (scp.contains (sourceRoots[i])) { 252 buildRoots.add (ClassPathSupport.createResource(url)); 253 break; 254 } 255 } 256 } 257 } 258 if (buildRoots.size()==0) { 259 return ccp; 262 } else { 263 if (ccp == null) { 265 return ClassPathSupport.createClassPath (buildRoots); 268 } else { 269 return ClassPathSupport.createProxyClassPath( new ClassPath[] { 270 ccp, 271 ClassPathSupport.createClassPath (buildRoots), 272 }); 273 } 274 } 275 } 276 } 277 | Popular Tags |