protected static Country parse()

in ch-commons-locale/src/main/java/com/cloudhopper/commons/locale/Country.java [139:170]


    protected static Country parse(String line) throws IOException {
        try {
            int pos = line.indexOf(' ');
            if (pos < 0) throw new IOException("Invalid format, could not parse 2 char code");
            String code2 = line.substring(0, pos);
            int pos2 = line.indexOf(' ', pos+1);
            if (pos2 < 0) throw new IOException("Invalid format, could not parse 3 char code");
            String code3 = line.substring(pos+1, pos2);
            int pos3 = line.indexOf(' ', pos2+1);
            if (pos3 < 0) throw new IOException("Invalid format, could not parse 3 char digit code");
            String num3 = line.substring(pos2+1, pos3);
            // rest of line is the long name
            String longName = line.substring(pos3+1).trim();
            // was there a name?
            if (longName == null || longName.equals("")) {
                throw new IOException("Country name was null or empty");
            }

            // parse long name into its short name (strip anything after the last comma)
            String name = longName;
            int pos4 = longName.lastIndexOf(',');
            if (pos4 > 0) {
                // strip out the final part such as ", Republic of" from something like "Nigeria, Republic Of"
                name = longName.substring(0, pos4);
            }

            // create the new country
            return new Country(code2, name, longName);
        } catch (Exception e) {
            throw new IOException("Failed while parsing country for line: " + line, e);
        }
    }