home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 5.1 KB | 215 lines |
- // JFSdirectory.java
- // A representation of one java directory, read from data in the following form
- //
- // DIRECTORY = { FILE '\n' }*
- //
- // FILE = Filename ':' MimeType ':' '-' ':' VERSION ':' Users { ':' USER }*
- //
- // FILE = Filename ':' MimeType ':' Versions { ':' VERSION }+
- // Users { ':' USER }+
- //
- // VERSION = Number '|' Creator '|' Time '|' Size
- //
- // USER = { User | Group } '|' 'r'opt 'w'opt 'p'opt
- //
- import java.io.*;
- import java.util.Vector;
-
- public class JFSdirectory
- {
- Vector files = new Vector();
-
- // Creates an empty directory, for adding to.
- JFSdirectory()
- {
- }
-
- // Creates a new directory, read from an input stream
- JFSdirectory(LineInputStream in) throws IOException
- {
- while(true) {
- String file;
- try file = in.gets();
- catch(EOFException e) break;
- files.addElement(parsefile(file));
- }
- }
-
- // output
- // Write this directory to an output stream
- void output(LineOutputStream out)
- {
- for(int i=0; i<files.size(); i++) {
- JFSfile f = (JFSfile)files.elementAt(i);
- StringJoiner fj = new StringJoiner(':');
- fj.add(f.name); fj.add(f.type);
- fj.add(f.multiversion ? String.valueOf(f.versions.size()):"-");
- for(int j=0; j<f.versions.size(); j++) {
- StringJoiner vj = new StringJoiner('|');
- JFSversion v = (JFSversion)f.versions.elementAt(j);
- vj.add(String.valueOf(v.number));
- vj.add(v.creator);
- vj.add(String.valueOf(v.time));
- vj.add(String.valueOf(v.size));
- fj.add(vj.toString());
- }
- fj.add(String.valueOf(f.users.size()));
- for(int k=0; k<f.users.size(); k++) {
- JFSfileuser u = (JFSfileuser)f.users.elementAt(k);
- StringJoiner uj = new StringJoiner('|');
- uj.add(u.name); uj.add(u.rwp);
- fj.add(uj.toString());
- }
- out.puts(fj.toString());
- }
- }
-
- // find
- // Find a file with the given name in this directory
- JFSfile find(String n)
- {
- for(int i=0; i<files.size(); i++)
- if (((JFSfile)files.elementAt(i)).name.equals(n))
- return (JFSfile)files.elementAt(i);
- return null;
- }
-
- // find
- // Find a numbered file in this directory
- JFSfile find(int n)
- {
- if (n<0 || n>=files.size())
- return null;
- else
- return (JFSfile)files.elementAt(n);
- }
-
- // size
- // Returns the number of files in this directory
- int size()
- {
- return files.size();
- }
-
- // add
- // Add one file to the directory
- void add(JFSfile fl)
- {
- files.addElement(fl);
- }
-
- // delete
- // Delete a file from a directory
- void delete(JFSfile fl)
- {
- files.removeElement(fl);
- }
-
- // delete
- // Delete a numbered file from a directory
- void delete(int n)
- {
- files.removeElementAt(n);
- }
-
- // replace
- // Replace a file entry with a new one, or add an new
- // entry if none exists
- void replace(JFSfile fl)
- {
- for(int i=0; i<files.size(); i++)
- if (((JFSfile)files.elementAt(i)).name.equals(fl.name)) {
- files.setElementAt(fl, i);
- return;
- }
- files.addElement(fl);
- }
-
- // parsefile
- // Extract file attributes from a string
- JFSfile parsefile(String f) throws IOException
- {
- StringSplitter tok = new StringSplitter(f,':');
- if (tok.countTokens() < 3)
- throw new IOException("JFSfile format error");
- JFSfile fl = new JFSfile();
- fl.name = tok.nextToken();
- fl.type = tok.nextToken();
- String ver = tok.nextToken();
- if (ver.equals("-")) {
- // no version tracking
- fl.multiversion = false;
- fl.versions.addElement(parseversion(tok.nextToken()));
- }
- else {
- // multiple versions
- fl.multiversion = true;
- for(int i=0; i<Integer.parseInt(ver); i++)
- fl.versions.addElement(parseversion(tok.nextToken()));
- }
- int users = Integer.parseInt(tok.nextToken());
- for(int i=0; i<users; i++)
- fl.users.addElement(parseuser(tok.nextToken()));
- return fl;
- }
-
- // parseversion
- // Parse a version of a file from a string
- JFSversion parseversion(String v) throws IOException
- {
- StringSplitter tok = new StringSplitter(v,'|');
- if (tok.countTokens() != 4)
- throw new IOException("JFSversion format error");
- JFSversion vr = new JFSversion();
- vr.number = Integer.parseInt(tok.nextToken());
- vr.creator = tok.nextToken();
- vr.time = Long.parseLong(tok.nextToken());
- vr.size = Long.parseLong(tok.nextToken());
- return vr;
- }
-
- // parseuser
- // Parse a fileuser from a string
- JFSfileuser parseuser(String u) throws IOException
- {
- StringSplitter tok = new StringSplitter(u,'|');
- if (tok.countTokens() != 2)
- throw new IOException("JFSfileuser format error");
- JFSfileuser fu = new JFSfileuser();
- fu.name = tok.nextToken();
- fu.rwp = tok.nextToken();
- return fu;
- }
- }
-
-
- // JFSfile
- // A storage class for information about a file
- class JFSfile
- {
- String name; // the JFS name of this file
- String type; // the mime type of this file
- boolean multiversion; // does this file have multiple versions
- Vector versions = new Vector(); // versions of this file
- Vector users = new Vector(); // users and groups with access
- }
-
- // JFSversion
- // A storage class for information about one version of a file
- class JFSversion
- {
- int number; // the version number
- String creator; // who created this version
- long time; // when was it last modified
- long size; // how big is it
- }
-
- // JFSfileuser
- // A storage class for a user or groups rights to a file
- class JFSfileuser
- {
- String name; // the user or group's name
- String rwp; // permissions for this file
- }
-
-