PageRenderTime 16ms CodeModel.GetById 9ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/webportal/src/main/java/au/org/emii/portal/util/IsoCountriesImpl.java

http://alageospatialportal.googlecode.com/
Java | 117 lines | 63 code | 20 blank | 34 comment | 11 complexity | 4ca2bbb3a2cfa78d0074de9eb0d0178c MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5package au.org.emii.portal.util;
  6
  7import au.org.emii.portal.aspect.CheckNotNull;
  8import au.org.emii.portal.settings.Settings;
  9import java.io.File;
 10import java.io.IOException;
 11import java.net.URL;
 12import java.util.ArrayList;
 13import java.util.List;
 14import org.apache.log4j.Logger;
 15import org.apache.commons.io.FileUtils;
 16import org.springframework.beans.factory.annotation.Required;
 17
 18/**
 19 * Load ISO country codes and names from xml file
 20 *
 21 * @author geoff
 22 */
 23public class IsoCountriesImpl implements IsoCountries {
 24
 25    private Logger logger = Logger.getLogger(getClass());
 26
 27    /**
 28     * List of country codes
 29     */
 30    private List<String[]> countries = null;
 31
 32    /**
 33     */
 34    
 35
 36    private Settings settings = null;
 37
 38    /**
 39     * Return a list of string arrays, one array per country code, with indexes
 40     * corresponding to KEY and VALUE as defined above
 41     * @return
 42     */
 43    @Override
 44    public List<String[]> getCountries() {
 45        if (countries == null) {
 46            countries = parseCountryCodes();
 47        }
 48        return countries;
 49    }
 50
 51
 52    private List<String> readFile() {
 53        List<String> lines = null;
 54
 55        //  XML file of country codes, obtained from web/geonetwork/loc/ru/xml/countries.xml
 56        // within the geonetwork source tree and existing somewhere on the classpath
 57        URL url = getClass().getResource(settings.getIsoCountriesFilename());
 58        if (url == null) {
 59            logger.error(
 60                    "Error loading list of countries from xml file for registration system " +
 61                    "'"+ settings.getIsoCountriesFilename() + "' not found on classpath");
 62        } else {
 63            try {
 64                lines = FileUtils.readLines(new File(url.getFile()));
 65            } catch (IOException ex) {
 66                logger.error("Error reading iso country files.  Reason:  " + ex.getMessage());
 67                lines = null;
 68            }
 69        }
 70        return lines;
 71    }
 72
 73    /**
 74     * Load the iso country codes (for use within registration system) into
 75     * a map.  This will be stored in the config class and be made available
 76     * through the user's sesion
 77     *
 78     * @return A list of ISO country codes.  Each element contains a two element array
 79     * of country code followed by value.  Use the integer constants above
 80     * to reference them
 81     */
 82    private List<String[]> parseCountryCodes() {
 83
 84        // use a linked hash map to preseve ordering
 85        List<String[]> codes = new ArrayList<String[]>();
 86        List<String> lines = readFile();
 87        if (lines != null) {
 88            for (String line : lines) {
 89                if (line != null && line.contains("iso2")) {
 90                    // the key is whatever is between the first and second double quote
 91                    String key = line.replaceAll("^[^\"]*\"([^\"]*)\".*$", "$1");
 92
 93                    // the value is whatever is between the >< element delimiters
 94                    String value = line.replaceAll("^[^>]*>([^>]*)<.*$", "$1");
 95
 96                    codes.add(new String[]{key, value});
 97                }
 98                // else skip lines that dont have iso2 in them (xml prologue, containing element, etc)
 99            }
100        }
101        logger.info("loaded " + codes.size() + " country codes from " + settings.getIsoCountriesFilename());
102
103        return codes;
104    }
105
106    public Settings getSettings() {
107        return settings;
108    }
109
110    @Required
111    @CheckNotNull
112    public void setSettings(Settings settings) {
113        this.settings = settings;
114    }
115
116    
117}