00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package dochelper.util;
00011
00012 import java.io.BufferedReader;
00013 import java.io.File;
00014 import java.io.FileInputStream;
00015 import java.io.FileNotFoundException;
00016 import java.io.IOException;
00017 import java.io.InputStreamReader;
00018 import java.util.Stack;
00019
00041 public class FileUtils {
00042
00044 public FileUtils() {
00045 }
00046
00054 public static String globToRE(String glob) {
00055 final Object NEG = new Object();
00056 final Object GROUP = new Object();
00057 Stack<Object> state = new Stack<Object>();
00058
00059 StringBuffer buf = new StringBuffer();
00060 boolean backslash = false;
00061
00062 for (int i = 0;i < glob.length();i++) {
00063 char c = glob.charAt(i);
00064 if (backslash) {
00065 buf.append('\\');
00066 buf.append(c);
00067 backslash = false;
00068 continue;
00069 }
00070
00071 switch (c) {
00072 case '\\':
00073 backslash = true;
00074 break;
00075 case '?':
00076 buf.append('.');
00077 break;
00078 case '.':
00079 case '+':
00080 case '(':
00081 case ')':
00082 buf.append('\\');
00083 buf.append(c);
00084 break;
00085 case '*':
00086 buf.append(".*");
00087 break;
00088 case '|':
00089 if (backslash)
00090 buf.append("\\|");
00091 else
00092 buf.append('|');
00093 break;
00094 case '{':
00095 buf.append('(');
00096 if (i + 1 != glob.length() && glob.charAt(i + 1) == '!') {
00097 buf.append('?');
00098 state.push(NEG);
00099 }
00100 else
00101 state.push(GROUP);
00102 break;
00103 case ',':
00104 if (!state.isEmpty() && state.peek() == GROUP)
00105 buf.append('|');
00106 else
00107 buf.append(',');
00108 break;
00109 case '}':
00110 if (!state.isEmpty()) {
00111 buf.append(")");
00112 if (state.pop() == NEG)
00113 buf.append(".*");
00114 }
00115 else
00116 buf.append('}');
00117 break;
00118 default:
00119 buf.append(c);
00120 }
00121 }
00122
00123 return buf.toString();
00124 }
00125
00131 public static File[] _listDirectory(String path) {
00132
00133 File directory = new File(path);
00134 File[] list = directory.listFiles();
00135 if (list == null) {
00136 System.err.println("Error in listDirectory(3 param)");
00137 return null;
00138 }
00139
00140 return list;
00141 }
00142
00150 public static String FileToString(File file) throws FileNotFoundException, IOException {
00151
00152 String fileStr = "";
00153
00154
00155 FileInputStream fis = new FileInputStream(file);
00156 BufferedReader d = new BufferedReader(new InputStreamReader(fis));
00157 String record = null;
00158
00159 while ((record = d.readLine()) != null) {
00160 fileStr += record + "\n";
00161 }
00162
00163 return fileStr;
00164 }
00165 }