2 // This software is now distributed according to
3 // the Lesser Gnu Public License. Please see
4 // http://www.gnu.org/copyleft/lesser.txt for
8 package com.stevesoft.pat;
14 * This class is a different form of Regex designed to work more like the file
15 * matching utility of a Unix shell. It is implemented by some simple string
16 * transformations: <center> <table border=1>
18 * <td> FileRegex </td>
47 * </table> </center> Note that a FileRegex pattern always ends with the Regex
48 * pattern element "$". If you like to experiment, try making FileRegex's and
49 * then printing them out. The toString() method does a decompile of the pattern
50 * to a standard Regex. Here are some more complete examples: <center> <table
53 * <td> FileRegex </td>
57 * <td> .*\.java$ </td>
59 * <td>*.{java,html} </td>
60 * <td> .*\.(java|html)$ </td>
62 * <td> foo.[chC] </td>
63 * <td> foo.[chC]$ </td>
66 public class FileRegex extends Regex
68 // /** Build an unitialized FileRegex. */
74 // /** Build a FileRegex form String s. */
75 // public FileRegex(String s)
82 // * Compile a new pattern. Throws
84 // * @exception com.stevesoft.pat.RegSyntax
85 // * for nonsensical patterns like "[9-0]+" just as Regex
87 // * @see com.stevesoft.pat#compile(java.lang.String)
89 // public void compile(String s) throws RegSyntax
91 // String npat = toFileRegex(s);
92 // super.compile(npat);
93 // if (File.separatorChar == '\\') // MS-DOS
100 // * This is the method required by FileNameFilter. To get a listing of files in
101 // * the current directory ending in .java, do this:
104 // * File dot = new File(".");
106 // * FileRegex java_files = new FileRegex("*.java");
108 // * String[] file_list = dot.list(java_files);
111 // public boolean accept(File dir, String s)
113 // if (dirflag != EITHER)
115 // File f = new File(s);
116 // if (f.isDirectory() && dirflag == NONDIR)
120 // if (!f.isDirectory() && dirflag == DIR)
125 // return matchAt(s, 0);
130 // final static int EITHER = 0, DIR = 1, NONDIR = 2;
133 // * Provides an alternative to File.list -- this separates its argument
134 // * according to File.pathSeparator. To each path, it splits off a directory --
135 // * all characters up to and including the first instance of File.separator --
136 // * and a file pattern -- the part that comes after the directory. It then
137 // * produces a list of all the pattern matches on all the paths. Thus
138 // * "*.java:../*.java" would produce a list of all the java files in this
139 // * directory and in the ".." directory on a Unix machine. "*.java;..\\*.java"
140 // * would do the same thing on a Dos machine.
142 // public static String[] list(String f)
144 // return list(f, EITHER);
147 // static String[] list(String f, int df)
149 // // return list_(f,new FileRegex());
150 // StringTokenizer st = new StringTokenizer(f, File.pathSeparator);
151 // Vector v = new Vector();
152 // while (st.hasMoreTokens())
154 // String path = st.nextToken();
155 // list1(path, v, df, true);
157 // String[] sa = new String[v.size()];
162 // final static Regex root = new Regex(File.separatorChar == '/' ? "/$"
163 // : "(?:.:|)\\\\$");
165 // static void list1(String path, Vector v, int df, boolean rec)
167 // // if path looks like a/b/c/ or d:\ then add .
168 // if (root.matchAt(path, 0))
170 // v.addElement(path + ".");
173 // File f = new File(path);
174 // if (f.getParent() != null && rec)
176 // Vector v2 = new Vector();
177 // list1(f.getParent(), v2, DIR, true);
178 // for (int i = 0; i < v2.size(); i++)
180 // String path2 = ((String) v2.elementAt(i)) + File.separator
182 // list1(path2, v, df, false);
187 // File base = new File(path);
189 // String dir_s = base.getParent();
190 // if (dir_s == null)
194 // File dir = new File(dir_s);
196 // FileRegex fr = new FileRegex(base.getName());
197 // if (fr.isLiteral())
199 // v.addElement(dir_s + File.separator + base.getName());
203 // String[] sa = dir.list(fr);
208 // for (int i = 0; i < sa.length; i++)
210 // v.addElement(dir_s + File.separator + sa[i]);
216 // * This method takes a file regular expression, and translates it into the
217 // * type of pattern used by a normal Regex.
219 // public static String toFileRegex(String s)
221 // StrPos sp = new StrPos(s, 0);
222 // StringBuffer sb = new StringBuffer();
223 // if (sp.incMatch("{?e="))
225 // char e = sp.thisChar();
227 // if (sp.incMatch("}"))
229 // sb.append("(?e=" + e + ")^");
233 // sb.append("^(?e=");
240 // if (File.separatorChar == '\\')
244 // sb.append("\\\\");
246 // sp.dontMatch = false;
248 // if (sp.incMatch("?"))
252 // else if (sp.incMatch("."))
254 // sb.append(sp.esc);
257 // else if (sp.incMatch("{??"))
261 // // allow negative lookahead to work
263 // else if (sp.incMatch("{?!"))
267 // // allow positive lookahead to work
269 // else if (sp.incMatch("{?="))
274 // else if (sp.incMatch("{"))
279 // else if (sp.incMatch("}"))
284 // else if (ParenLvl != 0 && sp.incMatch(","))
288 // else if (sp.incMatch("*"))
294 // sb.append(sp.thisChar());
299 // return sb.toString();
302 // public boolean isLiteral()
304 // Pattern x = thePattern;
305 // while (x != null && !(x instanceof End))
307 // if (x instanceof oneChar)
311 // else if (x instanceof Skipped)