KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import javax.tools.JavaFileObject;
30 import org.netbeans.api.java.classpath.ClassPath;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileUtil;
33
34 /**
35  *
36  * @author Tomas Zezula
37  */

38 public class OutputFileManager extends CachingFileManager {
39
40     private static boolean debug = Boolean.getBoolean("org.netbeans.modules.java.source.parsing.OutputFileManager.debug"); //NOI18N
41

42     private ClassPath scp;
43     
44     /** Creates a new instance of CachingFileManager */
45     public OutputFileManager(CachingArchiveProvider provider, final ClassPath outputClassPath, final ClassPath sourcePath) {
46         super (provider, outputClassPath, false, true);
47     assert sourcePath != null && outputClassPath != null;
48     this.scp = sourcePath;
49     }
50     
51     
52     public @Override JavaDoc JavaFileObject getJavaFileForOutput( Location l, String JavaDoc className, JavaFileObject.Kind kind, javax.tools.FileObject sibling )
53         throws IOException JavaDoc, UnsupportedOperationException JavaDoc, IllegalArgumentException JavaDoc {
54         
55         
56         if (kind != JavaFileObject.Kind.CLASS) {
57             throw new IllegalArgumentException JavaDoc ();
58         }
59         else {
60             int index;
61             if (sibling != null) {
62                 index = getActiveRoot (sibling);
63             }
64             else {
65                 index = getActiveRoot (FileObjects.convertPackage2Folder(className));
66             }
67             assert index >= 0 : "class: " + className +" sibling: " + sibling +" srcRoots: " + this.scp + " cacheRoots: " + this.cp;
68             assert index < this.cp.entries().size() : "index "+ index +" class: " + className +" sibling: " + sibling +" srcRoots: " + this.scp + " cacheRoots: " + this.cp;
69             File JavaDoc activeRoot = new File JavaDoc (URI.create(this.cp.entries().get(index).getURL().toExternalForm()));
70             String JavaDoc baseName = className.replace('.', File.separatorChar); //NOI18N
71
String JavaDoc nameStr = baseName + '.' + FileObjects.SIG;
72             int nameComponentIndex = nameStr.lastIndexOf(File.separatorChar);
73             if (nameComponentIndex != -1) {
74                 String JavaDoc pathComponent = nameStr.substring(0, nameComponentIndex);
75                 new File JavaDoc (activeRoot, pathComponent).mkdirs();
76             }
77             else {
78                 activeRoot.mkdirs();
79             }
80             File JavaDoc f = FileUtil.normalizeFile(new File JavaDoc (activeRoot, nameStr));
81             return OutputFileObject.create (activeRoot, f);
82         }
83     }
84         
85     public @Override JavaDoc javax.tools.FileObject getFileForOutput( Location l, String JavaDoc pkgName, String JavaDoc relativeName, javax.tools.FileObject sibling )
86         throws IOException JavaDoc, UnsupportedOperationException JavaDoc, IllegalArgumentException JavaDoc {
87         assert pkgName != null;
88         assert relativeName != null;
89         if (sibling == null) {
90             throw new IllegalArgumentException JavaDoc ("sibling == null");
91         }
92         final int index = getActiveRoot (sibling);
93         assert index >= 0 && index < this.cp.entries().size();
94         File JavaDoc activeRoot = new File JavaDoc (URI.create(this.cp.entries().get(index).getURL().toExternalForm()));
95         File JavaDoc folder;
96         if (pkgName.length() == 0) {
97             folder = activeRoot;
98         }
99         else {
100             folder = new File JavaDoc (activeRoot,FileObjects.convertPackage2Folder(pkgName));
101         }
102         if (!folder.exists()) {
103             if (!folder.mkdirs()) {
104                 throw new IOException JavaDoc ();
105             }
106         }
107         File JavaDoc file = new File JavaDoc (folder,relativeName);
108         return OutputFileObject.create (activeRoot,file);
109     }
110         
111         
112     
113     private int getActiveRoot (final javax.tools.FileObject file) throws IOException JavaDoc {
114         if (this.scp.entries().size() == 1) {
115             return 0;
116         }
117         Iterator JavaDoc<ClassPath.Entry> it = this.scp.entries().iterator();
118         for (int i = 0; it.hasNext(); i++) {
119             URL JavaDoc rootUrl = it.next().getURL();
120             if (isParentOf(rootUrl, file.toUri().toURL())) {
121                 return i;
122             }
123         }
124         return -1;
125     }
126     
127     private boolean isParentOf (URL JavaDoc folder, final URL JavaDoc file) throws IOException JavaDoc {
128         assert folder != null && file != null;
129         return file.toExternalForm().startsWith(folder.toExternalForm());
130     }
131     
132     private int getActiveRoot (String JavaDoc baseName) {
133         if (this.scp.entries().size() == 1) {
134             return 0;
135         }
136         String JavaDoc name, parent = null;
137     int index = baseName.lastIndexOf('/'); //NOI18N
138
if (index<0) {
139             name = baseName;
140     }
141     else {
142             parent = baseName.substring(0, index);
143             name = baseName.substring(index+1);
144     }
145         index = name.indexOf('$'); //NOI18N
146
if (index > 0) {
147         name = name.substring(0,index);
148     }
149         Iterator JavaDoc<ClassPath.Entry> it = this.scp.entries().iterator();
150         for (int i=0; it.hasNext(); i++) {
151             FileObject root = it.next().getRoot();
152             if (root != null) {
153                 FileObject parentFile = root.getFileObject(parent);
154                 if (parentFile != null) {
155                     if (parentFile.getFileObject(name, FileObjects.JAVA) != null) {
156                         return i;
157                     }
158                 }
159             }
160         }
161     return -1;
162     }
163     
164     private static boolean debug (String JavaDoc message) {
165         if (debug) {
166             Logger.getLogger("global").log(Level.INFO, message);
167         }
168         return true;
169     }
170     
171 }
172
Popular Tags