List of usage examples for org.apache.commons.csv CSVFormat EXCEL
CSVFormat EXCEL
To view the source code for org.apache.commons.csv CSVFormat EXCEL.
Click Source Link
From source file:co.cask.hydrator.plugin.CSVParser.java
@Override public void initialize(TransformContext context) throws Exception { super.initialize(context); String csvFormatString = config.format.toLowerCase(); switch (csvFormatString) { case "default": csvFormat = CSVFormat.DEFAULT;//from w w w . j a v a 2s . c om break; case "excel": csvFormat = CSVFormat.EXCEL; break; case "mysql": csvFormat = CSVFormat.MYSQL; break; case "rfc4180": csvFormat = CSVFormat.RFC4180; break; case "tdf": csvFormat = CSVFormat.TDF; break; case "pdl": csvFormat = PDL; break; default: throw new IllegalArgumentException( "Format {} specified is not one of the allowed format. Allowed formats are" + "DEFAULT, EXCEL, MYSQL, RFC4180, PDL and TDF"); } try { outSchema = Schema.parseJson(config.schema); fields = outSchema.getFields(); } catch (IOException e) { throw new IllegalArgumentException("Format of schema specified is invalid. Please check the format."); } }
From source file:br.edimarmanica.trinity.extract.Extract.java
protected void printResults(List<String> dataRecord, int offset) { /**/*from w w w .j a v a2s. co m*/ * ********************** results ****************** */ File dir = new File(Paths.PATH_TRINITY + site.getPath() + "/offset"); dir.mkdirs(); File file = new File(dir.getAbsolutePath() + "/result_" + offset + ".csv"); CSVFormat format = CSVFormat.EXCEL; try (Writer out = new FileWriter(file, append)) { try (CSVPrinter csvFilePrinter = new CSVPrinter(out, format)) { csvFilePrinter.printRecord(dataRecord); } } catch (IOException ex) { Logger.getLogger(Extract.class.getName()).log(Level.SEVERE, null, ex); } append = true; }
From source file:core.reporting.ImportFromFile.java
/** * validate the content of csv file against the column definition file. this methos store the valid record into a * buffer and record in system log file any error found in input file * /* w w w . ja v a 2 s . c o m*/ * @param infile - record list parsed from imput file * * @return number of error found. */ private int validateRecord(Iterable<CSVRecord> ircdlist) { // clear all previous log for import flag SystemLog.clearLogByFlag("ie"); // column definition Iterable<CSVRecord> coldefl = null; try { Reader in = new FileReader(TResourceUtils.getFile(columnModelDef + ".csv")); coldefl = (new CSVParser(in, CSVFormat.EXCEL.withHeader()).getRecords()); } catch (Exception e) { SystemLog.logException(e); } SimpleDateFormat dfomat = null; tempBuffer.clear(); int line = 0; int error = 0; int coldeflen = 0; fileColumns = ""; for (CSVRecord ircd : ircdlist) { Record frcd = new Record(recordModel); line++; coldeflen = 0; // premature return if error > coldefl if (error > coldeflen) { SystemLog.log("inputfile.msg05", "ie", "", error); return error; } for (CSVRecord cdr : coldefl) { coldeflen++; Object iobj = null; String siobj = null; String fieldn = null; // contain field name try { fieldn = cdr.get("field"); ircd.get(fieldn); } catch (Exception e) { // if field is mandatory, log error if (cdr.get("mandatory").equals("true")) { SystemLog.log("inputfile.msg02", "ie", "", line, cdr.get("field")); error++; } continue; } // value class. try { String cls = cdr.get("class"); // String class by default iobj = ircd.get(fieldn); if (cls.equals("Integer")) { iobj = Integer.parseInt(ircd.get(fieldn)); } if (cls.equals("Double")) { iobj = Double.parseDouble(ircd.get(fieldn)); } if (cls.equals("Date")) { // date may be not present dfomat = new SimpleDateFormat(cdr.get("format")); Date d = iobj.equals("") ? TStringUtils.ZERODATE : dfomat.parse((String) iobj); iobj = new java.sql.Date(d.getTime()); } } catch (Exception e) { SystemLog.log("inputfile.msg03", "ie", "", line, cdr.get("field"), cdr.get("class"), cdr.get("format")); error++; continue; } // valid value siobj = ircd.get(fieldn); boolean vvb = true; String vv = cdr.get("valid values"); if (!vv.equals("")) { vvb = false; String[] vvlst = vv.split(";"); for (String vvi : vvlst) { vvb = (siobj.equals(vvi)) ? true : vvb; } } if (vvb == false) { SystemLog.log("inputfile.msg04", "ie", "", line, cdr.get("field"), cdr.get("valid values")); error++; continue; } // no problem? add field String tf = cdr.get("target_field"); fileColumns += tf + ";"; frcd.setFieldValue(tf, iobj); } tempBuffer.add(frcd); } fileColumns = fileColumns.substring(0, fileColumns.length() - 1); return error; }
From source file:com.ibm.watson.app.qaclassifier.tools.GenerateTrainingAndPopulationData.java
/** * Reads in the question input file and creates a POJO for each question it finds. If the label associated with * the question does not exist in the previously read in answer store then it is skipped * /*w w w.j a v a2 s . c om*/ * @return TrainingData - full POJO of the training data */ private static NLClassifierTrainingData readQuestionInput(List<ManagedAnswer> store) { NLClassifierTrainingData data = null; try (FileReader reader = new FileReader(questionInput); CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL)) { // read in the csv file and get the records List<CSVRecord> records = parser.getRecords(); // now we can create the training data because we have read the records data = new NLClassifierTrainingData(); data.setLanguage("en"); for (CSVRecord r : records) { // order is: QuestionText, LabelId // check for existence of label first, if not there, skip // we only add the training instance if there is an associated answer String text = r.get(0); String label = r.get(1); if (labelHasAnswer(label, store)) { data.addTrainingData(text, label); } else { System.out.println(MessageKey.AQWQAC24009E_label_not_found_in_answer_store_including_2 .getMessage(text, label).getFormattedMessage()); } } } catch (Exception e) { e.printStackTrace(); } return data; }
From source file:edu.washington.gs.skyline.model.quantification.QuantificationTest.java
private List<ReplicateData> readReplicates(String filename) throws Exception { Map<String, ReplicateData> replicates = new LinkedHashMap<>(); Reader reader = new InputStreamReader(QuantificationTest.class.getResourceAsStream(filename)); try {// www .ja v a 2 s . co m CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader()); for (CSVRecord record : parser.getRecords()) { String fileName = record.get("FileName"); ReplicateData replicate = replicates.get(fileName); if (replicate == null) { replicate = new ReplicateData(); replicates.put(fileName, replicate); } } } finally { reader.close(); } throw new NotImplementedException(); }
From source file:com.edu.duke.FileResource.java
/** * Returns a <code>CSVParser</code> object to access the contents of an open file, possibly * without a header row and a different data delimiter than a comma. * // ww w . j av a 2 s. c om * Each line of the file should be formatted as data separated by the delimiter passed as a * parameter and with/without a header row to describe the column names. This is useful if the * data is separated by some character other than a comma. * * @param withHeader uses first row of data as a header row only if true * @param delimiter a single character that separates one field of data from another * @return a <code>CSVParser</code> that can provide access to the records in the file one at a * time */ public CSVParser getCSVParser(boolean withHeader, String delimiter) { if (delimiter == null || delimiter.length() != 1) { throw new ResourceException("FileResource: CSV delimiter must be a single character: " + delimiter); } try { char delim = delimiter.charAt(0); Reader input = new StringReader(mySource); if (withHeader) { return new CSVParser(input, CSVFormat.EXCEL.withHeader().withDelimiter(delim)); } else { return new CSVParser(input, CSVFormat.EXCEL.withDelimiter(delim)); } } catch (Exception e) { throw new ResourceException("FileResource: cannot read " + myPath + " as a CSV file."); } }
From source file:com.ggvaidya.scinames.ui.DatasetImporterController.java
private Dataset loadDataset() throws IOException { String format = fileFormatComboBox.getSelectionModel().getSelectedItem(); CSVFormat csvFormat = null;//from w ww . ja v a 2 s.c o m if (format == null) { csvFormat = CSVFormat.DEFAULT; } else { switch (format) { case "List of names": return Checklist.fromListInFile(currentFile); case "Default CSV": csvFormat = CSVFormat.DEFAULT; break; case "Microsoft Excel CSV": csvFormat = CSVFormat.EXCEL; break; case "RFC 4180 CSV": csvFormat = CSVFormat.RFC4180; break; case "Oracle MySQL CSV": csvFormat = CSVFormat.MYSQL; break; case "Tab-delimited file": csvFormat = CSVFormat.TDF; break; case "TaxDiff file": return ChecklistDiff.fromTaxDiffFile(currentFile); case "Excel file": return new ExcelImporter(currentFile).asDataset(0); } } if (csvFormat == null) { LOGGER.info("Could not determine CSV format from format '" + format + "', using CSV default."); csvFormat = CSVFormat.DEFAULT; } return Dataset.fromCSV(csvFormat, currentFile); }
From source file:com.ibm.watson.app.qaclassifier.tools.PopulateAnswerStore.java
/** * Reads in the answer input file and creates a POJO for each answer it finds. If the answer has no value * it is skipped.//from w w w. ja v a 2 s . co m * * @return AnswerStore - full POJO of the answer store read from the file */ private static List<ManagedAnswer> readAnswerInput(String content) { List<ManagedAnswer> store = null; // read the CVS of label to canonical question first try (StringReader reader = new StringReader(content); CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL);) { // read in the csv file and get the records List<CSVRecord> records = parser.getRecords(); // now we can create the answer store because we have read the records store = new ArrayList<ManagedAnswer>(); for (CSVRecord r : records) { // order is: LabelId, CanonicalQuestion // create the answer pojo ManagedAnswer answer = new ManagedAnswer(); answer.setClassName(r.get(0)); answer.setCanonicalQuestion(r.get(1)); answer.setType(TypeEnum.TEXT); // add to the managed answers list store.add(answer); } } catch (Exception e) { e.printStackTrace(); } return store; }
From source file:com.adobe.aem.demomachine.communities.SetupCommunities.java
@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException { PrintWriter out = response.getWriter(); // Checking if we have a valid admin user ResourceResolver resourceResolver = request.getResourceResolver(); String userId = resourceResolver.getUserID(); if (userId == null || !userId.equals("admin")) { out.println("Permission denied: admin user requested to access this feature"); return;/* w w w .j ava2 s . c o m*/ } // Checking if we have valid configuration parameters String csvPath = (String) request.getParameter("contentPath"); if (csvPath == null) { csvPath = ""; } // Checking the version of GraniteUI to be loaded String coralVersion = "3"; Resource resCoral = resourceResolver.getResource("/etc/clientlibs/granite/coralui3.js"); if (resCoral == null) coralVersion = "2"; response.setContentType("text/html"); out.println("<html><head>"); out.println("<link rel=\"stylesheet\" href=\"/etc/clientlibs/granite/coralui" + coralVersion + ".css\" type=\"text/css\">"); out.println("<script type=\"text/javascript\" src=\"/etc/clientlibs/granite/typekit.js\"></script>"); out.println("<script type=\"text/javascript\" src=\"/etc/clientlibs/granite/jquery.js\"></script>"); out.println("<script type=\"text/javascript\" src=\"/etc/clientlibs/granite/utils.js\"></script>"); out.println("<script type=\"text/javascript\" src=\"/etc/clientlibs/granite/moment.js\"></script>"); out.println("<script type=\"text/javascript\" src=\"/etc/clientlibs/granite/coralui" + coralVersion + ".js\"></script>"); out.println("</head><body class=\"coral--light u-coral-clearFix\" style=\"margin:40px\">"); // Checking if the page is loaded in a frame (e.g. authoring environment) out.println( "<script language=\"JavaScript\">if(window.frameElement){window.top.location=window.location.href;}</script>"); out.println("<a name=\"top\"/>"); out.println("<div><h1>AEM Communities - Demo Setup</h1>"); out.println( "<form action=\"/bin/CreateCommunities\" method=\"GET\" class=\"coral-Form coral-Form--vertical\" style=\"width:700px\">"); out.println("<section class=\"coral-Form-fieldset\">"); out.println( "<span>All the fun takes place on the Publish instance with AEM - please ensure yours is available at the following coordinates</spanl>"); out.println("<label class=\"coral-Form-fieldlabel\">Path to configuration files</label>"); out.println("<input is=\"coral-textfield\" name=\"contentPath\" type=\"text\" value=\"" + csvPath + "\" class=\"coral-Form-field coral-Textfield\">"); out.println("<label class=\"coral-Form-fieldlabel\">Author instance</label>"); out.println("<div class=\"coral-Form--aligned\">"); // Checking if the default host and port are reachable for the author server String hostname_author = "localhost"; String port_author = "4502"; if (!Hostname.isReachable(hostname_author, port_author)) { hostname_author = ""; port_author = ""; } out.println("<input is=\"coral-textfield\" name=\"hostname_author\" type=\"text\" value=\"" + hostname_author + "\" class=\"coral-Textfield\">"); out.println("<input is=\"coral-textfield\" name=\"port_author\" type=\"text\" value=\"" + port_author + "\" class=\"coral-Textfield\">"); out.println("</div>"); out.println("<label class=\"coral-Form-fieldlabel\">Publish instance</label>"); // Checking if the default host and port are reachable for the publish server String hostname_publish = "localhost"; String port_publish = "4503"; if (!Hostname.isReachable(hostname_publish, port_publish)) { hostname_publish = ""; port_publish = ""; out.println("<coral-alert>"); out.println("<coral-alert-header>WARNING</coral-alert-header>"); out.println( "<coral-alert-content>Using an AEM Publish instance is strongly recommended. If not using a Publish instance, all UGC will be posted against the Author instance, which might fail if the demo members are not granted appropriate permissions on Author.</coral-alert-content>"); out.println("</coral-alert>"); } out.println("<div class=\"coral-Form--aligned\">"); out.println("<input is=\"coral-textfield\" name=\"hostname\" type=\"text\" value=\"" + hostname_publish + "\" class=\"coral-Textfield\">"); out.println("<input is=\"coral-textfield\" name=\"port\" type=\"text\" value=\"" + port_publish + "\" class=\"coral-Textfield\">"); out.println("</div>"); out.println("<label class=\"coral-Form-fieldlabel\">Admin password</label>"); out.println( "<input is=\"coral-textfield\" name=\"password\" type=\"text\" value=\"admin\" class=\"coral-Form-field coral-Textfield\">"); out.println("<label class=\"coral-Form-fieldlabel\">Please select from the following options</label>"); // Getting the list of .csv configuration files for this content path int intOptions = 0; Resource resConfigFiles = resourceResolver.getResource(csvPath); if (!csvPath.equals("") && resConfigFiles != null) { ArrayList<String[]> configOptions = new ArrayList<String[]>(); for (Resource resConfigFile : resConfigFiles.getChildren()) { if (resConfigFile != null && resConfigFile.getName().endsWith(".csv")) { String[] resConfigSettings = resConfigFile.getName().split("-"); configOptions.add(resConfigSettings); } } Collections.sort(configOptions, new Comparator<String[]>() { public int compare(String[] strings, String[] otherStrings) { return strings[0].compareTo(otherStrings[0]); } }); for (String[] configOption : configOptions) { // Loading title and description String title = configOption[2]; String description = configOption[0] + "-" + configOption[1] + "-" + configOption[2]; Resource resConfigFile = resourceResolver.getResource(csvPath + "/" + description + "/jcr:content"); if (resConfigFile != null) { InputStream stream = resConfigFile.adaptTo(InputStream.class); Reader inConfigFile = new InputStreamReader(stream); Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(inConfigFile); for (CSVRecord record : records) { String rDescription = "# Description: "; if (record.get(0).startsWith(rDescription)) description = record.get(0).replace(rDescription, "").trim() + " (" + description + ")"; String rTitle = "# Title: "; if (record.get(0).startsWith(rTitle)) title = record.get(0).replace(rTitle, "").trim(); } intOptions++; try { inConfigFile.close(); stream.close(); } catch (IOException ioex) { //omitted. } } printCheckbox(out, "setup-" + configOption[0], title, description); } } if (intOptions > 0) { out.println("<div class=\"coral-Form-fieldwrapper coral-Form-fieldwrapper--alignRight\">"); out.println("<button class=\"coral-Form-field coral-Button coral-Button--primary\">Submit</button>"); out.println("</div>"); } else { out.println("<p>No configuration file to process</p>"); } String returnURL = (String) request.getParameter("returnURL"); if (returnURL != null) { out.println("<input type=\"hidden\" name=\"returnURL\" value=\"" + returnURL + "\">"); } out.println("</section></form>"); out.println("</body></html>"); }
From source file:com.hurence.logisland.service.cache.CSVKeyValueCacheService.java
@Override // @OnEnabled//from w ww.j a va2 s. co m public void init(ControllerServiceInitializationContext context) throws InitializationException { super.init(context); try { if (context.getPropertyValue(DATABASE_FILE_URI).isSet()) { dbUri = context.getPropertyValue(DATABASE_FILE_URI).asString(); } if (context.getPropertyValue(DATABASE_FILE_PATH).isSet()) { dbPath = context.getPropertyValue(DATABASE_FILE_PATH).asString(); } if ((dbUri == null) && (dbPath == null)) { throw new Exception( "You must declare " + DATABASE_FILE_URI.getName() + " or " + DATABASE_FILE_PATH.getName()); } InputStream is = null; if (dbUri != null) { logger.info("opening csv database from hdfs : " + dbUri); is = initFromUri(dbUri); } if (dbPath != null) { logger.info("opening csv database from local fs : " + dbPath); is = initFromPath(context, dbPath); } if (is == null) { throw new InitializationException("Something went wrong while initializing csv db from " + DATABASE_FILE_URI.getName() + " or " + DATABASE_FILE_PATH.getName()); } // final Reader reader = new InputStreamReader(is); CSVFormat format = CSVFormat.DEFAULT; if (context.getPropertyValue(CSV_FORMAT).asString().equals(CSV_EXCEL.getValue())) { format = CSVFormat.EXCEL; } else if (context.getPropertyValue(CSV_FORMAT).asString().equals(CSV_EXCEL_FR.getValue())) { format = CSVFormat.EXCEL.withDelimiter(';'); } else if (context.getPropertyValue(CSV_FORMAT).asString().equals(CSV_MYSQL.getValue())) { format = CSVFormat.MYSQL; } else if (context.getPropertyValue(CSV_FORMAT).asString().equals(CSV_RFC4180.getValue())) { format = CSVFormat.RFC4180; } else if (context.getPropertyValue(CSV_FORMAT).asString().equals(CSV_TDF.getValue())) { format = CSVFormat.TDF; } if (context.getPropertyValue(CSV_HEADER).isSet()) { String[] columnNames = context.getPropertyValue(CSV_HEADER).asString().split(","); for (String name : columnNames) { headers.get().put(name, "string"); } format = format.withHeader(columnNames); } else if (context.getPropertyValue(FIRST_LINE_HEADER).isSet()) { format = format.withFirstRecordAsHeader(); } else { throw new InitializationException("unable to get headers from somewhere"); } Charset charset = Charset.forName("UTF-8"); if (context.getPropertyValue(ENCODING_CHARSET).isSet()) { String encoding = context.getPropertyValue(ENCODING_CHARSET).asString(); charset = Charset.forName(encoding); } rowKey = context.getPropertyValue(ROW_KEY).asString(); CSVParser parser = CSVParser.parse(is, charset, format); //new CSVParser(reader, format); /* * CSVParser parser = null; if (context.getPropertyValue(ENCODING_CHARSET).isSet()) { String encoding = context.getPropertyValue(ENCODING_CHARSET).asString(); parser = CSVParser.parse(reader, Charset.forName(encoding), format); } else { parser = CSVParser.parse(reader, format); } */ long count = 0; try { final Set<String> columnNames = parser.getHeaderMap().keySet(); for (final CSVRecord record : parser) { Record logislandRecord = new StandardRecord(); for (final String column : columnNames) { logislandRecord.setStringField(column, record.get(column)); } set(logislandRecord.getField(rowKey).asString(), logislandRecord); count++; } } finally { logger.info("successfully loaded " + count + " records from CSV file"); parser.close(); is.close(); } } catch (Exception e) { getLogger().error("Could not load database file: {}", new Object[] { e.getMessage() }); throw new InitializationException(e); } }