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

FlatFile

public class FlatFile extends Object
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...

Fields Summary
Hashtable
families
Hashtable
people
String
lastLine
int
individualCount
int
familyCount
int
specialEnd
HumanInterface.FamilyFrame
target
Constructors Summary
public FlatFile()

note
The constructor just sets up the temporary storage hashtables for Individuals and Families.


             
    
    families = new Hashtable();
    people = new Hashtable();
  
Methods Summary
voidcreateFamily(java.io.BufferedReader in, java.lang.String line)

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.

    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);
    }
  
voidcreateIndividual(java.io.BufferedReader in, java.lang.String line)

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.

    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());
  
public java.util.HashtablegetFamily()

note
Returns the Families that have been read in.

    return families;
  
public java.util.HashtablegetIndividual()

note
Returns the Individuals that have been read in.

    return people;
  
public voidreadData(java.lang.String filename)

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.

    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) {}
  
public voidsetTarget(HumanInterface.FamilyFrame frame)

    target = frame;