KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > parsing > CachingFileManager


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
20 package org.netbeans.modules.java.source.parsing;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.tools.FileObject;
30 import javax.tools.JavaFileManager;
31 import javax.tools.JavaFileObject;
32 import org.netbeans.api.java.classpath.ClassPath;
33 import org.netbeans.modules.java.preprocessorbridge.spi.JavaFileFilterImplementation;
34 import org.netbeans.modules.java.source.util.Iterators;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.util.Exceptions;
37
38
39 /** Implementation of file manager for given classpath.
40  *
41  * @author Petr Hrebejk
42  */

43 public class CachingFileManager implements JavaFileManager {
44     
45     protected final CachingArchiveProvider provider;
46     protected final JavaFileFilterImplementation filter;
47     protected final ClassPath cp;
48     protected final boolean cacheFile;
49     protected final boolean ignoreExcludes;
50     
51     
52     public CachingFileManager( CachingArchiveProvider provider, final ClassPath cp, boolean cacheFile, boolean ignoreExcludes) {
53         this (provider, cp, null, cacheFile, ignoreExcludes);
54     }
55     
56     /** Creates a new instance of CachingFileManager */
57     public CachingFileManager( CachingArchiveProvider provider, final ClassPath cp, final JavaFileFilterImplementation filter, boolean cacheFile, boolean ignoreExcludes) {
58         this.provider = provider;
59         this.cp = cp;
60         this.cacheFile = cacheFile;
61         this.filter = filter;
62         this.ignoreExcludes = ignoreExcludes;
63     }
64     
65     // FileManager implementation ----------------------------------------------
66

67     // XXX omit files not of given kind
68
public Iterable JavaDoc<JavaFileObject> list( Location l, String JavaDoc packageName, Set JavaDoc<JavaFileObject.Kind> kinds, boolean recursive ) {
69      
70         if (recursive) {
71             throw new UnsupportedOperationException JavaDoc ("Recursive listing is not supported in archives");
72         }
73         
74 // long start = System.currentTimeMillis();
75

76         String JavaDoc folderName = FileObjects.convertPackage2Folder( packageName );
77                         
78         List JavaDoc<Iterator JavaDoc<JavaFileObject>> idxs = new LinkedList JavaDoc<Iterator JavaDoc<JavaFileObject>>();
79         for(ClassPath.Entry entry : this.cp.entries()) {
80             try {
81                 Archive archive = provider.getArchive( entry.getURL(), cacheFile );
82                 if (archive != null) {
83                     Iterable JavaDoc<JavaFileObject> entries = archive.getFiles( folderName, ignoreExcludes?null:entry, filter);
84                     idxs.add( entries.iterator() );
85                 }
86             } catch (IOException JavaDoc e) {
87                 Exceptions.printStackTrace(e);
88             }
89         }
90         // System.out.println(" LIST TIME for " + packageName + " = " + ( System.currentTimeMillis() - start ) );
91
return Iterators.toIterable( Iterators.chained( idxs ) );
92     }
93        
94     public javax.tools.FileObject getFileForInput( Location l, String JavaDoc pkgName, String JavaDoc relativeName ) {
95         
96         for( ClassPath.Entry root : this.cp.entries()) {
97             try {
98                 Archive archive = provider.getArchive (root.getURL(), cacheFile);
99                 if (archive != null) {
100                     Iterable JavaDoc<JavaFileObject> files = archive.getFiles(FileObjects.convertPackage2Folder(pkgName), ignoreExcludes?null:root, filter);
101                     for (JavaFileObject e : files) {
102                         if (relativeName.equals(e.getName())) {
103                             return e;
104                         }
105                     }
106                 }
107             } catch (IOException JavaDoc e) {
108                 Exceptions.printStackTrace(e);
109             }
110         }
111         return null;
112     }
113     
114     public JavaFileObject getJavaFileForInput (Location l, String JavaDoc className, JavaFileObject.Kind kind) {
115         String JavaDoc[] namePair = FileObjects.getParentRelativePathAndName(className);
116         if (namePair == null) {
117             return null;
118         }
119         namePair[1] = namePair[1] + kind.extension;
120         for( ClassPath.Entry root : this.cp.entries()) {
121             try {
122                 Archive archive = provider.getArchive (root.getURL(), cacheFile);
123                 if (archive != null) {
124                     Iterable JavaDoc<JavaFileObject> files = archive.getFiles(namePair[0], ignoreExcludes?null:root, filter);
125                     for (JavaFileObject e : files) {
126                         if (namePair[1].equals(e.getName())) {
127                             return e;
128                         }
129                     }
130                 }
131             } catch (IOException JavaDoc e) {
132                 Exceptions.printStackTrace(e);
133             }
134         }
135         return null;
136     }
137
138         
139     public javax.tools.FileObject getFileForOutput( Location l, String JavaDoc pkgName, String JavaDoc relativeName, javax.tools.FileObject sibling )
140         throws IOException JavaDoc, UnsupportedOperationException JavaDoc, IllegalArgumentException JavaDoc {
141         throw new UnsupportedOperationException JavaDoc ();
142     }
143     
144     public JavaFileObject getJavaFileForOutput( Location l, String JavaDoc className, JavaFileObject.Kind kind, javax.tools.FileObject sibling )
145         throws IOException JavaDoc, UnsupportedOperationException JavaDoc, IllegalArgumentException JavaDoc {
146         throw new UnsupportedOperationException JavaDoc ();
147     }
148     
149     public void flush() throws IOException JavaDoc {
150         // XXX Do nothing?
151
}
152
153     public void close() throws IOException JavaDoc {
154         // XXX Do nothing?
155
}
156     
157     public int isSupportedOption(String JavaDoc string) {
158         return -1;
159     }
160     
161     public boolean handleOption (final String JavaDoc head, final Iterator JavaDoc<String JavaDoc> tail) {
162         return false;
163     }
164
165     public boolean hasLocation(Location location) {
166         return true;
167     }
168     
169     public ClassLoader JavaDoc getClassLoader (final Location l) {
170         return null;
171     }
172     
173     public String JavaDoc inferBinaryName (Location l, JavaFileObject javaFileObject) {
174         if (javaFileObject instanceof FileObjects.Base) {
175             final FileObjects.Base base = (FileObjects.Base) javaFileObject;
176             final StringBuilder JavaDoc sb = new StringBuilder JavaDoc ();
177             sb.append (base.getPackage());
178             sb.append('.'); //NOI18N
179
sb.append(base.getNameWithoutExtension());
180             return sb.toString();
181         }
182         else if (javaFileObject instanceof SourceFileObject) {
183             org.openide.filesystems.FileObject fo = ((SourceFileObject)javaFileObject).file;
184             for (org.openide.filesystems.FileObject root : this.cp.getRoots()) {
185                 if (FileUtil.isParentOf(root,fo)) {
186                     String JavaDoc relativePath = FileUtil.getRelativePath(root,fo);
187                     int index = relativePath.lastIndexOf('.'); //NOI18N
188
assert index > 0;
189                     final String JavaDoc result = relativePath.substring(0,index).replace('/','.'); //NOI18N
190
return result;
191                 }
192             }
193         }
194         return null;
195     }
196     
197     //Static helpers - temporary
198

199     public static URL JavaDoc[] getClassPathRoots (final ClassPath cp) {
200        assert cp != null;
201        final List JavaDoc<ClassPath.Entry> entries = cp.entries();
202        final List JavaDoc<URL JavaDoc> result = new ArrayList JavaDoc<URL JavaDoc>(entries.size());
203        for (ClassPath.Entry entry : entries) {
204            result.add (entry.getURL());
205        }
206        return result.toArray(new URL JavaDoc[result.size()]);
207     }
208
209     public boolean isSameFile(FileObject fileObject, FileObject fileObject0) {
210         return fileObject instanceof FileObjects.FileBase
211                && fileObject0 instanceof FileObjects.FileBase
212                && ((FileObjects.FileBase)fileObject).getFile().equals(((FileObjects.FileBase)fileObject).getFile());
213     }
214 }
215
Popular Tags