List of usage examples for org.apache.commons.csv CSVStrategy COMMENTS_DISABLED
char COMMENTS_DISABLED
To view the source code for org.apache.commons.csv CSVStrategy COMMENTS_DISABLED.
Click Source Link
From source file:com.cloudera.science.ml.parallel.normalize.StringSplitFn.java
public static PCollection<Record> apply(PCollection<String> in, char delim, Character quote, Character comment) {//from ww w. java2 s.c o m if (quote == null) { quote = '"'; } if (comment == null) { comment = CSVStrategy.COMMENTS_DISABLED; } return in.parallelDo("string-split", new StringSplitFn(delim, quote, comment), MLRecords.csvRecord(in.getTypeFamily(), String.valueOf(delim))); }
From source file:com.appspot.relaxe.tools.CSVInsertTask.java
public void run(Connection connection, Reader input, Table table) throws IOException, SQLException { if (connection == null) { throw new NullPointerException("'connection' must not be null"); }// w w w.j a va 2s .c o m if (input == null) { throw new NullPointerException("'input' must not be null"); } if (table == null) { throw new NullPointerException("'table' must not be null"); } boolean committed = false; try { connection.setAutoCommit(false); CSVStrategy cs = new CSVStrategy('\t', '"', CSVStrategy.COMMENTS_DISABLED, false, false, false); CSVParser p = new CSVParser(input, cs); // get header line String[] line = p.getLine(); // configure by using the column headers: ColumnMap cm = table.getColumnMap(); List<Identifier> names = new ArrayList<Identifier>(); List<Column> columnList = new ArrayList<Column>(); for (String n : line) { Column column = cm.get(n); if (column == null) { throw new IllegalArgumentException("column not found " + n); } columnList.add(column); names.add(column.getColumnName()); } if (names.isEmpty()) { throw new IllegalStateException("no column names available"); } ElementList<Identifier> nel = ElementList.newElementList(names); final int expectedColumnCount = line.length; // int recno = 0; PreparedStatement ps = null; InsertStatement ins = null; VarcharParameter[] params = new VarcharParameter[expectedColumnCount]; ValueAssignerFactory vaf = getImplementation().getValueAssignerFactory(); AssignmentVisitor pa = null; while ((line = p.getLine()) != null) { // recno++; final int cols = line.length; int lineno = p.getLineNumber(); if (cols != expectedColumnCount) { throw new IllegalStateException("unexpected column count: " + cols + " at line " + lineno); } if (ps == null) { List<RowValueConstructorElement> vl = new ArrayList<RowValueConstructorElement>(params.length); for (int i = 0; i < params.length; i++) { Column column = columnList.get(i); VarcharHolder h = parse(column, line[i]); VarcharParameter param = new VarcharParameter(column, h); params[i] = param; vl.add(param); } RowValueConstructor rvc = AbstractRowValueConstructor.of(vl); ins = new InsertStatement(table, nel, rvc); String q = ins.generate(); ps = connection.prepareStatement(q); pa = new AssignmentVisitor(vaf, ps); // System.err.println("lineno: " + lineno); // System.err.println("record: " + recno); // System.err.println("query: " + q); } else { pa.reset(); for (int i = 0; i < line.length; i++) { Column column = columnList.get(i); VarcharHolder h = parse(column, line[i]); VarcharParameter param = params[i]; param.setValue(h); } } ins.traverse(null, pa); ps.addBatch(); } int[] updateCounts = ps.executeBatch(); updated(updateCounts); connection.commit(); committed = true; } finally { if (!(committed)) { QueryHelper.doRollback(connection); } } }
From source file:ispyb.client.common.shipping.CreateShippingFileAction.java
/** * uploadCsvFile/*w ww. ja v a 2 s . c o m*/ * * @param mapping * @param actForm * @param request * @param in_reponse * @return */ @SuppressWarnings({ "deprecation", "unchecked" }) public ActionForward uploadCsvFile(ActionMapping mapping, ActionForm actForm, HttpServletRequest request, HttpServletResponse in_reponse) { ActionMessages errors = new ActionMessages(); ActionMessages messages = new ActionMessages(); try { CreateShippingFileForm form = (CreateShippingFileForm) actForm; // Parameters submitted by form String currentProposalCode = (String) request.getSession().getAttribute(Constants.PROPOSAL_CODE); String currentProposalNumber = (String) request.getSession().getAttribute(Constants.PROPOSAL_NUMBER); Integer proposalId = (Integer) request.getSession().getAttribute(Constants.PROPOSAL_ID); boolean errorFile = false; Proposal3VO proposalVO = proposalService.findByPk(proposalId); List<LabContact3VO> listLabContacts = labContactService.findFiltered(proposalId, null); LabContact3VO shippingLabContactVO = null; if (listLabContacts != null && listLabContacts.size() > 0) { shippingLabContactVO = listLabContacts.get(0); } else { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.upload", "No labConctact found")); errorFile = true; } List<String> allowedSpaceGroups = (List<String>) request.getSession() .getAttribute(Constants.ISPYB_ALLOWED_SPACEGROUPS_LIST); String fieldSeparator = form.getFieldSeparator(); String textSeparator = form.getTextSeparator(); char fieldSep = fieldSeparator.charAt(0); char textSep = ' '; if (textSeparator.length() > 0) textSep = textSeparator.charAt(0); String uploadedFileName; String realCSVPath; List<Shipping3VO> listShippingCreated = new ArrayList<Shipping3VO>(); List<Dewar3VO> listDewarCreated = new ArrayList<Dewar3VO>(); List<Container3VO> listContainerCreated = new ArrayList<Container3VO>(); // List<Crystal3VO> listCrystalCreated = new ArrayList<Crystal3VO>(); List<Crystal3VO> listCrystalCreated = crystalService.findByProposalId(proposalId); List<DiffractionPlan3VO> listDifPlanCreated = new ArrayList<DiffractionPlan3VO>(); List<BLSample3VO> listSampleCreated = new ArrayList<BLSample3VO>(); if (request != null) { uploadedFileName = form.getRequestFile().getFileName(); if (uploadedFileName.equals("")) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.empty")); errorFile = true; } else if (!uploadedFileName.endsWith(".csv")) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.format")); errorFile = true; } if (!errorFile) { realCSVPath = request.getRealPath("/") + "/tmp/" + uploadedFileName; // Write the received file to tmp directory FormFile f = form.getRequestFile(); InputStream in = f.getInputStream(); File outputFile = new File(realCSVPath); if (outputFile.exists()) outputFile.delete(); FileOutputStream out = new FileOutputStream(outputFile); while (in.available() != 0) { out.write(in.read()); out.flush(); } out.flush(); out.close(); // received file Reader inFile = new FileReader(realCSVPath); LOG.info(" ---[uploadFile] Upload Shipment csv "); CSVStrategy csvStrategy = new CSVStrategy(fieldSep, textSep, CSVStrategy.COMMENTS_DISABLED); CSVParser parser = new CSVParser(inFile, csvStrategy); String[][] values = parser.getAllValues(); int nbRows = values.length; boolean isError = false; for (int i = 0; i < nbRows; i++) { int nbCol = values[i].length; if (nbCol != NB_COL) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "The number of columns is incorrect (" + nbCol + " instead of " + NB_COL + ")")); isError = true; break; } int j = 0; // proposalCode & proposalNumber String proposalCode = values[i][j++]; String proposalNumber = values[i][j++]; if (proposalCode == null || proposalNumber == null || !(proposalCode.equalsIgnoreCase(currentProposalCode)) || !(proposalNumber.equals(currentProposalNumber))) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "The proposal is incorrect line " + (i + 1))); isError = true; break; } // visitNumber j++; // shippingName String shippingName = values[i][j++]; if (shippingName == null || shippingName.trim().length() == 0 || shippingName.length() > MAX_SHIPPING_NAME) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "The shipping name is incorrect line " + (i + 1) + " (required and < " + MAX_SHIPPING_NAME + " characters)")); isError = true; break; } // dewarCode String dewarCode = values[i][j++]; if (dewarCode == null || dewarCode.trim().length() == 0 || dewarCode.length() > MAX_DEWAR_CODE) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "The dewar code is incorrect line " + (i + 1) + " (required and < " + MAX_DEWAR_CODE + " characters)")); isError = true; break; } // containerCode String containerCode = values[i][j++]; if (containerCode == null || containerCode.trim().length() == 0 || containerCode.length() > MAX_CONTAINER_CODE) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "The container code is incorrect line " + (i + 1) + " (required and < " + MAX_CONTAINER_CODE + " characters)")); isError = true; break; } // preObsResolution String preObsResolutionS = values[i][j++]; Double preObsResolution = null; if (preObsResolutionS == null || preObsResolutionS.trim().length() != 0) { try { preObsResolution = Double.parseDouble(preObsResolutionS); } catch (NumberFormatException e) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "Wrong fromat for preObsResolution line " + (i + 1))); isError = true; break; } } // neededResolution String neededResolutionS = values[i][j++]; Double neededResolution = null; if (neededResolutionS == null || neededResolutionS.trim().length() != 0) { try { neededResolution = Double.parseDouble(neededResolutionS); } catch (NumberFormatException e) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "Wrong fromat for neededResolution line " + (i + 1))); isError = true; break; } } // oscillationRange String oscillationRangeS = values[i][j++]; Double oscillationRange = null; if (oscillationRangeS == null || oscillationRangeS.trim().length() != 0) { try { oscillationRange = Double.parseDouble(oscillationRangeS); } catch (NumberFormatException e) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "Wrong fromat for oscillationRange line " + (i + 1))); isError = true; break; } } // proteinAcronym String proteinAcronym = values[i][j++]; Protein3VO protein = null; if (proteinAcronym == null || proteinAcronym.trim().length() == 0) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "The protein Acronym is required line " + (i + 1))); isError = true; break; } List<Protein3VO> proteinTab = proteinService.findByAcronymAndProposalId(proposalId, proteinAcronym); if (proteinTab == null || proteinTab.size() == 0) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage( "error.user.shipping.upload.file.data", "Protein " + proteinAcronym + " can not be found in ISPyB, line " + (i + 1))); isError = true; break; } else { protein = proteinTab.get(0); } // proteinName String proteinName = values[i][j++]; if (proteinName == null || proteinName.trim().length() == 0 || !protein.getName().equalsIgnoreCase(proteinName)) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage( "error.user.shipping.upload.file.data", "The protein name is incorrect line " + (i + 1) + " (required and must correspond to the proteinAcronym). The expected proteinName is " + protein.getName())); isError = true; break; } // spaceGroup String spaceGroup = values[i][j++]; if (spaceGroup != null && !spaceGroup.equals("") && !allowedSpaceGroups.contains(spaceGroup.toUpperCase())) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "The space group can not be found in ISPyB line " + (i + 1))); isError = true; break; } // sampleBarcode String sampleBarcode = values[i][j++]; if (sampleBarcode != null && sampleBarcode.length() > MAX_SAMPLE_BARCODE) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "The sample barcode is incorrect line " + (i + 1) + " (< " + MAX_SAMPLE_BARCODE + " characters)")); isError = true; break; } // sampleName String sampleName = values[i][j++]; if (sampleName == null || sampleName.trim().length() == 0 || sampleName.length() > MAX_SAMPLE_NAME || !sampleName .matches(Constants.MASK_BASIC_CHARACTERS_WITH_DASH_UNDERSCORE_NO_SPACE)) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage( "error.user.shipping.upload.file.data", "The sample name is incorrect line " + (i + 1) + " (required and < " + MAX_SAMPLE_NAME + " characters, unique name for the protein acronym, must contain only a-z, A-Z or 0-9 or - or _ characters.)")); isError = true; break; } // samplePosition String samplePos = values[i][j++]; int samplePosition = 0; try { samplePosition = Integer.parseInt(samplePos); } catch (NumberFormatException e) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "The sample position is incorrect: " + samplePos + ", line " + (i + 1) + " (required number between 1 and 10)")); isError = true; break; } if (samplePosition < 1 || samplePosition > Constants.BASKET_SAMPLE_CAPACITY) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "The sample position is incorrect: " + samplePos + ", line " + (i + 1) + " (required number between 1 and 10)")); isError = true; break; } // sample comments String sampleComments = values[i][j++]; if (sampleComments != null && sampleComments.length() > MAX_SAMPLE_COMMENTS) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "The sample comments is incorrect line " + (i + 1) + " ( < " + MAX_SAMPLE_COMMENTS + " characters)")); isError = true; break; } // cell_a String cellA = values[i][j++]; Double cell_a = null; if (cellA == null || cellA.trim().length() != 0) { try { cell_a = Double.parseDouble(cellA); } catch (NumberFormatException e) { isError = true; errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "Wrong fromat for cell_a line " + (i + 1))); break; } } // cell_b String cellB = values[i][j++]; Double cell_b = null; if (cellB == null || cellB.trim().length() != 0) { try { cell_b = Double.parseDouble(cellB); } catch (NumberFormatException e) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "Wrong fromat for cell_b line " + (i + 1))); isError = true; break; } } // cell_c String cellC = values[i][j++]; Double cell_c = null; if (cellC == null || cellC.trim().length() != 0) { try { cell_c = Double.parseDouble(cellC); } catch (NumberFormatException e) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "Wrong fromat for cell_c line " + (i + 1))); isError = true; break; } } // cell_alpha String cellAlpha = values[i][j++]; Double cell_alpha = null; if (cellAlpha == null || cellAlpha.trim().length() != 0) { try { cell_alpha = Double.parseDouble(cellAlpha); } catch (NumberFormatException e) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "Wrong fromat for cell_alpha line " + (i + 1))); isError = true; break; } } // cell_beta String cellBeta = values[i][j++]; Double cell_beta = null; if (cellBeta == null || cellBeta.trim().length() != 0) { try { cell_beta = Double.parseDouble(cellBeta); } catch (NumberFormatException e) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "Wrong fromat for cell_beta line " + (i + 1))); isError = true; break; } } // cell_gamma String cellGamma = values[i][j++]; Double cell_gamma = null; if (cellGamma == null || cellGamma.trim().length() != 0) { try { cell_gamma = Double.parseDouble(cellGamma); } catch (NumberFormatException e) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "Wrong fromat for cell_gamma line " + (i + 1))); isError = true; break; } } // creation of the objects // Shipping Shipping3VO shippingVO = new Shipping3VO(); shippingVO.setProposalVO(proposalVO); shippingVO.setShippingName(shippingName); shippingVO.setCreationDate(new Date()); shippingVO.setShippingStatus(Constants.SHIPPING_STATUS_OPENED); shippingVO.setTimeStamp(StringUtils.getCurrentTimeStamp()); shippingVO.setShippingType(Constants.DEWAR_TRACKING_SHIPPING_TYPE); shippingVO.setSendingLabContactVO(shippingLabContactVO); shippingVO.setReturnLabContactVO(shippingLabContactVO); Shipping3VO shipment = getShipment(listShippingCreated, shippingVO); if (shipment == null) { shippingVO = shippingService.create(shippingVO); listShippingCreated.add(shippingVO); } else { shippingVO = shipment; } // Dewar Dewar3VO dewarVO = new Dewar3VO(); dewarVO.setShippingVO(shippingVO); dewarVO.setCode(dewarCode); dewarVO.setType(Constants.PARCEL_DEWAR_TYPE); dewarVO.setDewarStatus(Constants.SHIPPING_STATUS_OPENED); dewarVO.setTimeStamp(StringUtils.getCurrentTimeStamp()); dewarVO.setCustomsValue(shippingLabContactVO.getDewarAvgCustomsValue()); dewarVO.setTransportValue(shippingLabContactVO.getDewarAvgTransportValue()); Dewar3VO dewar = getDewar(listDewarCreated, dewarVO); if (dewar == null) { dewarVO = dewarService.create(dewarVO); listDewarCreated.add(dewarVO); } else { dewarVO = dewar; } // Container Container3VO containerVO = new Container3VO(); containerVO.setContainerType("Puck"); containerVO.setCode(containerCode); containerVO.setCapacity(Constants.BASKET_SAMPLE_CAPACITY); containerVO.setTimeStamp(StringUtils.getCurrentTimeStamp()); containerVO.setDewarVO(dewarVO); Container3VO container = getContainer(listContainerCreated, containerVO); if (container == null) { containerVO = containerService.create(containerVO); listContainerCreated.add(containerVO); } else { containerVO = container; } if (isLocationOccInContainer(samplePosition, listSampleCreated, containerVO)) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage( "error.user.shipping.upload.file.data", "The sample position is incorrect: " + samplePos + ", line " + (i + 1))); isError = true; break; } // DiffractionPlan DiffractionPlan3VO difPlan = new DiffractionPlan3VO(); difPlan.setObservedResolution(preObsResolution); difPlan.setRequiredResolution(neededResolution); difPlan.setExposureTime((double) 0); difPlan.setOscillationRange(oscillationRange); difPlan.setExperimentKind(DEFAULT_EXPERIMENT_TYPE); difPlan = difPlanService.create(difPlan); listDifPlanCreated.add(difPlan); // Crystal Crystal3VO crystalVO = new Crystal3VO(); String crystalID = UUID.randomUUID().toString(); crystalVO.setProteinVO(protein); crystalVO.setCrystalUUID(crystalID); crystalVO.setSpaceGroup(spaceGroup); crystalVO.setName(crystalID); crystalVO.setCellA(cell_a); crystalVO.setCellB(cell_b); crystalVO.setCellC(cell_c); crystalVO.setCellAlpha(cell_alpha); crystalVO.setCellBeta(cell_beta); crystalVO.setCellGamma(cell_gamma); crystalVO.setDiffractionPlanVO(difPlan); if ((crystalVO.getSpaceGroup() == null) || (crystalVO.getSpaceGroup().equals(""))) { crystalVO.setSpaceGroup(DEFAULT_SPACE_GROUP); } Crystal3VO crystal = getCrystal(listCrystalCreated, crystalVO); if (crystal == null) { crystalVO = crystalService.create(crystalVO); listCrystalCreated.add(crystalVO); } else { crystalVO = crystal; } if (!crystalVO.hasCellInfo()) { messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("message.free", "Warning: the unit cell parameters are not filled for the spaceGroup " + crystalVO.getSpaceGroup() + "!")); } // BLSample BLSample3VO sample = new BLSample3VO(); sample.setCrystalVO(crystalVO); sample.setDiffractionPlanVO(difPlan); sample.setName(sampleName); sample.setCode(sampleBarcode); sample.setLocation("" + samplePosition); sample.setHolderLength(DEFAULT_HOLDER_LENGTH); sample.setLoopLength(DEFAULT_LOOP_LENGTH); sample.setLoopType(DEFAULT_LOOP_TYPE); sample.setWireWidth(DEFAULT_WIRE_WIDTH); sample.setComments(sampleComments); sample.setContainerVO(containerVO); if (isSampleNameAlreadyExist(sampleName, protein, listSampleCreated)) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.user.shipping.upload.file.data", "The sample name already exists: " + sampleName + ", line " + (i + 1))); isError = true; break; } sample = sampleService.create(sample); listSampleCreated.add(sample); } if (isError) { // remove created in db for (Iterator<Shipping3VO> ship = listShippingCreated.iterator(); ship.hasNext();) { Shipping3VO shipVO = ship.next(); shippingService.delete(shipVO); } } else { messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("message.upload.shipping")); } } } } catch (Exception e) { errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.detail", e.toString())); LOG.error(e.toString()); saveErrors(request, errors); return mapping.findForward("error"); } if (!messages.isEmpty()) saveMessages(request, messages); if (!errors.isEmpty()) saveErrors(request, errors); return display(mapping, actForm, request, in_reponse); }
From source file:org.alfresco.repo.web.scripts.DeclarativeSpreadsheetWebScript.java
/** * Generates the spreadsheet, based on the properties in the header * and a callback for the body./*from w w w.j a v a 2 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].*)"); String delimiterParam = req.getParameter(PARAM_REQ_DELIMITER); CSVStrategy reqCSVstrategy = null; if (delimiterParam != null && !delimiterParam.isEmpty()) { reqCSVstrategy = new CSVStrategy(delimiterParam.charAt(0), '"', CSVStrategy.COMMENTS_DISABLED); } // 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, reqCSVstrategy != null ? reqCSVstrategy : getCsvStrategy()); 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); 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:org.apache.ctakes.jdl.data.loader.CsvLoader.java
/** * @param loader/*from w ww . java 2 s.c o m*/ * the loader * @param file * the file * @throws FileNotFoundException * exception */ @SuppressWarnings({ "rawtypes", "unchecked" }) public CsvLoader(final CsvLoadType loader, final File file) throws FileNotFoundException { InputStream inputStrem = new FileInputStream(file); Reader reader = new InputStreamReader(inputStrem); char delimiter = CharUtils.toChar(loader.getDelimiter()); char encapsulator = (loader.getEncapsulator() == null || loader.getEncapsulator().length() == 0) ? CSVStrategy.ENCAPSULATOR_DISABLED : CharUtils.toChar(loader.getEncapsulator()); log.info(String.format("delimiter %d encapsulator %d", (int) delimiter, (int) encapsulator)); CSVStrategy strategy = new CSVStrategy(delimiter, encapsulator, CSVStrategy.COMMENTS_DISABLED, CSVStrategy.ESCAPE_DISABLED, true, true, false, true); parser = new CSVParser(reader, strategy); this.loader = loader; formatMap = new HashMap<String, Format>(); try { for (Column col : loader.getColumn()) { if (col.getFormat() != null && col.getFormat().length() > 0) { Class cf = Class.forName(col.getFormat()); Constructor ccf = cf.getConstructor(String.class); this.formatMap.put(col.getName(), (Format) ccf.newInstance(col.getPattern())); } } } catch (Exception e) { throw new RuntimeException("oops", e); } }
From source file:org.apache.solr.handler.CSVRequestHandler.java
CSVLoader(SolrQueryRequest req, UpdateRequestProcessor processor) { this.processor = processor; this.params = req.getParams(); schema = req.getSchema();/*from ww w . j a v a 2 s. c om*/ templateAdd = new AddUpdateCommand(); templateAdd.allowDups = false; templateAdd.overwriteCommitted = true; templateAdd.overwritePending = true; if (params.getBool(OVERWRITE, true)) { templateAdd.allowDups = false; templateAdd.overwriteCommitted = true; templateAdd.overwritePending = true; } else { templateAdd.allowDups = true; templateAdd.overwriteCommitted = false; templateAdd.overwritePending = false; } templateAdd.commitWithin = params.getInt(UpdateParams.COMMIT_WITHIN, -1); strategy = new CSVStrategy(',', '"', CSVStrategy.COMMENTS_DISABLED, CSVStrategy.ESCAPE_DISABLED, false, false, false, true); String sep = params.get(SEPARATOR); if (sep != null) { if (sep.length() != 1) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid separator:'" + sep + "'"); strategy.setDelimiter(sep.charAt(0)); } String encapsulator = params.get(ENCAPSULATOR); if (encapsulator != null) { if (encapsulator.length() != 1) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid encapsulator:'" + encapsulator + "'"); } String escape = params.get(ESCAPE); if (escape != null) { if (escape.length() != 1) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid escape:'" + escape + "'"); } // if only encapsulator or escape is set, disable the other escaping mechanism if (encapsulator == null && escape != null) { strategy.setEncapsulator(CSVStrategy.ENCAPSULATOR_DISABLED); strategy.setEscape(escape.charAt(0)); } else { if (encapsulator != null) { strategy.setEncapsulator(encapsulator.charAt(0)); } if (escape != null) { char ch = escape.charAt(0); strategy.setEscape(ch); if (ch == '\\') { // If the escape is the standard backslash, then also enable // unicode escapes (it's harmless since 'u' would not otherwise // be escaped. strategy.setUnicodeEscapeInterpretation(true); } } } String fn = params.get(FIELDNAMES); fieldnames = fn != null ? commaSplit.split(fn, -1) : null; Boolean hasHeader = params.getBool(HEADER); skipLines = params.getInt(SKIPLINES, 0); if (fieldnames == null) { if (null == hasHeader) { // assume the file has the headers if they aren't supplied in the args hasHeader = true; } else if (!hasHeader) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "CSVLoader: must specify fieldnames=<fields>* or header=true"); } } else { // if the fieldnames were supplied and the file has a header, we need to // skip over that header. if (hasHeader != null && hasHeader) skipLines++; prepareFields(); } }
From source file:org.apache.solr.handler.CSVRequestHandler.java
/** create the FieldAdders that control how each field is indexed */ void prepareFields() { // Possible future optimization: for really rapid incremental indexing // from a POST, one could cache all of this setup info based on the params. // The link from FieldAdder to this would need to be severed for that to happen. fields = new SchemaField[fieldnames.length]; adders = new CSVLoader.FieldAdder[fieldnames.length]; String skipStr = params.get(SKIP); List<String> skipFields = skipStr == null ? null : StrUtils.splitSmart(skipStr, ','); CSVLoader.FieldAdder adder = new CSVLoader.FieldAdder(); CSVLoader.FieldAdder adderKeepEmpty = new CSVLoader.FieldAdderEmpty(); for (int i = 0; i < fields.length; i++) { String fname = fieldnames[i]; // to skip a field, leave the entries in fields and addrs null if (fname.length() == 0 || (skipFields != null && skipFields.contains(fname))) continue; fields[i] = schema.getField(fname); boolean keepEmpty = params.getFieldBool(fname, EMPTY, false); adders[i] = keepEmpty ? adderKeepEmpty : adder; // Order that operations are applied: split -> trim -> map -> add // so create in reverse order. // Creation of FieldAdders could be optimized and shared among fields String[] fmap = params.getFieldParams(fname, MAP); if (fmap != null) { for (String mapRule : fmap) { String[] mapArgs = colonSplit.split(mapRule, -1); if (mapArgs.length != 2) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Map rules must be of the form 'from:to' ,got '" + mapRule + "'"); adders[i] = new CSVLoader.FieldMapperSingle(mapArgs[0], mapArgs[1], adders[i]); }//from w ww.j a v a2 s. c o m } if (params.getFieldBool(fname, TRIM, false)) { adders[i] = new CSVLoader.FieldTrimmer(adders[i]); } if (params.getFieldBool(fname, SPLIT, false)) { String sepStr = params.getFieldParam(fname, SEPARATOR); char fsep = sepStr == null || sepStr.length() == 0 ? ',' : sepStr.charAt(0); String encStr = params.getFieldParam(fname, ENCAPSULATOR); char fenc = encStr == null || encStr.length() == 0 ? (char) -2 : encStr.charAt(0); String escStr = params.getFieldParam(fname, ESCAPE); char fesc = escStr == null || escStr.length() == 0 ? CSVStrategy.ESCAPE_DISABLED : escStr.charAt(0); CSVStrategy fstrat = new CSVStrategy(fsep, fenc, CSVStrategy.COMMENTS_DISABLED, fesc, false, false, false, false); adders[i] = new CSVLoader.FieldSplitter(fstrat, adders[i]); } } }
From source file:org.apache.solr.response.CSVResponseWriter.java
public void writeResponse() throws IOException { SolrParams params = req.getParams(); strategy = new CSVStrategy(',', '"', CSVStrategy.COMMENTS_DISABLED, CSVStrategy.ESCAPE_DISABLED, false, false, false, true);/*from w w w. jav a 2 s .c o m*/ CSVStrategy strat = strategy; String sep = params.get(CSV_SEPARATOR); if (sep != null) { if (sep.length() != 1) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid separator:'" + sep + "'"); strat.setDelimiter(sep.charAt(0)); } String nl = params.get(CSV_NEWLINE); if (nl != null) { if (nl.length() == 0) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid newline:'" + nl + "'"); strat.setPrinterNewline(nl); } String encapsulator = params.get(CSV_ENCAPSULATOR); String escape = params.get(CSV_ESCAPE); if (encapsulator != null) { if (encapsulator.length() != 1) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid encapsulator:'" + encapsulator + "'"); strat.setEncapsulator(encapsulator.charAt(0)); } if (escape != null) { if (escape.length() != 1) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid escape:'" + escape + "'"); strat.setEscape(escape.charAt(0)); if (encapsulator == null) { strat.setEncapsulator(CSVStrategy.ENCAPSULATOR_DISABLED); } } if (strat.getEscape() == '\\') { // If the escape is the standard backslash, then also enable // unicode escapes (it's harmless since 'u' would not otherwise // be escaped. strat.setUnicodeEscapeInterpretation(true); } printer = new CSVPrinter(writer, strategy); CSVStrategy mvStrategy = new CSVStrategy(strategy.getDelimiter(), CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED, '\\', false, false, false, false); strat = mvStrategy; sep = params.get(MV_SEPARATOR); if (sep != null) { if (sep.length() != 1) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv separator:'" + sep + "'"); strat.setDelimiter(sep.charAt(0)); } encapsulator = params.get(MV_ENCAPSULATOR); escape = params.get(MV_ESCAPE); if (encapsulator != null) { if (encapsulator.length() != 1) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv encapsulator:'" + encapsulator + "'"); strat.setEncapsulator(encapsulator.charAt(0)); if (escape == null) { strat.setEscape(CSVStrategy.ESCAPE_DISABLED); } } escape = params.get(MV_ESCAPE); if (escape != null) { if (escape.length() != 1) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv escape:'" + escape + "'"); strat.setEscape(escape.charAt(0)); // encapsulator will already be disabled if it wasn't specified } returnScore = returnFields != null && returnFields.contains("score"); boolean needListOfFields = returnFields == null || returnFields.size() == 0 || (returnFields.size() == 1 && returnScore) || returnFields.contains("*"); Collection<String> fields = returnFields; Object responseObj = rsp.getValues().get("response"); if (needListOfFields) { if (responseObj instanceof SolrDocumentList) { // get the list of fields from the SolrDocumentList fields = new LinkedHashSet<String>(); for (SolrDocument sdoc : (SolrDocumentList) responseObj) { fields.addAll(sdoc.getFieldNames()); } } else { // get the list of fields from the index fields = req.getSearcher().getFieldNames(); } if (returnScore) { fields.add("score"); } else { fields.remove("score"); } } CSVSharedBufPrinter csvPrinterMV = new CSVSharedBufPrinter(mvWriter, mvStrategy); for (String field : fields) { if (field.equals("score")) { CSVField csvField = new CSVField(); csvField.name = "score"; csvFields.put("score", csvField); continue; } SchemaField sf = schema.getFieldOrNull(field); if (sf == null) { FieldType ft = new StrField(); sf = new SchemaField(field, ft); } // if we got the list of fields from the index, only list stored fields if (returnFields == null && sf != null && !sf.stored()) { continue; } // check for per-field overrides sep = params.get("f." + field + '.' + CSV_SEPARATOR); encapsulator = params.get("f." + field + '.' + CSV_ENCAPSULATOR); escape = params.get("f." + field + '.' + CSV_ESCAPE); CSVSharedBufPrinter csvPrinter = csvPrinterMV; if (sep != null || encapsulator != null || escape != null) { // create a new strategy + printer if there were any per-field overrides strat = (CSVStrategy) mvStrategy.clone(); if (sep != null) { if (sep.length() != 1) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv separator:'" + sep + "'"); strat.setDelimiter(sep.charAt(0)); } if (encapsulator != null) { if (encapsulator.length() != 1) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv encapsulator:'" + encapsulator + "'"); strat.setEncapsulator(encapsulator.charAt(0)); if (escape == null) { strat.setEscape(CSVStrategy.ESCAPE_DISABLED); } } if (escape != null) { if (escape.length() != 1) throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid mv escape:'" + escape + "'"); strat.setEscape(escape.charAt(0)); if (encapsulator == null) { strat.setEncapsulator(CSVStrategy.ENCAPSULATOR_DISABLED); } } csvPrinter = new CSVSharedBufPrinter(mvWriter, strat); } CSVField csvField = new CSVField(); csvField.name = field; csvField.sf = sf; csvField.mvPrinter = csvPrinter; csvFields.put(field, csvField); } NullValue = params.get(CSV_NULL, ""); if (params.getBool(CSV_HEADER, true)) { for (CSVField csvField : csvFields.values()) { printer.print(csvField.name); } printer.println(); } if (responseObj instanceof DocList) { writeDocList(null, (DocList) responseObj, null, null); } else if (responseObj instanceof SolrDocumentList) { writeSolrDocumentList(null, (SolrDocumentList) responseObj, null, null); } }