Example usage for com.fasterxml.jackson.dataformat.yaml YAMLMapper readTree

List of usage examples for com.fasterxml.jackson.dataformat.yaml YAMLMapper readTree

Introduction

In this page you can find the example usage for com.fasterxml.jackson.dataformat.yaml YAMLMapper readTree.

Prototype

@Override
public <T extends TreeNode> T readTree(JsonParser jp) throws IOException, JsonProcessingException 

Source Link

Document

Method to deserialize JSON content as tree expressed using set of JsonNode instances.

Usage

From source file:cat.calidos.morfeu.model.injection.URIToParsedModule.java

@Produces
@Named("FetchedTransformedContent")
public static InputStream fetchedTransformedContent(@Named("FetchableContentURI") URI uri,
        @Named("FetchedRawContent") InputStream fetchedRawContent, YAMLMapper mapper, Producer<Model> model)
        throws FetchingException, TransformException {

    // get the yaml and apply the transformation from yaml to xml

    try {//from  w  w  w . j  a  v  a2  s.  com

        log.trace("Converting yaml to xml '{}'", uri);

        JsonNode yaml = mapper.readTree(fetchedRawContent);
        Map<String, Object> values = new HashMap<String, Object>(2);

        List<CellModel> rootCellModels = model.get().get().getRootCellModels();
        values.put("cellmodels", rootCellModels);
        values.put("yaml", yaml);
        values.put("case", "yaml-to-xml");
        //rootCellModels.stream().map(cm -> cm.getName()).forEach(name -> log.trace("CellModel:{}",name));

        String transformedContent = DaggerViewComponent.builder()
                .withTemplate("templates/transform/content-yaml-to-xml.twig").withValue(values).build()
                .render();
        log.trace("Transformed yaml to xml '{}'", transformedContent);

        return IOUtils.toInputStream(transformedContent, Config.DEFAULT_CHARSET);

    } catch (IOException e) {
        log.error("Could not fetch yaml '{}' ({}", uri, e);
        throw new FetchingException("Problem when fetching yaml '" + uri + "'", e);
    } catch (InterruptedException | ExecutionException e) {
        log.error("Could not transform yaml to xml '{}' ({}", uri, e);
        throw new TransformException("Problem when transforming yaml to xml '" + uri + "'", e);
    }

}

From source file:cat.calidos.morfeu.model.transform.TransformNoValueIntTest.java

@Test
public void testTransformJSONToYAML() throws Exception {

    Document doc = produceDocumentFromPath("test-resources/documents/document4.json");
    assertNotNull(doc);/*from  www. j  av a  2s . c  om*/

    Map<String, Object> values = new HashMap<String, Object>(2);
    values.put("cells", doc.getContent().asList());
    values.put("model", doc.getModel());

    String transformed = DaggerViewComponent.builder().withTemplate("templates/transform/content-to-yaml.twig")
            .withValue(values).build().render();
    //System.err.println(transformed);

    YAMLMapper mapper = new YAMLMapper();
    JsonNode yaml = mapper.readTree(transformed);
    assertNotNull(yaml);
    assertTrue(yaml.isObject());
    assertTrue(yaml.has("rows"));

    JsonNode col = yaml.get("rows").get(0).get("cols").get(0); //rows/cols/col
    assertNotNull(col);
    assertTrue(col.isObject());
    assertTrue(col.has("stuff"));
    assertTrue(col.has("data4"));

    JsonNode stuff = col.get("stuff");
    assertTrue(stuff.isArray());
    assertEquals("stuff : [] in transformed yaml should have no elements", 0, stuff.size());

    JsonNode data4 = col.get("data4");
    assertTrue(data4.isObject());
    assertEquals("data4: {} in transformed yaml should be empty", 0, data4.size());

}

From source file:cat.calidos.morfeu.model.injection.ContentSaverParserIntTest.java

@Test
public void testSaveToYAML() throws Exception {

    String outputPath = temporaryOutputFilePath() + ".yaml";
    URI outputURI = new URI("file://" + outputPath);

    FileSaver saver = DaggerContentSaverParserComponent.builder().from(content).to(outputURI).having(contentURI)
            .model(modelURI).withModelFetchedFrom(modelFetchableURI).build().saver().get();
    saver.save();/*from  www  .  j  a v  a 2s.  c o m*/
    File savedFile = new File(outputPath);
    assertTrue("Saver component did not create a file", savedFile.exists());
    savedFile.deleteOnExit();

    String writtenContent = FileUtils.readFileToString(savedFile, Config.DEFAULT_CHARSET);

    YAMLMapper mapper = new YAMLMapper();
    JsonNode yaml = mapper.readTree(writtenContent);
    assertNotNull(yaml);
    assertTrue(yaml.isObject());
    assertTrue(yaml.has("rows"));

    JsonNode rows = yaml.get("rows"); //rows
    assertNotNull(rows);
    assertTrue(rows.isArray());
    assertEquals(1, rows.size());

}

From source file:cat.calidos.morfeu.model.transform.TransformTezt.java

protected String transformYAMLToXML(String yamlPath, String documentPath) throws Exception {

    YAMLMapper mapper = new YAMLMapper();
    File inputFile = new File(yamlPath);
    String content = FileUtils.readFileToString(inputFile, Config.DEFAULT_CHARSET);
    JsonNode yaml = mapper.readTree(content);

    Document doc = produceDocumentFromPath(documentPath);
    assertNotNull(doc);//from  w  w  w .  ja  v  a2s .  c om
    Map<String, Object> values = new HashMap<String, Object>(2);
    values.put("yaml", yaml);
    values.put("cellmodels", doc.getModel().getRootCellModels());
    values.put("case", "yaml-to-xml");

    return DaggerViewComponent.builder().withTemplate("templates/transform/content-yaml-to-xml.twig")
            .withValue(values).build().render();
    // sed -E 's/\$(.+)?\$/\$\1\$ $(- set zzzz = deb("\1") -)$/g'

}