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

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

Introduction

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

Prototype

public ObjectReader reader(DeserializationFeature feature) 

Source Link

Document

Factory method for constructing ObjectReader with specified feature enabled (compared to settings that this mapper instance has).

Usage

From source file:nl.esciencecenter.medim.dicom.types.DicomTags.java

protected void readFromText(String txt) throws IOException {
    // Pass I: remove comments including the ending newline!
    Pattern pat = Pattern.compile("^#.*\n", Pattern.MULTILINE);
    String newTxt = pat.matcher(txt).replaceAll("");
    // Not needed: Pass II: remove empty lines as a result of the
    // pat=Pattern.compile("\n\n",Pattern.MULTILINE);
    // newTxt=pat.matcher(newTxt).replaceAll("");

    // ObjectMapper mapper=new ObjectMapper();
    CsvMapper mapper = new CsvMapper();
    CsvSchema schema = mapper.schemaFor(CsvTagLine.class); // create object mapping from CsvLine.class

    // CsvSchema schema = CsvSchema.builder()
    // .addColumn(CSV_GROUP)
    // .addColumn(CSV_ELEMENT)
    // .addColumn(CSV_VR)
    // .addColumn(CSV_NAME)
    // .build();/*from   ww w . j  ava2  s .  co m*/

    MappingIterator<CsvTagLine> mi = mapper.reader(CsvTagLine.class).with(schema).readValues(newTxt);

    List<TagDirective> tags = new ArrayList<TagDirective>();

    // skip first:
    CsvTagLine header = mi.nextValue();

    // check header values.
    while (mi.hasNextValue()) {
        CsvTagLine line = mi.nextValue();
        TagDirective tag = new TagDirective();
        // do something?
        tag.tagNr = StringUtil.parseHexidecimal(line.group) * 0x10000
                + StringUtil.parseHexidecimal(line.element);
        tag.name = line.name;
        line.keep = StringUtil.stripWhiteSpace(line.keep);
        line.options = StringUtil.stripWhiteSpace(line.options);

        // Support OX
        if (StringUtil.equalsIgnoreCase(line.VR, "OX"))
            line.VR = "OB"; // treat as bytes;

        VRType vrType = VRType.valueOf(line.VR);
        tag.vr = vrType.vr();

        boolean keep = false;

        if (StringUtil.isWhiteSpace(line.keep) == false)
            keep = (Integer.parseInt(line.keep) > 0);

        if (keep == false) {
            tag.option = TagProcessingOption.DELETE;
        } else {
            // check option:
            // System.err.printf("- %s | %s | %s | %s\n",line.group,line.element,line.keep,line.options);
            if (StringUtil.isWhiteSpace(line.options) == false) {
                tag.option = TagProcessingOption.valueOfOrNull(line.options, true);
                // error parsing option:
                if (tag.option == null) {
                    throw new IOException("Parse Error: could not parse Tag Option:" + line.options);
                }
            } else {
                tag.option = TagProcessingOption.KEEP; // no option -> keep.
            }
        }

        tags.add(tag);
    }

    // POST: check tags:

    for (int i = 0; i < tags.size(); i++) {
        TagDirective tag = tags.get(i);
        // logger.debugPritnf("TagOption: 0x%8x '%s' : %s\n",tag.tagNr,tag.name,tag.option);
        this.dicomTags.put(tag.tagNr, tag); // register
    }
}