Back to index

wims  3.65+svn20090927
FileName.java
Go to the documentation of this file.
00001 package rene.util;
00002 
00003 import java.io.File;
00004 
00009 public class FileName
00010 {      static public int ChopLength=48; 
00011        static public String purefilename (String filename)
00012        {      char a[]=filename.toCharArray(); 
00013               int i=a.length-1; 
00014               char fs=File.separatorChar; 
00015               while (i>=0)
00016               {      if (a[i]==fs || a[i]=='/' || i==0)
00017                      {      if (i==0) i=-1; 
00018                             if (i<a.length-1)
00019                             {      int j=a.length-1; 
00020                                    while (j>i && a[j]!='.') j--; 
00021                                    if (j>i+1) return new String(a,i+1,j-i-1); 
00022                                    else return ""; 
00023                             }
00024                             else return ""; 
00025                      }
00026                      i--; 
00027               }
00028               return filename; 
00029        }
00030        static public String path (String filename)
00031        {      char a[]=filename.toCharArray(); 
00032               int i=a.length-1; 
00033               char fs=File.separatorChar; 
00034               while (i>0)
00035               {      if (a[i]==fs || a[i]=='/')
00036                      {      return new String(a,0,i); 
00037                      }
00038                      i--; 
00039               }
00040               return ""; 
00041        }
00042        static public String pathAndSeparator (String filename)
00043        {      char a[]=filename.toCharArray(); 
00044               int i=a.length-1; 
00045               char fs=File.separatorChar; 
00046               while (i>0)
00047               {      if (a[i]==fs || a[i]=='/')
00048                      {      return new String(a,0,i+1); 
00049                      }
00050                      i--; 
00051               }
00052               return ""; 
00053        }
00054        static public String filename (String filename)
00055        {      char a[]=filename.toCharArray(); 
00056               int i=a.length-1; 
00057               char fs=File.separatorChar; 
00058               while (i>0)
00059               {      if (a[i]==fs || a[i]=='/')
00060                      {      if (i+1<a.length) return new String(a,i+1,a.length-i-1); 
00061                             else return ""; 
00062                      }
00063                      i--; 
00064               }
00065               return filename; 
00066        }
00067        static public String extension (String filename)
00068        {      char a[]=filename.toCharArray(); 
00069               int i=a.length-1; 
00070               char fs=File.separatorChar; 
00071               while (i>0)
00072               {      if (a[i]=='.')
00073                      {      if (i+1<a.length) return new String(a,i+1,a.length-i-1); 
00074                             else return ""; 
00075                      }
00076                      if (a[i]==fs || a[i]=='/') break; 
00077                      i--; 
00078               }
00079               return ""; 
00080        }
00081        static public String chop (String filename, int chop)
00082               // chop the filename to 32 characters
00083        {      if (filename.length()>chop)
00084               {      filename="... "+filename.substring(filename.length()-chop); 
00085               }
00086               return filename; 
00087        }
00088        static public String chop (String filename)
00089        {      return chop(filename,ChopLength);
00090        }
00091        static public String chop (int start, String filename, int chop)
00092               // chop the filename.substring(start) to 32 characters
00093        {      if (filename.length()>start+chop)
00094               {      filename=filename.substring(0,start)+
00095                             " ... "+
00096                             filename.substring(filename.length()-chop); 
00097               }
00098               return filename; 
00099        }
00100        static public String chop (int start, String filename)
00101        {      return chop(start,filename,ChopLength);
00102        }
00103        static public String relative (String dir, String filename)
00104        {      dir=dir+System.getProperty("file.separator"); 
00105               if (filename.startsWith(dir))
00106               {      return filename.substring(dir.length()); 
00107               }
00108               else return filename; 
00109        }
00110        static public String canonical (String filename)
00111        {      File f=new File(filename); 
00112               try
00113               {      String s=f.getCanonicalPath();
00114                      if (s.length()>2 && s.charAt(1)==':')
00115                             s=s.substring(0,2).toLowerCase()+s.substring(2);
00116                      return s; 
00117               }
00118               catch (Exception e) {       return f.getAbsolutePath(); }
00119        }
00120        static public String toURL (String filename)
00121        {      int n=filename.indexOf(' ');
00122               if (n>=0)
00123                      return filename.substring(0,n)+"%20"+toURL(filename.substring(n+1));
00124               else
00125                      return filename;
00126        }      
00127        static boolean match (char filename[], int n, char filter[], int m)
00128        {      if (filter==null) return true;
00129               if (m>=filter.length) return n>=filename.length;
00130               if (n>=filename.length) return m==filter.length-1 && filter[m]=='*';
00131               if (filter[m]=='?')
00132               {      return match(filename,n+1,filter,m+1);
00133               }
00134               if (filter[m]=='*')
00135               {      if (m==filter.length-1) return true;
00136                      for (int i=n; i<filename.length; i++)
00137                      {      if (match(filename,i,filter,m+1)) return true;
00138                      }
00139                      return false;
00140               }
00141               if (filter[m]==filename[n]) return match(filename,n+1,filter,m+1);
00142               return false;
00143        }
00144        public static boolean match (String filename, String filter)
00145        {      char fn[]=filename.toCharArray(),f[]=filter.toCharArray();
00146               return match(fn,0,f,0);
00147        }
00148        public static void main (String args[])
00149        {      System.out.println("-"+toURL(" test test test ")+"-");
00150        }
00151 }