X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fcom%2Fstevesoft%2Fpat%2FFileRegex.java;h=37729eb4b9b6c54659fed314d8fe3c314a9a3fc7;hb=055eda0580313a9449ec41004f26e5b06ebdfc07;hp=f5ac4485a37102d9ceebf353743ab1185e09ad5d;hpb=c40cf903f740a72ab63dd1abc10fa33450ce660d;p=jalview.git diff --git a/src/com/stevesoft/pat/FileRegex.java b/src/com/stevesoft/pat/FileRegex.java index f5ac448..37729eb 100755 --- a/src/com/stevesoft/pat/FileRegex.java +++ b/src/com/stevesoft/pat/FileRegex.java @@ -6,6 +6,7 @@ // -- Happy Computing! // package com.stevesoft.pat; + import java.io.*; import java.util.*; @@ -40,176 +41,253 @@ import java.util.*; */ -public class FileRegex extends Regex { - /** Build an unitialized FileRegex. */ - public FileRegex() { dirflag=EITHER; } - /** Build a FileRegex form String s. */ - public FileRegex(String s) { - super(s); - dirflag = EITHER; +public class FileRegex + extends Regex +{ + /** Build an unitialized FileRegex. */ + public FileRegex() + { + dirflag = EITHER; + } + + /** Build a FileRegex form String s. */ + public FileRegex(String s) + { + super(s); + dirflag = EITHER; + } + + /** Compile a new pattern. + Throws @exception com.stevesoft.pat.RegSyntax for + nonsensical patterns like "[9-0]+" just as Regex does. + @see com.stevesoft.pat#compile(java.lang.String) + */ + public void compile(String s) + throws RegSyntax + { + String npat = toFileRegex(s); + super.compile(npat); + if (File.separatorChar == '\\') // MS-DOS + { + ignoreCase = true; } - /** Compile a new pattern. - Throws @exception com.stevesoft.pat.RegSyntax for - nonsensical patterns like "[9-0]+" just as Regex does. - @see com.stevesoft.pat#compile(java.lang.String) - */ - public void compile(String s) throws RegSyntax { - String npat = toFileRegex(s); - super.compile(npat); - if(File.separatorChar == '\\') // MS-DOS - ignoreCase = true; + } + + /** This is the method required by FileNameFilter. + To get a listing of files in the current directory + ending in .java, do this: +
+      File dot = new File(".");
+      FileRegex java_files = new FileRegex("*.java");
+      String[] file_list = dot.list(java_files);
+      
+ */ + public boolean accept(File dir, String s) + { + if (dirflag != EITHER) + { + File f = new File(s); + if (f.isDirectory() && dirflag == NONDIR) + { + return false; + } + if (!f.isDirectory() && dirflag == DIR) + { + return false; + } } - /** This is the method required by FileNameFilter. - To get a listing of files in the current directory - ending in .java, do this: -
-        File dot = new File(".");
-        FileRegex java_files = new FileRegex("*.java");
-        String[] file_list = dot.list(java_files);
-        
- */ - public boolean accept(File dir,String s) { - if(dirflag != EITHER) { - File f = new File(s); - if(f.isDirectory() && dirflag == NONDIR) - return false; - if(!f.isDirectory() && dirflag == DIR) - return false; - } - return matchAt(s,0); + return matchAt(s, 0); + } + + int dirflag = 0; + final static int EITHER = 0, DIR = 1, NONDIR = 2; + + /** Provides an alternative to File.list -- this + separates its argument according to File.pathSeparator. + To each path, it splits off a directory -- all characters + up to and including the first instance of File.separator -- + and a file pattern -- the part that comes after the directory. + It then produces a list of all the pattern matches on all + the paths. Thus "*.java:../*.java" would produce a list of + all the java files in this directory and in the ".." directory + on a Unix machine. "*.java;..\\*.java" would do the same thing + on a Dos machine. */ + public static String[] list(String f) + { + return list(f, EITHER); + } + + static String[] list(String f, int df) + { + //return list_(f,new FileRegex()); + StringTokenizer st = new StringTokenizer(f, File.pathSeparator); + Vector v = new Vector(); + while (st.hasMoreTokens()) + { + String path = st.nextToken(); + list1(path, v, df, true); } - int dirflag = 0; - final static int EITHER=0,DIR=1,NONDIR=2; + String[] sa = new String[v.size()]; + v.copyInto(sa); + return sa; + } - /** Provides an alternative to File.list -- this - separates its argument according to File.pathSeparator. - To each path, it splits off a directory -- all characters - up to and including the first instance of File.separator -- - and a file pattern -- the part that comes after the directory. - It then produces a list of all the pattern matches on all - the paths. Thus "*.java:../*.java" would produce a list of - all the java files in this directory and in the ".." directory - on a Unix machine. "*.java;..\\*.java" would do the same thing - on a Dos machine. */ - public static String[] list(String f) { - return list(f,EITHER); + final static Regex root = new Regex(File.separatorChar == '/' ? + "/$" : "(?:.:|)\\\\$"); + static void list1(String path, Vector v, int df, boolean rec) + { + // if path looks like a/b/c/ or d:\ then add . + if (root.matchAt(path, 0)) + { + v.addElement(path + "."); + return; } - static String[] list(String f,int df) { - //return list_(f,new FileRegex()); - StringTokenizer st = new StringTokenizer(f,File.pathSeparator); - Vector v = new Vector(); - while(st.hasMoreTokens()) { - String path = st.nextToken(); - list1(path,v,df,true); - } - String[] sa = new String[v.size()]; - v.copyInto(sa); - return sa; + File f = new File(path); + if (f.getParent() != null && rec) + { + Vector v2 = new Vector(); + list1(f.getParent(), v2, DIR, true); + for (int i = 0; i < v2.size(); i++) + { + String path2 = ( (String) v2.elementAt(i)) + + File.separator + f.getName(); + list1(path2, v, df, false); + } } - final static Regex root=new Regex(File.separatorChar=='/' ? - "/$" : "(?:.:|)\\\\$"); - static void list1(String path,Vector v,int df,boolean rec) { - // if path looks like a/b/c/ or d:\ then add . - if(root.matchAt(path,0)) { - v.addElement(path+"."); - return; - } - File f = new File(path); - if(f.getParent() != null && rec) { - Vector v2 = new Vector(); - list1(f.getParent(),v2,DIR,true); - for(int i=0;i