KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > UtilName


1 /* $$ID$ */
2 package org.ofbiz.base.util;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9
10 /**
11  * UtilName
12  *
13  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
14  * @version $Rev: 7330 $
15  * @since Mar 1, 2006
16  */

17 public class UtilName {
18
19     public static final String JavaDoc module = UtilName.class.getName();
20     public static final String JavaDoc[] prefix = { "MR", "MS", "MISS", "MRS","DR" };
21     public static final String JavaDoc[] suffix = { "JR", "SR", "III", "PHD", "MD" };
22
23     public static final int PREFIX = 1;
24     public static final int FIRST = 2;
25     public static final int MIDDLE = 3;
26     public static final int LAST = 4;
27     public static final int SUFFIX = 5;
28
29     protected boolean middleIsInitial = false;
30     protected String JavaDoc name[] = null;
31
32     public UtilName(String JavaDoc name, boolean initial) {
33         this.middleIsInitial = initial;
34
35         // check the name for empty elements
36
String JavaDoc[] splitStr = name.split(" ");
37         List JavaDoc goodElements = new ArrayList JavaDoc();
38         for (int i = 0; i < splitStr.length; i++) {
39             if (splitStr[i] != null && splitStr[i].length() > 0 && !splitStr[i].matches("\\W+")) {
40                 goodElements.add(splitStr[i]);
41             }
42         }
43
44         // fill in the name array
45
this.name = new String JavaDoc[goodElements.size()];
46         for (int i = 0; i < this.name.length; i++) {
47             this.name[i] = (String JavaDoc) goodElements.get(i);
48         }
49     }
50
51     public UtilName(String JavaDoc name) {
52         this(name, false);
53     }
54
55     protected UtilName() {
56     }
57
58     public String JavaDoc getPrefix() {
59         return this.getField(PREFIX);
60     }
61
62     public String JavaDoc getFirstName() {
63         return this.getField(FIRST);
64     }
65
66     public String JavaDoc getMiddleName() {
67         return this.getField(MIDDLE);
68     }
69
70     public String JavaDoc getLastName() {
71         return this.getField(LAST);
72     }
73
74     public String JavaDoc getSuffix() {
75         return this.getField(SUFFIX);
76     }
77
78     public String JavaDoc getField(int field) {
79         int index[] = this.getFieldIndex(field);
80         //System.out.println("Field : " + field + " - Index : " + indexString(index));
81
if (index != null && index.length > 0) {
82             if (index.length == 1) {
83                 return name[index[0]];
84             } else {
85                 StringBuffer JavaDoc nameBuf = new StringBuffer JavaDoc();
86                 for (int i = 0; i < index.length; i++) {
87                     if (nameBuf.length() > 0) {
88                         nameBuf.append(" ");
89                     }
90                     nameBuf.append(name[index[i]]);
91                 }
92                 return nameBuf.toString();
93             }
94         }
95         return null;
96     }
97
98     public Map JavaDoc getNameMap() {
99         Map JavaDoc nameMap = new HashMap JavaDoc();
100         nameMap.put("personalTitle", this.getPrefix());
101         nameMap.put("firstName", this.getFirstName());
102         nameMap.put("middleName", this.getMiddleName());
103         nameMap.put("lastName", this.getLastName());
104         nameMap.put("suffix", this.getSuffix());
105         return nameMap;
106     }
107
108     protected String JavaDoc indexString(int[] index) {
109         String JavaDoc str = "";
110         if (index != null) {
111             for (int i = 0; i < index.length; i++) {
112                 if (!"".equals(str)) {
113                     str = str + ", ";
114                 }
115                 str = str + index[0];
116             }
117         }
118
119         return str;
120     }
121
122     protected int[] getFieldIndex(int field) {
123         if (name == null || name.length == 0) {
124             return null;
125         }
126         switch(field) {
127             case 1:
128                 // prefix
129
String JavaDoc prefixChk = name[0].toUpperCase();
130                 prefixChk = prefixChk.replaceAll("\\W", "");
131                 if (this.checkValue(prefixChk, prefix)) {
132                     return new int[] { 0 };
133                 }
134                 return null;
135             case 2:
136                 // first name
137
if (name.length == 2 || this.getFieldIndex(PREFIX) == null) {
138                     return new int[] { 0 };
139                 } else {
140                     for (int i = 1; i < name.length; i++) {
141                         String JavaDoc s = name[i].toUpperCase().replaceAll("\\W", "");
142                         if (!this.checkValue(s, prefix)) {
143                             return new int[] { i };
144                         }
145                     }
146                     return null;
147                 }
148             case 3:
149                 // middle name
150

151                 int prefixIdx[] = this.getFieldIndex(PREFIX);
152                 int suffixIdx[] = this.getFieldIndex(SUFFIX);
153                 int minLength = 3;
154                 if (prefixIdx != null) {
155                     minLength++;
156                 }
157                 if (suffixIdx != null) {
158                     minLength++;
159                 }
160
161                 if (name.length >= minLength) {
162                     int midIdx = prefixIdx != null ? 2 : 1;
163                     if (middleIsInitial) {
164                         String JavaDoc value = name[midIdx];
165                         value = value.replaceAll("\\W", "");
166                         if (value.length() == 1) {
167                             return new int[] { midIdx };
168                         }
169                     } else {
170                         return new int[] { midIdx };
171                     }
172                 }
173                 return null;
174             case 4:
175                 // last name
176
if (name.length > 2) {
177                     int firstIndex[] = this.getFieldIndex(FIRST);
178                     int middleIndex[] = this.getFieldIndex(MIDDLE);
179                     int suffixIndex[] = this.getFieldIndex(SUFFIX);
180
181                     int lastBegin, lastEnd;
182                     if (middleIndex != null) {
183                         lastBegin = (middleIndex[middleIndex.length - 1]) + 1;
184                     } else {
185                         lastBegin = (firstIndex[firstIndex.length - 1]) + 1;
186                     }
187                     if (suffixIndex != null) {
188                         lastEnd = (suffixIndex[0]) - 1;
189                     } else {
190                         lastEnd = name.length - 1;
191                     }
192
193                     if (lastEnd > lastBegin) {
194                         return new int[] { lastBegin, lastEnd };
195                     } else {
196                         return new int[] { lastBegin };
197                     }
198                 }
199                 return new int[] { name.length - 1 };
200             case 5:
201                 // suffix
202
int suffixIndex = name.length - 1;
203                 String JavaDoc suffixChk = name[suffixIndex].toUpperCase();
204                 suffixChk = suffixChk.replaceAll("\\W", "");
205
206                 for (int i = 0; i < suffix.length; i++) {
207                     if (suffixChk.equals(suffix[i])) {
208                         return new int[] { suffixIndex };
209                     }
210                 }
211                 return null;
212             default:
213                 return null;
214         }
215     }
216
217     protected boolean checkValue(String JavaDoc field, String JavaDoc[] values) {
218         for (int i = 0; i < values.length; i++) {
219             if (values[i].equals(field)) {
220                 return true;
221             }
222         }
223         return false;
224     }
225
226     public static Map JavaDoc parseName(String JavaDoc name, boolean middleIsInitial) {
227         return (new UtilName(name, middleIsInitial)).getNameMap();
228     }
229
230     public static Map JavaDoc parseName(String JavaDoc name) {
231         return parseName(name, false);
232     }
233
234     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
235         String JavaDoc name = "";
236         for (int i = 0; i < args.length; i++) {
237             if (!"".equals(name)) {
238                 name = name + " ";
239             }
240             name = name + args[i];
241         }
242
243         Map JavaDoc nameMap = parseName(name, true);
244         Iterator JavaDoc i = nameMap.keySet().iterator();
245         while (i.hasNext()) {
246             String JavaDoc f = (String JavaDoc) i.next();
247             System.out.println(f + " - " + nameMap.get(f));
248         }
249     }
250 }
251
Popular Tags