Search in sources :

Example 1 with CSVWriter

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;
}
Also used : ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) CSVWriter(com.talend.csv.CSVWriter) BufferedWriter(java.io.BufferedWriter) CSVWriter(com.talend.csv.CSVWriter) BufferedLineWriter(org.talend.daikon.runtime.BufferedLineWriter) StringWriter(java.io.StringWriter) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with 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());
        }
    }
}
Also used : ParserRule(org.talend.dataquality.rules.ParserRule) TdExpression(org.talend.cwm.relational.TdExpression) FileNotFoundException(java.io.FileNotFoundException) CSVWriter(com.talend.csv.CSVWriter) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParserRuleToExcelEnum(org.talend.dataprofiler.core.ui.wizard.parserrule.ParserRuleToExcelEnum) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 3 with 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, 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());
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) CSVWriter(com.talend.csv.CSVWriter) IndicatorDefinition(org.talend.dataquality.indicators.definition.IndicatorDefinition) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with CSVWriter

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());
        }
    }
}
Also used : Pattern(org.talend.dataquality.domain.pattern.Pattern) FileNotFoundException(java.io.FileNotFoundException) CSVWriter(com.talend.csv.CSVWriter) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 5 with CSVWriter

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;
}
Also used : FileOutputStream(java.io.FileOutputStream) CSVWriter(com.talend.csv.CSVWriter) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Aggregations

CSVWriter (com.talend.csv.CSVWriter)6 File (java.io.File)4 FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 IFile (org.eclipse.core.resources.IFile)3 BufferedWriter (java.io.BufferedWriter)2 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 BufferedOutputStream (java.io.BufferedOutputStream)1 OutputStream (java.io.OutputStream)1 StringWriter (java.io.StringWriter)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 TdExpression (org.talend.cwm.relational.TdExpression)1 BufferedLineWriter (org.talend.daikon.runtime.BufferedLineWriter)1 ParserRuleToExcelEnum (org.talend.dataprofiler.core.ui.wizard.parserrule.ParserRuleToExcelEnum)1 Pattern (org.talend.dataquality.domain.pattern.Pattern)1 IndicatorDefinition (org.talend.dataquality.indicators.definition.IndicatorDefinition)1 ParserRule (org.talend.dataquality.rules.ParserRule)1 RepositoryWorkUnit (org.talend.repository.RepositoryWorkUnit)1