KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > dbobj > ISOCountryCodes


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

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 JavaDoc;
83 import java.lang.ref.WeakReference JavaDoc;
84 import java.util.Vector JavaDoc;
85
86
87 /**
88  * This database table contains a list of all known ISO 3166-1 country codes.
89  * See www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/index.html
90  * for the source of this data.
91  * <p/>
92  * This is highly useful for registration data. Allow a person to choose from a
93  * dropdown box. Also can check postal code against known regular expressions
94  * depending on the iso country code associated with the registration.
95  *
96  * @author Michael Rimov
97  * @since Expresso 5.0
98  */

99 public class ISOCountryCodes extends SecuredDBObject {
100     public static final String JavaDoc FLD_ISOCODE = "ISOCode";
101     public static final String JavaDoc FLD_COUNTRY = "Country";
102     public static final String JavaDoc FLD_POSTALREGEXP = "PostalRegExp";
103     public static final String JavaDoc TABLE = "ISOCOUNTRYCODES";
104
105     /**
106      * Used to hold compiled regular expressions in an attempt to speed
107      * up pattern matching.
108      */

109     protected static Cache regExpCache = new CacheLRU();
110
111     /**
112      * Used for regular expression compilation
113      */

114     protected static PatternCompiler compiler = new Perl5Compiler();
115
116     /**
117      * Used to match compiled patterns against strings
118      */

119     protected static PatternMatcher matcher = new Perl5Matcher();
120
121     /**
122      * Constructor
123      */

124     public ISOCountryCodes()
125             throws DBException {
126         super();
127     } /* User() */
128
129     /**
130      * Constructor
131      *
132      * @param myConnection The DBConnection to use Locally
133      */

134     public ISOCountryCodes(DBConnection myConnection)
135             throws DBException {
136         super(myConnection);
137     } /* User(String) */
138
139     /**
140      * Use over (String) constructor. Initializes the object in the context
141      * of the user who's uid belongs to the parameter.
142      *
143      * @param uid the Uid of the user context
144      * @throws DBException if there's an initialization problem
145      */

146     public ISOCountryCodes(int uid)
147             throws DBException {
148         super(uid);
149     }
150
151     /**
152      * For using DBObjects within Controllers. Initializes based upon the current
153      * user and the requested db. [Of course this can be modified later]
154      *
155      * @param request - The controller request handed to you by the framework.
156      * @throws DBException if there's an initialization problem
157      */

158     public ISOCountryCodes(RequestContext request)
159             throws DBException {
160         super(request);
161     }
162
163     /**
164      * This function populates the table with the current listing of ISO country codes
165      *
166      * @throws DBException if an error occurs while populating the table.
167      * @see com.jcorporate.expresso.core.dbobj.DBObject#populateDefaultValues
168      */

169     public synchronized void populateDefaultValues()
170             throws DBException {
171         int i;
172         String JavaDoc[][] 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     } /* populateDefaultValues() */
185
186
187     /**
188      * Useful method for unit testing to make sure that everything got added during
189      * setup as expected.
190      *
191      * @return the length of what's expected to be in the table
192      */

193     public int getExpectedDefaultPopulation() {
194         String JavaDoc[][] countryCodes = getCountryCodes();
195         return countryCodes.length;
196     }
197
198     /**
199      * @return Vector of ValidValue Value/Description pairs for ISO Country Codes
200      * @throws DBException If the values cannot be retrieved
201      */

202     public Vector JavaDoc getValues()
203             throws DBException {
204         return getValuesDefault(FLD_ISOCODE, FLD_COUNTRY);
205     } /* getValues() */
206
207
208     /**
209      * Checks a postal code for proper formatting The iso country definition
210      * should already have been retrieved.
211      *
212      * @param testPostalCode the postalCode to check
213      * @return true if the postal code existed for this country
214      * @throws DBException if the test code didn't match the regular expression
215      * stored in the database OR if the regular expression in the database
216      * is syntactically invalid
217      */

218     public boolean checkPostalCode(String JavaDoc testPostalCode)
219             throws DBException {
220         String JavaDoc 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     /**
238      * Get's the regular expression patter to match against.
239      *
240      * @param postalCodePattern the postalCode pattern to check
241      * @return ORO Compiled Regular Expression Pattern
242      * @throws DBException if the mattern match is bad.
243      */

244     private synchronized Pattern getPattern(String JavaDoc 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     /**
262      * @throws DBException
263      */

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     } /* setupFields() */
279
280     protected synchronized static String JavaDoc[][] getCountryCodes() {
281         synchronized (countryCodeLock) {
282             if (countryCodes == null || countryCodes.get() == null) {
283                 countryCodes = new WeakReference JavaDoc(new String JavaDoc[][]{
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 JavaDoc[][]) countryCodes.get();
394         }
395     }
396
397     protected static Reference JavaDoc countryCodes = null;
398     protected static Object JavaDoc countryCodeLock = new Object JavaDoc();
399
400
401 }
Popular Tags