home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / smartcache / src / regexp.java < prev    next >
Text File  |  2001-01-26  |  4KB  |  162 lines

  1. /*
  2.  *  Smart Cache, http proxy cache server
  3.  *  Copyright (C) 1998, 1999 Radim Kolar 
  4.  *
  5.  *  Last change: Apr 14 2000
  6.  *
  7.  *    Smart Cache is Open Source Software; you may redistribute it
  8.  *  and/or modify it under the terms of the GNU General Public
  9.  *  License as published by the Free Software Foundation; either
  10.  *  version 2, or (at your option) any later version.
  11.  *
  12.  *    This program distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15.  *  General Public License for more details.
  16.  *
  17.  *    A copy of the GNU General Public License is available as
  18.  *  /usr/doc/copyright/GPL in the Debian GNU/Linux distribution or on
  19.  *  the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
  20.  *  can also obtain it by writing to the Free Software Foundation,
  21.  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22.  */
  23.  
  24. public final class regexp
  25. {
  26.  public boolean ignoreCase; 
  27.  
  28.  private String elements[];
  29.  
  30.  private String prefix;
  31.  private String suffix;
  32.  
  33.  private int prefixlen;
  34.  private int suffixlen;
  35.  
  36.  private boolean exact;
  37.  
  38.  
  39.  regexp(String exp, boolean ign)
  40.  {
  41.   ignoreCase=ign;
  42.   exact=false;
  43.   int i;
  44.   
  45.   if(exp.indexOf('*',0)==-1) { exact=true; 
  46.                                prefix=exp; 
  47.                                prefixlen=exp.length(); 
  48.                                if(ign) prefix=prefix.toLowerCase();
  49.                                return;
  50.                              }
  51.   i=1;
  52.   if(!exp.startsWith("*"))
  53.    {
  54.     /* mame prefix */
  55.     prefix=exp.substring(0,exp.indexOf('*',0));
  56.     if(ign) prefix=prefix.toLowerCase();
  57.     prefixlen=prefix.length();
  58.     i=prefixlen+1;
  59.    }
  60.    int pos;
  61.    String part;
  62.    while(true)
  63.    {
  64.     pos=exp.indexOf('*',i);
  65.     if(pos==-1)
  66.       {
  67.        /* mame suffix */
  68.        if(i==exp.length()) return;
  69.        suffix=exp.substring(i);
  70.        if(ign) suffix=suffix.toLowerCase();
  71.        suffixlen=suffix.length();
  72.        return;
  73.       }
  74.       
  75.     part=exp.substring(i,pos);
  76.     if(ign) part=part.toLowerCase();
  77.     if(!part.equals(""))
  78.      {
  79.       /* pridame part do pole */
  80.       
  81.        if(elements==null) elements=new String[1];
  82.         else
  83.        {
  84.         String tmp[]=new String[elements.length+1];
  85.         System.arraycopy(elements,0,tmp,0,elements.length);
  86.         elements=tmp;
  87.        } 
  88.       elements[elements.length-1]=part;
  89.      }/* pridani */
  90.      
  91.     i=pos+1;
  92.     if(i>=exp.length()) break;
  93.     
  94.    
  95.    }/* while */
  96.  
  97.  }
  98.  
  99.  public String dump()
  100.  {
  101.   String result="exact="+exact+" prefix="+prefix+"\nsuffix="+suffix+"\nelements=";
  102.   if (elements==null) return result+="null";
  103.   for(int i=0;i<elements.length;i++)
  104.    result+=elements[i]+",";
  105.  
  106.   return result;
  107.  }
  108.  
  109.  final public String toString()
  110.  {
  111.   if(exact==true) return prefix;
  112.   String result;
  113.   if(prefix==null) result="*";
  114.    else
  115.     result=prefix+"*";
  116.   /* pridat elementy */
  117.   if(elements!=null)
  118.    for(int i=0;i<elements.length;i++)
  119.      result+=elements[i]+"*";
  120.   
  121.   /* suffix */
  122.   if(suffix!=null) result+=suffix;
  123.   
  124.   return result;
  125.  }
  126.  final public boolean matches(String str)
  127.  {
  128.   int pos;
  129.   pos=0;
  130.   if(ignoreCase) str=str.toLowerCase();
  131.   if(exact) return str.equals(prefix);
  132.   
  133.   if(prefix!=null)
  134.    if(!str.startsWith(prefix)) return false;
  135.        else
  136.      pos=prefixlen;
  137.   if(elements!=null)
  138.    {
  139.     int j=elements.length;
  140.     for(int i=0;i<j;i++)
  141.      {
  142.      if( (pos=str.indexOf(elements[i],pos))==-1 ) return false;
  143.      pos+=elements[i].length();
  144.      }
  145.    }
  146.   if(suffix==null) return true; 
  147.   if(str.length()-pos<suffixlen) return false;
  148.   
  149.   return str.endsWith(suffix);
  150.  }
  151.  
  152.  /*
  153.  public static void main(String argv[])
  154.  {
  155.   if (argv.length<2) return;
  156.   regexp r=new regexp(argv[0],false);
  157.   System.out.println(r);
  158.   System.out.println("Matches : "+argv[1]+" : "+r.matches(argv[1]));
  159.  }
  160.  */
  161. }
  162.