use of org.apache.commons.csv.CSVPrinter in project fql by CategoricalData.
the class ToCsvPragmaInstance method execute.
@Override
public void execute() {
try {
Map<En, String> ens = new HashMap<>();
String idCol = (String) op.getOrDefault(AqlOption.id_column_name);
int startId = (int) op.getOrDefault(AqlOption.start_ids_at);
for (En en : I.schema().ens) {
StringBuffer sb = new StringBuffer();
CSVPrinter printer = new CSVPrinter(sb, getFormat(op));
List<String> header = new LinkedList<>();
header.add(idCol);
for (Fk fk : Util.alphabetical(I.schema().fksFrom(en))) {
header.add(fk.toString());
}
for (Att att : Util.alphabetical(I.schema().attsFrom(en))) {
header.add(att.toString());
}
printer.printRecord(header);
Pair<Map<X, Integer>, Map<Integer, X>> J = I.algebra().intifyX(startId);
for (X x : Util.alphabetical(I.algebra().en(en))) {
List<String> row = new LinkedList<>();
row.add(J.first.get(x).toString());
for (Fk fk : Util.alphabetical(I.schema().fksFrom(en))) {
row.add(J.first.get(I.algebra().fk(fk, x)).toString());
}
for (Att att : Util.alphabetical(I.schema().attsFrom(en))) {
row.add(print(I.algebra().att(att, x)));
}
printer.printRecord(row);
}
ens.put(en, sb.toString());
printer.close();
}
delete();
String ext = (String) op.getOrDefault(AqlOption.csv_file_extension);
for (En en : ens.keySet()) {
FileWriter out = new FileWriter(fil + en + "." + ext);
out.write(ens.get(en));
out.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.apache.commons.csv.CSVPrinter in project storm by apache.
the class CsvSerializer method write.
@Override
public ByteBuffer write(List<Object> data, ByteBuffer buffer) {
try {
StringWriter writer = new StringWriter();
CSVPrinter printer = new CSVPrinter(writer, CSVFormat.RFC4180);
for (Object o : data) {
printer.print(o);
}
// since using StringWriter, we do not need to close it.
return ByteBuffer.wrap(writer.getBuffer().toString().getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.apache.commons.csv.CSVPrinter in project zaproxy by zaproxy.
the class TableExportAction method actionPerformed.
@Override
public void actionPerformed(ActionEvent e) {
WritableFileChooser chooser = new WritableFileChooser(Model.getSingleton().getOptionsParam().getUserDirectory()) {
private static final long serialVersionUID = 1L;
@Override
public void approveSelection() {
File file = getSelectedFile();
if (file != null) {
String filePath = file.getAbsolutePath();
if (!filePath.toLowerCase(Locale.ROOT).endsWith(CSV_EXTENSION)) {
setSelectedFile(new File(filePath + CSV_EXTENSION));
}
}
super.approveSelection();
}
};
chooser.setSelectedFile(new File(Constant.messages.getString("export.button.default.filename")));
if (chooser.showSaveDialog(View.getSingleton().getMainFrame()) == WritableFileChooser.APPROVE_OPTION) {
boolean success = true;
try (CSVPrinter pw = new CSVPrinter(Files.newBufferedWriter(chooser.getSelectedFile().toPath(), StandardCharsets.UTF_8), CSVFormat.DEFAULT)) {
pw.printRecord(getColumnNames());
int rowCount = getTable().getRowCount();
for (int row = 0; row < rowCount; row++) {
pw.printRecord(getRowCells(row));
}
} catch (Exception ex) {
success = false;
JOptionPane.showMessageDialog(View.getSingleton().getMainFrame(), Constant.messages.getString("export.button.error") + "\n" + ex.getMessage());
LOGGER.error("Export Failed: " + ex.getMessage(), ex);
}
// flushed.
if (success) {
JOptionPane.showMessageDialog(View.getSingleton().getMainFrame(), Constant.messages.getString("export.button.success"));
}
}
}
use of org.apache.commons.csv.CSVPrinter in project jgnash by ccavanaugh.
the class CsvExport method exportAccountTree.
public static void exportAccountTree(@NotNull final Engine engine, @NotNull final Path path) {
Objects.requireNonNull(engine);
Objects.requireNonNull(path);
// force a correct file extension
final String fileName = FileUtils.stripFileExtension(path.toString()) + ".csv";
final CSVFormat csvFormat = CSVFormat.EXCEL.withQuoteMode(QuoteMode.ALL);
try (final OutputStreamWriter outputStreamWriter = new OutputStreamWriter(Files.newOutputStream(Paths.get(fileName)), StandardCharsets.UTF_8);
final CSVPrinter writer = new CSVPrinter(new BufferedWriter(outputStreamWriter), csvFormat)) {
// write UTF-8 byte order mark to the file for easier imports
outputStreamWriter.write(BYTE_ORDER_MARK);
writer.printRecord(ResourceUtils.getString("Column.Account"), ResourceUtils.getString("Column.Code"), ResourceUtils.getString("Column.Entries"), ResourceUtils.getString("Column.Balance"), ResourceUtils.getString("Column.ReconciledBalance"), ResourceUtils.getString("Column.Currency"), ResourceUtils.getString("Column.Type"));
// Create a list sorted by depth and account code and then name if code is not specified
final List<Account> accountList = engine.getAccountList();
accountList.sort(Comparators.getAccountByTreePosition(Comparators.getAccountByCode()));
final CurrencyNode currencyNode = engine.getDefaultCurrency();
final LocalDate today = LocalDate.now();
for (final Account account : accountList) {
final String indentedName = SPACE.repeat((account.getDepth() - 1) * INDENT) + account.getName();
final String balance = account.getTreeBalance(today, currencyNode).toPlainString();
final String reconcileBalance = account.getReconciledTreeBalance().toPlainString();
writer.printRecord(indentedName, String.valueOf(account.getAccountCode()), String.valueOf(account.getTransactionCount()), balance, reconcileBalance, account.getCurrencyNode().getSymbol(), account.getAccountType().toString());
}
} catch (final IOException e) {
Logger.getLogger(CsvExport.class.getName()).log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}
use of org.apache.commons.csv.CSVPrinter in project ddf by codice.
the class CsvTransformer method writeMetacardsToCsv.
public static Appendable writeMetacardsToCsv(final List<Metacard> metacards, final List<AttributeDescriptor> orderedAttributeDescriptors, final Map<String, String> aliasMap) throws CatalogTransformerException {
StringBuilder stringBuilder = new StringBuilder();
try {
CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.RFC4180);
printColumnHeaders(csvPrinter, orderedAttributeDescriptors, aliasMap);
metacards.forEach(metacard -> printMetacard(csvPrinter, metacard, orderedAttributeDescriptors));
return csvPrinter.getOut();
} catch (IOException ioe) {
throw new CatalogTransformerException(ioe);
}
}
Aggregations