Search in sources :

Example 1 with NotNull

use of org.supercsv.cellprocessor.constraint.NotNull in project Xponents by OpenSextant.

the class TextUtils method initLOCLanguageData.

/**
     * This is Libray of Congress data for language IDs. This is offered as a
     * tool to help downstream language ID and enrich metadata when tagging data
     * from particular countries.
     *
     * Reference: http://www.loc.gov/standards/iso639-2/ISO-639-2_utf-8.txt
     *
     * @throws java.io.IOException
     *             if resource file is not found
     */
public static void initLOCLanguageData() throws java.io.IOException {
    //
    // DATA FILE: http://www.loc.gov/standards/iso639-2/ISO-639-2_utf-8.txt
    java.io.InputStream io = TextUtils.class.getResourceAsStream("/ISO-639-2_utf-8.txt");
    java.io.Reader featIO = new InputStreamReader(io, "UTF-8");
    CsvListReader langReader = new CsvListReader(featIO, new CsvPreference.Builder('"', '|', "\n").build());
    CellProcessor[] cells = { new Optional(), new Optional(), new Optional(), new Optional(), new NotNull() };
    List<Object> lang = null;
    /*
         * ISO3,XX,ISO2,NAME,NAME_FR
         */
    while ((lang = langReader.read(cells)) != null) {
        //
        String names = (String) lang.get(3);
        if (isBlank(names)) {
            continue;
        }
        if ("NAME".equals(names)) {
            continue;
        }
        List<String> namelist = TextUtils.string2list(names, ";");
        String iso3 = (String) lang.get(0);
        if (iso3.startsWith("#")) {
            continue;
        }
        String iso2 = (String) lang.get(2);
        Language l = new Language(iso3, iso2, namelist.get(0));
        addLanguage(l);
    }
    langReader.close();
    // Popular languages that go by other codes.
    // ISO languages as listed by LOC are listed with Bibliographic vs.
    // Terminological codes.
    // FRE vs. FRA are subtle difference for French, but important if you
    // cannot find French by lang ID.
    //
    // Fully override French and Trad Chinese:
    Language fr = new Language("fra", "fr", "French");
    addLanguage(fr, true);
    Language zhtw = new Language("zh-tw", "zt", "Chinese/Taiwain");
    addLanguage(zhtw, true);
    // Delicately insert more common names and codes as well as locales
    // here.
    Language zh = new Language("zho", "zh", "Chinese");
    languageMapISO639.put("zho", zh);
    Language zhcn = new Language("chi", "zh", "Chinese");
    languageMapISO639.put("zh-cn", zhcn);
    Language fas = new Language("per", "fa", "Farsi");
    languageMapISO639.put("farsi", fas);
    // Locales of English -- are still "English"
    Language en1 = new Language("eng", "en", "English");
    languageMapISO639.put("en-gb", en1);
    languageMapISO639.put("en-us", en1);
    languageMapISO639.put("en-au", en1);
}
Also used : CsvListReader(org.supercsv.io.CsvListReader) InputStreamReader(java.io.InputStreamReader) Optional(org.supercsv.cellprocessor.Optional) NotNull(org.supercsv.cellprocessor.constraint.NotNull) Language(org.opensextant.data.Language) CellProcessor(org.supercsv.cellprocessor.ift.CellProcessor)

Example 2 with NotNull

use of org.supercsv.cellprocessor.constraint.NotNull in project voltdb by VoltDB.

the class Symbols method loadFile.

public void loadFile(String filename) {
    // Schema for CSV file
    final CellProcessor[] processors = new CellProcessor[] { // Symbol
    new UniqueHashCode(), // Name
    new NotNull(), // LastSale
    new NotNull(), // MarketCap
    new NotNull(), // ADR TSO
    new NotNull(), // IPOyear
    new NotNull(), // Sector
    new NotNull(), // Industry
    new NotNull(), // Summary Quote
    new NotNull(), // blank column
    new Optional() };
    ICsvMapReader mapReader = null;
    try {
        mapReader = new CsvMapReader(new FileReader(filename), CsvPreference.STANDARD_PREFERENCE);
        // the header columns are used as the keys to the Map
        final String[] header = mapReader.getHeader(true);
        Map<String, Object> tuple;
        int rowsRead = 0;
        while ((tuple = mapReader.read(header, processors)) != null) {
            Symbol s = new Symbol();
            s.symbol = (String) tuple.get("Symbol");
            String price = (String) tuple.get("LastSale");
            if (price.equals("n/a")) {
                price = "20";
            }
            BigDecimal priceBD = new BigDecimal(price);
            s.price = priceBD.multiply(BD10000).intValue();
            symbols.add(s);
            rowsRead++;
        }
        System.out.printf("Read %d rows from CSV file at: %s\n", rowsRead, filename);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    } finally {
        if (mapReader != null) {
            try {
                mapReader.close();
            } catch (Exception e) {
            }
        }
    }
}
Also used : Optional(org.supercsv.cellprocessor.Optional) NotNull(org.supercsv.cellprocessor.constraint.NotNull) BigDecimal(java.math.BigDecimal) UniqueHashCode(org.supercsv.cellprocessor.constraint.UniqueHashCode) CellProcessor(org.supercsv.cellprocessor.ift.CellProcessor) ICsvMapReader(org.supercsv.io.ICsvMapReader) CsvMapReader(org.supercsv.io.CsvMapReader) FileReader(java.io.FileReader) ICsvMapReader(org.supercsv.io.ICsvMapReader)

Aggregations

Optional (org.supercsv.cellprocessor.Optional)2 NotNull (org.supercsv.cellprocessor.constraint.NotNull)2 CellProcessor (org.supercsv.cellprocessor.ift.CellProcessor)2 FileReader (java.io.FileReader)1 InputStreamReader (java.io.InputStreamReader)1 BigDecimal (java.math.BigDecimal)1 Language (org.opensextant.data.Language)1 UniqueHashCode (org.supercsv.cellprocessor.constraint.UniqueHashCode)1 CsvListReader (org.supercsv.io.CsvListReader)1 CsvMapReader (org.supercsv.io.CsvMapReader)1 ICsvMapReader (org.supercsv.io.ICsvMapReader)1