001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.java.source.parsing;
043:
044: import java.io.IOException;
045: import java.util.ArrayList;
046: import java.util.Iterator;
047: import java.util.LinkedList;
048: import java.util.List;
049: import java.util.Set;
050: import java.util.logging.Level;
051: import java.util.logging.Logger;
052: import javax.tools.FileObject;
053: import javax.tools.JavaFileManager;
054: import javax.tools.JavaFileObject;
055: import javax.tools.StandardLocation;
056: import org.netbeans.modules.java.source.util.Iterators;
057:
058: /**
059: *
060: * @author Tomas Zezula
061: */
062: public class ProxyFileManager implements JavaFileManager {
063:
064: private static final Location ALL = new Location() {
065: public String getName() {
066: return "ALL";
067: } //NOI18N
068:
069: public boolean isOutputLocation() {
070: return false;
071: }
072: };
073:
074: private final JavaFileManager bootPath;
075: private final JavaFileManager classPath;
076: private final JavaFileManager sourcePath;
077: private final JavaFileManager outputhPath;
078:
079: private JavaFileObject lastInfered;
080: private String lastInferedResult;
081:
082: private static final Logger LOG = Logger
083: .getLogger(ProxyFileManager.class.getName());
084:
085: /** Creates a new instance of ProxyFileManager */
086: public ProxyFileManager(JavaFileManager bootPath,
087: JavaFileManager classPath, JavaFileManager sourcePath,
088: JavaFileManager outputhPath) {
089: assert bootPath != null;
090: assert classPath != null;
091: this .bootPath = bootPath;
092: this .classPath = classPath;
093: this .sourcePath = sourcePath;
094: this .outputhPath = outputhPath;
095: }
096:
097: private JavaFileManager[] getFileManager(Location location) {
098: if (location == StandardLocation.CLASS_PATH) {
099: return this .outputhPath == null ? new JavaFileManager[] { this .classPath }
100: : new JavaFileManager[] { this .classPath,
101: this .outputhPath };
102: } else if (location == StandardLocation.PLATFORM_CLASS_PATH) {
103: return new JavaFileManager[] { this .bootPath };
104: } else if (location == StandardLocation.SOURCE_PATH
105: && this .sourcePath != null) {
106: return new JavaFileManager[] { this .sourcePath };
107: } else if (location == StandardLocation.CLASS_OUTPUT
108: && this .outputhPath != null) {
109: return new JavaFileManager[] { this .outputhPath };
110: } else if (location == ALL) {
111: return this .outputhPath == null ? new JavaFileManager[] {
112: this .sourcePath, this .bootPath, this .classPath }
113: : new JavaFileManager[] { this .sourcePath,
114: this .bootPath, this .classPath,
115: this .outputhPath };
116: } else {
117: return new JavaFileManager[0];
118: }
119: }
120:
121: private JavaFileManager[] getAllFileManagers() {
122: List<JavaFileManager> result = new ArrayList<JavaFileManager>(4);
123: result.add(this .bootPath);
124: result.add(this .classPath);
125: if (this .sourcePath != null) {
126: result.add(this .sourcePath);
127: }
128: if (this .outputhPath != null) {
129: result.add(this .outputhPath);
130: }
131: return result.toArray(new JavaFileManager[result.size()]);
132: }
133:
134: public Iterable<JavaFileObject> list(Location l,
135: String packageName, Set<JavaFileObject.Kind> kinds,
136: boolean recurse) throws IOException {
137: List<Iterable<JavaFileObject>> iterables = new LinkedList<Iterable<JavaFileObject>>();
138: JavaFileManager[] fms = getFileManager(l);
139: for (JavaFileManager fm : fms) {
140: iterables.add(fm.list(l, packageName, kinds, recurse));
141: }
142: final Iterable<JavaFileObject> result = Iterators
143: .chained(iterables);
144: if (LOG.isLoggable(Level.FINER)) {
145: final StringBuilder urls = new StringBuilder();
146: for (JavaFileObject jfo : result) {
147: urls.append(jfo.toUri().toString());
148: urls.append(", "); //NOI18N
149: }
150: LOG.finer(String.format(
151: "list %s package: %s type: %s found files: [%s]", l
152: .toString(), packageName, kinds.toString(),
153: urls.toString())); //NOI18N
154: }
155: return result;
156: }
157:
158: public FileObject getFileForInput(Location l, String packageName,
159: String relativeName) throws IOException {
160: JavaFileManager[] fms = getFileManager(l);
161: for (JavaFileManager fm : fms) {
162: FileObject result = fm.getFileForInput(l, packageName,
163: relativeName);
164: if (result != null) {
165: return result;
166: }
167: }
168: return null;
169: }
170:
171: public FileObject getFileForOutput(Location l, String packageName,
172: String relativeName, FileObject sibling)
173: throws IOException, UnsupportedOperationException,
174: IllegalArgumentException {
175: JavaFileManager[] fms = getFileManager(l);
176: assert fms.length <= 1;
177: if (fms.length == 0) {
178: return null;
179: } else {
180: return fms[0].getFileForOutput(l, packageName,
181: relativeName, sibling);
182: }
183: }
184:
185: public ClassLoader getClassLoader(Location l) {
186: return null;
187: }
188:
189: public void flush() throws IOException {
190: JavaFileManager[] fms = getAllFileManagers();
191: for (JavaFileManager fm : fms) {
192: fm.flush();
193: }
194: }
195:
196: public void close() throws IOException {
197: JavaFileManager[] fms = getAllFileManagers();
198: for (JavaFileManager fm : fms) {
199: fm.close();
200: }
201: }
202:
203: public int isSupportedOption(String string) {
204: return -1;
205: }
206:
207: public boolean handleOption(String current, Iterator<String> remains) {
208: return false;
209: }
210:
211: public boolean hasLocation(JavaFileManager.Location location) {
212: return location == StandardLocation.CLASS_PATH
213: || location == StandardLocation.PLATFORM_CLASS_PATH
214: || location == StandardLocation.SOURCE_PATH
215: || location == StandardLocation.CLASS_OUTPUT;
216: }
217:
218: public JavaFileObject getJavaFileForInput(Location l,
219: String className, JavaFileObject.Kind kind)
220: throws IOException {
221: JavaFileManager[] fms = getFileManager(l);
222: for (JavaFileManager fm : fms) {
223: JavaFileObject result = fm.getJavaFileForInput(l,
224: className, kind);
225: if (result != null) {
226: return result;
227: }
228: }
229: return null;
230: }
231:
232: public JavaFileObject getJavaFileForOutput(Location l,
233: String className, JavaFileObject.Kind kind,
234: FileObject sibling) throws IOException,
235: UnsupportedOperationException, IllegalArgumentException {
236: JavaFileManager[] fms = getFileManager(l);
237: assert fms.length <= 1;
238: if (fms.length == 0) {
239: return null;
240: } else {
241: return fms[0].getJavaFileForOutput(l, className, kind,
242: sibling);
243: }
244: }
245:
246: public String inferBinaryName(JavaFileManager.Location location,
247: JavaFileObject javaFileObject) {
248: assert javaFileObject != null;
249: //If cached return it dirrectly
250: if (javaFileObject == lastInfered) {
251: return lastInferedResult;
252: }
253: String result;
254: //If instanceof FileObject.Base no need to delegate it
255: if (javaFileObject instanceof FileObjects.Base) {
256: final FileObjects.Base base = (FileObjects.Base) javaFileObject;
257: final StringBuilder sb = new StringBuilder();
258: sb.append(base.getPackage());
259: if (sb.length() > 0) {
260: sb.append('.'); //NOI18N
261: }
262: sb.append(base.getNameWithoutExtension());
263: result = sb.toString();
264: this .lastInfered = javaFileObject;
265: this .lastInferedResult = result;
266: return result;
267: }
268: //Ask delegates to infer the binary name
269: JavaFileManager[] fms = getFileManager(location);
270: for (JavaFileManager fm : fms) {
271: result = fm.inferBinaryName(location, javaFileObject);
272: if (result != null && result.length() > 0) {
273: this .lastInfered = javaFileObject;
274: this .lastInferedResult = result;
275: return result;
276: }
277: }
278: return null;
279: }
280:
281: public boolean isSameFile(FileObject fileObject,
282: FileObject fileObject0) {
283: final JavaFileManager[] fms = getFileManager(ALL);
284: for (JavaFileManager fm : fms) {
285: if (fm.isSameFile(fileObject, fileObject0)) {
286: return true;
287: }
288: }
289: return fileObject.toUri().equals(fileObject0.toUri());
290: }
291:
292: }
|