Example usage for com.fasterxml.jackson.databind MappingIterator hasNextValue

List of usage examples for com.fasterxml.jackson.databind MappingIterator hasNextValue

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind MappingIterator hasNextValue.

Prototype

public boolean hasNextValue() throws IOException 

Source Link

Document

Equivalent of #next but one that may throw checked exceptions from Jackson due to invalid input.

Usage

From source file:org.wikidata.wdtk.client.JsonSerializationActionTest.java

@Test
public void testJsonGzipOutput() throws IOException {
    String[] args = new String[] { "-a", "json", "-o", "/path/to/output.json", "-z", "gz" };

    DirectoryManagerFactory.setDirectoryManagerClass(MockDirectoryManager.class);

    ClientConfiguration config = new ClientConfiguration(args);
    JsonSerializationAction jsa = (JsonSerializationAction) config.getActions().get(0);

    ItemIdValue subject1 = Datamodel.makeWikidataItemIdValue("Q42");
    MonolingualTextValue mtv1 = Datamodel.makeMonolingualTextValue("Test1", "en");
    MonolingualTextValue mtv2 = Datamodel.makeMonolingualTextValue("Test2", "fr");

    ItemDocument id1 = Datamodel.makeItemDocument(subject1, Arrays.asList(mtv1, mtv2), Arrays.asList(mtv1),
            Collections.<MonolingualTextValue>emptyList(), Collections.<StatementGroup>emptyList(),
            Collections.<String, SiteLink>emptyMap());

    jsa.open();//www .  j  ava 2 s  .c  o m
    jsa.processItemDocument(id1);
    jsa.close();

    MockDirectoryManager mdm = new MockDirectoryManager(Paths.get("/path/to/"), false);

    ObjectMapper mapper = new ObjectMapper();
    ObjectReader documentReader = mapper.reader(JacksonTermedStatementDocument.class);
    MappingIterator<JacksonTermedStatementDocument> documentIterator = documentReader
            .readValues(mdm.getInputStreamForFile("output.json.gz", CompressionType.GZIP));

    List<EntityDocument> results = new ArrayList<>();
    while (documentIterator.hasNextValue()) {
        JacksonTermedStatementDocument document = documentIterator.nextValue();
        document.setSiteIri(Datamodel.SITE_WIKIDATA);
        results.add(document);
    }
    documentIterator.close();

    assertEquals(1, results.size());
    assertEquals(id1, results.get(0));
}

From source file:org.wikidata.wdtk.client.JsonSerializationActionTest.java

@Test
public void testJsonBz2Output() throws IOException {
    String[] args = new String[] { "-a", "json", "-o", "output.json", "-z", "bz2" };

    DirectoryManagerFactory.setDirectoryManagerClass(MockDirectoryManager.class);

    ClientConfiguration config = new ClientConfiguration(args);
    JsonSerializationAction jsa = (JsonSerializationAction) config.getActions().get(0);

    ItemIdValue subject1 = Datamodel.makeWikidataItemIdValue("Q42");
    MonolingualTextValue mtv1 = Datamodel.makeMonolingualTextValue("Test1", "en");
    MonolingualTextValue mtv2 = Datamodel.makeMonolingualTextValue("Test2", "fr");

    ItemDocument id1 = Datamodel.makeItemDocument(subject1, Arrays.asList(mtv1, mtv2), Arrays.asList(mtv1),
            Collections.<MonolingualTextValue>emptyList(), Collections.<StatementGroup>emptyList(),
            Collections.<String, SiteLink>emptyMap());

    jsa.open();//from   w w w.j a  v  a2  s  .c o  m
    jsa.processItemDocument(id1);
    jsa.close();

    MockDirectoryManager mdm = new MockDirectoryManager(Paths.get("."), false);

    ObjectMapper mapper = new ObjectMapper();
    ObjectReader documentReader = mapper.reader(JacksonTermedStatementDocument.class);
    MappingIterator<JacksonTermedStatementDocument> documentIterator = documentReader
            .readValues(mdm.getInputStreamForFile("output.json.bz2", CompressionType.BZ2));

    List<EntityDocument> results = new ArrayList<>();
    while (documentIterator.hasNextValue()) {
        JacksonTermedStatementDocument document = documentIterator.nextValue();
        document.setSiteIri(Datamodel.SITE_WIKIDATA);
        results.add(document);
    }
    documentIterator.close();

    assertEquals(1, results.size());
    assertEquals(id1, results.get(0));
}

From source file:org.wikidata.wdtk.client.JsonSerializationActionTest.java

@Test
public void testJsonOutput() throws IOException {
    String[] args = new String[] { "-a", "json", "-o", "/path/to/output.json" };

    DirectoryManagerFactory.setDirectoryManagerClass(MockDirectoryManager.class);

    ClientConfiguration config = new ClientConfiguration(args);
    JsonSerializationAction jsa = (JsonSerializationAction) config.getActions().get(0);

    ItemIdValue subject1 = Datamodel.makeWikidataItemIdValue("Q42");
    ItemIdValue subject2 = Datamodel.makeWikidataItemIdValue("Q43");
    MonolingualTextValue mtv1 = Datamodel.makeMonolingualTextValue("Test1", "en");
    MonolingualTextValue mtv2 = Datamodel.makeMonolingualTextValue("Test2", "fr");

    ItemDocument id1 = Datamodel.makeItemDocument(subject1, Arrays.asList(mtv1, mtv2), Arrays.asList(mtv1),
            Collections.<MonolingualTextValue>emptyList(), Collections.<StatementGroup>emptyList(),
            Collections.<String, SiteLink>emptyMap());

    ItemDocument id2 = Datamodel.makeItemDocument(subject2, Collections.<MonolingualTextValue>emptyList(),
            Arrays.asList(mtv2), Collections.<MonolingualTextValue>emptyList(),
            Collections.<StatementGroup>emptyList(), Collections.<String, SiteLink>emptyMap());

    PropertyDocument pd1 = Datamodel.makePropertyDocument(Datamodel.makeWikidataPropertyIdValue("P31"),
            Arrays.asList(mtv1), Collections.<MonolingualTextValue>emptyList(), Arrays.asList(mtv1),
            Datamodel.makeDatatypeIdValue(DatatypeIdValue.DT_MONOLINGUAL_TEXT));

    jsa.open();//  w  w  w .  j  a va 2s .  c om
    jsa.processItemDocument(id1);
    jsa.processPropertyDocument(pd1);
    jsa.processItemDocument(id2);
    jsa.close();

    MockDirectoryManager mdm = new MockDirectoryManager(Paths.get("/path/to/"), false);

    ObjectMapper mapper = new ObjectMapper();
    ObjectReader documentReader = mapper.reader(JacksonTermedStatementDocument.class);
    MappingIterator<JacksonTermedStatementDocument> documentIterator = documentReader
            .readValues(mdm.getInputStreamForFile("output.json", CompressionType.NONE));

    List<EntityDocument> results = new ArrayList<>();
    while (documentIterator.hasNextValue()) {
        JacksonTermedStatementDocument document = documentIterator.nextValue();
        document.setSiteIri(Datamodel.SITE_WIKIDATA);
        results.add(document);
    }
    documentIterator.close();

    assertEquals(3, results.size());
    assertEquals(id1, results.get(0));
    assertEquals(pd1, results.get(1));
    assertEquals(id2, results.get(2));

}

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   w w  w.  ja va2  s.  com

    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
    }
}