KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > util > NameParser


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.util;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import org.columba.core.resourceloader.GlobalResourceLoader;
25 /**
26  * Used to parse display names, e.g. John Doe, John Q. Public, Dr.
27  * @author Rick Horowitz
28  *
29  */

30 public class NameParser {
31     
32     public static void main(String JavaDoc args[]) {
33         String JavaDoc[] testNames = {"Dr. Richard K. Ellington", "Bob Jones", "Ms. Jill Hark", "Robert B. Smith, Esq.", "Johnson, PhD.",
34                 "Sue & Gene Stark", "George", "", null};
35         NameParser parser = NameParser.getInstance();
36         for (int i = 0; i < testNames.length; i++) {
37             String JavaDoc name = testNames[i];
38             System.out.println(i +": " +parser.parseDisplayName(name).toString());
39         }
40     }
41
42     private static final String JavaDoc RESOURCE_PATH = "org.columba.core.i18n.global";
43     
44     /** Salutations used at the beginning of a display name. */
45 // private static final String[] SALUTATIONS = { "Mr.", "Mrs.", "Ms.", "Dr.", "Congressman", "Congresswoman", "Senator", "President",
46
// "Chancellor", "Minister", "Prime Minister" };
47
/** Titles used at the end of a display name. */
48 // private static final String[] TITLES = { "PhD", "Ph.D.", "PhD.", "Esq.", "Esquire" };
49

50     private static NameParser _instance;
51     /**
52      * Get the singleton instance of NameParser
53      * @return NameParser instance.
54      */

55     public static NameParser getInstance() {
56         if (_instance == null)
57             _instance = new NameParser();
58         return _instance;
59     }
60     
61     /** Salutations used at the beginning of a display name. */
62     private final String JavaDoc[] SALUTATIONS;
63     /** Titles used at the end of a display name. */
64     private final String JavaDoc[] TITLES;
65     
66     /**
67      * Construct a singleton instance
68      */

69     private NameParser() {
70         
71         // Initialize salutation strings
72
List JavaDoc<String JavaDoc> salutations = new ArrayList JavaDoc<String JavaDoc>();
73         String JavaDoc salutationsStr = GlobalResourceLoader.getString(RESOURCE_PATH, "global", "name_parser_salutations");
74         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(salutationsStr, ",");
75         while (st.hasMoreTokens()) {
76             salutations.add(st.nextToken());
77         }
78         SALUTATIONS = salutations.toArray(new String JavaDoc[salutations.size()]);
79         
80         // Initialize title strings
81
List JavaDoc<String JavaDoc> titles = new ArrayList JavaDoc<String JavaDoc>();
82         String JavaDoc titlesStr = GlobalResourceLoader.getString(RESOURCE_PATH, "global", "name_parser_titles");
83         st = new StringTokenizer JavaDoc(titlesStr, ",");
84         while (st.hasMoreTokens()) {
85             titles.add(st.nextToken());
86         }
87         TITLES = titles.toArray(new String JavaDoc[titles.size()]);
88         
89     }
90     
91     public Name parseDisplayName(String JavaDoc nameInputStr) {
92         
93         // Treat a null name input string the same as an empty input string
94
if (nameInputStr == null)
95             nameInputStr = "";
96         
97         String JavaDoc displayName = nameInputStr;
98         String JavaDoc salutation = null;
99         String JavaDoc firstName = null;
100         String JavaDoc middleName = null;
101         String JavaDoc lastName = null;
102         String JavaDoc title = null;
103         
104         // Get the salutation if one is specified
105
salutation = findSalutation(nameInputStr);
106         if (salutation != null) {
107             // Remove the salutation
108
nameInputStr = nameInputStr.substring(salutation.length());
109         }
110         
111         // Get the title if one is specified
112
title = findTitle(nameInputStr);
113         if (title != null) {
114             // Remove the title and the preceding comma
115
nameInputStr = nameInputStr.substring(0, nameInputStr.length() - title.length());
116             nameInputStr = nameInputStr.substring(0, nameInputStr.lastIndexOf(','));
117         }
118         
119         // Determine whether there are 1, 2, or 3 names specified. These names should be separated by spaces
120
// or commas. If a comma separates the first two names, assume that the last name is specified first,
121
// Otherwise, assume the first name is specified first. Middle name is always specified after the first name.
122
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(nameInputStr, ", ");
123         List JavaDoc<String JavaDoc> tokens = new ArrayList JavaDoc<String JavaDoc>();
124         while (st.hasMoreTokens()) {
125             tokens.add(st.nextToken());
126         }
127
128         boolean commaSpecified = nameInputStr.indexOf(',') >= 0;
129         if (tokens.size() == 0) {
130             // Do nothing
131
} else if (tokens.size() == 1) {
132             // Assume last name only
133
lastName = tokens.get(0);
134         } else if (tokens.size() == 2) {
135             if (commaSpecified) {
136                 lastName = tokens.get(0);
137                 firstName = tokens.get(1);
138             } else {
139                 firstName = tokens.get(0);
140                 lastName = tokens.get(1);
141             }
142         } else if (tokens.size() == 3) {
143             if (commaSpecified) {
144                 lastName = tokens.get(0);
145                 firstName = tokens.get(1);
146                 middleName = tokens.get(2);
147             } else {
148                 firstName = tokens.get(0);
149                 middleName = tokens.get(1);
150                 lastName = tokens.get(2);
151             }
152         } else {
153             // More than 3 tokens. Assume the last token is the last name and take the rest of the names as the first name.
154
// This handles names like this: "Sue & Gene Stark".
155
StringBuffer JavaDoc firstSB = new StringBuffer JavaDoc();
156             lastName = (String JavaDoc)tokens.get(tokens.size()-1);
157             firstName = nameInputStr.substring(0, nameInputStr.length() - lastName.length()).trim();
158         }
159         
160         return new Name(displayName, salutation, firstName, middleName, lastName, title);
161     }
162
163     /**
164      * Find the title in the "nameInputStr", if one exists. Searches the salutations including in
165      * NameParser.TITLES.
166      * @param nameInputStr The display name to search.
167      * @return The title, or null, if none is found.
168      */

169     private String JavaDoc findTitle(String JavaDoc nameInputStr) {
170         String JavaDoc title = null;
171         for (String JavaDoc s : TITLES) {
172             if (nameInputStr.endsWith(s)) {
173                 title = s;
174                 break;
175             }
176         }
177         return title;
178     }
179     
180     /**
181      * Find the salutation in the "nameInputStr", if one exists. Searches the salutations including in
182      * NameParser.SALUTATIONS.
183      * @param nameInputStr The display name to search.
184      * @return The salutation, or null, if none is found.
185      */

186     private String JavaDoc findSalutation(String JavaDoc nameInputStr) {
187         String JavaDoc salutation = null;
188         for (String JavaDoc s : SALUTATIONS) {
189             if (nameInputStr.startsWith(s)) {
190                 salutation = s;
191                 break;
192             }
193         }
194         return salutation;
195     }
196     
197     /**
198      * The parsed name.
199      */

200     public static class Name {
201         private String JavaDoc _displayName;
202         private String JavaDoc _firstName;
203         private String JavaDoc _middleName;
204         private String JavaDoc _lastName;
205         private String JavaDoc _salutation;
206         private String JavaDoc _title;
207         public Name(String JavaDoc displayName, String JavaDoc salutation, String JavaDoc firstName, String JavaDoc middleName, String JavaDoc lastName, String JavaDoc title) {
208             _displayName = displayName;
209             _salutation = salutation;
210             _firstName = firstName;
211             _middleName = middleName;
212             _lastName = lastName;
213             _title = title;
214         }
215         /**
216          * @return the displayName
217          */

218         public String JavaDoc getDisplayName() {
219             return _displayName;
220         }
221         /**
222          * @return the first name
223          */

224         public String JavaDoc getFirstName() {
225             return _firstName;
226         }
227         /**
228          * @return the last name
229          */

230         public String JavaDoc getLastName() {
231             return _lastName;
232         }
233         /**
234          * @return the middle name
235          */

236         public String JavaDoc getMiddleName() {
237             return _middleName;
238         }
239         /**
240          * @return the salutation, e.g. Mr., Ms., Dr., etc.
241          */

242         public String JavaDoc getSalutation() {
243             return _salutation;
244         }
245         /**
246          * @return the title, e.g. PhD, Esquire, Senator, etc.
247          */

248         public String JavaDoc getTitle() {
249             return _title;
250         }
251         
252         /* (non-Javadoc)
253          * @see java.lang.Object#toString()
254          */

255         public String JavaDoc toString() {
256             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
257             if (_salutation != null) {
258                 sb.append(_salutation);
259                 sb.append(" ");
260             }
261             if (_firstName != null) {
262                 sb.append(_firstName);
263                 sb.append(" ");
264             }
265             if (_middleName != null) {
266                 sb.append(_middleName);
267                 sb.append(" ");
268             }
269             if (_lastName != null) {
270                 sb.append(_lastName);
271             }
272             if (_title != null) {
273                 sb.append(", ");
274                 sb.append(_title);
275             }
276             
277             return sb.toString();
278         }
279     }
280
281 }
282
Popular Tags