1 19 20 package org.netbeans.modules.j2ee.ejbjarproject.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 final class FileSearchUtility { 38 39 40 private FileSearchUtility() { 41 } 42 43 51 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[] guessJavaRoots(final FileObject dir) { 75 List foundRoots = new ArrayList (); 76 if (null == dir) 77 return null; 78 Enumeration ch = FileSearchUtility.getChildrenToDepth(dir, 10, true); try { 80 while (ch.hasMoreElements () && foundRoots.isEmpty()) { 82 FileObject f = (FileObject) ch.nextElement (); 83 if (f.getExt().equals("java") && !f.isFolder()) { String pckg = guessPackageName(f); 85 String pkgPath = f.getParent().getPath(); 86 if (pckg != null && pkgPath.endsWith(pckg.replace('.', '/'))) { 87 String rootName = pkgPath.substring(0, pkgPath.length() - pckg.length()); 88 FileObject fr = f.getFileSystem().findResource(rootName); 89 if (!foundRoots.contains(fr)) { 90 foundRoots.add(fr); 91 } 92 } 93 } 94 } 95 } catch (FileStateInvalidException fsie) { 96 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, fsie); 97 } 98 if (foundRoots.size() == 0) { 99 return null; 100 } else { 101 FileObject[] resultArr = new FileObject[foundRoots.size()]; 102 for (int i = 0; i < foundRoots.size(); i++) { 103 resultArr[i] = (FileObject) foundRoots.get(i); 104 } 105 return resultArr; 106 } 107 } 108 109 static FileObject guessConfigFilesPath(final FileObject dir) { 110 if (null == dir) 111 return null; 112 Enumeration ch = FileSearchUtility.getChildrenToDepth(dir, 3, true); try { 114 while (ch.hasMoreElements()) { 115 FileObject f = (FileObject) ch.nextElement(); 116 if (f.getNameExt().equals("ejb-jar.xml")) { String rootName = f.getParent().getPath(); 118 return f.getFileSystem().findResource(rootName); 119 } 120 } 121 } catch (FileStateInvalidException fsie) { 122 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, fsie); 123 } 124 return null; 125 } 126 127 128 static File [] guessJavaRootsAsFiles(final FileObject dir) { 129 FileObject[] rootsFOs = guessJavaRoots(dir); 130 if (rootsFOs == null) { 131 return new File [0]; 132 } 133 File [] resultArr = new File [rootsFOs.length]; 134 for (int i = 0; i < resultArr.length; i++) { 135 resultArr[i] = FileUtil.toFile(rootsFOs[i]); 136 } 137 return resultArr; 138 } 139 140 private static String guessPackageName(final FileObject f) { 141 java.io.Reader r = null; 142 try { 143 r = new BufferedReader (new InputStreamReader (f.getInputStream(), "utf-8")); StringBuffer sb = new StringBuffer (); 145 final char[] buffer = new char[4096]; 146 int len; 147 148 for (;;) { 149 len = r.read(buffer); 150 if (len == -1) { break; } 151 sb.append(buffer, 0, len); 152 } 153 int idx = sb.indexOf("package"); if (idx >= 0) { 155 int idx2 = sb.indexOf(";", idx); if (idx2 >= 0) { 157 return sb.substring(idx + "package".length(), idx2).trim(); 158 } 159 } 160 } catch (java.io.IOException ioe) { 161 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 162 } finally { 163 try { if (r != null) { r.close(); }} catch (java.io.IOException ioe) { ; } 165 } 166 return ""; } 169 170 static FileObject guessLibrariesFolder(final FileObject dir) { 171 if (dir != null) { 172 FileObject lib = dir.getFileObject("lib"); if (lib != null) { 174 return lib; 175 } 176 } 177 Enumeration ch = FileSearchUtility.getChildrenToDepth(dir, 3, true); 178 while (ch.hasMoreElements()) { 179 FileObject f = (FileObject) ch.nextElement(); 180 if (f.getExt().equals("jar")) { return f.getParent(); 182 } 183 } 184 return null; 185 } 186 187 private static int getDepth(final FileObject fo) { 188 String path = FileUtil.toFile(fo).getAbsolutePath(); 189 StringTokenizer toker = new StringTokenizer (path, File.separator); 190 return toker.countTokens(); 191 } 192 } 193 | Popular Tags |