KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > parser > ECRequestDescImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.javacore.parser;
20
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.Reader JavaDoc;
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 JavaDoc;
31 import java.io.File JavaDoc;
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 /**
39  *
40  * @author Pavel Flaska
41  */

42 public class ECRequestDescImpl implements ECRequestDesc {
43     private final String JavaDoc sourcePath;
44     private final String JavaDoc classPath;
45     private final String JavaDoc bootPath;
46     private final String JavaDoc fileName;
47     private final ASTProvider provider;
48     private final ErrConsumer consumer;
49     private Map modifiedMap;
50
51     /** Creates a new instance of ECRequestDescImpl */
52     public ECRequestDescImpl(String JavaDoc name, ASTProvider provider, ErrConsumer consumer) throws CompilerException {
53         this.provider = provider;
54         this.consumer = consumer;
55         fileName = name;
56
57         StringBuffer JavaDoc compileRoots=new StringBuffer JavaDoc(241);
58         StringBuffer JavaDoc sourceRoots=new StringBuffer JavaDoc(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 JavaDoc("FileObject " + fo.getPath() + " is no more valid. EC request cancelled.")); //NOI18N
65
}
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         // initialize map of modified JavaDataObjects that are on the classpath
77
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 JavaDoc filename = fobj.getPath();
84                     modifiedMap.put(filename, dobjs[i]);
85                 }
86             }
87         }
88         return modifiedMap;
89     }
90     
91     public String JavaDoc getBootClassPath() {
92         return bootPath;
93     }
94     
95     public String JavaDoc getClassPath() {
96         return classPath;
97     }
98     
99     public ErrConsumer getErrConsumer() {
100         return consumer;
101     }
102     
103     public String JavaDoc getFileName() {
104         return fileName;
105     }
106     
107     public Reader JavaDoc getReader() {
108         try {
109             return provider.getFileReader(false);
110         } catch (FileNotFoundException JavaDoc fnfe) {
111             //#63694
112
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, fnfe);
113             return null;
114         } catch (Exception JavaDoc ex) {
115             ErrorManager.getDefault().notify(ex);
116             return null;
117         }
118     }
119     
120     public String JavaDoc getSourceClassPath() {
121         return sourcePath;
122     }
123     
124     public String JavaDoc getSourceLevel() {
125         return provider.getSourceLevel();
126     }
127     
128     private void getCompileAndSourcePath(ClassPath classPath, StringBuffer JavaDoc sourceRoots, StringBuffer JavaDoc 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                     } // if
145
} // for
146
} // for
147
} // if
148
}
149     
150     private void getSourcePath(ClassPath classPath, StringBuffer JavaDoc 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             } // for
159
} // if
160
}
161
162     void getFileName(FileObject fo,StringBuffer JavaDoc buf) {
163         try {
164             URL JavaDoc url=fo.getURL();
165             if (url.getProtocol().equals("jar")) { // NOI18N
166
fo = FileUtil.getArchiveFile(fo);
167             }
168             File JavaDoc f = FileUtil.toFile(fo);
169             
170             if (buf.length()>0)
171                 buf.append(File.pathSeparatorChar);
172             buf.append(f.getAbsolutePath());
173         } catch (Exception JavaDoc ex) {
174             ex.printStackTrace();
175         }
176     }
177     
178     String JavaDoc getClassPathString(ClassPath cp) {
179         FileObject[] roots = cp.getRoots();
180         if (roots.length == 0)
181             return "";
182         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(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 JavaDoc getReader(String JavaDoc filename) {
190         filename = filename.replace('\\', '/');
191         if (filename.startsWith("/")) { // NOI18N
192
filename = filename.substring(1);
193         }
194         Reader JavaDoc reader = null;
195         FileObject fo;
196         DataObject dobj = (DataObject) getModifiedMap().get(filename);
197         if (dobj == null) {
198             // EC is asking reader for a file that is not modified
199
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 JavaDoc 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 JavaDoc e) {
218                 // ignore -> reader will be null
219
}
220         }
221         return reader;
222     }
223     
224     public boolean isModified(String JavaDoc filename) {
225         filename = filename.replace('\\', '/');
226         if (filename.startsWith("/")) { // NOI18N
227
filename = filename.substring(1);
228         }
229         return getModifiedMap().containsKey(filename);
230     }
231
232     /**
233      * Creates extended copile Classpath, the classpath is
234      * ClassPath.COMPILE + build destination folder(s)
235      * @param ccp - ClassPath.COMPILE
236      * @param ecp - ClassPath.EXECUTE
237      * @param scp - ClassPath.SOURCE
238      * @return ClassPath
239      */

240     private static ClassPath getCompileClassPathWithCompiledSrc (final ClassPath ccp, final ClassPath ecp, final ClassPath scp) {
241         assert scp != null : "Source path can not be null"; //Every Java file should have source path // NOI18N
242
List buildRoots;
243         if (ecp == null) {
244             buildRoots = Collections.EMPTY_LIST;
245         } else {
246             buildRoots = new ArrayList (5); //Mostly 1
247
for (Iterator it = ecp.entries().iterator(); it.hasNext();) {
248                 URL JavaDoc 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             //No EXEC classpath for sources (DefaultClassPathProvider)
260
//return just COMPILE classpath
261
return ccp;
262         } else {
263             //Return COMPILE + buildRoots
264
if (ccp == null) {
265                 //Not common case
266
//but may be caused by some strange ClassPathProvider
267
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