1 64 65 package com.jcorporate.expresso.ext.dbobj; 66 67 import com.jcorporate.expresso.core.db.DBConnection; 68 import com.jcorporate.expresso.core.db.DBException; 69 import com.jcorporate.expresso.core.dbobj.DBField; 70 import com.jcorporate.expresso.core.dbobj.RequestContext; 71 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 72 import com.jcorporate.expresso.core.misc.StringUtil; 73 import org.apache.oro.text.regex.MalformedPatternException; 74 import org.apache.oro.text.regex.Pattern; 75 import org.apache.oro.text.regex.PatternCompiler; 76 import org.apache.oro.text.regex.PatternMatcher; 77 import org.apache.oro.text.regex.Perl5Compiler; 78 import org.apache.oro.text.regex.Perl5Matcher; 79 import org.apache.oro.util.Cache; 80 import org.apache.oro.util.CacheLRU; 81 82 import java.lang.ref.Reference ; 83 import java.lang.ref.WeakReference ; 84 import java.util.Vector ; 85 86 87 99 public class ISOCountryCodes extends SecuredDBObject { 100 public static final String FLD_ISOCODE = "ISOCode"; 101 public static final String FLD_COUNTRY = "Country"; 102 public static final String FLD_POSTALREGEXP = "PostalRegExp"; 103 public static final String TABLE = "ISOCOUNTRYCODES"; 104 105 109 protected static Cache regExpCache = new CacheLRU(); 110 111 114 protected static PatternCompiler compiler = new Perl5Compiler(); 115 116 119 protected static PatternMatcher matcher = new Perl5Matcher(); 120 121 124 public ISOCountryCodes() 125 throws DBException { 126 super(); 127 } 128 129 134 public ISOCountryCodes(DBConnection myConnection) 135 throws DBException { 136 super(myConnection); 137 } 138 139 146 public ISOCountryCodes(int uid) 147 throws DBException { 148 super(uid); 149 } 150 151 158 public ISOCountryCodes(RequestContext request) 159 throws DBException { 160 super(request); 161 } 162 163 169 public synchronized void populateDefaultValues() 170 throws DBException { 171 int i; 172 String [][] localCodes = getCountryCodes(); 173 int len = localCodes.length; 174 175 for (i = 0; i < len; i++) { 176 this.clear(); 177 this.setField(FLD_ISOCODE, localCodes[i][1]); 178 179 if (!this.find()) { 180 setField(FLD_COUNTRY, localCodes[i][0]); 181 this.add(); 182 } 183 } 184 } 185 186 187 193 public int getExpectedDefaultPopulation() { 194 String [][] countryCodes = getCountryCodes(); 195 return countryCodes.length; 196 } 197 198 202 public Vector getValues() 203 throws DBException { 204 return getValuesDefault(FLD_ISOCODE, FLD_COUNTRY); 205 } 206 207 208 218 public boolean checkPostalCode(String testPostalCode) 219 throws DBException { 220 String regExp = StringUtil.notNull(this.getField(FLD_POSTALREGEXP)); 221 222 if (regExp.length() == 0) { 223 return false; 224 } 225 226 Pattern compiledRegExp = getPattern(regExp); 227 228 synchronized (matcher) { 229 if (!matcher.matches(testPostalCode, compiledRegExp)) { 230 throw new DBException("You've entered an invalid postal code"); 231 } 232 } 233 234 return true; 235 } 236 237 244 private synchronized Pattern getPattern(String postalCodePattern) 245 throws DBException { 246 Pattern p = (Pattern) regExpCache.getElement(postalCodePattern); 247 248 if (p == null) { 249 try { 250 p = compiler.compile(postalCodePattern, Perl5Compiler.READ_ONLY_MASK); 251 regExpCache.addElement(postalCodePattern, p); 252 } catch (MalformedPatternException mpe) { 253 throw new DBException(this.getClass().getName() + 254 "getPattern(String)", mpe); 255 } 256 } 257 258 return p; 259 } 260 261 264 protected void setupFields() 265 throws DBException { 266 setTargetTable(TABLE); 267 setDescription("ISO Country Codes"); 268 setCharset("ISO-8859-1"); 269 addField(FLD_ISOCODE, DBField.VARCHAR_TYPE, 10, false, "ISO Country Code"); 270 addField(FLD_COUNTRY, DBField.VARCHAR_TYPE, 128, false, "Country Name"); 271 addField(FLD_POSTALREGEXP, DBField.VARCHAR_TYPE, 128, true, 272 "Postal Code Valid Regular Expression"); 273 setStringFilter(FLD_ISOCODE, "stripFilter"); 274 setStringFilter(FLD_COUNTRY, "rawFilter"); 275 addKey(FLD_ISOCODE); 276 setReadOnly(FLD_ISOCODE); 277 addIndex("CountryNames", FLD_COUNTRY, true); 278 } 279 280 protected synchronized static String [][] getCountryCodes() { 281 synchronized (countryCodeLock) { 282 if (countryCodes == null || countryCodes.get() == null) { 283 countryCodes = new WeakReference (new String [][]{ 284 {"United States", "US", ""}, {"Afghanistan", "AF", ""}, {"Aland Islands", "AX", ""}, 285 {"Albania", "AL", ""}, {"Algeria", "DZ", ""}, 286 {"American Samoa", "AS", ""}, {"Andorra", "AD", ""}, 287 {"Angola", "AO", ""}, {"Anguilla", "AI", ""}, {"Antarctica", "AQ", ""}, 288 {"Antigua And Barbuda", "AG", ""}, {"Argentina", "AR", ""}, 289 {"Armenia", "AM", ""}, {"Aruba", "AW", ""}, {"Australia", "AU", ""}, 290 {"Austria", "AT", ""}, {"Azerbaijan", "AZ", ""}, {"Bahamas", "BS", ""}, 291 {"Bahrain", "BH", ""}, {"Bangladesh", "BD", ""}, 292 {"Barbados", "BB", ""}, {"Belarus", "BY", ""}, {"Belgium", "BE", ""}, 293 {"Belize", "BZ", ""}, {"Benin", "BJ", ""}, {"Bermuda", "BM", ""}, 294 {"Bhutan", "BT", ""}, {"Bolivia", "BO", ""}, 295 {"Bosnia And Herzegovina", "BA", ""}, {"Botswana", "BW", ""}, 296 {"Bouvet Island", "BV", ""}, {"Brazil", "BR", ""}, 297 {"British Indian Ocean Territory", "IO", ""}, 298 {"Brunei Darussalam", "BN", ""}, {"Bulgaria", "BG", ""}, 299 {"Burkina Faso", "BF", ""}, {"Burundi", "BI", ""}, 300 {"Cambodia", "KH", ""}, {"Cameroon", "CM", ""}, {"Canada", "CA", ""}, 301 {"Cape Verde", "CV", ""}, {"Cayman Islands", "KY", ""}, 302 {"Central African Republic", "CF", ""}, {"Chad", "TD", ""}, 303 {"Chile", "CL", ""}, {"China", "CN", ""}, 304 {"Christmas Island", "CX", ""}, {"Cocos (Keeling) Islands", "CC", ""}, 305 {"Colombia", "CO", ""}, {"Comoros", "KM", ""}, {"Congo", "CG", ""}, 306 {"Congo, The Democratic Republic Of The", "CD", ""}, 307 {"Cook Islands", "CK", ""}, {"Costa Rica", "CR", ""}, 308 {"Cote D'Ivoire", "CI", ""}, {"Croatia", "HR", ""}, {"Cuba", "CU", ""}, 309 {"Cyprus", "CY", ""}, {"Czech Republic", "CZ", ""}, 310 {"Denmark", "DK", ""}, {"Djibouti", "DJ", ""}, {"Dominica", "DM", ""}, 311 {"Dominican Republic", "DO", ""}, 312 {"Ecuador", "EC", ""}, {"Egypt", "EG", ""}, {"El Salvador", "SV", ""}, 313 {"Equatorial Guinea", "GQ", ""}, {"Eritrea", "ER", ""}, 314 {"Estonia", "EE", ""}, {"Ethiopia", "ET", ""}, 315 {"Falkland Islands", "FK", ""}, {"Faroe Islands", "FO", ""}, 316 {"Fiji", "FJ", ""}, {"Finland", "FI", ""}, {"France", "FR", ""}, 317 {"French Guiana", "GF", ""}, {"French Polynesia", "PF", ""}, 318 {"French Southern Territories", "TF", ""}, {"Gabon", "GA", ""}, 319 {"Gambia", "GM", ""}, {"Georgia", "GE", ""}, {"Germany", "DE", ""}, 320 {"Ghana", "GH", ""}, {"Gibraltar", "GI", ""}, {"Greece", "GR", ""}, 321 {"Greenland", "GL", ""}, {"Grenada", "GD", ""}, 322 {"Guadeloupe", "GP", ""}, {"Guam", "GU", ""}, {"Guatemala", "GT", ""}, 323 {"Guinea", "GN", ""}, {"Guinea-bissau", "GW", ""}, 324 {"Guyana", "GY", ""}, {"Haiti", "HT", ""}, 325 {"Heard Island And Mcdonald Islands", "HM", ""}, 326 {"Holy See (Vatican City State)", "VA", ""}, {"Honduras", "HN", ""}, 327 {"Hong Kong", "HK", ""}, {"Hungary", "HU", ""}, {"Iceland", "IS", ""}, 328 {"India", "IN", ""}, {"Indonesia", "ID", ""}, {"Iran", "IR", ""}, 329 {"Iraq", "IQ", ""}, {"Ireland", "IE", ""}, {"Israel", "IL", ""}, 330 {"Italy", "IT", ""}, {"Jamaica", "JM", ""}, {"Japan", "JP", ""}, 331 {"Jordan", "JO", ""}, {"Kazakhstan", "KZ", ""}, {"Kenya", "KE", ""}, 332 {"Kiribati", "KI", ""}, {"North Korea", "KP", ""}, 333 {"South Korea", "KR", ""}, {"Kuwait", "KW", ""}, 334 {"Kyrgyzstan", "KG", ""}, 335 {"Lao People's Democratic Republic", "LA", ""}, {"Latvia", "LV", ""}, 336 {"Lebanon", "LB", ""}, {"Lesotho", "LS", ""}, {"Liberia", "LR", ""}, 337 {"Libyan Arab Jamahiriya", "LY", ""}, {"Liechtenstein", "LI", ""}, 338 {"Lithuania", "LT", ""}, {"Luxembourg", "LU", ""}, {"Macao", "MO", ""}, 339 {"Macedonia", "MK", ""}, {"Madagascar", "MG", ""}, 340 {"Malawi", "MW", ""}, {"Malaysia", "MY", ""}, {"Maldives", "MV", ""}, 341 {"Mali", "ML", ""}, {"Malta", "MT", ""}, 342 {"Marshall Islands", "MH", ""}, {"Martinique", "MQ", ""}, 343 {"Mauritania", "MR", ""}, {"Mauritius", "MU", ""}, 344 {"Mayotte", "YT", ""}, {"Mexico", "MX", ""}, {"Micronesia", "FM", ""}, 345 {"Moldova", "MD", ""}, {"Monaco", "MC", ""}, {"Mongolia", "MN", ""}, 346 {"Montserrat", "MS", ""}, {"Morocco", "MA", ""}, 347 {"Mozambique", "MZ", ""}, {"Myanmar", "MM", ""}, {"Namibia", "NA", ""}, 348 {"Nauru", "NR", ""}, {"Nepal", "NP", ""}, {"Netherlands", "NL", ""}, 349 {"Netherlands Antilles", "AN", ""}, {"New Caledonia", "NC", ""}, 350 {"New Zealand", "NZ", ""}, {"Nicaragua", "NI", ""}, 351 {"Niger", "NE", ""}, {"Nigeria", "NG", ""}, {"Niue", "NU", ""}, 352 {"Norfolk Island", "NF", ""}, {"Northern Mariana Islands", "MP", ""}, 353 {"Norway", "NO", ""}, {"Oman", "OM", ""}, {"Pakistan", "PK", ""}, 354 {"Palau", "PW", ""}, {"Palestinian Territory", "PS", ""}, 355 {"Panama", "PA", ""}, {"Papua New Guinea", "PG", ""}, 356 {"Paraguay", "PY", ""}, {"Peru", "PE", ""}, {"Philippines", "PH", ""}, 357 {"Pitcairn", "PN", ""}, {"Poland", "PL", ""}, {"Portugal", "PT", ""}, 358 {"Puerto Rico", "PR", ""}, {"Qatar", "QA", ""}, {"Reunion", "RE", ""}, 359 {"Romania", "RO", ""}, {"Russian Federation", "RU", ""}, 360 {"Rwanda", "RW", ""}, {"Saint Helena", "SH", ""}, 361 {"Saint Kitts And Nevis", "KN", ""}, {"Saint Lucia", "LC", ""}, 362 {"Saint Pierre And Miquelon", "PM", ""}, 363 {"Saint Vincent And The Grenadines", "VC", ""}, {"Samoa", "WS", ""}, 364 {"San Marino", "SM", ""}, {"Sao Tome And Principe", "ST", ""}, 365 {"Saudi Arabia", "SA", ""}, {"Senegal", "SN", ""}, {"Serbia And Montenegro", "CS", ""}, 366 {"Seychelles", "SC", ""}, {"Sierra Leone", "SL", ""}, 367 {"Singapore", "SG", ""}, {"Slovakia", "SK", ""}, 368 {"Slovenia", "SI", ""}, {"Solomon Islands", "SB", ""}, 369 {"Somalia", "SO", ""}, {"South Africa", "ZA", ""}, 370 {"South Georgia And The South Sandwich Islands", "GS", ""}, 371 {"Spain", "ES", ""}, {"Sri Lanka", "LK", ""}, {"Sudan", "SD", ""}, 372 {"Suriname", "SR", ""}, {"Svalbard And Jan Mayen", "SJ", ""}, 373 {"Swaziland", "SZ", ""}, {"Sweden", "SE", ""}, 374 {"Switzerland", "CH", ""}, {"Syrian Arab Republic", "SY", ""}, 375 {"Taiwan", "TW", ""}, {"Tajikistan", "TJ", ""}, 376 {"Tanzania", "TZ", ""}, {"Thailand", "TH", ""}, {"Timor-Leste", "TL", ""}, 377 {"Togo", "TG", ""}, {"Tokelau", "TK", ""}, {"Tonga", "TO", ""}, 378 {"Trinidad And Tobago", "TT", ""}, {"Tunisia", "TN", ""}, 379 {"Turkey", "TR", ""}, {"Turkmenistan", "TM", ""}, 380 {"Turks And Caicos Islands", "TC", ""}, {"Tuvalu", "TV", ""}, 381 {"Uganda", "UG", ""}, {"Ukraine", "UA", ""}, 382 {"United Arab Emirates", "AE", ""}, {"United Kingdom", "GB", ""}, 383 {"United States Minor Outlying Islands", "UM", ""}, 384 {"Uruguay", "UY", ""}, {"Uzbekistan", "UZ", ""}, {"Vanuatu", "VU", ""}, 385 {"Venezuela", "VE", ""}, {"Viet Nam", "VN", ""}, 386 {"Virgin Islands, British", "VG", ""}, 387 {"Virgin Islands, U.S.", "VI", ""}, {"Wallis And Futuna", "WF", ""}, 388 {"Western Sahara", "EH", ""}, {"Yemen", "YE", ""}, 389 {"Zambia", "ZM", ""}, {"Zimbabwe", "ZW", ""} 390 }); 391 } 392 393 return (String [][]) countryCodes.get(); 394 } 395 } 396 397 protected static Reference countryCodes = null; 398 protected static Object countryCodeLock = new Object (); 399 400 401 } | Popular Tags |