Example usage for com.fasterxml.jackson.dataformat.csv CsvMapper setDateFormat

List of usage examples for com.fasterxml.jackson.dataformat.csv CsvMapper setDateFormat

Introduction

In this page you can find the example usage for com.fasterxml.jackson.dataformat.csv CsvMapper setDateFormat.

Prototype

public ObjectMapper setDateFormat(DateFormat dateFormat) 

Source Link

Document

Method for configuring the default DateFormat to use when serializing time values as Strings, and deserializing from JSON Strings.

Usage

From source file:eu.scape_project.cdx_creator.CDXCreationTask.java

public void createIndex() {
    FileInputStream fileInputStream = null;
    ArchiveReader reader = null;//from www  .  j  a  v  a  2s .  co  m
    FileOutputStream outputStream = null;
    try {
        fileInputStream = new FileInputStream(archiveFile);
        reader = ArchiveReaderFactory.getReader(fileInputStream, this.archiveFileName);
        reader.setComputePayloadDigest(config.isCreatePayloadDigest());
        List<CdxArchiveRecord> cdxArchRecords = new ArrayList<CdxArchiveRecord>();
        while (reader.hasNext()) {
            ArchiveRecord archRec = (ArchiveRecord) reader.next();
            CdxArchiveRecord cdxArchRec = CdxArchiveRecord.fromArchiveRecord(archRec);
            cdxArchRec.setContainerFileName(archiveFileName);
            cdxArchRec.setContainerLengthStr(Long.toString(archiveFile.length()));
            cdxArchRecords.add(cdxArchRec);
        }

        CsvMapper mapper = new CsvMapper();
        mapper.setDateFormat(GMTGTechDateFormat);

        String cdxfileCsColumns = config.getCdxfileCsColumns();
        List<String> cdxfileCsColumnsList = Arrays.asList(cdxfileCsColumns.split("\\s*,\\s*"));
        String[] cdxfileCsColumnsArray = cdxfileCsColumnsList.toArray(new String[cdxfileCsColumnsList.size()]);

        CsvSchema.Builder builder = CsvSchema.builder();
        for (String cdxField : cdxfileCsColumnsList) {
            builder.addColumn(cdxField);
        }
        builder.setColumnSeparator(' ');
        CsvSchema schema = builder.build();
        schema = schema.withoutQuoteChar();

        SimpleFilterProvider filterProvider = new SimpleFilterProvider().addFilter("cdxfields",
                FilterExceptFilter.filterOutAllExcept(cdxfileCsColumnsArray));

        ObjectWriter cdxArchRecordsWriter = mapper.writer(filterProvider).withSchema(schema);

        PrintStream pout = null;
        String outputPathStr = config.getOutputStr();
        if (outputPathStr != null) {
            FileOutputStream fos;
            try {
                fos = new FileOutputStream(outputPathStr, true);
                pout = new PrintStream(fos);
                System.setOut(pout);
            } catch (FileNotFoundException ex) {
                LOG.error("File not found error", ex);
            }
        }
        System.out.println(" " + config.getCdxfileCsHeader());

        cdxArchRecordsWriter.writeValue(System.out, cdxArchRecords);

        if (pout != null) {
            pout.close();
        }

    } catch (FileNotFoundException ex) {
        LOG.error("File not found error", ex);
    } catch (IOException ex) {
        LOG.error("I/O Error", ex);
    } finally {
        try {
            if (fileInputStream != null) {
                fileInputStream.close();
            }

            if (outputStream != null) {
                outputStream.close();
            }

        } catch (IOException ex) {
            LOG.error("I/O Error", ex);
        }
    }
}