package edu.mit.lcs; import java.util.Vector; import java.util.StringTokenizer; import java.beans.*; import java.sql.*; import java.util.Hashtable; /** * Supports creation, parsing and output of names in the English formats. * Names are considered to contain a single first name, any number of * middle names, possibly a series of last name prefixes (such as "de * la"), and possibly a suffix. * * Constructors give any of the following means of creating the name: * -- A string from which the name is parsed * -- Last, First * -- Last, First, Middles * -- Last, First, Middles, Suffix * -- Last, First, Middles, Suffix, vons * -- a resultSet from a SQL query * * */ public class EnglishName { String first = null; String last = null; Vector middles = null; Vector vons = null; String suffix = null; // The following are cached names in the two usual formats: String lnfName = null; String fnfName = null; /** * With no arguments, we create a null EnglishName. Its content * must then be set by various setxxx methods. */ public EnglishName() { super(); } /** * @ param lastname is the person's last (family) name * @ param firstname is the first name * @ param middlenames is a String of middle names, separated by spaces * @ param suffixname is the suffix, if any * @ param vonnames is a String of vons, separated by spaces */ public EnglishName(String lastname, String firstname, String middlenames, String suffixname, String vonnames) { last = (lastname==null||lastname.equals("")) ? null : lastname; first = (firstname==null||firstname.equals("")) ? null : firstname; middles = toVector((middlenames==null||middlenames.equals("")) ? null : middlenames); suffix = (suffixname==null||suffixname.equals("")) ? null : suffixname; vons = toVector((vonnames==null||vonnames.equals("")) ? null : vonnames); lnfName = this.toString(); fnfName = this.toFNF(); } /** * @param s A string that gives the name either as * First Middle Middle de Last, Jr. * or * de Last, First Middle Middle, Jr. */ public EnglishName(String s) throws Exception { this(); this.setName(s); } public EnglishName(String lastname, String firstname) { this(lastname,firstname,"","",""); } public EnglishName(String lastname, String firstname, String middlenames) { this(lastname,firstname,middlenames,"",""); } public EnglishName(String lastname, String firstname, String middlenames, String suffixname) { this(lastname,firstname,middlenames,suffixname,""); } public static String[] suffixes = {"Jr.", "Sr.", "II", "III"}; private boolean isSuffix(String s) { for (int i = 0; i0) sb.append(" "); sb.append((String)v.elementAt(i)); } } return sb.toString(); } public synchronized String toString() { String result = ""; if (vons!=null) { for (int i = 0; i0 && v[numCommas].size()==1 && isSuffix((String)v[numCommas].elementAt(0))) { suffix = (String)v[numCommas].elementAt(0); numCommas = numCommas-1; } // First-name-first if there are no commas left if (numCommas==0) { Vector v1 = v[0]; int l = v1.size(); if (l==1) { // This is a single name, such as Arvind. We treat it as // a last name. last = (String)v1.elementAt(0); } else { // First name is, oddly, the first name. first = (String)v1.elementAt(0); // Successive names are middles until there is only one left, // which is the last name, or a name starts in lower case, // in which case it starts the vons. for (int j=1; j0) first = (String)v1.elementAt(0); if (k>1) { middles = new Vector(); for (int i = 1; i < k; i++) { middles.add(v1.elementAt(i)); } } } else { throw new Exception("Improper format for name: + s"); } // Finish by constructing name strings lnfName = this.toString(); fnfName = this.toFNF(); } public static String[] colNames = {"last", "first", "middles", "suffix", "vons"}; /** * The constructor that allows us to create an EnglishName from a * database access ResultSet requires some conventions that are * encoded in settable class (static) variables. * * colNames holds an array of Strings that name the successive * keys in a ResultSet that hold the last, first, middles, suffix * and vons components of a name. These will be passed to * rs.getString() to extact the corresponding column values as * Strings. */ public EnglishName(ResultSet rs) throws SQLException { this(rs.getString(colNames[0]), rs.getString(colNames[1]), rs.getString(colNames[2]), rs.getString(colNames[3]), rs.getString(colNames[4])); } public EnglishName(Hashtable h) { this((String)h.get(colNames[0]), (String)h.get(colNames[1]), (String)h.get(colNames[2]), (String)h.get(colNames[3]), (String)h.get(colNames[4])); } public EnglishName(Entity e) { this((String)e.get(colNames[0]), (String)e.get(colNames[1]), (String)e.get(colNames[2]), (String)e.get(colNames[3]), (String)e.get(colNames[4])); } }