FileDocCategorySizeDatePackage
FlatFile.javaAPI DocExample11109Tue Dec 08 01:21:00 GMT 1998DataManagement.FlatFile

FlatFile.java


package DataManagement.FlatFile;

import ProblemDomain.*;
import java.util.*;
import java.io.*;

/**@note The FlatFile object is a simple object to hande persistent data 
storage to a flat file, storing the records in another format 
would only require another object with the behaviour that it 
needs to store in the new format.  In this way it could for 
instance be linked to a database etc...*/
public class FlatFile {

/**@note A temporary store for the Families that this object reads in 
from file.*/
  Hashtable families = null;
/**@note A temporary store for the Individuals that this object reads in 
from file.*/  
  Hashtable people = null;

/**@note The last line that has been read from the file.*/
  String lastLine = new String();
/**@note A counter for the number of Individuals read in.*/
  int individualCount = 0;
/**@note A counter for the number of Families read in.*/  
  int familyCount = 0;
/**@note To detect if the reading in of a record has come to an end 
without reading in the end of record field.*/
  int specialEnd = 0;
/**@note A link back to its calling object so that it can directly pass
back messages to the object.*/
  HumanInterface.FamilyFrame target;

/**@note The constructor just sets up the temporary storage hashtables for 
Individuals and Families.*/
  public FlatFile() {
    families = new Hashtable();
    people = new Hashtable();
  }

  public void setTarget(HumanInterface.FamilyFrame frame) {
    target = frame;
  }

/**@note Returns the Families that have been read in.*/
  public Hashtable getFamily() {
    return families;
  }

/**@note Returns the Individuals that have been read in.*/
  public Hashtable getIndividual() {
    return people;
  }

/**@note Opens the file using the filename parameter.  Then works out 
whether or not it is reading a Individual record or a Family 
record.  Will keep reading from the file until it reaches the 'TRLR' field.*/
  public void readData(String filename) {
    String inText = " ";
    int individualCount = 0;
    int familyCount = 0;
    int occurances = 0;

    try {
      BufferedReader in = new BufferedReader(new FileReader(filename));
      while(!inText.equals("0 TRLR")) {
        if(specialEnd != 1)
          inText = in.readLine();
        else inText = lastLine;
        StringTokenizer st = new StringTokenizer(inText);
        occurances = 0;
        while(st.hasMoreTokens()) {
          String line = (String)st.nextElement();
          occurances++;
          if(line.equals("INDI") && occurances == 3) {
            specialEnd = 0;
            individualCount++;
/*            target.textArea.appendText("\nIndividal count: " + individualCount);
            if(individualCount == 115) {
              int i = 9;
            }*/
            createIndividual(in,inText);
          }
          if(line.equals("FAM") && occurances == 3) {
            specialEnd = 0;
            familyCount++;
//            target.textArea.appendText("\nFamily count: " + familyCount);
            createFamily(in,inText);
          }
        }
      }
      in.close();
    }
    catch(FileNotFoundException ex) {}
    catch(IOException exe) {}
  }

/**@note Create a Family record from the file until the end of record is 
come across. This is a complex method because it reads lines and strips 
them down into individual components to check what data has been read in,
it knows this by tokenizing the read in string and looking for specific
entries and to create the necessary data structures to create a complete Family.*/
  void createFamily(BufferedReader in, String line) {
    int endOfRecord = 0;
    String inline = new String();
    String verb = new String();
    int firstTime = 1;

    int refNo = 0;
    Individual husband = null;
    Individual wife = null;
    Marriage ceremony = null;
    String place = new String();
    Divorce decree = null;
    String bmonth = new String();
    String d = "";
    Family ff;
    Vector children = null;

    while(endOfRecord != 1) {
      try {
        inline = in.readLine();
      } catch(IOException ex) {}
      StringTokenizer st = new StringTokenizer(inline);
      st.nextElement();
      verb = (String)st.nextElement();
      if(verb.equals("_FRECT")) endOfRecord = 1;
      else {
        if(verb.equals("REFN")) {
          verb = (String)st.nextElement();
          verb = verb.substring(1,verb.length());
          refNo = Integer.parseInt(verb);
        }
        if(verb.equals("HUSB")) {
          verb = (String)st.nextElement();
          verb = verb.substring(1,verb.lastIndexOf('@'));
          verb = verb.substring(1,verb.length());
          int tempRefNo = Integer.parseInt(verb);
          husband = (Individual)people.get(new Integer(tempRefNo));
        }
        if(verb.equals("WIFE")) {
          verb = (String)st.nextElement();
          verb = verb.substring(1,verb.lastIndexOf('@'));
          verb = verb.substring(1,verb.length());
          int tempRefNo = Integer.parseInt(verb);
          wife = (Individual)people.get(new Integer(tempRefNo));
        }
        if(verb.equals("CHIL")) {
          if(children == null) children = new Vector();
          verb = (String)st.nextElement();
          verb = verb.substring(1,verb.lastIndexOf('@'));
          verb = verb.substring(1,verb.length());
          int tempRefNo = Integer.parseInt(verb);
          children.addElement(people.get(new Integer(tempRefNo)));
        }
        if(verb.equals("MARR")) {
          try {
            inline = in.readLine();
          }catch(IOException exex) {}
          StringTokenizer stt = new StringTokenizer(inline);

          if(stt.countTokens() < 5)
          {
            d = "?";
          }
          else {
            stt.nextElement();
            verb = (String)stt.nextElement();
            if(verb.equals("DATE")) {
              d = d + (String)stt.nextElement() + "/";
              d = d + (String)stt.nextElement() + "/";
              d = d + (String)stt.nextElement();
            }
            try {
              inline = in.readLine();
            }catch(IOException exxe) {}
            StringTokenizer sttr = new StringTokenizer(inline);
            sttr.nextElement();
            verb = (String)sttr.nextElement();
            if(verb.equals("PLAC")) {
              while(sttr.hasMoreTokens()) {
                place = place + sttr.nextElement() + " ";
              }
            }
          }
        }
      }
    }
    if(husband != null && wife != null) {
      ceremony = new Marriage(d,"places","notes");
      ff = new Family(refNo,husband,wife,ceremony,null);
      if(children != null) {
        Enumeration list = children.elements();
        while(list.hasMoreElements()) {
          ff.addChild((Individual)list.nextElement());
        }
      }
//      target.textArea.setText(ff.getDetails());
      families.put(new Integer(refNo),ff);
    }
  }

/**@note Create an Individuals record from the file until the end of record is 
come across. This is a complex method because it reads lines and strips 
them down into individual components to check what data has been read in,
it knows this by tokenizing the read in string and looking for specific
entries and to create the necessary data structures to create a complete Family.*/
  void createIndividual(BufferedReader in,String line) {
    String inline;
    String verb;
    int endOfRecord = 0;
    int firstTim = 1;
    int refNo = 0;
    String FamilyName = new String();
    String GivenNames = new String();
    String place = new String();
    String sex = new String();
    Birth birth = null;
    Death death = null;
    String lastIndex;
    String d = "";
    int indexFirst = 1;
    Individual record;

    inline = line;
//    if(people == null) people = new Hashtable();
    while(endOfRecord == 0) {
      StringTokenizer st = new StringTokenizer(inline);
      lastIndex = (String)st.nextElement();
      verb = (String)st.nextElement();
      if(verb.equals("_BLOB"))
      {
        endOfRecord = 1;
      }
      else {
        if(lastIndex.equals("0") && indexFirst == 0)
        {
          endOfRecord = 1;
          specialEnd = 1;
          lastLine = inline;
        }
        else {
        indexFirst = 0;
        if(verb.equals("REFN")) {
          verb = (String)st.nextElement();
          verb = verb.substring(1,verb.length());
          refNo = Integer.parseInt(verb);
        }
        if(verb.equals("BIRT")) {
          try {
            inline = in.readLine();
          }catch(IOException exex) {}
          StringTokenizer stt = new StringTokenizer(inline);

          if(stt.countTokens() < 5)
          {
            d = "?";
          }
          else
          {
            stt.nextElement();
            verb = (String)stt.nextElement();
            if(verb.equals("DATE")) {
              d = d + (String)stt.nextElement() + "/";
              d = d + (String)stt.nextElement() + "/";
              d = d + (String)stt.nextElement();
            }
          }
          try {
            inline = in.readLine();
          }catch(IOException exex) {}
          StringTokenizer sttr = new StringTokenizer(inline);
          sttr.nextElement();
          verb = (String)sttr.nextElement();
          if(verb.equals("PLAC")) place = (String)sttr.nextElement();
        }
        if(verb.equals("NAME")) {
          verb = (String)st.nextElement();
          while(!verb.startsWith("/") && st.hasMoreElements()) {
            if(firstTim == 1) {
              GivenNames = verb;
              firstTim = 0;
            }
            else
              GivenNames = GivenNames + " " + verb;
            if(st.hasMoreElements())
              verb = (String)st.nextElement();
          }
          if(verb.startsWith("/")) {
            if(verb.equals("//")) verb = "Unknown";
            else {
              verb = verb.substring(1,verb.length()-1);
              verb = new String(verb.toLowerCase());
              String tmp = verb.valueOf(verb.charAt(0));
              tmp = tmp.toUpperCase();
              verb = new String(tmp + verb.substring(1,verb.length()));
            }
          }
          FamilyName = verb;
        }
        if(verb.equals("SEX")) {
          verb = (String)st.nextElement();
          if(verb.equals("M")) sex = "M";
          else sex = "F";
        }
        try {
            inline = in.readLine();
        }catch(IOException ex) {}
      }
    }
    }
    if(GivenNames.length() == 0) GivenNames = "Unknown";
    birth = new Birth(d,place,"notes");
    record = new Individual(refNo,FamilyName,GivenNames,sex, birth,null);
//    individualDM.add(refNo,FamilyName,GivenNames,sex, birth,null);
    people.put(new Integer(refNo),record);
//    target.textArea.setText(record.getDetails());
  }

}