1 2 package org.ofbiz.base.util; 3 4 import java.util.ArrayList ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 17 public class UtilName { 18 19 public static final String module = UtilName.class.getName(); 20 public static final String [] prefix = { "MR", "MS", "MISS", "MRS","DR" }; 21 public static final String [] 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 name[] = null; 31 32 public UtilName(String name, boolean initial) { 33 this.middleIsInitial = initial; 34 35 String [] splitStr = name.split(" "); 37 List goodElements = new ArrayList (); 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 this.name = new String [goodElements.size()]; 46 for (int i = 0; i < this.name.length; i++) { 47 this.name[i] = (String ) goodElements.get(i); 48 } 49 } 50 51 public UtilName(String name) { 52 this(name, false); 53 } 54 55 protected UtilName() { 56 } 57 58 public String getPrefix() { 59 return this.getField(PREFIX); 60 } 61 62 public String getFirstName() { 63 return this.getField(FIRST); 64 } 65 66 public String getMiddleName() { 67 return this.getField(MIDDLE); 68 } 69 70 public String getLastName() { 71 return this.getField(LAST); 72 } 73 74 public String getSuffix() { 75 return this.getField(SUFFIX); 76 } 77 78 public String getField(int field) { 79 int index[] = this.getFieldIndex(field); 80 if (index != null && index.length > 0) { 82 if (index.length == 1) { 83 return name[index[0]]; 84 } else { 85 StringBuffer nameBuf = new StringBuffer (); 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 getNameMap() { 99 Map nameMap = new HashMap (); 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 indexString(int[] index) { 109 String 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 String 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 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 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 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 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 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 int suffixIndex = name.length - 1; 203 String 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 field, String [] 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 parseName(String name, boolean middleIsInitial) { 227 return (new UtilName(name, middleIsInitial)).getNameMap(); 228 } 229 230 public static Map parseName(String name) { 231 return parseName(name, false); 232 } 233 234 public static void main(String [] args) throws Exception { 235 String 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 nameMap = parseName(name, true); 244 Iterator i = nameMap.keySet().iterator(); 245 while (i.hasNext()) { 246 String f = (String ) i.next(); 247 System.out.println(f + " - " + nameMap.get(f)); 248 } 249 } 250 } 251 | Popular Tags |