home *** CD-ROM | disk | FTP | other *** search
Java Source | 2003-05-20 | 6.4 KB | 246 lines |
- package nl.pcactive.bean;
-
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.sql.Connection;
- import java.sql.Statement;
-
- import nl.pcactive.db.Jdbc;
-
- /**
- * <p>Description: GEDCOM 2 DATABASE</p>
- * <p>Copyright: Copyright (c) 2002</p>
- * <p>Company: Pc-Active</p>
- * @author Benny Lootens
- * @version 2.0
- */
- public class GedcomBean {
-
- private int factor;
- private IndividualBean indiv;
- private FamilyBean family;
- private int type;
- private int individuals;
- private int families;
- private final int BIRT = 0;
- private final int DEAT = 1;
- private final int BAPM = 2;
- private final int BURI = 3;
- private final int MARR = 4;
-
- public GedcomBean(String as[]) {
- factor = 10;
- indiv = new IndividualBean();
- family = new FamilyBean();
- type = 0;
- individuals = 0;
- families = 0;
- }
-
- public void run() {
- long l = System.currentTimeMillis();
-
- try {
-
- System.out.println(
- "Ged2DB: GEDCOM To Database Converter\n"
- + "_____________________________________\n");
-
- // Jdbc.init();
-
- File file = new File(Jdbc.getGedcom());
-
- if (!file.exists()) {
- System.out.println(
- "GEDCOM " + Jdbc.getGedcom() + " does not exist.");
- System.exit(-1);
- }
-
- System.out.println("Creating tables...");
- Jdbc.createDatabase();
-
- System.out.println("Parsing file...");
- parse();
-
- } catch (Exception e) {
- e.printStackTrace(System.err);
- } finally {
- long l1 = System.currentTimeMillis();
- System.out.println("\nStatistics...");
- System.out.println("\tInvididuals: " + individuals);
- System.out.println("\tFamilies: " + families);
- System.out.println("\tTime: " + (l1 - l) + " milliseconds");
- }
- }
-
- private void parse() {
-
- Connection con = null;
- Statement stmt = null;
-
- try {
- con = Jdbc.openConnection();
- stmt = con.createStatement();
-
- BufferedReader bufferedreader =
- new BufferedReader(new FileReader(Jdbc.getGedcom()));
- System.out.print("\t|");
- String s;
-
- while ((s = bufferedreader.readLine()) != null) {
- for (s = s.trim(); s.endsWith("INDI") || s.endsWith("FAM");) {
- if (s.endsWith("INDI")) {
- s = parseIndividual(bufferedreader, s, stmt);
- individuals++;
- }
- if (s.endsWith("FAM")) {
- s = parseFamily(bufferedreader, s, stmt);
- families++;
- }
- if ((individuals + families) % factor == 0) {
- System.out.print("=");
- }
- }
-
- }
- System.out.println("|");
- bufferedreader.close();
- } catch (Exception e) {
- e.printStackTrace(System.err);
- } finally {
- Jdbc.close(stmt);
- Jdbc.close(con);
- }
- }
-
- private String parseIndividual(
- BufferedReader bufferedreader,
- String s,
- Statement stmt)
- throws Exception {
-
- indiv.clear();
- indiv.setId(s.substring(4, s.lastIndexOf('@')));
- while ((s = bufferedreader.readLine()) != null) {
- if (s.startsWith("0")) {
- break;
- }
- if (s.startsWith("1 NAME")) {
- s = s.substring(7).trim().replace('\'', '`');
- // int i = 0;
- // int j = 0;
- // for(int k = 0; k < s.length(); k++)
- // {
- // char c = s.charAt(k);
- // if(c == ' ' || c == '/')
- // {
- // switch(i)
- // {
- // case 0: // '\0'
- // indiv.setFirst(s.substring(0, k));
- // j = k + 1;
- // i = c != ' ' ? 2 : 1;
- // break;
- //
- // case 1: // '\001'
- // indiv.setMiddle(indiv.getMiddle() + " " + s.substring(j, k));
- // j = k + 1;
- // i = c != ' ' ? 2 : 1;
- // break;
- //
- // case 2: // '\002'
- // indiv.setLast(indiv.getLast() + " " + s.substring(j, k));
- // j = k + 1;
- // break;
- // }
- // }
- // }
- String lastname =
- s.substring(s.indexOf("/") + 1, s.lastIndexOf("/"));
- String firstname = s.substring(s.lastIndexOf("/") + 1);
-
- indiv.setFirst(firstname.trim());
- //indiv.setMiddle(indiv.getMiddle().trim());
- indiv.setLast(lastname.trim());
- } else if (s.startsWith("1 SEX")) {
- indiv.setSex(s.substring(6));
- } else if (s.startsWith("1 NOTE")) {
- indiv.setNotes(s.substring(7).replace('\'', '`'));
- } else if (s.startsWith("2 CONT")) {
- if (s.length() > 7)
- indiv.setNotes(
- indiv.getNotes()
- + "\n"
- + s.substring(7).replace('\'', '`'));
- } else if (s.startsWith("2 CONC")) {
- if (s.length() > 7) indiv.setNotes(
- indiv.getNotes() + s.substring(7).replace('\'', '`'));
- } else if (s.startsWith("1 BIRT")) { type = 0;
- } else if (s.startsWith("1 BAPM")) { type = 2;
- } else if (s.startsWith("1 DEAT")) { type = 1;
- } else if (s.startsWith("1 BURI")) { type = 3;
- } else if (s.startsWith("2 DATE")) {
- switch (type) {
- case 0 : indiv.getBirth().setDate(s.substring(7)); break;
- case 2 : indiv.getBaptism().setDate(s.substring(7)); break;
- case 1 : indiv.getDeath().setDate(s.substring(7)); break;
- case 3 : indiv.getBurried().setDate(s.substring(7)); break;
- }
- } else if (s.startsWith("2 PLAC")) {
- s = s.substring(7).replace('\'', '`');
- switch (type) {
- case 0 : // '\0'
- indiv.getBirth().setPlace(s);
- break;
-
- case 2 : // '\002'
- indiv.getBaptism().setPlace(s);
- break;
-
- case 1 : // '\001'
- indiv.getDeath().setPlace(s);
- break;
-
- case 3 : // '\003'
- indiv.getBurried().setPlace(s);
- break;
- }
- }
- }
- Jdbc.addIndividual(indiv, stmt);
- return s;
- }
-
- private String parseFamily(
- BufferedReader bufferedreader,
- String s,
- Statement stmt)
- throws Exception {
- family.clear();
- family.setId(s.substring(4, s.lastIndexOf('@')));
-
- while ((s = bufferedreader.readLine()) != null) {
- if (s.startsWith("0")) {
- break;
- }
- if (s.startsWith("1 HUSB")) {
- family.setHusband(s.substring(9, s.lastIndexOf('@')));
- } else if (s.startsWith("1 WIFE")) {
- family.setWife(s.substring(9, s.lastIndexOf('@')));
- } else if (s.startsWith("1 CHIL")) {
- String s1 = s.substring(9, s.lastIndexOf('@'));
- Jdbc.addChild(family.getId(), s1, stmt);
- } else if (s.startsWith("2 DATE")) {
- family.getMarried().setDate(s.substring(7));
- } else if (s.startsWith("2 PLAC")) {
- family.getMarried().setPlace(s.substring(7).replace('\'', '`'));
- }
- }
-
- Jdbc.addFamily(family, stmt);
- return s;
- }
-
- }
-