KickJava   Java API By Example, From Geeks To Geeks.

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


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.MalformedURLException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.tools.JavaFileManager;
30 import javax.tools.JavaFileManager.Location;
31 import javax.tools.JavaFileObject;
32 import org.netbeans.api.java.classpath.ClassPath;
33 import org.openide.ErrorManager;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.filesystems.URLMapper;
37
38 /**
39  *
40  * @author Tomas Zezula
41  */

42 public class SourceFileManager implements JavaFileManager {
43     
44     private final ClassPath sourceRoots;
45     private final boolean ignoreExcludes;
46     
47     /** Creates a new instance of SourceFileManager */
48     public SourceFileManager (final ClassPath sourceRoots, final boolean ignoreExcludes) {
49         this.sourceRoots = sourceRoots;
50         this.ignoreExcludes = ignoreExcludes;
51     }
52
53     public List JavaDoc<JavaFileObject> list(final Location l, final String JavaDoc packageName, final Set JavaDoc<JavaFileObject.Kind> kinds, final boolean recursive) {
54         //Todo: Caching of results, needs listening on FS
55
List JavaDoc<JavaFileObject> result = new ArrayList JavaDoc<JavaFileObject> ();
56         String JavaDoc _name = packageName.replace('.','/'); //NOI18N
57
if (_name.length() != 0) {
58             _name+='/'; //NOI18N
59
}
60         for (ClassPath.Entry entry : this.sourceRoots.entries()) {
61             if (ignoreExcludes || entry.includes(_name)) {
62                 FileObject root = entry.getRoot();
63                 if (root != null) {
64                     FileObject tmpFile = root.getFileObject(_name);
65                     if (tmpFile != null && tmpFile.isFolder()) {
66                         Enumeration JavaDoc<? extends FileObject> files = tmpFile.getChildren (recursive);
67                         while (files.hasMoreElements()) {
68                             FileObject file = files.nextElement();
69                             if (ignoreExcludes || entry.includes(file)) {
70                                 JavaFileObject.Kind kind;
71                                 final String JavaDoc ext = file.getExt();
72                                 if (FileObjects.JAVA.equalsIgnoreCase(ext)) {
73                                     kind = JavaFileObject.Kind.SOURCE;
74                                 }
75                                 else if (FileObjects.CLASS.equalsIgnoreCase(ext) || "sig".equalsIgnoreCase(ext)) {
76                                     kind = JavaFileObject.Kind.CLASS;
77                                 }
78                                 else if (FileObjects.HTML.equalsIgnoreCase(ext)) {
79                                     kind = JavaFileObject.Kind.HTML;
80                                 }
81                                 else {
82                                     kind = JavaFileObject.Kind.OTHER;
83                                 }
84                                 if (kinds.contains(kind)) {
85                                     result.add (SourceFileObject.create(file));
86                                 }
87                             }
88                         }
89                     }
90                 }
91             }
92         }
93         return result;
94     }
95
96     public javax.tools.FileObject getFileForInput (final Location l, final String JavaDoc pkgName, final String JavaDoc relativeName) {
97         String JavaDoc rp = FileObjects.getRelativePath (pkgName, relativeName);
98         for (ClassPath.Entry entry : this.sourceRoots.entries()) {
99             if (ignoreExcludes || entry.includes(rp)) {
100                 FileObject root = entry.getRoot();
101                 if (root != null) {
102                     FileObject file = root.getFileObject(rp);
103                     if (file != null) {
104                         return SourceFileObject.create (file);
105                     }
106                 }
107             }
108         }
109         return null;
110     }
111     
112     public JavaFileObject getJavaFileForInput (Location l, final String JavaDoc className, JavaFileObject.Kind kind) {
113         String JavaDoc[] namePair = FileObjects.getParentRelativePathAndName (className);
114         if (namePair == null) {
115             return null;
116         }
117         String JavaDoc ext = kind == JavaFileObject.Kind.CLASS ? "sig" : kind.extension.substring(1); //Skeep the .
118
for (ClassPath.Entry entry : this.sourceRoots.entries()) {
119             FileObject root = entry.getRoot();
120             if (root != null) {
121                 FileObject parent = root.getFileObject(namePair[0]);
122                 if (parent != null) {
123                     FileObject[] children = parent.getChildren();
124                     for (FileObject child : children) {
125                         if (namePair[1].equals(child.getName()) && ext.equalsIgnoreCase(child.getExt()) && (ignoreExcludes || entry.includes(child))) {
126                             return SourceFileObject.create (child);
127                         }
128                     }
129                 }
130             }
131         }
132         return null;
133     }
134
135     public javax.tools.FileObject getFileForOutput(Location l, String JavaDoc pkgName, String JavaDoc relativeName, javax.tools.FileObject sibling)
136         throws IOException JavaDoc, UnsupportedOperationException JavaDoc, IllegalArgumentException JavaDoc {
137         throw new UnsupportedOperationException JavaDoc ("The SourceFileManager does not support write operations."); // NOI18N
138
}
139
140     public JavaFileObject getJavaFileForOutput (Location l, String JavaDoc className, JavaFileObject.Kind kind, javax.tools.FileObject sibling)
141         throws IOException JavaDoc, UnsupportedOperationException JavaDoc, IllegalArgumentException JavaDoc {
142         throw new UnsupportedOperationException JavaDoc("The SourceFileManager does not support write operations."); // NOI18N
143
}
144     
145     public void flush() throws java.io.IOException JavaDoc {
146         //Nothing to do
147
}
148
149     public void close() throws java.io.IOException JavaDoc {
150         //Nothing to do
151
}
152     
153     public int isSupportedOption(String JavaDoc string) {
154         return -1;
155     }
156     
157     public boolean handleOption (final String JavaDoc head, final Iterator JavaDoc<String JavaDoc> tail) {
158         return false;
159     }
160  
161     public boolean hasLocation(Location location) {
162         return true;
163     }
164        
165     public ClassLoader JavaDoc getClassLoader (Location l) {
166         return null;
167     }
168     
169     public String JavaDoc inferBinaryName (final Location l, final JavaFileObject jfo) {
170         try {
171             FileObject fo;
172             if (jfo instanceof SourceFileObject) {
173                 fo = ((SourceFileObject)jfo).file;
174             }
175             else {
176                 //Should never happen in the IDE
177
fo = URLMapper.findFileObject(jfo.toUri().toURL());
178             }
179             for (FileObject root : this.sourceRoots.getRoots()) {
180                 if (FileUtil.isParentOf(root,fo)) {
181                     String JavaDoc relativePath = FileUtil.getRelativePath(root,fo);
182                     int index = relativePath.lastIndexOf('.');
183                     assert index > 0;
184                     final String JavaDoc result = relativePath.substring(0,index).replace('/','.');
185                     return result;
186                 }
187             }
188         } catch (MalformedURLException JavaDoc e) {
189             ErrorManager.getDefault().notify(e);
190         }
191         return null;
192     }
193
194     public boolean isSameFile(javax.tools.FileObject fileObject, javax.tools.FileObject fileObject0) {
195         return fileObject instanceof SourceFileObject
196                && fileObject0 instanceof SourceFileObject
197                && ((SourceFileObject)fileObject).file == ((SourceFileObject)fileObject0).file;
198     }
199 }
200
Popular Tags