Search in sources :

Example 1 with Optional

use of org.supercsv.cellprocessor.Optional in project Xponents by OpenSextant.

the class CSVFormatter method buildSchema.

/**
     * Create a schema instance with the fields properly typed and ordered
     *
     * @throws ConfigException schema configuration error
     */
protected void buildSchema() throws ConfigException {
    if (!this.includeOffsets) {
        //field_order.add("start");
        //field_order.add("end");
        //} else {
        fieldOrder.remove("start");
        fieldOrder.remove("end");
    }
    outputSchema = new CellProcessor[fieldOrder.size()];
    header = new String[fieldOrder.size()];
    fieldOrder.toArray(header);
    for (int x = 0; x < fieldOrder.size(); ++x) {
        outputSchema[x] = new Optional();
    }
    this.fieldSet.addAll(fieldOrder);
}
Also used : Optional(org.supercsv.cellprocessor.Optional)

Example 2 with Optional

use of org.supercsv.cellprocessor.Optional 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 3 with Optional

use of org.supercsv.cellprocessor.Optional 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)

Example 4 with Optional

use of org.supercsv.cellprocessor.Optional in project photon-model by vmware.

the class AWSCsvBillParser method parseDetailedCsvBill.

private void parseDetailedCsvBill(InputStream inputStream, Collection<String> ignorableInvoiceCharge, Set<String> configuredAccounts, BiConsumer<Map<String, AwsAccountDetailDto>, String> hourlyStatsConsumer, Consumer<Map<String, AwsAccountDetailDto>> monthlyStatsConsumer) throws IOException {
    final CsvPreference STANDARD_SKIP_COMMENTS = new CsvPreference.Builder(CsvPreference.STANDARD_PREFERENCE).skipComments(new CommentStartsWith(AWS_SKIP_COMMENTS)).build();
    try (InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
        ICsvMapReader mapReader = new CsvMapReader(reader, STANDARD_SKIP_COMMENTS)) {
        final String[] header = mapReader.getHeader(true);
        List<CellProcessor> processorList = new ArrayList<>();
        final CellProcessor[] basicProcessors = getDetailedProcessors(header);
        processorList.addAll(Arrays.asList(basicProcessors));
        List<String> tagHeaders = new ArrayList<>();
        // Add new cell-processors for each extra tag column
        int numberOfTags = header.length - basicProcessors.length;
        if (numberOfTags > 0) {
            for (int i = 0; i < numberOfTags; i++) {
                processorList.add(new Optional());
                tagHeaders.add(header[basicProcessors.length + i]);
            }
        }
        CellProcessor[] cellProcessorArray = new CellProcessor[processorList.size()];
        Map<String, AwsAccountDetailDto> monthlyBill = new HashMap<>();
        cellProcessorArray = processorList.toArray(cellProcessorArray);
        Map<String, Object> rowMap;
        Long prevRowTime = null;
        Long prevRowEndTime;
        String interval = null;
        while ((rowMap = mapReader.read(header, cellProcessorArray)) != null) {
            LocalDateTime currRowLocalDateTime = (LocalDateTime) rowMap.get(DetailedCsvHeaders.USAGE_START_DATE);
            Long curRowTime = getMillisForHour(currRowLocalDateTime);
            if (prevRowTime != null && curRowTime != null && !prevRowTime.equals(curRowTime) && !StringUtils.contains(interval, "-")) {
                // This indicates that we have processed all rows belonging to a corresponding hour in the
                // current month bill. Consume the batch
                hourlyStatsConsumer.accept(monthlyBill, interval);
            }
            try {
                readRow(rowMap, monthlyBill, tagHeaders, ignorableInvoiceCharge, configuredAccounts);
            } catch (Exception e) {
                this.logger.warning(String.format("Got error while parsing a row in aws bill of %s", getStringFieldValue(rowMap, DetailedCsvHeaders.PAYER_ACCOUNT_ID) + e));
            }
            if (curRowTime != null) {
                prevRowTime = curRowTime;
                prevRowEndTime = getMillisForHour((LocalDateTime) rowMap.get(DetailedCsvHeaders.USAGE_END_DATE));
                interval = createInterval(prevRowTime, prevRowEndTime);
            }
        }
        // Consume the final batch of parsed rows
        hourlyStatsConsumer.accept(monthlyBill, interval);
        monthlyStatsConsumer.accept(monthlyBill);
    }
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) InputStreamReader(java.io.InputStreamReader) Optional(org.supercsv.cellprocessor.Optional) HashMap(java.util.HashMap) CommentStartsWith(org.supercsv.comment.CommentStartsWith) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SuperCsvCellProcessorException(org.supercsv.exception.SuperCsvCellProcessorException) CsvPreference(org.supercsv.prefs.CsvPreference) AwsAccountDetailDto(com.vmware.photon.controller.model.adapters.aws.dto.AwsAccountDetailDto) ICsvMapReader(org.supercsv.io.ICsvMapReader) CsvMapReader(org.supercsv.io.CsvMapReader) CellProcessor(org.supercsv.cellprocessor.ift.CellProcessor) ICsvMapReader(org.supercsv.io.ICsvMapReader)

Example 5 with Optional

use of org.supercsv.cellprocessor.Optional in project apex-malhar by apache.

the class AbstractCsvParser method initialise.

// Initialise the properties and processors.
public void initialise(String[] properties, CellProcessor[] processors) {
    for (int i = 0; i < getFields().size(); i++) {
        FIELD_TYPE type = getFields().get(i).type;
        properties[i] = getFields().get(i).name;
        if (type == FIELD_TYPE.DOUBLE) {
            processors[i] = new Optional(new ParseDouble());
        } else if (type == FIELD_TYPE.INTEGER) {
            processors[i] = new Optional(new ParseInt());
        } else if (type == FIELD_TYPE.FLOAT) {
            processors[i] = new Optional(new ParseDouble());
        } else if (type == FIELD_TYPE.LONG) {
            processors[i] = new Optional(new ParseLong());
        } else if (type == FIELD_TYPE.SHORT) {
            processors[i] = new Optional(new ParseInt());
        } else if (type == FIELD_TYPE.STRING) {
            processors[i] = new Optional();
        } else if (type == FIELD_TYPE.CHARACTER) {
            processors[i] = new Optional(new ParseChar());
        } else if (type == FIELD_TYPE.BOOLEAN) {
            processors[i] = new Optional(new ParseChar());
        } else if (type == FIELD_TYPE.DATE) {
            processors[i] = new Optional(new ParseDate("dd/MM/yyyy"));
        }
    }
}
Also used : Optional(org.supercsv.cellprocessor.Optional) ParseLong(org.supercsv.cellprocessor.ParseLong) ParseDate(org.supercsv.cellprocessor.ParseDate) ParseDouble(org.supercsv.cellprocessor.ParseDouble) ParseInt(org.supercsv.cellprocessor.ParseInt) ParseChar(org.supercsv.cellprocessor.ParseChar)

Aggregations

Optional (org.supercsv.cellprocessor.Optional)6 CellProcessor (org.supercsv.cellprocessor.ift.CellProcessor)4 InputStreamReader (java.io.InputStreamReader)2 NotNull (org.supercsv.cellprocessor.constraint.NotNull)2 CsvMapReader (org.supercsv.io.CsvMapReader)2 ICsvMapReader (org.supercsv.io.ICsvMapReader)2 AwsAccountDetailDto (com.vmware.photon.controller.model.adapters.aws.dto.AwsAccountDetailDto)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Field (org.apache.apex.malhar.contrib.parser.DelimitedSchema.Field)1 LocalDateTime (org.joda.time.LocalDateTime)1 Language (org.opensextant.data.Language)1 FmtDate (org.supercsv.cellprocessor.FmtDate)1 ParseChar (org.supercsv.cellprocessor.ParseChar)1 ParseDate (org.supercsv.cellprocessor.ParseDate)1 ParseDouble (org.supercsv.cellprocessor.ParseDouble)1 ParseInt (org.supercsv.cellprocessor.ParseInt)1