1 19 20 package org.netbeans.modules.web.project.ui.wizards; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.InputStreamReader ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Collection ; 28 import java.util.Enumeration ; 29 import java.util.List ; 30 import java.util.StringTokenizer ; 31 import org.openide.ErrorManager; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileStateInvalidException; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.util.Enumerations; 36 37 public final class FileSearchUtility { 38 39 40 private FileSearchUtility() { 41 } 42 43 51 public static Enumeration getChildrenToDepth(final FileObject root, final int depth, final boolean onlyWritables) { 52 class WithChildren implements Enumerations.Processor { 53 private int rootDepth; 54 public WithChildren(final int rootDepth) { 55 this.rootDepth = rootDepth; 56 } 57 public Object process(final Object obj, final Collection toAdd) { 58 FileObject fo = (FileObject)obj; 59 if (!onlyWritables || (onlyWritables && fo.canWrite())) { 60 if (fo.isFolder() && (getDepth(fo) - rootDepth) < depth) { 61 toAdd.addAll(Arrays.asList(fo.getChildren())); 62 } 63 } 64 return fo; 65 } 66 } 67 68 return Enumerations.queue( 69 Enumerations.array(root.getChildren()), 70 new WithChildren(getDepth(root)) 71 ); 72 } 73 74 static FileObject guessDocBase (FileObject dir) { 75 Enumeration ch = getChildrenToDepth(dir, 3, true); 76 while (ch.hasMoreElements ()) { 77 FileObject f = (FileObject) ch.nextElement (); 78 if (f.isFolder () && f.getName ().equals ("WEB-INF")) { 79 final FileObject webXmlFO = f.getFileObject ("web.xml"); 80 if (webXmlFO != null && webXmlFO.isData()) { 81 return f.getParent(); 82 } 83 } 84 } 85 return null; 86 } 87 88 static FileObject guessLibrariesFolder (FileObject dir) { 89 FileObject docBase = guessDocBase (dir); 90 if (docBase != null) { 91 FileObject lib = docBase.getFileObject ("WEB-INF/lib"); if (lib != null) { 93 return lib; 94 } 95 } 96 Enumeration ch = getChildrenToDepth(dir, 3, true); 97 while (ch.hasMoreElements ()) { 98 FileObject f = (FileObject) ch.nextElement (); 99 if (f.getExt ().equals ("jar")) { return f.getParent (); 101 } 102 } 103 return null; 104 } 105 106 static FileObject[] guessJavaRoots(final FileObject dir) { 107 List foundRoots = new ArrayList (); 108 if (null == dir) 109 return null; 110 Enumeration ch = FileSearchUtility.getChildrenToDepth(dir, 10, true); try { 112 while (ch.hasMoreElements () && foundRoots.isEmpty()) { 114 FileObject f = (FileObject) ch.nextElement (); 115 if (f.getExt().equals("java") && !f.isFolder()) { String pckg = guessPackageName(f); 117 String pkgPath = f.getParent().getPath(); 118 if (pckg != null && pkgPath.endsWith(pckg.replace('.', '/'))) { 119 String rootName = pkgPath.substring(0, pkgPath.length() - pckg.length()); 120 FileObject fr = f.getFileSystem().findResource(rootName); 121 if (!foundRoots.contains(fr)) { 122 foundRoots.add(fr); 123 } 124 } 125 } 126 } 127 } catch (FileStateInvalidException fsie) { 128 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, fsie); 129 } 130 if (foundRoots.size() == 0) { 131 FileObject docBase = guessDocBase (dir); 132 if (docBase != null) { 133 FileObject classes = docBase.getFileObject ("WEB-INF/classes"); if (classes != null) { 135 foundRoots.add(classes); 136 } 137 } 138 } 139 140 if (foundRoots.size() == 0) { 141 return null; 142 } else { 143 FileObject[] resultArr = new FileObject[foundRoots.size()]; 144 for (int i = 0; i < foundRoots.size(); i++) { 145 resultArr[i] = (FileObject) foundRoots.get(i); 146 } 147 return resultArr; 148 } 149 } 150 151 static File [] guessJavaRootsAsFiles(final FileObject dir) { 152 FileObject[] rootsFOs = guessJavaRoots(dir); 153 if (rootsFOs == null) { 154 return new File [0]; 155 } 156 File [] resultArr = new File [rootsFOs.length]; 157 for (int i = 0; i < resultArr.length; i++) { 158 resultArr[i] = FileUtil.toFile(rootsFOs[i]); 159 } 160 return resultArr; 161 } 162 163 private static String guessPackageName(final FileObject f) { 164 java.io.Reader r = null; 165 try { 166 r = new BufferedReader (new InputStreamReader (f.getInputStream(), "utf-8")); StringBuffer sb = new StringBuffer (); 168 final char[] buffer = new char[4096]; 169 int len; 170 171 for (;;) { 172 len = r.read(buffer); 173 if (len == -1) { break; } 174 sb.append(buffer, 0, len); 175 } 176 int idx = sb.indexOf("package"); if (idx >= 0) { 178 int idx2 = sb.indexOf(";", idx); if (idx2 >= 0) { 180 return sb.substring(idx + "package".length(), idx2).trim(); 181 } 182 } 183 } catch (java.io.IOException ioe) { 184 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 185 } finally { 186 try { if (r != null) { r.close(); }} catch (java.io.IOException ioe) { ; } 188 } 189 return ""; } 192 193 static boolean containsWebInf(FileObject dir) { 194 Enumeration ch = getChildrenToDepth(dir, 3, true); 195 while (ch.hasMoreElements ()) { 196 FileObject f = (FileObject) ch.nextElement (); 197 if (f.isFolder()) 198 if (f.getName().equals("WEB-INF")) return true; 200 } 201 return false; 202 } 203 204 private static int getDepth(final FileObject fo) { 205 String path = FileUtil.toFile(fo).getAbsolutePath(); 206 StringTokenizer toker = new StringTokenizer (path, File.separator); 207 return toker.countTokens(); 208 } 209 } 210 | Popular Tags |