home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / pr8adpl7 / jfsdirectory.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  5.1 KB  |  215 lines

  1. // JFSdirectory.java
  2. // A representation of one java directory, read from data in the following form
  3. //
  4. //   DIRECTORY = { FILE '\n' }*
  5. //
  6. //   FILE = Filename ':' MimeType ':' '-' ':' VERSION ':' Users { ':' USER }*
  7. //
  8. //   FILE = Filename ':' MimeType ':' Versions { ':' VERSION }+ 
  9. //        Users { ':' USER }+
  10. //
  11. //   VERSION = Number '|' Creator '|' Time '|' Size
  12. //
  13. //   USER = { User | Group } '|' 'r'opt 'w'opt 'p'opt
  14. //
  15. import java.io.*;
  16. import java.util.Vector;
  17.  
  18. public class JFSdirectory
  19. {
  20.     Vector files = new Vector();
  21.  
  22.     // Creates an empty directory, for adding to.
  23.     JFSdirectory()
  24.     {
  25.     }
  26.  
  27.     // Creates a new directory, read from an input stream
  28.     JFSdirectory(LineInputStream in) throws IOException
  29.     {
  30.     while(true) {
  31.         String file;
  32.         try file = in.gets();
  33.         catch(EOFException e) break;
  34.         files.addElement(parsefile(file));
  35.         }
  36.     }
  37.  
  38.     // output
  39.     // Write this directory to an output stream
  40.     void output(LineOutputStream out)
  41.     {
  42.     for(int i=0; i<files.size(); i++) {
  43.         JFSfile f = (JFSfile)files.elementAt(i);
  44.         StringJoiner fj = new StringJoiner(':');
  45.         fj.add(f.name); fj.add(f.type);
  46.         fj.add(f.multiversion ? String.valueOf(f.versions.size()):"-");
  47.         for(int j=0; j<f.versions.size(); j++) {
  48.             StringJoiner vj = new StringJoiner('|');
  49.             JFSversion v = (JFSversion)f.versions.elementAt(j);
  50.             vj.add(String.valueOf(v.number));
  51.             vj.add(v.creator);
  52.             vj.add(String.valueOf(v.time));
  53.             vj.add(String.valueOf(v.size));
  54.             fj.add(vj.toString());
  55.             }
  56.         fj.add(String.valueOf(f.users.size()));
  57.         for(int k=0; k<f.users.size(); k++) {
  58.             JFSfileuser u = (JFSfileuser)f.users.elementAt(k);
  59.             StringJoiner uj = new StringJoiner('|');
  60.             uj.add(u.name); uj.add(u.rwp);
  61.             fj.add(uj.toString());
  62.             }
  63.         out.puts(fj.toString());
  64.         }
  65.     }
  66.  
  67.     // find
  68.     // Find a file with the given name in this directory
  69.     JFSfile find(String n)
  70.     {
  71.     for(int i=0; i<files.size(); i++)
  72.         if (((JFSfile)files.elementAt(i)).name.equals(n))
  73.             return (JFSfile)files.elementAt(i);
  74.     return null;
  75.     }
  76.  
  77.     // find
  78.     // Find a numbered file in this directory
  79.     JFSfile find(int n)
  80.     {
  81.     if (n<0 || n>=files.size())
  82.         return null;
  83.     else
  84.         return (JFSfile)files.elementAt(n);
  85.     }
  86.  
  87.     // size
  88.     // Returns the number of files in this directory
  89.     int size()
  90.     {
  91.     return files.size();
  92.     }
  93.  
  94.     // add
  95.     // Add one file to the directory
  96.     void add(JFSfile fl)
  97.     {
  98.     files.addElement(fl);
  99.     }
  100.  
  101.     // delete
  102.     // Delete a file from a directory
  103.     void delete(JFSfile fl)
  104.     {
  105.     files.removeElement(fl);
  106.     }
  107.  
  108.     // delete
  109.     // Delete a numbered file from a directory
  110.     void delete(int n)
  111.     {
  112.     files.removeElementAt(n);
  113.     }
  114.  
  115.     // replace
  116.     // Replace a file entry with a new one, or add an new
  117.     // entry if none exists
  118.     void replace(JFSfile fl)
  119.     {
  120.     for(int i=0; i<files.size(); i++)
  121.         if (((JFSfile)files.elementAt(i)).name.equals(fl.name)) {
  122.             files.setElementAt(fl, i);
  123.             return;
  124.             }
  125.     files.addElement(fl);
  126.     }
  127.  
  128.     // parsefile
  129.     // Extract file attributes from a string
  130.     JFSfile parsefile(String f) throws IOException 
  131.     {
  132.     StringSplitter tok = new StringSplitter(f,':');
  133.     if (tok.countTokens() < 3)
  134.         throw new IOException("JFSfile format error");
  135.     JFSfile fl = new JFSfile();
  136.     fl.name = tok.nextToken();
  137.     fl.type = tok.nextToken();
  138.     String ver = tok.nextToken();
  139.     if (ver.equals("-")) {
  140.         // no version tracking
  141.         fl.multiversion = false;
  142.         fl.versions.addElement(parseversion(tok.nextToken()));
  143.         }
  144.     else {
  145.         // multiple versions
  146.         fl.multiversion = true;
  147.         for(int i=0; i<Integer.parseInt(ver); i++)
  148.             fl.versions.addElement(parseversion(tok.nextToken()));
  149.         }
  150.     int users = Integer.parseInt(tok.nextToken());
  151.     for(int i=0; i<users; i++)
  152.         fl.users.addElement(parseuser(tok.nextToken()));
  153.     return fl;
  154.     }
  155.  
  156.     // parseversion
  157.     // Parse a version of a file from a string
  158.     JFSversion parseversion(String v) throws IOException
  159.     {
  160.     StringSplitter tok = new StringSplitter(v,'|');
  161.     if (tok.countTokens() != 4)
  162.         throw new IOException("JFSversion format error");
  163.     JFSversion vr = new JFSversion();
  164.     vr.number = Integer.parseInt(tok.nextToken());
  165.     vr.creator = tok.nextToken();
  166.     vr.time = Long.parseLong(tok.nextToken());
  167.     vr.size = Long.parseLong(tok.nextToken());
  168.     return vr;
  169.     }
  170.  
  171.     // parseuser
  172.     // Parse a fileuser from a string
  173.     JFSfileuser parseuser(String u) throws IOException
  174.     {
  175.     StringSplitter tok = new StringSplitter(u,'|');
  176.     if (tok.countTokens() != 2)
  177.         throw new IOException("JFSfileuser format error");
  178.     JFSfileuser fu = new JFSfileuser();
  179.     fu.name = tok.nextToken();
  180.     fu.rwp = tok.nextToken();
  181.     return fu;
  182.     }
  183. }
  184.  
  185.  
  186. // JFSfile
  187. // A storage class for information about a file
  188. class JFSfile
  189. {
  190.     String name;            // the JFS name of this file
  191.     String type;            // the mime type of this file
  192.     boolean multiversion;        // does this file have multiple versions
  193.     Vector versions = new Vector();    // versions of this file
  194.     Vector users = new Vector();    // users and groups with access
  195. }
  196.  
  197. // JFSversion
  198. // A storage class for information about one version of a file
  199. class JFSversion
  200. {
  201.     int number;            // the version number
  202.     String creator;            // who created this version
  203.     long time;            // when was it last modified
  204.     long size;            // how big is it
  205. }
  206.  
  207. // JFSfileuser
  208. // A storage class for a user or groups rights to a file
  209. class JFSfileuser
  210. {
  211.     String name;            // the user or group's name
  212.     String rwp;            // permissions for this file
  213. }
  214.  
  215.