| Countries.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * Copyright: 2005
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: Countries.java 1462 2007-07-02 02:32:23Z dmsmith $
21 */
22 package org.crosswire.common.util;
23
24 import java.util.Locale;
25 import java.util.MissingResourceException;
26 import java.util.ResourceBundle;
27
28 /**
29 * A utility class that converts ISO-3166 codes or locales to their "friendly"
30 * country name.
31 *
32 * @see gnu.lgpl.License for license details.<br>
33 * The copyright to this program is held by it's authors.
34 * @author DM Smith [dmsmith555 at yahoo dot com]
35 */
36 public class Countries {
37 /**
38 * Make the class a true utility class by having a private constructor.
39 */
40 private Countries() {
41 }
42
43 /**
44 * Determine whether the country code is valid. The code is valid if it is
45 * null or empty. The code is valid if it is in iso3166.properties. If a
46 * locale is used for the iso3166Code, it will use the part after the '_'.
47 * Thus, this code does not support dialects.
48 *
49 * @param iso3166Code
50 * @return true if the country is valid.
51 */
52 public static boolean isValidCountry(String iso3166Code) {
53 String lookup = iso3166Code;
54 if (lookup == null || lookup.length() == 0) {
55 return true;
56 }
57
58 if (lookup.indexOf('_') != -1) {
59 String[] locale = StringUtil.split(lookup, '_');
60 return isValidCountry(locale[1]);
61 }
62
63 if (lookup.length() > 2) {
64 return false;
65 }
66
67 try {
68 countries.getString(lookup);
69 return true;
70 } catch (MissingResourceException e) {
71 return false;
72 }
73 }
74
75 /**
76 * Get the country name from the country code. If the code is null or empty
77 * then it is considered to be DEFAULT_COUNTRY_CODE (that is, US).
78 * Otherwise, it will generate a log message and return unknown. If a locale
79 * is used for the iso3166Code, it will use the part before the '_'. Thus,
80 * this code does not support dialects, except as found in the iso3166.
81 *
82 * @param iso3166Code
83 * @return the name of the country
84 */
85 public static String getCountry(String iso3166Code) {
86 String lookup = iso3166Code;
87 if (lookup == null || lookup.length() == 0) {
88 return getCountry(DEFAULT_COUNTRY_CODE);
89 }
90
91 if (lookup.indexOf('_') != -1) {
92 String[] locale = StringUtil.split(lookup, '_');
93 return getCountry(locale[1]);
94 }
95
96 try {
97 return countries.getString(lookup);
98 } catch (MissingResourceException e) {
99 return getCountry(UNKNOWN_COUNTRY_CODE);
100 }
101 }
102
103 public static final String DEFAULT_COUNTRY_CODE = "US";
104 private static final String UNKNOWN_COUNTRY_CODE = "XX";
105
106 private static/* final */ResourceBundle countries;
107 static {
108 try {
109 countries = ResourceBundle.getBundle("iso3166", Locale.getDefault(), CWClassLoader.instance());
110 } catch (MissingResourceException e) {
111 assert false;
112 }
113 }
114 }
115