home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Java / Java.zip / jload18.zip / localstore.java < prev    next >
Text File  |  2000-04-13  |  4KB  |  188 lines

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. /* localstore */
  5. public class localstore
  6. {
  7.  protected String arg[];
  8.  
  9.  private Hashtable dirs;
  10.  
  11. public localstore(String argv[])
  12. {
  13.   if(argv==null) arg=new String[0];
  14.    else
  15.    arg=argv;
  16.  
  17.  // static init
  18.  init(arg);
  19.  localinit();
  20. }
  21.  
  22. protected void localinit()
  23. {
  24.  if(arg.length==0) 
  25.   {
  26.     arg=new String[1];
  27.     arg[0]="data"+File.separator;
  28.   } else if(!arg[0].endsWith(File.separator))
  29.          arg[0]+=File.separator;
  30.   dirs=new Hashtable();
  31. }
  32.  
  33.  public String toString()
  34.  {
  35.   return "SIMPLE localstore";
  36.  }
  37.  
  38.  public boolean isReadOnly()
  39.  {
  40.   return false;
  41.  }
  42.  
  43.  public void close()
  44.  {
  45.   if(dirs==null) return;
  46.   // System.out.println("[debug] closing localstore");
  47.   Enumeration en=dirs.elements();
  48.   while(en.hasMoreElements())
  49.   {
  50.    ((localdir)en.nextElement()).close();
  51.   }
  52.   dirs=null;
  53. }  
  54.  
  55.  public localurl getURL(String URL) throws java.net.MalformedURLException
  56.  {
  57.   String parsed[]=parseURL(URL);
  58.   StringBuffer ld=new StringBuffer(80);
  59.   // System.out.println("Parsed: host="+parsed[0]+" port="+parsed[1]+" dir="+parsed[2]+" file="+parsed[3]+" proto="+parsed[4]+" auth="+parsed[5]);
  60.   ld.append(arg[0]);
  61.   /* host */
  62.   ld.append(parsed[0]);
  63.   /* port */
  64.   if(parsed[1]!=null) {
  65.                          ld.append('_');
  66.                          ld.append(parsed[1]);
  67.               }
  68.   /* protocol */
  69.   if(parsed[4]!=null)
  70.                       {
  71.                  ld.append('^');
  72.              ld.append(parsed[4]);
  73.                   }
  74.  /* directory */
  75.  if(File.separatorChar!='/') parsed[2].replace('/',File.separatorChar);
  76.  ld.append(parsed[2]);
  77.   
  78.  return getLocaldir(ld.toString()).getObject(parsed[3]);
  79.  }
  80.  
  81.  private localdir getLocaldir(String directory)
  82.  {
  83.   synchronized (dirs)
  84.   {
  85.    localdir ld=(localdir)dirs.get(directory);
  86.    if(ld!=null) return ld;
  87.    ld=new localdir(directory);
  88.    dirs.put(directory,ld);
  89.    return ld;
  90.   }
  91.  }
  92.  
  93.  protected static void init(String arg[])
  94.  {
  95.    localdir.init(arg);
  96.    localurl.init(arg);
  97.  }
  98.  
  99. /*  prechrousta URL a vrati
  100. 0 - hostname
  101. 1 - port (if any) jinak null
  102. 2 - directory
  103. 3 - file including query string ("" if empty)
  104. 4 - protocol (null if http)
  105. 5 - Auth string (null if empty)
  106. */
  107.  
  108. final public static String[] parseURL(String url) throws java.net.MalformedURLException
  109. {
  110.   String[] res=new String[6];
  111.   res[3]=""; /* HashTable do not likes NULL */
  112.   int i,j,seven;
  113.   i=url.indexOf("://");
  114.   if(i==-1) throw new java.net.MalformedURLException(url);
  115.   res[4]=url.substring(0,i);
  116.   /* protocol check */
  117.   seven=i+3; /* '://' */
  118.   
  119.   if(seven==7)
  120.    {
  121.     char c;
  122.     c=url.charAt(0);
  123.     if(c=='h' || c=='H') res[4]=null; /* HTTP protocol */
  124.    }
  125.  
  126.   i=url.indexOf('/',seven);
  127.   if(i==-1) { url+='/'; i=url.length()-1;}
  128.  
  129.    /* check for losername/password */
  130.  try
  131.  {
  132.   int zav=url.indexOf('@',seven);
  133.   if(zav>0)
  134.    {
  135.     /*   @   / - ok     */ 
  136.     /*   /  @ - ignore  */
  137.     if( i>zav)
  138.     {
  139.      /* mame heslo */
  140.      res[5]=url.substring(seven,zav);
  141.      seven=zav+1;
  142.     }
  143.    }
  144.  }
  145.  catch (Exception e) 
  146.    { throw new java.net.MalformedURLException(url);} // So no need to check index out of bounds
  147.  
  148.   
  149.   j=url.indexOf(':',seven);
  150.   /* http://zero.dsd:434/ */
  151.   if(j!=-1 && j<i)  /* mame tu portname */
  152.                     {
  153.                       res[0]=url.substring(seven,j).toLowerCase();
  154.                       res[1]=url.substring(j+1,i);
  155.                       if(res[1].equals("80")) res[1]=null;
  156.                     }
  157.            else
  158.              {
  159.               res[0]=url.substring(seven,i).toLowerCase();
  160.              }
  161.  
  162.   j=url.length()-1;
  163.    byte v[];
  164.    v=new byte[j+1];
  165.    url.getBytes(i,j+1,v,i);
  166.    // getChars(i,j,v,0);
  167.    loop1:for(int zz=i;zz<=j;zz++)
  168.     {
  169.     switch(v[zz])
  170.     {
  171.     case 0x3b: // ;
  172.     case 0x3a: // :
  173.     case 0x3d: // =
  174.     case 0x3f: // ?
  175.               j=zz;break loop1;
  176.     case 0x23: // #
  177.      url=url.substring(0,zz);break loop1;
  178.     }
  179.     }
  180.    j=url.lastIndexOf('/',j);
  181.  
  182.   res[2]=url.substring(i,j+1); // adresar
  183.   if(j+1!=url.length()) res[3]=url.substring(j+1);
  184.   
  185.   return res;  
  186. }
  187.