KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.tools.FileObject;
29 import javax.tools.JavaFileManager;
30 import javax.tools.JavaFileObject;
31 import javax.tools.StandardLocation;
32 import org.netbeans.modules.java.source.util.Iterators;
33
34 /**
35  *
36  * @author Tomas Zezula
37  */

38 public class ProxyFileManager implements JavaFileManager {
39
40     private static final Location ALL = new Location () {
41         public String JavaDoc getName() { return "ALL";} //NOI18N
42

43         public boolean isOutputLocation() { return false; }
44     };
45     
46     private final JavaFileManager bootPath;
47     private final JavaFileManager classPath;
48     private final JavaFileManager sourcePath;
49     private final JavaFileManager outputhPath;
50     
51     private JavaFileObject lastInfered;
52     private String JavaDoc lastInferedResult;
53     
54     /** Creates a new instance of ProxyFileManager */
55     public ProxyFileManager(JavaFileManager bootPath, JavaFileManager classPath, JavaFileManager sourcePath, JavaFileManager outputhPath) {
56         assert bootPath != null;
57         assert classPath != null;
58         this.bootPath = bootPath;
59         this.classPath = classPath;
60         this.sourcePath = sourcePath;
61         this.outputhPath = outputhPath;
62     }
63     
64     private JavaFileManager[] getFileManager (Location location) {
65         if (location == StandardLocation.CLASS_PATH) {
66             return this.outputhPath == null ?
67                 new JavaFileManager[] {this.classPath} :
68                 new JavaFileManager[] {this.classPath, this.outputhPath};
69         }
70         else if (location == StandardLocation.PLATFORM_CLASS_PATH) {
71             return new JavaFileManager[] {this.bootPath};
72         }
73         else if (location == StandardLocation.SOURCE_PATH && this.sourcePath != null) {
74             return new JavaFileManager[] {this.sourcePath};
75         }
76         else if (location == StandardLocation.CLASS_OUTPUT && this.outputhPath != null) {
77             return new JavaFileManager[] {this.outputhPath};
78         }
79         else if (location == ALL) {
80             return this.outputhPath == null ?
81                 new JavaFileManager[] {
82                     this.sourcePath,
83                     this.bootPath,
84                     this.classPath
85                 }:
86                 new JavaFileManager[] {
87                     this.sourcePath,
88                     this.bootPath,
89                     this.classPath,
90                     this.outputhPath
91                 };
92         }
93         else {
94             return new JavaFileManager[0];
95         }
96     }
97     
98     private JavaFileManager[] getAllFileManagers () {
99         List JavaDoc<JavaFileManager> result = new ArrayList JavaDoc<JavaFileManager> (4);
100         result.add(this.bootPath);
101         result.add (this.classPath);
102         if (this.sourcePath!=null) {
103             result.add (this.sourcePath);
104         }
105         if (this.outputhPath!=null) {
106             result.add (this.outputhPath);
107         }
108         return result.toArray(new JavaFileManager[result.size()]);
109     }
110     
111     public Iterable JavaDoc<JavaFileObject> list(Location l, String JavaDoc packageName, Set JavaDoc<JavaFileObject.Kind> kinds, boolean recurse) throws IOException JavaDoc {
112         List JavaDoc<Iterator JavaDoc<JavaFileObject>> iterators = new LinkedList JavaDoc<Iterator JavaDoc<JavaFileObject>>();
113         JavaFileManager[] fms = getFileManager (l);
114         for (JavaFileManager fm : fms) {
115             iterators.add( fm.list(l, packageName, kinds, recurse).iterator() );
116         }
117         return Iterators.toIterable(Iterators.chained( iterators ));
118     }
119
120     public FileObject getFileForInput(Location l, String JavaDoc packageName, String JavaDoc relativeName) throws IOException JavaDoc {
121         JavaFileManager[] fms = getFileManager(l);
122         for (JavaFileManager fm : fms) {
123             FileObject result = fm.getFileForInput(l, packageName, relativeName);
124             if (result != null) {
125                 return result;
126             }
127         }
128         return null;
129     }
130     
131     public FileObject getFileForOutput(Location l, String JavaDoc packageName, String JavaDoc relativeName, FileObject sibling)
132         throws IOException JavaDoc, UnsupportedOperationException JavaDoc, IllegalArgumentException JavaDoc {
133         JavaFileManager[] fms = getFileManager (l);
134         assert fms.length <=1;
135         if (fms.length == 0) {
136             return null;
137         }
138         else {
139             return fms[0].getFileForOutput(l, packageName, relativeName, sibling);
140         }
141     }
142     
143     public ClassLoader JavaDoc getClassLoader (Location l) {
144         return null;
145     }
146     
147     public void flush() throws IOException JavaDoc {
148         JavaFileManager[] fms = getAllFileManagers ();
149         for (JavaFileManager fm : fms) {
150             fm.flush();
151         }
152     }
153
154     public void close() throws IOException JavaDoc {
155         JavaFileManager[] fms = getAllFileManagers ();
156         for (JavaFileManager fm : fms) {
157             fm.close();
158         }
159     }
160
161     public int isSupportedOption(String JavaDoc string) {
162         return -1;
163     }
164     
165     public boolean handleOption (String JavaDoc current, Iterator JavaDoc<String JavaDoc> remains) {
166         return false;
167     }
168
169     public boolean hasLocation(JavaFileManager.Location location) {
170         return location == StandardLocation.CLASS_PATH ||
171                location == StandardLocation.PLATFORM_CLASS_PATH ||
172                location == StandardLocation.SOURCE_PATH ||
173                location == StandardLocation.CLASS_OUTPUT;
174     }
175     
176     public JavaFileObject getJavaFileForInput (Location l, String JavaDoc className, JavaFileObject.Kind kind) throws IOException JavaDoc {
177         JavaFileManager[] fms = getFileManager (l);
178         for (JavaFileManager fm : fms) {
179             JavaFileObject result = fm.getJavaFileForInput(l,className,kind);
180             if (result != null) {
181                 return result;
182             }
183         }
184         return null;
185     }
186
187     public JavaFileObject getJavaFileForOutput(Location l, String JavaDoc className, JavaFileObject.Kind kind, FileObject sibling)
188         throws IOException JavaDoc, UnsupportedOperationException JavaDoc, IllegalArgumentException JavaDoc {
189         JavaFileManager[] fms = getFileManager (l);
190         assert fms.length <=1;
191         if (fms.length == 0) {
192             return null;
193         }
194         else {
195             return fms[0].getJavaFileForOutput (l, className, kind, sibling);
196         }
197     }
198     
199     
200     public String JavaDoc inferBinaryName(JavaFileManager.Location location, JavaFileObject javaFileObject) {
201         assert javaFileObject != null;
202         //If cached return it dirrectly
203
if (javaFileObject == lastInfered) {
204             return lastInferedResult;
205         }
206         String JavaDoc result;
207         //If instanceof FileObject.Base no need to delegate it
208
if (javaFileObject instanceof FileObjects.Base) {
209             final FileObjects.Base base = (FileObjects.Base) javaFileObject;
210             final StringBuilder JavaDoc sb = new StringBuilder JavaDoc ();
211             sb.append (base.getPackage());
212             if (sb.length()>0) {
213                 sb.append('.'); //NOI18N
214
}
215             sb.append(base.getNameWithoutExtension());
216             result = sb.toString();
217             this.lastInfered = javaFileObject;
218             this.lastInferedResult = result;
219             return result;
220         }
221         //Ask delegates to infer the binary name
222
JavaFileManager[] fms = getFileManager (location);
223         for (JavaFileManager fm : fms) {
224             result = fm.inferBinaryName (location, javaFileObject);
225             if (result != null && result.length() > 0) {
226                 this.lastInfered = javaFileObject;
227                 this.lastInferedResult = result;
228                 return result;
229             }
230         }
231         return null;
232     }
233
234     public boolean isSameFile(FileObject fileObject, FileObject fileObject0) {
235         final JavaFileManager[] fms = getFileManager(ALL);
236         for (JavaFileManager fm : fms) {
237             if (fm.isSameFile(fileObject, fileObject0)) {
238                 return true;
239             }
240         }
241         return fileObject.toUri().equals (fileObject0.toUri());
242     }
243     
244 }
245
Popular Tags