home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Java / Java.zip / jload18.zip / mask.java < prev    next >
Text File  |  2000-04-16  |  20KB  |  628 lines

  1. import java.util.*;
  2. import java.net.*;
  3.  
  4. public final class mask {
  5.  
  6. /* load flags */
  7. //    act=    (what to do with this object?)
  8. //      reject,stop- don't download it (same as q=0.0)
  9. //       noparse   - don't parse it (same as depth=-1)
  10. //       fastclose - close connection after sending request (will not be parsed even if parseable) 
  11. //       close     - if object is unparseable (from content-type), close connection without
  12. //                     downloading more data
  13.  
  14. public static final byte ACT_LOAD=0;
  15. public static final byte ACT_REJECT=1;
  16. public static final byte ACT_NOPARSE=2;
  17. public static final byte ACT_FASTCLOSE=3;
  18. public static final byte ACT_CLOSE=4;
  19. public static final byte ACT_NOSAVE=5;
  20. public static final byte ACT_NOPROXY=6;
  21.  
  22. /* log flags */
  23. // log=
  24. //    none - no loging done
  25. //    queue - when element has been taken from queue
  26. //    load  - when trying to load
  27. //     parse - when parsing
  28. //    saving - when saving to disk
  29. //    err    - when loading error occurs
  30. //    fatalerr - when fatal loading error occurs
  31. //    urlonly  - log URL only
  32.  
  33. public static final short LOG_SERVERDEFAULT =-1;
  34. public static final short LOG_NONE   =0;
  35. public static final short LOG_QUEUE  =1;
  36. public static final short LOG_LOAD   =2;
  37. public static final short LOG_PARSE  =4;
  38. public static final short LOG_SAVE   =8;
  39. public static final short LOG_ERR    =16;
  40. public static final short LOG_FATALERR=32;
  41. public static final short LOG_IOERR   =64;
  42. public static final short LOG_STORED  =128;
  43. public static final short LOG_URLONLY =256; // MUST be last
  44. // aliases
  45. public static final short LOG_ALL=LOG_URLONLY-1;
  46. public static final short LOG_DEFAULT=LOG_LOAD|LOG_IOERR|LOG_ERR|LOG_FATALERR;
  47.  
  48. /* update status flags */
  49. //       norefresh - if object allready exists, don't try to load it
  50. //       forcereload - force cache to reloading object
  51. //       load,continue - load object in classic way (don't care about if old copy exists)
  52. //       update    - if object in cache is older than XXXX hours, start loading.
  53. //       forceupdate - forced load if older
  54.  
  55. public static final byte UPD_NOLIMIT=-1;
  56. public static final byte UPD_LOAD=0;
  57. public static final byte UPD_RELOAD=1;
  58. public static final byte UPD_UPDATE=2;
  59. public static final byte UPD_NOREFRESH=3;
  60. public static final byte UPD_FORCEUPDATE=4;
  61. public static final byte UPD_NOREPARSE=5;
  62.  
  63. // strip=none,null -  no URL striping before mask testing
  64. //      =auto
  65. //      =location,loc odstrani http://xxx/
  66. //      =server       odstrani http://xxx
  67.  
  68. public static final byte STRIP_AUTO=0;
  69. public static final byte STRIP_LOCATION=1;
  70. public static final byte STRIP_SERVER=2;
  71. public static final byte STRIP_NONE=3;
  72. public static final byte STRIP_DIRECTORY=4;
  73.  
  74.  
  75.  
  76. // size=xxxx bytes - only if object is bigger than xxxx bytes
  77. // size=known - only if we know size of object
  78. // size=unknown - only if we don't know size of object beeing downloaded
  79. // size=any     - don't care about it
  80.  
  81. public static final byte SIZE_NOLIMIT=-1;
  82. public static final byte SIZE_ANY=0;
  83. public static final byte SIZE_KNOWN=1;
  84. public static final byte SIZE_UNKNOWN=2;
  85. public static final byte SIZE_LIMITED=3;
  86.  
  87. // target=any,anyserver - any wwwserver in the world (including my own)
  88. //                         alias for world,known,site,me
  89. //        world  - any undefined location
  90. //        known  - any known location (but not me or myserver)
  91. //        server,site - alias for me,sameserver
  92. //        same,sameserver - file located on the same server (not including me)
  93. //        location, samelocation, me, this - located in Location URL
  94. //        subdir - located in subdirectory
  95. //
  96.  
  97. public static final byte      TARGET_ANY=127;
  98. /* official subtypes */
  99. public static final byte      TARGET_NONE=0;
  100. public static final byte      TARGET_WORLD=1;
  101. public static final byte      TARGET_KNOWN=2;
  102. public static final byte      TARGET_SERVER=4;
  103. public static final byte      TARGET_LOCATION=8;
  104. public static final byte      TARGET_SUBDIR=16; // subdir in location
  105. public static final byte      TARGET_DIRECTORY=32;
  106.  
  107. /* aliases */
  108.  
  109. public static final byte      TARGET_SITE=TARGET_SERVER|
  110.                                           TARGET_LOCATION|
  111.                                           TARGET_SUBDIR|
  112.                                           TARGET_DIRECTORY;
  113.  
  114. public static final byte      TARGET_ME  =
  115.                                           TARGET_LOCATION|
  116.                                           TARGET_SUBDIR|
  117.                                           TARGET_DIRECTORY;
  118.                                           
  119. /* http - ANY highest */
  120. /*  /   - SITE        */
  121. /* xxxx = location    */
  122. /* *    = cokoliv     */
  123.  
  124. /* target guess priority */
  125. public static final byte GUESS_TARGET_ANY=10;
  126. public static final byte GUESS_TARGET_SITE=8;
  127. public static final byte GUESS_TARGET_LOCATION=5;
  128. public static final byte GUESS_TARGET_ANYWHERE=0;
  129.  
  130.                                           
  131. public byte action,update,strip,size,target;
  132. public short log;
  133. public int sizelimit,updatelimit; // -1 is no limit
  134.  
  135. public float q;
  136.  
  137. public static final byte DEPTH_NOCHANGE=-2;
  138.  
  139. public short depth;
  140.  
  141. /* content section */
  142.  
  143. private regexp contentmasks[]; /* regexp masks */
  144. private boolean contentok[]; /* true=normal, false=must NOT be matched */
  145. private boolean anycontent; /* true=OR, false=AND */
  146.  
  147. /* extensions */
  148.  
  149. private regexp extmasks[]; /* regexp masks */
  150. private boolean extok[]; /* true=normal, false=must NOT be matched */
  151. private boolean anyext; /* true=OR, false=AND */
  152.  
  153. /* urlmasks */
  154. private regexp urlmasks[]; /* regexp masks */
  155. private boolean urlsok[]; /* true=normal, false=must NOT be matched */
  156. private boolean anyurl; /* true=OR, false=AND */
  157.  
  158.  
  159. /* src masks */
  160. private regexp srcmasks[]; /* regexp masks */
  161. private boolean srcok[]; /* true=normal, false=must NOT be matched */
  162. private boolean anysrc;  /* true=OR, false=AND */
  163.  
  164. private final void systemdefaults()
  165. {
  166.  action=ACT_LOAD;
  167.  update=UPD_LOAD;
  168.  strip=STRIP_AUTO;
  169.  target=TARGET_NONE;
  170.  size=SIZE_ANY;
  171.  log=LOG_NONE; // SERVERDEFAULT;
  172.  sizelimit=SIZE_NOLIMIT;
  173.  updatelimit=UPD_NOLIMIT;
  174.  anycontent=anyext=anyurl=anysrc=true;
  175.  q=1.0f;
  176.  depth=DEPTH_NOCHANGE;
  177. }
  178.  
  179. public mask(options o)
  180. {
  181.  systemdefaults();
  182.  if(o==null) throw new NullPointerException("mask");
  183.  for(int i=o.parsed.size()-1;i>=0;i--)
  184.  {
  185.    try
  186.    {
  187.     String opt;
  188.     StringTokenizer st;
  189.     st=new StringTokenizer((String)o.parsed.elementAt(i));
  190.     opt=st.nextToken();
  191.     // System.out.println("line="+o.parsed.elementAt(i));
  192.     whl:while(true)
  193.     {
  194.      String s;
  195.      boolean ok;
  196.      s=st.nextToken();
  197.      ok=true;
  198.      if(s.length()==1 && s.charAt(0)=='!')
  199.       {
  200.        ok=false;
  201.        s=st.nextToken();
  202.       }
  203.       
  204.      if(opt.equals("q")) { q=Float.valueOf(s).floatValue();break whl;}
  205.      else if (opt.equals("url"))
  206.              { 
  207.                if(s.equals("*") || s.equals("any")) {urlmasks=null;urlsok=null;}
  208.                 else
  209.                {             
  210.                  urlmasks=util.addRegexpToArray(s, urlmasks);
  211.                  urlsok=util.addBooleanToArray(ok,urlsok);
  212.                }
  213.                s=st.nextToken();
  214.                if(s.equals(",")) anyurl=true; else anyurl=false;
  215.                continue;
  216.              }
  217.      else if (opt.equals("content") || opt.equals("ct"))
  218.              { 
  219.                if(s.equals("*") || s.equals("any")) {contentmasks=null;contentok=null;}
  220.                 else
  221.                {
  222.                  contentmasks=util.addRegexpToArray(s, contentmasks);
  223.                  contentok=util.addBooleanToArray(ok,contentok);
  224.                }
  225.                s=st.nextToken();
  226.                if(s.equals(",")) anycontent=true; else anycontent=false;
  227.                continue;
  228.              }
  229.      else if (opt.equals("ext"))
  230.              { 
  231.                if(s.equals("*") || s.equals("any")) {extmasks=null;extok=null;}
  232.                 else
  233.                {
  234.                 extmasks=util.addRegexpToArray(s, extmasks);
  235.                 extok=util.addBooleanToArray(ok,extok);
  236.                }
  237.                s=st.nextToken();
  238.                if(s.equals(",")) anyext=true; else anyext=false;
  239.                continue;
  240.              }
  241.  
  242.      else if (opt.equals("src"))
  243.              { 
  244.                if(s.equals("*") || s.equals("any")) {srcmasks=null;srcok=null;}
  245.                 else
  246.                {
  247.                 srcmasks=util.addRegexpToArray(s, srcmasks);
  248.                 srcok=util.addBooleanToArray(ok,srcok);
  249.                }
  250.                s=st.nextToken();
  251.                if(s.equals(",")) anysrc=true; else anysrc=false;
  252.                continue;
  253.              }
  254.  
  255.      else if (opt.equals("depth"))
  256.              { 
  257.                  try
  258.                   {
  259.                    depth=(short)Integer.valueOf(s).intValue();
  260.                   }
  261.                  catch (NumberFormatException n)
  262.                   {
  263.                    System.err.println("[CONFIG_ERROR] Bad depth : "+s);
  264.                   }
  265.                 
  266.                s=st.nextToken();
  267.                continue;
  268.              }
  269.              
  270.      else if (opt.equals("size"))
  271.              { 
  272.                if(s.equals("any")) { size=SIZE_ANY;sizelimit=SIZE_NOLIMIT;}
  273.                 else if (s.equals("known")) 
  274.                      if(ok==false) size=SIZE_UNKNOWN;
  275.                         else size=SIZE_KNOWN;
  276.                 else if (s.equals("unknown")) 
  277.                   if(ok==true) size=SIZE_UNKNOWN;
  278.                       else size=SIZE_KNOWN;
  279.                 else
  280.                  try
  281.                   {
  282.                    sizelimit=Integer.valueOf(s).intValue();
  283.                    if(size==SIZE_ANY) size=SIZE_LIMITED;
  284.                   }
  285.                  catch (NumberFormatException n)
  286.                   {
  287.                    System.err.println("[CONFIG_ERROR] Bad size : "+s);
  288.                   }
  289.                 
  290.                s=st.nextToken();
  291.                continue;
  292.              }
  293.  
  294.      else if (opt.equals("strip"))
  295.              { 
  296.                if(s.equals("none")) strip=STRIP_NONE;
  297.                 else if (s.equals("server")) strip=STRIP_SERVER;
  298.                 else if (s.equals("location")) strip=STRIP_LOCATION;
  299.                 else if (s.equals("auto")) strip=STRIP_AUTO;
  300.                 else if (s.equals("dir")) strip=STRIP_DIRECTORY;
  301.                 else
  302.                    System.err.println("[CONFIG_ERROR] Invalid parameter to strip option : "+s);
  303.                 
  304.                s=st.nextToken();
  305.                continue;
  306.              }
  307.  
  308.      else if (opt.equals("target"))
  309.              { 
  310.                if(s.equals("any")) target=TARGET_ANY;
  311.                 else if (s.equals("anyserver")) target=TARGET_ANY;
  312.                 else if (s.equals("world")) target|=TARGET_WORLD;
  313.                 else if (s.equals("known")) target|=TARGET_KNOWN;
  314.                 else if (s.equals("server")) target|=TARGET_SERVER;
  315.                 else if (s.equals("location")) target|=TARGET_LOCATION;
  316.                 else if (s.equals("directory")) target|=TARGET_DIRECTORY;
  317.                 else if (s.equals("dir")) target|=TARGET_DIRECTORY;
  318.                 else if (s.equals("subdir")) target|=TARGET_SUBDIR;
  319.                 
  320.                 
  321.         // aliases
  322.                 else if (s.equals("loc")) target|=TARGET_LOCATION;                
  323.                 else if (s.equals("me")) target|=TARGET_ME;
  324.                 else if (s.equals("site")) target|=TARGET_SITE;
  325.                                 
  326.                 
  327.                 else if (s.equals("auto")) target=0;
  328.                 else
  329.                    System.err.println("[CONFIG_ERROR] Invalid parameter to target option : "+s);
  330.                 
  331.                s=st.nextToken();
  332.                continue;
  333.              }
  334.              
  335.      else if (opt.equals("act"))
  336.              { 
  337.                if(s.equals("reject") || 
  338.                   s.equals("stop") || 
  339.                   (s.equals("load") && ok==false) ) 
  340.                                           { q=0.0f;action=ACT_REJECT;}
  341.                 else if (s.equals("noparse")) { depth=-1;action=ACT_NOPARSE;}
  342.                 else if (s.equals("fastclose")) action=ACT_FASTCLOSE;
  343.                 else if (s.equals("close")) action=ACT_CLOSE;
  344.                 else if (s.equals("load")) action=ACT_LOAD;
  345.                 else if (s.equals("nosave")) action=ACT_NOSAVE;
  346.                 else if (s.equals("direct") || 
  347.                   s.equals("noproxy")  ) 
  348.                                           action=ACT_NOPROXY;
  349.                 
  350.                 else
  351.                    System.err.println("[CONFIG_ERROR] Invalid parameter to act option : "+s);
  352.                 
  353.                s=st.nextToken();
  354.                continue;
  355.              }
  356.  
  357.      else if (opt.equals("log"))
  358.              { 
  359.                if((s.equals("none") || 
  360.                   s.equals("off")
  361.                   ) && ok==true)  
  362.                                              log=LOG_NONE;
  363.                 else if (s.equals("queue"))  log|=LOG_QUEUE;
  364.                 else if (s.equals("server")
  365.                ||s.equals("serverdefault")
  366.                 )                    log=LOG_SERVERDEFAULT;
  367.                 else if (s.equals("load"))   log|=LOG_LOAD;
  368.                 else if (s.equals("stored")
  369.                ||s.equals("saved")
  370.                ||s.equals("store")
  371.                ||s.equals("loaded")
  372.                )                     log|=LOG_STORED;
  373.                 else if (s.equals("parse"))  log|=LOG_PARSE;
  374.                 else if (s.equals("save"))   log|=LOG_SAVE;
  375.                 else if (s.equals("err")
  376.                ||s.equals("error")
  377.                )                     log|=LOG_ERR;
  378.                 else if (s.equals("all"))    log|=LOG_ALL;
  379.                 else if (s.equals("ioerr")
  380.                  || s.equals("io"))
  381.                                  log|=LOG_IOERR;
  382.                 else if (s.equals("fatalerr")
  383.                  || s.equals("fatal"))
  384.                                  log|=LOG_FATALERR;
  385.                 else if (s.equals("default"))  log=LOG_DEFAULT;
  386.                 else if (s.equals("url") || 
  387.                   s.equals("urlonly")  ||
  388.           s.equals("short")  
  389.           ) 
  390.              if (ok==true) log=(short)((log & LOG_ALL)|LOG_URLONLY);
  391.                     else   log&=LOG_ALL;
  392.                 
  393.                 else
  394.                    System.err.println("[CONFIG_ERROR] Invalid parameter to log option : "+s);
  395.                 
  396.                s=st.nextToken();
  397.                continue;
  398.              }
  399.      else if (opt.equals("upd"))
  400.              { 
  401.                if(s.equals("load")) update=UPD_LOAD;
  402.                 else if (s.equals("norefresh") ||
  403.                          s.equals("none")
  404.                         )                      update=UPD_NOREFRESH;
  405.                 else if (s.equals("reload") ||
  406.                          s.equals("force")  ||
  407.              s.equals("forceload") ||
  408.                          s.equals("forcereload") 
  409.                          )                      update=UPD_RELOAD;
  410.                 else if (s.equals("update")) update=UPD_UPDATE;
  411.                 else if (s.equals("forceupdate")) update=UPD_FORCEUPDATE;
  412.                 else if (s.equals("noreparse")) update=UPD_NOREPARSE;
  413.                 else
  414.                  try
  415.                   {
  416.                    updatelimit=Integer.valueOf(s).intValue();
  417.                    if(update!=UPD_UPDATE || update!=UPD_FORCEUPDATE ) 
  418.                      update=UPD_UPDATE;
  419.                   }
  420.                  catch (NumberFormatException n)
  421.                   {
  422.                    System.err.println("[CONFIG_ERROR] Bad update interval : "+s);
  423.                   }
  424.                 
  425.                s=st.nextToken();
  426.                continue;
  427.              }
  428.      else
  429.       {
  430.        System.err.println("[CONFIG_ERROR] Unknown mask option "+opt);
  431.        break;
  432.       }
  433.     }
  434.     
  435.    }
  436.    catch (NoSuchElementException ignore)
  437.    {}
  438.  
  439.  
  440.  }
  441.  guessTarget();
  442.  guessStrip();
  443. }
  444.  
  445. private final void guessTarget()
  446. {
  447.  if(target!=0) return;
  448.  if(urlmasks==null) { target=TARGET_ME;return;}
  449.  /* guess: */
  450.  byte ttarget=GUESS_TARGET_ANYWHERE;
  451.  
  452.  for(int i=urlmasks.length-1;i>=0;i--)
  453.  {
  454.   String s;
  455.   s=urlmasks[i].toString();
  456.   
  457.   if(s.indexOf("://")>0) 
  458.    {
  459.     if(ttarget<GUESS_TARGET_ANY)
  460.       ttarget=GUESS_TARGET_ANY;
  461.     break;
  462.    }
  463.  
  464.   if(s.startsWith("/")) 
  465.    {
  466.     if(ttarget<GUESS_TARGET_SITE)
  467.       ttarget=GUESS_TARGET_SITE;
  468.     continue;
  469.    }
  470.   
  471.   if(s.startsWith("*")) 
  472.    {
  473.     if(ttarget<GUESS_TARGET_ANYWHERE)
  474.       ttarget=GUESS_TARGET_ANYWHERE;
  475.     continue;
  476.    }
  477.  
  478.   if(ttarget<GUESS_TARGET_LOCATION)
  479.       ttarget=GUESS_TARGET_LOCATION;
  480.  
  481.  }
  482.  /* and setup..... */
  483.  switch(ttarget)
  484.  {
  485.   case GUESS_TARGET_ANYWHERE: 
  486.   case GUESS_TARGET_ANY:   
  487.             target=TARGET_ANY;break;
  488.   case GUESS_TARGET_SITE:
  489.             target=TARGET_SITE;break;
  490.   case GUESS_TARGET_LOCATION:
  491.                         target=TARGET_ME;break;
  492.   default: throw new IllegalArgumentException ("Screwed in guessTarget();");
  493.  }
  494.  
  495. }
  496.  
  497. private final void guessStrip()
  498. {
  499.  if( (strip==STRIP_LOCATION || strip==STRIP_DIRECTORY)
  500.      &&
  501.       (target & TARGET_ME)==0 )
  502.                           { strip=STRIP_SERVER;return;}
  503.  if(strip!=STRIP_AUTO) return;
  504.  if(urlmasks==null) { strip=STRIP_NONE;return;}
  505.  if( (target & TARGET_KNOWN)!=0 ||
  506.      (target & TARGET_WORLD)!=0 ) { strip=STRIP_NONE;return;}
  507.  if( (target & TARGET_SERVER)!=0 ) { strip=STRIP_SERVER;return;}
  508.  if( (target & TARGET_LOCATION)!=0 ) { strip=STRIP_LOCATION;return;}
  509.  strip=STRIP_DIRECTORY;
  510.  
  511. public final boolean hasContent()
  512. {
  513.  if(contentmasks==null) return false; else return true;
  514. }
  515.  
  516. public final static byte getTarget(String frombase,String fromdir,String locbase,String url)
  517. {
  518.   // int target=mask.getTarget(frombase,fromdir,url);
  519.   URL u;
  520.   try
  521.    {
  522.      u=new URL(url);
  523.  
  524.    }
  525.   catch (MalformedURLException badurl)
  526.    {
  527.     return TARGET_WORLD;
  528.    }
  529.   String urldir=util.getDirname(u.getFile()); 
  530.   
  531.   if(locbase.regionMatches(0,url,0,locbase.length()))
  532.     {
  533.       /* stejna location */
  534.       if(fromdir.equals(urldir)) return TARGET_DIRECTORY;
  535.       if(fromdir.regionMatches(0,urldir,0,fromdir.length()))
  536.        return TARGET_SUBDIR;
  537.       return TARGET_LOCATION;
  538.     }
  539.     
  540.   String urlbase=u.getProtocol()+"://"+u.getHost();
  541.   if(frombase.equals(urlbase))
  542.    {
  543.     
  544.     /* stejny server */
  545.     return TARGET_SERVER;
  546.    }
  547.   /* prohledama databazi known locations */
  548.   for(int i=loader.loc.length-1;i>=0;i--)
  549.    if(loader.loc[i].locbase.regionMatches(0,url,0,loader.loc[i].locbase.length()))
  550.     return TARGET_KNOWN;
  551.   return TARGET_WORLD;
  552. }
  553.  
  554. public final boolean match(String url,String ext,String src,byte target,String locbase)
  555. {
  556.  // System.out.println("url="+url+"\n\tsrc="+src+" mask.target="+this.target+" target="+target);
  557.  if( (target & this.target)==0) return false;
  558.  // System.out.println("target ok");
  559.  srcscan:while(true)
  560.  {
  561.    if(srcmasks!=null)
  562.     {
  563.       /* check SRC */
  564.       for(int i=0;i<srcmasks.length;i++)
  565.         // System.out.println(srcok[i]+" "+srcmasks[i]);
  566.         if(srcmasks[i].matches(src) == srcok[i])
  567.           if(anysrc) break srcscan;
  568.            else ;
  569.         else
  570.          if(!anysrc) return false;
  571.        
  572.       if(anysrc) return false;
  573.     }
  574.     break;
  575.  }
  576.  // System.out.println("src ok");
  577.  extscan:while(true)
  578.  {
  579.    if(extmasks!=null)
  580.     {
  581.       /* check EXT */
  582.       for(int i=0;i<extmasks.length;i++)
  583.         if(extmasks[i].matches(ext) == extok[i])
  584.           if(anyext) break extscan;
  585.            else ;
  586.         else
  587.          if(!anyext) return false;
  588.       if(anyext) return false;
  589.     }
  590.     break;
  591.  }
  592.  // System.out.println("ext ok");
  593.  urlscan:while(true)
  594.  {
  595.    if(urlmasks!=null)
  596.     {
  597.       String stripped=url;
  598.       switch(strip)
  599.       {
  600.         case STRIP_NONE:break;
  601.         case STRIP_SERVER:
  602.           int i=url.indexOf("://",0);
  603.           i=url.indexOf("/",i+3);
  604.           stripped=url.substring(i);
  605.           break;
  606.         case STRIP_LOCATION:
  607.           stripped=url.substring(locbase.length());
  608.           break;
  609.       }
  610.     
  611.  
  612.       for(int i=0;i<urlmasks.length;i++)
  613.         if(urlmasks[i].matches(stripped) == urlsok[i])
  614.           if(anyurl) break urlscan;
  615.            else ;
  616.         else
  617.          if(!anyurl) return false;
  618.       if(anyurl) return false;
  619.     }
  620.     break;
  621.  }
  622.  // System.out.println("ALL OK");
  623.  return true;
  624. }
  625.  
  626. }
  627.