001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.region;
028:
029: import org.databene.commons.ConfigurationError;
030: import org.databene.commons.ArrayUtil;
031: import org.databene.commons.OrderedMap;
032: import org.databene.document.csv.CSVLineIterator;
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: import java.util.*;
037: import java.io.IOException;
038:
039: /**
040: * Represents a country, provides constants for most bigger countries and serves as a generator for city object.
041: * Country information is read from the file org/databene/domain/address/country.csv.<br/>
042: * <br/>
043: * Created: 11.06.2006 08:15:37
044: */
045: public class Country {
046:
047: private static final Log logger = LogFactory.getLog(Country.class);
048:
049: private static String FILE_NAME = "org/databene/domain/address/country.csv";
050:
051: private static Map<String, Country> instances = new HashMap<String, Country>(
052: 250);
053:
054: static {
055: parseConfigFile();
056: }
057:
058: public static final Country GERMANY = getInstance("DE");
059: public static final Country AUSTRIA = getInstance("AT");
060: public static final Country SWITZERLAND = getInstance("CH");
061: public static final Country LIECHTENSTEIN = getInstance("LI");
062:
063: public static final Country BELGIUM = getInstance("BE");
064: public static final Country NETHERLANDS = getInstance("NL");
065: public static final Country LUXEMBURG = getInstance("LU");
066:
067: public static final Country ITALY = getInstance("IT");
068: public static final Country FRANCE = getInstance("FR");
069: public static final Country SPAIN = getInstance("ES");
070: public static final Country PORTUGAL = getInstance("PT");
071: public static final Country ANDORRA = getInstance("AD");
072:
073: public static final Country GREECE = getInstance("gr");
074:
075: public static final Country SLOWENIA = getInstance("SI");
076: public static final Country CZECH_REPUBLIC = getInstance("CZ");
077: public static final Country HUNGARY = getInstance("HR");
078: public static final Country POLAND = getInstance("PL");
079: public static final Country RUSSIA = getInstance("RU");
080: public static final Country ROMANIA = getInstance("RO");
081: public static final Country BULGARIA = getInstance("BG");
082: public static final Country CROATIA = getInstance("HR");
083: public static final Country BOSNIA_AND_HERZEGOVINA = getInstance("BA");
084: public static final Country TURKEY = getInstance("TR");
085: public static final Country ESTONIA = getInstance("EE");
086: public static final Country LITHUANIA = getInstance("LT");
087: public static final Country LATVIA = getInstance("LV");
088:
089: public static final Country UNITED_KINGDOM = getInstance("UK");
090: public static final Country DENMARK = getInstance("DK");
091: public static final Country SWEDEN = getInstance("SE");
092: public static final Country NORWAY = getInstance("NO");
093: public static final Country FINLAND = getInstance("FI");
094: public static final Country IRELAND = getInstance("IE");
095: public static final Country ICELAND = getInstance("IS");
096:
097: public static final Country USA = getInstance("US");
098: public static final Country JAPAN = getInstance("JP");
099:
100: private static Country defaultCountry;
101:
102: static {
103: defaultCountry = Country.getInstance(Locale.getDefault()
104: .getCountry().toLowerCase());
105: }
106:
107: private static void parseConfigFile() {
108: CSVLineIterator iterator = null;
109: try {
110: iterator = new CSVLineIterator(FILE_NAME, ',', true);
111: logger.debug("Parsing country setup file " + FILE_NAME);
112: while (iterator.hasNext()) {
113: String[] cells = iterator.next();
114: String isoCode = cells[0];
115: String phoneCode = (cells.length > 1 ? cells[1] : null);
116: String[] mobilCodes = (cells.length > 2 ? ArrayUtil
117: .copyOfRange(cells, 2, cells.length - 2)
118: : new String[] { "???" });
119: Country country = new Country(isoCode, phoneCode,
120: mobilCodes);
121: if (logger.isDebugEnabled())
122: logger.debug("parsed " + country);
123: }
124: } catch (IOException e) {
125: throw new ConfigurationError(
126: "Country definition file could not be processed. ",
127: e);
128: } finally {
129: if (iterator != null)
130: iterator.close();
131: }
132: }
133:
134: private String isoCode;
135: private Region region;
136: private String phoneCode;
137: private String[] mobileCodes;
138: private Locale countryLocale;
139: private Map<String, State> states;
140:
141: private Country(String isoCode, String phoneCode,
142: String... mobileCodes) {
143: this .isoCode = isoCode;
144: this .phoneCode = phoneCode;
145: this .countryLocale = new Locale("xx", isoCode);
146: this .region = new BasicRegion(isoCode);
147: this .mobileCodes = mobileCodes;
148: this .states = new OrderedMap<String, State>();
149: instances.put(isoCode, this );
150: }
151:
152: public String getIsoCode() {
153: return isoCode;
154: }
155:
156: public String getName() {
157: return countryLocale.getDisplayCountry(Locale.getDefault());
158: }
159:
160: /*
161: public void setDisplayLocale(Locale displayLocale) {
162: this.displayLocale = displayLocale;
163: }
164: */
165: public String getPhoneCode() {
166: return phoneCode;
167: }
168:
169: public String[] getMobileCodes() {
170: return mobileCodes;
171: }
172:
173: public Region getRegion() {
174: return region;
175: }
176:
177: public State getState(String stateId) {
178: return states.get(stateId);
179: }
180:
181: public Collection<State> getStates() {
182: return states.values();
183: }
184:
185: public void addState(String id, State state) {
186: state.setCountry(this );
187: states.put(id, state);
188: }
189:
190: public String toString() {
191: return getName();
192: }
193:
194: public static Collection<Country> getInstances() {
195: return instances.values();
196: }
197:
198: /**
199: * Retrieves a country from the country configuration file.
200: * @param isoCode the ISO code of the country to retrieve
201: * @return if it is a predfined country, an instance with the configured data is returned,
202: * else one with the specified ISO code and default settings, e.g. phoneCode 'UNKNOWN'.
203: */
204: public static Country getInstance(String isoCode) {
205: Country country = instances.get(isoCode.toUpperCase());
206: if (country == null)
207: country = new Country(isoCode, "UNKNOWN");
208: return country;
209: }
210:
211: public static Country getDefault() {
212: return defaultCountry;
213: }
214:
215: }
|