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);
}
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);
}
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) {
}
}
}
}
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);
}
}
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"));
}
}
}
Aggregations