KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > ui > wizards > FileSearchUtility


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.j2ee.clientproject.ui.wizards;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33 import org.netbeans.modules.j2ee.clientproject.AppClientProvider;
34 import org.openide.ErrorManager;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileStateInvalidException;
37 import org.openide.filesystems.FileUtil;
38 import org.openide.util.Enumerations;
39
40 final class FileSearchUtility {
41     
42     /** Creates a new instance of FileSearchUtility. */
43     private FileSearchUtility() {
44     }
45         
46    /** Recursively enumerate all children of this folder to some specified depth.
47     * All direct children are listed; then children of direct subfolders; and so on.
48     *
49     * @param root the starting directory
50     * @param depth the search limit
51     * @param onlyWritables only recurse into writable directories
52     * @return enumeration of type <code>FileObject</code>
53     */

54     static Enumeration JavaDoc getChildrenToDepth(final FileObject root, final int depth, final boolean onlyWritables) {
55         class WithChildren implements Enumerations.Processor {
56             private final int rootDepth;
57             public WithChildren(final int rootDepth) {
58                 this.rootDepth = rootDepth;
59             }
60             @SuppressWarnings JavaDoc("unchecked")
61             public Object JavaDoc process(final Object JavaDoc obj, final Collection JavaDoc toAdd) {
62                 FileObject fo = (FileObject)obj;
63                 if (!onlyWritables || (onlyWritables && fo.canWrite())) {
64                     if (fo.isFolder() && (getDepth(fo) - rootDepth) < depth) {
65                         toAdd.addAll(Arrays.asList(fo.getChildren()));
66                     }
67                 }
68                 return fo;
69             }
70         }
71
72         return Enumerations.queue(
73             Enumerations.array(root.getChildren()),
74             new WithChildren(getDepth(root))
75         );
76     }
77
78     static FileObject[] guessJavaRoots(final FileObject dir) {
79         List JavaDoc<FileObject> foundRoots = new ArrayList JavaDoc<FileObject>();
80         if (null == dir)
81             return null;
82         Enumeration JavaDoc ch = FileSearchUtility.getChildrenToDepth(dir, 10, true); // .getChildren(true);
83
try {
84             // digging through 10 levels exhaustively is WAY TOO EXPENSIVE
85
while (ch.hasMoreElements () && foundRoots.isEmpty()) {
86                 FileObject f = (FileObject) ch.nextElement ();
87                 if (f.getExt().equals("java") && !f.isFolder()) { //NOI18N
88
String JavaDoc pckg = guessPackageName(f);
89                     String JavaDoc pkgPath = f.getParent().getPath();
90                     if (pckg != null && pkgPath.endsWith(pckg.replace('.', '/'))) {
91                         String JavaDoc rootName = pkgPath.substring(0, pkgPath.length() - pckg.length());
92                         FileObject fr = f.getFileSystem().findResource(rootName);
93                         if (!foundRoots.contains(fr)) {
94                             foundRoots.add(fr);
95                         }
96                     }
97                 }
98             }
99         } catch (FileStateInvalidException fsie) {
100             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, fsie);
101         }
102         if (foundRoots.size() == 0) {
103             return null;
104         } else {
105             FileObject[] resultArr = new FileObject[foundRoots.size()];
106             for (int i = 0; i < foundRoots.size(); i++) {
107                 resultArr[i] = foundRoots.get(i);
108             }
109             return resultArr;
110         }
111     }
112     
113     static FileObject guessConfigFilesPath(final FileObject dir) {
114         if (null == dir)
115             return null;
116         Enumeration JavaDoc ch = FileSearchUtility.getChildrenToDepth(dir, 3, true); //getChildren(true);
117
try {
118             while (ch.hasMoreElements()) {
119                 FileObject f = (FileObject) ch.nextElement();
120                 if (f.getNameExt().equals(AppClientProvider.FILE_DD)) {
121                     String JavaDoc rootName = f.getParent().getPath();
122                     return f.getFileSystem().findResource(rootName);
123                 }
124             }
125         } catch (FileStateInvalidException fsie) {
126             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, fsie);
127         }
128         return null;
129     }
130
131
132     static File JavaDoc[] guessJavaRootsAsFiles(final FileObject dir) {
133         FileObject[] rootsFOs = guessJavaRoots(dir);
134         if (rootsFOs == null) {
135             return new File JavaDoc[0];
136         }
137         File JavaDoc[] resultArr = new File JavaDoc[rootsFOs.length];
138         for (int i = 0; i < resultArr.length; i++) {
139             resultArr[i] = FileUtil.toFile(rootsFOs[i]);
140         }
141         return resultArr;
142     }
143
144     private static String JavaDoc guessPackageName(final FileObject f) {
145         Reader JavaDoc r = null;
146         try {
147             r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(f.getInputStream(), "utf-8")); // NOI18N
148
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
149             final char[] buffer = new char[4096];
150             int len;
151
152             for (;;) {
153                 len = r.read(buffer);
154                 if (len == -1) { break; }
155                 sb.append(buffer, 0, len);
156             }
157             int idx = sb.indexOf("package"); // NOI18N
158
if (idx >= 0) {
159                 int idx2 = sb.indexOf(";", idx); // NOI18N
160
if (idx2 >= 0) {
161                     return sb.substring(idx + "package".length(), idx2).trim(); // NOI18N
162
}
163             }
164         } catch (IOException JavaDoc ioe) {
165             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
166         } finally {
167             try {
168                 if (r != null) {
169                     r.close();
170                 }
171             } catch (IOException JavaDoc ioe) {
172                 // ignore this
173
}
174         }
175         // AB: fix for #56160: assume the class is in the default package
176
return "";
177     }
178
179     static FileObject guessLibrariesFolder(final FileObject dir) {
180         if (dir != null) {
181             FileObject lib = dir.getFileObject("lib"); //NOI18N
182
if (lib != null) {
183                 return lib;
184             }
185         }
186         Enumeration JavaDoc ch = FileSearchUtility.getChildrenToDepth(dir, 3, true);
187         while (ch.hasMoreElements()) {
188             FileObject f = (FileObject) ch.nextElement();
189             if (f.getExt().equals("jar")) { //NOI18N
190
return f.getParent();
191             }
192         }
193         return null;
194     }
195
196     private static int getDepth(final FileObject fo) {
197         String JavaDoc path = FileUtil.toFile(fo).getAbsolutePath();
198         StringTokenizer JavaDoc toker = new StringTokenizer JavaDoc(path, File.separator);
199         return toker.countTokens();
200     }
201 }
202
Popular Tags