use of com.talend.csv.CSVWriter in project components by Talend.
the class FileOutputDelimitedRuntime method getCsvWriter.
public CSVWriter getCsvWriter() throws IOException {
com.talend.csv.CSVWriter csvWriter = null;
if (!useStream) {
if (!props.split.getValue()) {
if (props.compress.getValue() && !props.append.getValue()) {
// compress the dest file
file = new File(fileName);
String zipName = fullName + ".zip";
zipFile = new File(zipName);
if (zipFile.exists()) {
zipFile.delete();
}
zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipName)));
zipOut.putNextEntry(new ZipEntry(file.getName()));
this.streamWriter = new OutputStreamWriter(zipOut, encoding);
} else {
if (!props.append.getValue()) {
File fileToDelete = new File(fileName);
if (fileToDelete.exists()) {
fileToDelete.delete();
}
}
streamWriter = new OutputStreamWriter(new FileOutputStream(fileName, props.append.getValue()), encoding);
}
} else {
file = new File(fullName + splitedFileNo + extension);
if (!props.append.getValue()) {
if (file.exists()) {
file.delete();
}
}
streamWriter = new OutputStreamWriter(new FileOutputStream(fullName + splitedFileNo + extension, props.append.getValue()), encoding);
splitedFileNo++;
}
if (props.rowMode.getValue()) {
this.writer = new BufferedLineWriter(streamWriter);
this.strWriter = new StringWriter();
csvWriter = new CSVWriter(strWriter);
} else {
csvWriter = new CSVWriter(new BufferedWriter(streamWriter));
}
} else {
if (props.compress.getValue()) {
// compress the dest output stream
zipOut = new ZipOutputStream(new BufferedOutputStream((OutputStream) props.fileName.getValue()));
zipOut.putNextEntry(new ZipEntry("TalendOutputDelimited"));
streamWriter = new OutputStreamWriter(zipOut, encoding);
} else {
streamWriter = new OutputStreamWriter((OutputStream) props.fileName.getValue(), encoding);
}
if (props.rowMode.getValue()) {
this.writer = new BufferedLineWriter(streamWriter);
this.strWriter = new StringWriter();
csvWriter = new com.talend.csv.CSVWriter(strWriter);
} else {
BufferedWriter bufferWriter = new BufferedWriter(streamWriter);
csvWriter = new com.talend.csv.CSVWriter(bufferWriter);
}
}
csvWriter.setSeparator(fieldSeparator.charAt(0));
if (!props.useOsRowSeparator.getValue()) {
csvWriter.setLineEnd(rowSeparator);
} else {
if (!"\r\n".equals(rowSeparator) && rowSeparator.charAt(0) != '\r' && rowSeparator.charAt(0) != '\n') {
csvWriter.setLineEnd(rowSeparator);
}
}
// This would set after write header
if (!props.includeHeader.getValue()) {
csvWriter.setEscapeChar(escapeChar);
csvWriter.setQuoteChar(textEnclosureChar);
csvWriter.setQuoteStatus(com.talend.csv.CSVWriter.QuoteStatus.FORCE);
}
return csvWriter;
}
use of com.talend.csv.CSVWriter in project tdq-studio-se by Talend.
the class ExportFactory method export.
public static void export(File exportFile, IFolder folder, ParserRule... parserRules) {
if (exportFile.isDirectory()) {
for (ParserRule pr : parserRules) {
// $NON-NLS-1$
File file = new File(exportFile, pr.getName() + ".csv");
export(file, folder, pr);
}
}
String fileExtName = getFileExtName(exportFile);
if (FileUtils.isCSV(fileExtName)) {
try {
CSVWriter out = FileUtils.createCSVWriter(exportFile, FileUtils.TEXT_QUAL, FileUtils.TEXT_QUAL);
List<TdExpression> expressions = parserRules[0].getExpression();
ParserRuleToExcelEnum[] values = getParserRuleEnumValues();
String[] temp = new String[values.length];
for (int i = 0; i < expressions.toArray().length + 1; i++) {
for (int j = 0; j < values.length; j++) {
if (i == 0) {
temp[j] = values[j].getLiteral();
} else {
Map<ParserRuleToExcelEnum, String> relatedValueFromParserRule = getRelatedValueFromParserRule(parserRules[0], folder, expressions.get(i - 1));
temp[j] = relatedValueFromParserRule.get(values[j]);
}
}
out.writeNext(temp);
}
out.flush();
out.close();
} catch (FileNotFoundException fe) {
MessageDialogWithToggle.openError(null, DefaultMessagesImpl.getString("ExportFactory.errorOne"), // $NON-NLS-1$ //$NON-NLS-2$
DefaultMessagesImpl.getString("ExportFactory.notFoundFile"));
} catch (Exception e) {
log.error(e.getMessage());
}
}
}
use of com.talend.csv.CSVWriter in project tdq-studio-se by Talend.
the class ExportFactory method export.
public static void export(File exportFile, IFolder folder, IndicatorDefinition... indicatorDefinitions) {
if (exportFile.isDirectory()) {
for (IndicatorDefinition id : indicatorDefinitions) {
// $NON-NLS-1$
File file = new File(exportFile, id.getName() + ".csv");
export(file, folder, id);
}
}
String fileExtName = getFileExtName(exportFile);
if (FileUtils.isCSV(fileExtName)) {
try {
CSVWriter out = FileUtils.createCSVWriter(exportFile, FileUtils.TEXT_QUAL, FileUtils.TEXT_QUAL);
PatternToExcelEnum[] values = PatternToExcelEnum.values();
String[] temp = new String[values.length];
for (int i = 0; i < indicatorDefinitions.length + 1; i++) {
for (int j = 0; j < values.length; j++) {
if (i == 0) {
temp[j] = values[j].getLiteral();
} else {
temp[j] = getRelatedValueFromIndicatorDefinition(indicatorDefinitions[i - 1], folder).get(values[j]);
}
}
out.writeNext(temp);
}
out.flush();
out.close();
} catch (FileNotFoundException fe) {
MessageDialogWithToggle.openError(null, DefaultMessagesImpl.getString("ExportFactory.errorOne"), // $NON-NLS-1$ //$NON-NLS-2$
DefaultMessagesImpl.getString("ExportFactory.notFoundFile"));
} catch (Exception e) {
log.error(e.getMessage());
}
}
}
use of com.talend.csv.CSVWriter in project tdq-studio-se by Talend.
the class ExportFactory method export.
static void export(File exportFile, IFolder folder, Pattern... patterns) {
if (exportFile.isDirectory()) {
for (Pattern pattern : patterns) {
// $NON-NLS-1$
File file = new File(exportFile, toLocalFileName(pattern.getName() + ".csv"));
export(file, folder, pattern);
}
}
String fileExtName = getFileExtName(exportFile);
if (FileUtils.isCSV(fileExtName)) {
try {
CSVWriter out = FileUtils.createCSVWriter(exportFile, FileUtils.TEXT_QUAL, FileUtils.TEXT_QUAL);
PatternToExcelEnum[] values = PatternToExcelEnum.values();
String[] temp = new String[values.length];
Map<PatternToExcelEnum, String> relatedValueMap = null;
for (int i = 0; i < patterns.length + 1; i++) {
if (i != 0) {
relatedValueMap = getRelatedValueFromPattern(patterns[i - 1], folder);
}
for (int j = 0; j < values.length; j++) {
if (i == 0) {
temp[j] = values[j].getLiteral();
} else {
if (relatedValueMap == null) {
// $NON-NLS-1$
throw new NullPointerException(DefaultMessagesImpl.getString("ExportFactory.RelatedValueMapNull"));
}
temp[j] = relatedValueMap.get(values[j]);
}
}
out.writeNext(temp);
}
out.flush();
out.close();
} catch (FileNotFoundException fe) {
MessageDialogWithToggle.openError(null, DefaultMessagesImpl.getString("ExportFactory.errorOne"), // $NON-NLS-1$ //$NON-NLS-2$
DefaultMessagesImpl.getString("ExportFactory.notFoundFile"));
} catch (Exception e) {
log.error(e.getMessage());
}
}
}
use of com.talend.csv.CSVWriter in project tdq-studio-se by Talend.
the class FileUtils method createCSVWriter.
/**
* create a CSVWriter with necessory setting.
*
* @param reportListFile
* @return
* @throws FileNotFoundException
*/
public static CSVWriter createCSVWriter(final File reportListFile, char quoteChar, char escapeChar) throws FileNotFoundException {
CSVWriter out = new CSVWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(reportListFile), Charset.defaultCharset())));
out.setSeparator(CURRENT_SEPARATOR);
out.setEscapeChar(escapeChar);
out.setQuoteChar(quoteChar);
out.setQuoteStatus(QuoteStatus.FORCE);
return out;
}
Aggregations