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