001package edu.pdx.cs410J.family;
002
003import java.io.*;
004import java.text.*;
005import java.util.*;
006
007/**
008 * This program makes note of a marriage between two people in a
009 * family tree. 
010 *
011 * @author David Whitlock
012 * @since Fall 2000
013 */
014public class NoteMarriage {
015
016  private static PrintWriter err = new PrintWriter(System.err, true);
017
018  private static int husbandId = 0;
019  private static int wifeId = 0;
020  private static Date date;
021  private static String location;
022  private static String fileName;
023  private static boolean useXml = false;
024
025  /**
026   * Displays a help message on how to use this program.
027   */
028  private static void usage(String s) {
029    err.println("\n** " + s + "\n");
030
031    err.println("Makes note of a marriage between two people");
032    err.println("usage: java NoteMarriage [options] <args>");
033    err.println("  args are (in this order):");
034    err.println("    file                File containing family info");
035    err.println("    husbandId           The id of the husband");
036    err.println("    wifeId              The id of the wife");
037
038    err.println("  options are (options may appear in any order):");
039    err.println("    -date date          Date marriage took place");
040    err.println("    -location string    Where marriage took place");
041    err.println("    -xml                File in XML format");
042
043    err.println("\n");
044    System.exit(1);
045  }  
046
047  /**
048   * Parses the command line.
049   *
050   * @return An error message if the command line was not parsed
051   *         sucessfully
052   */
053  private static String parseCommandLine(String[] args) {
054    DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
055
056    for (int i = 0; i < args.length; i++) {
057      if (args[i].equals("-xml")) {
058        useXml = true;
059
060      } else if (args[i].equals("-date")) {
061        if(++i >= args.length) {
062          return "Missing marriage date";
063        }
064
065        // A date will take up three arguments
066        StringBuffer sb = new StringBuffer();
067        for(int j = 0; j < 3; j++) {
068          if (i >= args.length) {
069            return "Malformatted date of birth: " + sb;
070
071          } else {
072            sb.append(args[i] + " ");
073          }
074
075          i++;
076        }
077        i--;
078
079        try {
080          date = df.parse(sb.toString().trim());
081
082        } catch (ParseException ex) {
083          return "Malformatted marriage date: " + args[i];
084        }
085
086      } else if (args[i].equals("-location")) {
087        if(++i >= args.length) {
088          return "Missing marriage location";
089        }
090
091        location = args[i];
092
093      } else if (fileName == null) {
094        fileName = args[i];
095
096      } else if (husbandId == 0) {
097        try {
098          husbandId = Integer.parseInt(args[i]);
099
100        } catch (NumberFormatException ex) {
101          return "Malformatted husband id: " + args[i];
102        }
103
104        if (husbandId < 1) {
105          return "Illegal husband id value: " + husbandId;
106        }
107
108      } else if (wifeId == 0) {
109        try {
110          wifeId = Integer.parseInt(args[i]);
111
112        } catch (NumberFormatException ex) {
113          return "Malformatted wife id: " + args[i];
114        }
115
116        if(wifeId < 1) {
117          return "Illegal wife id value: " + husbandId;
118        }
119
120      } else {
121        return("Unknown command line option: " + args[i]);
122      }
123    }
124
125    // Make some additional checks
126    if (husbandId == 0) {
127      return "No husband id specified";
128
129    } else if (wifeId == 0) {
130      return "No wife id specified";
131
132    } else if (fileName == null) {
133      return "No file specified";
134    }
135
136    // No errors
137    return null;
138  }
139
140  /**
141   * Main program that parses the command line and creates a marriage.
142   */
143  public static void main(String[] args) {
144    // Parse the command line
145    String message = parseCommandLine(args);
146
147    if (message != null) {
148      usage(message);
149    }
150
151    FamilyTree tree = null;
152
153    // If the data file exists, read it in
154    File file = new File(fileName);
155    if (file.exists()) {
156      Parser parser = null;
157      if (useXml) {
158        // File in XML format
159        try {
160          parser = new XmlParser(file);
161
162        } catch (FileNotFoundException ex) {
163          err.println("** Could not find file " + fileName);
164          System.exit(1);
165        }
166
167      } else {
168        // File in text format
169        try {
170          parser = new TextParser(file);
171
172        } catch (FileNotFoundException ex) {
173          err.println("** Could not find file " + fileName);
174          System.exit(1);
175        }
176
177      }
178
179      try {
180        tree = parser.parse();
181
182      } catch (FamilyTreeException ex) {
183        err.println("** File " + fileName + " is malformatted");
184        System.exit(1);
185      }
186
187    } else {
188      // No file to read, create a new family tree
189      tree = new FamilyTree();
190    }
191
192    // Get the husband the wife and set up the marriage
193    Person husband = tree.getPerson(husbandId);
194    Person wife = tree.getPerson(wifeId);
195
196    Marriage marriage = new Marriage(husband, wife);
197    husband.addMarriage(marriage);
198    wife.addMarriage(marriage);
199
200    if (location != null) {
201      marriage.setLocation(location);
202    }
203
204    if (date != null) {
205      marriage.setDate(date);
206    }
207
208    // Now write the family tree to the file
209    Dumper dumper = null;
210    if (useXml) {
211      try {
212        dumper = new XmlDumper(file);
213
214      } catch (IOException ex) {
215        err.println("** Error while dealing with " + file);
216        System.exit(1);
217      }
218
219    } else {
220      try {
221        dumper = new TextDumper(file);
222
223      } catch (IOException ex) {
224        err.println("** Error while dealing with " + file);
225        System.exit(1);
226      }
227    }
228    dumper.dump(tree);
229
230
231  }
232
233}