private String lookupCountryCode(String code) { Country c = Country.fromIsoCode(code); if (c != null) { return c.getTitle(); } return code; }
private void insertCountryLinks() { final String countryUrl = getBaseUrl() + "/country/"; // main description Matcher m = COUNTRY_REG_EX.matcher(getDataset().getDescription()); StringBuffer sb = new StringBuffer(); while (m.find()) { Country c = COUNTRY_MAP.get(m.group(1).toLowerCase()); String replacement; if (c != null) { replacement = "<a href='" + countryUrl + c.getIso2LetterCode() + "'>" + m.group(1) + "</a>"; } else { replacement = m.group(1); } m.appendReplacement(sb, replacement); } m.appendTail(sb); getDataset().setDescription(sb.toString()); // spatial coverage for (GeospatialCoverage gc : getDataset().getGeographicCoverages()) { // There are not yet any links to the actual countries m = COUNTRY_REG_EX.matcher(gc.getDescription()); sb = new StringBuffer(); while (m.find()) { Country c = COUNTRY_MAP.get(m.group(1).toLowerCase()); String replacement = "<a href='" + countryUrl + c.getIso2LetterCode() + "'>" + m.group(1) + "</a>"; m.appendReplacement(sb, replacement); } m.appendTail(sb); gc.setDescription(sb.toString()); } }
/** Builds a regular expression from all the country names, to allow replacement as links. */ private static String countryTitleRegEx() { ImmutableList.Builder<String> b = ImmutableList.builder(); for (Country c : Country.values()) { if (c == null || Strings.isNullOrEmpty(c.getTitle())) { continue; } b.add(c.getTitle()); COUNTRY_MAP.put(c.getTitle().toLowerCase(), c); } return "\\b(" + Joiner.on("|").join(b.build()) + ")\\b"; }