home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / intface / pca / special4 / software / eclipse.project / WEB-INF / classes / nl / pcactive / bean / GedcomBean.java < prev    next >
Encoding:
Java Source  |  2003-05-20  |  6.4 KB  |  246 lines

  1. package nl.pcactive.bean;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.sql.Connection;
  7. import java.sql.Statement;
  8.  
  9. import nl.pcactive.db.Jdbc;
  10.  
  11. /**
  12.  * <p>Description: GEDCOM 2 DATABASE</p>
  13.  * <p>Copyright: Copyright (c) 2002</p>
  14.  * <p>Company: Pc-Active</p>
  15.  * @author Benny Lootens
  16.  * @version 2.0
  17.  */
  18. public class GedcomBean {
  19.  
  20.     private int factor;
  21.     private IndividualBean indiv;
  22.     private FamilyBean family;
  23.     private int type;
  24.     private int individuals;
  25.     private int families;
  26.     private final int BIRT = 0;
  27.     private final int DEAT = 1;
  28.     private final int BAPM = 2;
  29.     private final int BURI = 3;
  30.     private final int MARR = 4;
  31.  
  32.     public GedcomBean(String as[]) {
  33.         factor = 10;
  34.         indiv = new IndividualBean();
  35.         family = new FamilyBean();
  36.         type = 0;
  37.         individuals = 0;
  38.         families = 0;
  39.     }
  40.  
  41.     public void run() {
  42.         long l = System.currentTimeMillis();
  43.  
  44.         try {
  45.  
  46.             System.out.println(
  47.                 "Ged2DB: GEDCOM To Database Converter\n"
  48.                     + "_____________________________________\n");
  49.  
  50.             //        Jdbc.init();
  51.  
  52.             File file = new File(Jdbc.getGedcom());
  53.  
  54.             if (!file.exists()) {
  55.                 System.out.println(
  56.                     "GEDCOM " + Jdbc.getGedcom() + " does not exist.");
  57.                 System.exit(-1);
  58.             }
  59.  
  60.             System.out.println("Creating tables...");
  61.             Jdbc.createDatabase();
  62.  
  63.             System.out.println("Parsing file...");
  64.             parse();
  65.  
  66.         } catch (Exception e) {
  67.             e.printStackTrace(System.err);
  68.         } finally {
  69.             long l1 = System.currentTimeMillis();
  70.             System.out.println("\nStatistics...");
  71.             System.out.println("\tInvididuals: " + individuals);
  72.             System.out.println("\tFamilies:    " + families);
  73.             System.out.println("\tTime:        " + (l1 - l) + " milliseconds");
  74.         }
  75.     }
  76.  
  77.     private void parse() {
  78.  
  79.         Connection con = null;
  80.         Statement stmt = null;
  81.  
  82.         try {
  83.             con = Jdbc.openConnection();
  84.             stmt = con.createStatement();
  85.  
  86.             BufferedReader bufferedreader =
  87.                 new BufferedReader(new FileReader(Jdbc.getGedcom()));
  88.             System.out.print("\t|");
  89.             String s;
  90.  
  91.             while ((s = bufferedreader.readLine()) != null) {
  92.                 for (s = s.trim(); s.endsWith("INDI") || s.endsWith("FAM");) {
  93.                     if (s.endsWith("INDI")) {
  94.                         s = parseIndividual(bufferedreader, s, stmt);
  95.                         individuals++;
  96.                     }
  97.                     if (s.endsWith("FAM")) {
  98.                         s = parseFamily(bufferedreader, s, stmt);
  99.                         families++;
  100.                     }
  101.                     if ((individuals + families) % factor == 0) {
  102.                         System.out.print("=");
  103.                     }
  104.                 }
  105.  
  106.             }
  107.             System.out.println("|");
  108.             bufferedreader.close();
  109.         } catch (Exception e) {
  110.             e.printStackTrace(System.err);
  111.         } finally {
  112.             Jdbc.close(stmt);
  113.             Jdbc.close(con);
  114.         }
  115.     }
  116.  
  117.     private String parseIndividual(
  118.         BufferedReader bufferedreader,
  119.         String s,
  120.         Statement stmt)
  121.         throws Exception {
  122.  
  123.         indiv.clear();
  124.         indiv.setId(s.substring(4, s.lastIndexOf('@')));
  125.         while ((s = bufferedreader.readLine()) != null) {
  126.             if (s.startsWith("0")) {
  127.                 break;
  128.             }
  129.             if (s.startsWith("1 NAME")) {
  130.                 s = s.substring(7).trim().replace('\'', '`');
  131.                 //                  int i = 0;
  132.                 //                  int j = 0;
  133.                 //                  for(int k = 0; k < s.length(); k++)
  134.                 //                  {
  135.                 //                      char c = s.charAt(k);
  136.                 //                      if(c == ' ' || c == '/')
  137.                 //                      {
  138.                 //                          switch(i)
  139.                 //                          {
  140.                 //                          case 0: // '\0'
  141.                 //                              indiv.setFirst(s.substring(0, k));
  142.                 //                              j = k + 1;
  143.                 //                              i = c != ' ' ? 2 : 1;
  144.                 //                              break;
  145.                 //
  146.                 //                          case 1: // '\001'
  147.                 //                              indiv.setMiddle(indiv.getMiddle() + " " + s.substring(j, k));
  148.                 //                              j = k + 1;
  149.                 //                              i = c != ' ' ? 2 : 1;
  150.                 //                              break;
  151.                 //
  152.                 //                          case 2: // '\002'
  153.                 //                              indiv.setLast(indiv.getLast() + " " + s.substring(j, k));
  154.                 //                              j = k + 1;
  155.                 //                              break;
  156.                 //                          }
  157.                 //                      }
  158.                 //                  }
  159.                 String lastname =
  160.                     s.substring(s.indexOf("/") + 1, s.lastIndexOf("/"));
  161.                 String firstname = s.substring(s.lastIndexOf("/") + 1);
  162.  
  163.                 indiv.setFirst(firstname.trim());
  164.                 //indiv.setMiddle(indiv.getMiddle().trim());
  165.                 indiv.setLast(lastname.trim());
  166.             } else if (s.startsWith("1 SEX")) {
  167.                 indiv.setSex(s.substring(6));
  168.             } else if (s.startsWith("1 NOTE")) {
  169.                 indiv.setNotes(s.substring(7).replace('\'', '`'));
  170.             } else if (s.startsWith("2 CONT")) {
  171.                 if (s.length() > 7)
  172.                     indiv.setNotes(
  173.                         indiv.getNotes()
  174.                             + "\n"
  175.                             + s.substring(7).replace('\'', '`'));
  176.             } else if (s.startsWith("2 CONC")) {
  177.                 if (s.length() > 7) indiv.setNotes(
  178.                   indiv.getNotes() + s.substring(7).replace('\'', '`'));
  179.             } else if (s.startsWith("1 BIRT")) { type = 0;
  180.             } else if (s.startsWith("1 BAPM")) { type = 2;
  181.             } else if (s.startsWith("1 DEAT")) { type = 1;
  182.             } else if (s.startsWith("1 BURI")) { type = 3;
  183.             } else if (s.startsWith("2 DATE")) {
  184.              switch (type) {
  185.                case 0 : indiv.getBirth().setDate(s.substring(7)); break;
  186.                case 2 : indiv.getBaptism().setDate(s.substring(7)); break;
  187.                case 1 : indiv.getDeath().setDate(s.substring(7)); break;
  188.                case 3 : indiv.getBurried().setDate(s.substring(7)); break;
  189.              }
  190.             } else if (s.startsWith("2 PLAC")) {
  191.                 s = s.substring(7).replace('\'', '`');
  192.                 switch (type) {
  193.                     case 0 : // '\0'
  194.                         indiv.getBirth().setPlace(s);
  195.                         break;
  196.  
  197.                     case 2 : // '\002'
  198.                         indiv.getBaptism().setPlace(s);
  199.                         break;
  200.  
  201.                     case 1 : // '\001'
  202.                         indiv.getDeath().setPlace(s);
  203.                         break;
  204.  
  205.                     case 3 : // '\003'
  206.                         indiv.getBurried().setPlace(s);
  207.                         break;
  208.                 }
  209.             }
  210.         }
  211.         Jdbc.addIndividual(indiv, stmt);
  212.         return s;
  213.     }
  214.  
  215.     private String parseFamily(
  216.         BufferedReader bufferedreader,
  217.         String s,
  218.         Statement stmt)
  219.         throws Exception {
  220.         family.clear();
  221.         family.setId(s.substring(4, s.lastIndexOf('@')));
  222.  
  223.         while ((s = bufferedreader.readLine()) != null) {
  224.             if (s.startsWith("0")) {
  225.                 break;
  226.             }
  227.             if (s.startsWith("1 HUSB")) {
  228.                 family.setHusband(s.substring(9, s.lastIndexOf('@')));
  229.             } else if (s.startsWith("1 WIFE")) {
  230.                 family.setWife(s.substring(9, s.lastIndexOf('@')));
  231.             } else if (s.startsWith("1 CHIL")) {
  232.                 String s1 = s.substring(9, s.lastIndexOf('@'));
  233.                 Jdbc.addChild(family.getId(), s1, stmt);
  234.             } else if (s.startsWith("2 DATE")) {
  235.                 family.getMarried().setDate(s.substring(7));
  236.             } else if (s.startsWith("2 PLAC")) {
  237.                 family.getMarried().setPlace(s.substring(7).replace('\'', '`'));
  238.             }
  239.         }
  240.  
  241.         Jdbc.addFamily(family, stmt);
  242.         return s;
  243.     }
  244.  
  245. }
  246.