List of usage examples for org.apache.commons.csv CSVStrategy EXCEL_STRATEGY
CSVStrategy EXCEL_STRATEGY
To view the source code for org.apache.commons.csv CSVStrategy EXCEL_STRATEGY.
Click Source Link
From source file:com.griddynamics.jagger.providers.csv.CSVProviderTest.java
@Test public static void test() throws Exception { CsvProvider<RequestPath> iterable = new CsvProvider<RequestPath>(); iterable.setObjectCreator(new RequestPathCvsWrapper()); iterable.setPath("src/test/resources/requests.csv"); iterable.setStrategy(CSVStrategy.EXCEL_STRATEGY); iterable.setReadHeader(true);//w w w . ja va 2s. c om RequestPath[] requestPaths = new RequestPath[] { new RequestPath("http://localhost", "sleep/10"), new RequestPath("http://localhost", "sleep/10"), new RequestPath("http://localhost:8080", "sleep/10") }; testIterable(iterable, requestPaths); testIterable(iterable, requestPaths); iterable = new CsvProvider<RequestPath>(); iterable.setObjectCreator(new RequestPathCvsWrapper()); iterable.setPath("src/test/resources/requests.csv"); iterable.setReadHeader(true); testIterable(iterable, requestPaths); }
From source file:com.griddynamics.jagger.xml.beanParsers.workload.queryProvider.CsvProviderDefinitionParser.java
@Override protected void parse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { Preconditions.checkArgument(element.hasAttribute("path")); builder.addPropertyValue("path", element.getAttribute("path")); if (element.hasAttribute("readHeader")) { builder.addPropertyValue("readHeader", element.getAttribute("readHeader").equals("true")); }/*from w w w .j a va2 s.co m*/ if (element.hasAttribute("strategy")) { if (element.getAttribute("strategy").equals("DEFAULT")) { builder.addPropertyValue("strategy", CSVStrategy.DEFAULT_STRATEGY); } else if (element.getAttribute("strategy").equals("EXCEL")) { builder.addPropertyValue("strategy", CSVStrategy.EXCEL_STRATEGY); } else if (element.getAttribute("strategy").equals("TDF")) { builder.addPropertyValue("strategy", CSVStrategy.TDF_STRATEGY); } else { throw new TechnicalException("Strategy '" + element.getAttribute("strategy") + "' not found!"); } } List childes = parseCustomListElement(element, parserContext, builder.getBeanDefinition()); Preconditions.checkState(childes != null, "Must specify objectCreator in CSVProvider"); builder.addPropertyValue("objectCreator", childes.get(0)); }
From source file:com.quanticate.opensource.datalistdownload.DeclarativeSpreadsheetWebScript.java
/** * Generates the spreadsheet, based on the properties in the header * and a callback for the body./*from www . jav a2 s .c o m*/ */ public void generateSpreadsheet(Object resource, String format, WebScriptRequest req, Status status, Map<String, Object> model) throws IOException { Pattern qnameMunger = Pattern.compile("([A-Z][a-z]+)([A-Z].*)"); // Build up the details of the header List<Pair<QName, Boolean>> propertyDetails = buildPropertiesForHeader(resource, format, req); String[] headings = new String[propertyDetails.size()]; String[] descriptions = new String[propertyDetails.size()]; boolean[] required = new boolean[propertyDetails.size()]; for (int i = 0; i < headings.length; i++) { Pair<QName, Boolean> property = propertyDetails.get(i); if (property == null || property.getFirst() == null) { headings[i] = ""; required[i] = false; } else { QName column = property.getFirst(); required[i] = property.getSecond(); // Ask the dictionary service nicely for the details PropertyDefinition pd = dictionaryService.getProperty(column); if (pd != null && pd.getTitle(dictionaryService) != null) { // Use the friendly titles, which may even be localised! headings[i] = pd.getTitle(dictionaryService); descriptions[i] = pd.getDescription(dictionaryService); } else { // Nothing friendly found, try to munge the raw qname into // something we can show to a user... String raw = column.getLocalName(); raw = raw.substring(0, 1).toUpperCase() + raw.substring(1); Matcher m = qnameMunger.matcher(raw); if (m.matches()) { headings[i] = m.group(1) + " " + m.group(2); } else { headings[i] = raw; } } } } // Build a list of just the properties List<QName> properties = new ArrayList<QName>(propertyDetails.size()); for (Pair<QName, Boolean> p : propertyDetails) { QName qn = null; if (p != null) { qn = p.getFirst(); } properties.add(qn); } // Output if ("csv".equals(format)) { StringWriter sw = new StringWriter(); CSVPrinter csv = new CSVPrinter(sw, CSVStrategy.EXCEL_STRATEGY); csv.println(headings); populateBody(resource, csv, properties); model.put(MODEL_CSV, sw.toString()); } else if ("odf".equals(format) || "ods".equals(format)) { try { SpreadsheetDocument odf = SpreadsheetDocument.newSpreadsheetDocument(); // Add the header row Table sheet = odf.appendSheet("Export"); org.odftoolkit.simple.table.Row hr = sheet.appendRow(); // TODO // Have the contents populated // TODO // Save it for the template ByteArrayOutputStream baos = new ByteArrayOutputStream(); odf.save(baos); model.put(MODEL_ODF, baos.toByteArray()); } catch (Exception e) { throw new WebScriptException("Error creating ODF file", e); } } else { Workbook wb; if ("xlsx".equals(format)) { wb = new XSSFWorkbook(); // TODO Properties } else { wb = new HSSFWorkbook(); // TODO Properties } // Add our header row Sheet sheet = wb.createSheet("Export"); Row hr = sheet.createRow(0); sheet.createFreezePane(0, 1); Font fb = wb.createFont(); fb.setBoldweight(Font.BOLDWEIGHT_BOLD); Font fi = wb.createFont(); fi.setBoldweight(Font.BOLDWEIGHT_BOLD); fi.setItalic(true); CellStyle csReq = wb.createCellStyle(); csReq.setFont(fb); CellStyle csOpt = wb.createCellStyle(); csOpt.setFont(fi); // Populate the header Drawing draw = null; for (int i = 0; i < headings.length; i++) { Cell c = hr.createCell(i); c.setCellValue(headings[i]); if (required[i]) { c.setCellStyle(csReq); } else { c.setCellStyle(csOpt); } if (headings[i].length() == 0) { sheet.setColumnWidth(i, 3 * 250); } else { sheet.setColumnWidth(i, 18 * 250); } if (descriptions[i] != null && descriptions[i].length() > 0) { // Add a description for it too if (draw == null) { draw = sheet.createDrawingPatriarch(); } ClientAnchor ca = wb.getCreationHelper().createClientAnchor(); ca.setCol1(c.getColumnIndex()); ca.setCol2(c.getColumnIndex() + 1); ca.setRow1(hr.getRowNum()); ca.setRow2(hr.getRowNum() + 2); Comment cmt = draw.createCellComment(ca); cmt.setAuthor(""); cmt.setString(wb.getCreationHelper().createRichTextString(descriptions[i])); cmt.setVisible(false); c.setCellComment(cmt); } } // Have the contents populated populateBody(resource, wb, sheet, properties); // Save it for the template ByteArrayOutputStream baos = new ByteArrayOutputStream(); wb.write(baos); model.put(MODEL_EXCEL, baos.toByteArray()); } }
From source file:de.fme.alfresco.repo.web.scripts.DeclarativeSpreadsheetWebScript.java
/** * Generates the spreadsheet, based on the properties in the header * and a callback for the body.//from ww w.ja v a2s .com */ public void generateSpreadsheet(Object resource, String format, WebScriptRequest req, Status status, Map<String, Object> model) throws IOException { Pattern qnameMunger = Pattern.compile("([A-Z][a-z]+)([A-Z].*)"); // Build up the details of the header List<Pair<QName, Boolean>> propertyDetails = buildPropertiesForHeader(resource, format, req); String[] headings = new String[propertyDetails.size()]; String[] descriptions = new String[propertyDetails.size()]; boolean[] required = new boolean[propertyDetails.size()]; for (int i = 0; i < headings.length; i++) { Pair<QName, Boolean> property = propertyDetails.get(i); if (property == null || property.getFirst() == null) { headings[i] = ""; required[i] = false; } else { QName column = property.getFirst(); required[i] = property.getSecond(); // Ask the dictionary service nicely for the details PropertyDefinition pd = dictionaryService.getProperty(column); if ((pd != null) && (pd.getTitle(messageLookup) != null)) { // Use the friendly titles, which may even be localised! headings[i] = pd.getTitle(messageLookup); descriptions[i] = pd.getDescription(messageLookup); } else { // Nothing friendly found, try to munge the raw qname into // something we can show to a user... String raw = column.getLocalName(); raw = raw.substring(0, 1).toUpperCase() + raw.substring(1); Matcher m = qnameMunger.matcher(raw); if (m.matches()) { headings[i] = m.group(1) + " " + m.group(2); } else { headings[i] = raw; } } } } // Build a list of just the properties List<QName> properties = new ArrayList<QName>(propertyDetails.size()); for (Pair<QName, Boolean> p : propertyDetails) { QName qn = null; if (p != null) { qn = p.getFirst(); } properties.add(qn); } // Output if ("csv".equals(format)) { StringWriter sw = new StringWriter(); CSVPrinter csv = new CSVPrinter(sw, CSVStrategy.EXCEL_STRATEGY); csv.println(headings); populateBody(resource, csv, properties); model.put(MODEL_CSV, sw.toString()); } else { Workbook wb; if ("xlsx".equals(format)) { wb = new XSSFWorkbook(); // TODO Properties } else { wb = new HSSFWorkbook(); // TODO Properties } // Add our header row Sheet sheet = wb.createSheet("Export"); Row hr = sheet.createRow(0); try { sheet.createFreezePane(0, 1); } catch (IndexOutOfBoundsException e) { //https://issues.apache.org/bugzilla/show_bug.cgi?id=51431 & http://stackoverflow.com/questions/6469693/apache-poi-clearing-freeze-split-panes } Font fb = wb.createFont(); fb.setBoldweight(Font.BOLDWEIGHT_BOLD); Font fi = wb.createFont(); fi.setBoldweight(Font.BOLDWEIGHT_BOLD); fi.setItalic(true); CellStyle csReq = wb.createCellStyle(); csReq.setFont(fb); CellStyle csOpt = wb.createCellStyle(); csOpt.setFont(fi); // Populate the header Drawing draw = null; for (int i = 0; i < headings.length; i++) { Cell c = hr.createCell(i); c.setCellValue(headings[i]); if (required[i]) { c.setCellStyle(csReq); } else { c.setCellStyle(csOpt); } if (headings[i].length() == 0) { sheet.setColumnWidth(i, 3 * 250); } else { sheet.setColumnWidth(i, 18 * 250); } if (descriptions[i] != null && descriptions[i].length() > 0) { // Add a description for it too if (draw == null) { draw = sheet.createDrawingPatriarch(); } ClientAnchor ca = wb.getCreationHelper().createClientAnchor(); ca.setCol1(c.getColumnIndex()); ca.setCol2(c.getColumnIndex() + 1); ca.setRow1(hr.getRowNum()); ca.setRow2(hr.getRowNum() + 2); Comment cmt = draw.createCellComment(ca); cmt.setAuthor(""); cmt.setString(wb.getCreationHelper().createRichTextString(descriptions[i])); cmt.setVisible(false); c.setCellComment(cmt); } } // Have the contents populated populateBody(resource, wb, sheet, properties); // Save it for the template ByteArrayOutputStream baos = new ByteArrayOutputStream(); wb.write(baos); model.put(MODEL_EXCEL, baos.toByteArray()); } }
From source file:org.alfresco.repo.web.scripts.DeclarativeSpreadsheetWebScript.java
/** * Get the CSVStrategy. Returns {@link CSVStrategy#EXCEL_STRATEGY} if none was set. * /* w ww .j av a 2 s.c o m*/ * @return CSVStrategy */ public CSVStrategy getCsvStrategy() { if (csvStrategy == null) { return CSVStrategy.EXCEL_STRATEGY; } else { return csvStrategy; } }
From source file:org.alfresco.repo.web.scripts.person.UserCSVUploadPost.java
protected void processCSVUpload(InputStream input, List<Map<QName, String>> users) throws IOException { InputStreamReader reader = new InputStreamReader(input, Charset.forName("UTF-8")); CSVParser csv = new CSVParser(reader, CSVStrategy.EXCEL_STRATEGY); String[][] data = csv.getAllValues(); if (data != null && data.length > 0) { processSpreadsheetUpload(data, users); }/*from w w w. ja v a 2 s . co m*/ }
From source file:org.marketcetera.orderloader.OrderParser.java
/** * Parses rows out of the supplied file and uses the processors to * process them.//w w w . j av a 2 s . c om * * @param inStream the input stream with csv input containing orders. * The stream is closed when this method returns. * * @throws IOException if there was an error opening the supplied file or * if the file had no orders to send. * @throws OrderParsingException if the file didn't have the * column headers specified correctly or if the file didn't have any orders. */ public void parseOrders(InputStream inStream) throws IOException, OrderParsingException { UnicodeInputStreamReader reader = null; try { reader = new UnicodeInputStreamReader(inStream, DecodingStrategy.SIG_REQ); String[][] rows = new CSVParser(reader, CSVStrategy.EXCEL_STRATEGY).getAllValues(); boolean isProcessorInit = false; if (rows != null) { for (String[] row : rows) { mNumLines++; //Ignore empty lines. if (row.length == 0 || row.length == 1 && row[0].trim().isEmpty()) { mNumBlankLines++; } else if (row[0].startsWith(COMMENT_MARKER)) { mNumComments++; } else { if (isProcessorInit) { getProcessor().processOrder(mNumLines, row); } else { getProcessor().initialize(row); isProcessorInit = true; } } } } if (getProcessor().getTotal() < 1) { throw new OrderParsingException(ERROR_NO_ORDERS); } } finally { if (reader != null) { reader.close(); } if (inStream != null) { inStream.close(); } } }
From source file:org.openiam.idm.srvc.synch.service.generic.CSVAdapterForGenericObject.java
@Override public SyncResponse startSynch(SynchConfig config, SynchReviewEntity sourceReview, SynchReviewEntity resultReview) { log.debug("Starting to Sync CSV File..^^^^^^^^"); File file = new File(config.getFileName()); InputStream input = null;/*from ww w . j ava 2s . co m*/ try { input = new FileInputStream(file); } catch (FileNotFoundException fe) { fe.printStackTrace(); log.error(fe); SyncResponse resp = new SyncResponse(ResponseStatus.FAILURE); resp.setErrorCode(ResponseCode.FILE_EXCEPTION); return resp; } try { CSVHelper parser = new CSVHelper(input, CSVStrategy.EXCEL_STRATEGY); String[][] fileContentAry = parser.getAllValues(); int ctr = 0; for (String[] lineAry : fileContentAry) { log.debug("File Row #= " + lineAry[0]); if (ctr == 0) { populateTemplate(lineAry); ctr++; } else { // populate the data object pUser = new ProvisionUser(); LineObject rowObj = rowHeader.copy(); populateRowObject(rowObj, lineAry); try { // validate if (config.getValidationRule() != null && config.getValidationRule().length() > 0) { SynchReview review = null; if (sourceReview != null) { review = synchReviewDozerConverter.convertToDTO(sourceReview, false); } ValidationScript script = SynchScriptFactory.createValidationScript(config, review); int retval = script.isValid(rowObj); if (retval == ValidationScript.NOT_VALID) { log.debug("Validation failed..."); // log this object in the exception log } if (retval == ValidationScript.SKIP) { continue; } else if (retval == ValidationScript.SKIP_TO_REVIEW) { if (resultReview != null) { resultReview.addRecord(generateSynchReviewRecord(rowObj)); } continue; } } System.out.println("Getting column map..."); // check if the user exists or not Map<String, Attribute> rowAttr = rowObj.getColumnMap(); // // show the user object } catch (ClassNotFoundException cnfe) { log.error(cnfe); SyncResponse resp = new SyncResponse(ResponseStatus.FAILURE); resp.setErrorCode(ResponseCode.CLASS_NOT_FOUND); return resp; } } } } catch (IOException io) { io.printStackTrace(); SyncResponse resp = new SyncResponse(ResponseStatus.FAILURE); resp.setErrorCode(ResponseCode.IO_EXCEPTION); return resp; } finally { if (resultReview != null) { if (CollectionUtils.isNotEmpty(resultReview.getReviewRecords())) { // add header row resultReview.addRecord(generateSynchReviewRecord(rowHeader, true)); } } if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } log.debug("CSV SYNCHRONIZATION COMPLETE^^^^^^^^"); SyncResponse resp = new SyncResponse(ResponseStatus.SUCCESS); return resp; }
From source file:org.openiam.idm.srvc.synch.srcadapter.CSVAdapter.java
@Override public SyncResponse startSynch(final SynchConfig config, SynchReviewEntity sourceReview, final SynchReviewEntity resultReview) { log.debug("CSV startSynch CALLED.^^^^^^^^"); System.out.println("CSV startSynch CALLED.^^^^^^^^"); SyncResponse res = new SyncResponse(ResponseStatus.SUCCESS); SynchReview review = null;/*from w ww . j a va 2 s . c o m*/ if (sourceReview != null) { review = synchReviewDozerConverter.convertToDTO(sourceReview, false); } LineObject rowHeaderForReport = null; InputStream input = null; try { final ValidationScript validationScript = StringUtils.isNotEmpty(config.getValidationRule()) ? SynchScriptFactory.createValidationScript(config, review) : null; final List<TransformScript> transformScripts = SynchScriptFactory.createTransformationScript(config, review); final MatchObjectRule matchRule = matchRuleFactory.create(config.getCustomMatchRule()); // check if matchRule exists if (validationScript == null || transformScripts == null || matchRule == null) { res = new SyncResponse(ResponseStatus.FAILURE); res.setErrorText("The problem in initialization of CSVAdapter, please check validationScript= " + validationScript + ", transformScripts=" + transformScripts + ", matchRule=" + matchRule + " all must be set!"); res.setErrorCode(ResponseCode.INVALID_ARGUMENTS); return res; } if (sourceReview != null && !sourceReview.isSourceRejected()) { return startSynchReview(config, sourceReview, resultReview, validationScript, transformScripts, matchRule); } CSVHelper parser; String csvFileName = config.getFileName(); if (useRemoteFilestorage) { input = remoteFileStorageManager.downloadFile(SYNC_DIR, csvFileName); parser = new CSVHelper(input, "UTF-8"); } else { String fileName = uploadRoot + File.separator + SYNC_DIR + File.separator + csvFileName; input = new FileInputStream(fileName); parser = new CSVHelper(input, "UTF-8", CSVStrategy.EXCEL_STRATEGY); } final String[][] rows = parser.getAllValues(); //Get Header final LineObject rowHeader = populateTemplate(rows[0]); rowHeaderForReport = rowHeader; if (rows.length > 1) { int part = rows.length / THREAD_COUNT; int remains = rows.length - part * THREAD_COUNT; List<Part> partsList = new ArrayList<Part>(); for (int i = 0; i < THREAD_COUNT; i++) { if (i != THREAD_COUNT - 1) { partsList.add(new Part(i * part, (i + 1) * part)); } else { partsList.add(new Part(i * part, (i + 1) * part + remains)); } } final Counter counter = new Counter(); ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT); List<Future<Integer>> list = new ArrayList<Future<Integer>>(); final String[][] rowsWithoutHeader = Arrays.copyOfRange(rows, 1, rows.length); for (final Part p : partsList) { Callable<Integer> worker = new Callable<Integer>() { @Override public Integer call() throws Exception { System.out.println("======= CSV Adapter Part [" + p.getStartIndx() + "; " + p.getEndIndx() + "] started."); int number = 0; String[][] rowsForProcessing = Arrays.copyOfRange(rowsWithoutHeader, p.getStartIndx(), p.getEndIndx()); for (String[] row : rowsForProcessing) { LineObject rowObj = rowHeader.copy(); populateRowObject(rowObj, row); processLineObject(rowObj, config, resultReview, validationScript, transformScripts, matchRule); number = counter.increment(); System.out.println("======= CSV Adapter Part [" + p.getStartIndx() + "; " + p.getEndIndx() + "] counter.increment = " + number); } System.out.println("======= CSV Adapter Part [" + p.getStartIndx() + "; " + p.getEndIndx() + "] finished."); return number; } }; Future<Integer> submit = executor.submit(worker); list.add(submit); } // This will make the executor accept no new threads // and finish all existing threads in the queue executor.shutdown(); // Wait until all threads are finish while (!executor.isTerminated()) { } Integer set = 0; for (Future<Integer> future : list) { try { set += future.get(); } catch (InterruptedException e) { log.warn(e.getMessage()); } catch (ExecutionException e) { log.warn("CSVAdapter: future.get() throw problem message"); } } System.out.println("CSV ================= All Processed records = " + set); } } catch (ClassNotFoundException cnfe) { log.error(cnfe); res = new SyncResponse(ResponseStatus.FAILURE); res.setErrorCode(ResponseCode.CLASS_NOT_FOUND); return res; } catch (FileNotFoundException fe) { fe.printStackTrace(); log.error(fe); // auditBuilder.addAttribute(AuditAttributeName.DESCRIPTION, "FileNotFoundException: "+fe.getMessage()); // auditLogProvider.persist(auditBuilder); SyncResponse resp = new SyncResponse(ResponseStatus.FAILURE); resp.setErrorCode(ResponseCode.FILE_EXCEPTION); log.debug("CSV SYNCHRONIZATION COMPLETE WITH ERRORS ^^^^^^^^"); return resp; } catch (IOException io) { io.printStackTrace(); /* synchStartLog.updateSynchAttributes("FAIL", ResponseCode.IO_EXCEPTION.toString(), io.toString()); auditHelper.logEvent(synchStartLog); */ SyncResponse resp = new SyncResponse(ResponseStatus.FAILURE); resp.setErrorCode(ResponseCode.IO_EXCEPTION); log.debug("CSV SYNCHRONIZATION COMPLETE WITH ERRORS ^^^^^^^^"); return resp; } catch (SftpException sftpe) { log.error(sftpe); /* synchStartLog.updateSynchAttributes("FAIL", ResponseCode.FILE_EXCEPTION.toString(), sftpe.toString()); auditHelper.logEvent(synchStartLog); */ SyncResponse resp = new SyncResponse(ResponseStatus.FAILURE); resp.setErrorCode(ResponseCode.FILE_EXCEPTION); sftpe.printStackTrace(); log.debug("CSV SYNCHRONIZATION COMPLETE WITH ERRORS ^^^^^^^^"); } catch (JSchException jsche) { log.error(jsche); /* synchStartLog.updateSynchAttributes("FAIL", ResponseCode.FILE_EXCEPTION.toString(), jsche.toString()); auditHelper.logEvent(synchStartLog); */ SyncResponse resp = new SyncResponse(ResponseStatus.FAILURE); resp.setErrorCode(ResponseCode.FILE_EXCEPTION); jsche.printStackTrace(); log.debug("CSV SYNCHRONIZATION COMPLETE WITH ERRORS ^^^^^^^^"); } finally { if (resultReview != null) { if (CollectionUtils.isNotEmpty(resultReview.getReviewRecords())) { // add header row resultReview.addRecord(generateSynchReviewRecord(rowHeaderForReport, true)); } } if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } log.debug("CSV SYNCHRONIZATION COMPLETE^^^^^^^^"); // auditBuilder.addAttribute(AuditAttributeName.DESCRIPTION, "CSV SYNCHRONIZATION COMPLETE^^^^^^^^"); return new SyncResponse(ResponseStatus.SUCCESS); }
From source file:org.slc.sli.sample.transform.CcsCsvReader.java
void load() throws IOException { File file = new File(fileLocation); file = removeEmptyLinesFromCsv(file); if (containsCopyright) { copyright = removeTrailingCharacters(tail(file), ','); file = removeLastLine(file);/*from ww w .j av a 2s.c o m*/ } InputStreamReader isReader = new InputStreamReader(new FileInputStream(file), "UTF-8"); csvParser = new CSVParser(isReader, CSVStrategy.EXCEL_STRATEGY); firstLine = csvParser.getLine(); getNextRecord(); }