Search in sources :

Example 1 with Prefix

use of de.uni_koblenz.aggrimm.icp.crypto.sign.graph.Prefix in project webofneeds by researchstudio-sat.

the class ModelConverter method namedGraphToModel.

private static Model namedGraphToModel(NamedGraph graph, List<Prefix> prefixes) throws Exception {
    // Here, the simplest, but probably not the most efficient, approach is
    // applied:  Signingframework's reader and Jena's writer are used to
    // transform data from one data structure to another. Works fine.
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
    String dfPref = "";
    // write prefixes
    for (Prefix p : prefixes) {
        writer.write(p.toString());
        if (p.getPrefix().equals(":")) {
            dfPref = p.getIriContent();
        }
    }
    // write NamedGraph
    TriGPlusWriter.writeGraph(writer, graph, 3);
    writer.close();
    String content = os.toString();
    // read the result with Jena as Dataset
    ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    Dataset dataset = DatasetFactory.createGeneral();
    RDFDataMgr.read(dataset, is, RDFFormat.TRIG.getLang());
    // extract the Model that corresponds to the NamedGraph
    String modelName = graphNameToModelName(graph.getName(), dfPref);
    Model model = dataset.getNamedModel(modelName);
    for (Prefix pref : prefixes) {
        model.setNsPrefix(pref.getPrefix().replace(":", ""), pref.getIriContent());
    }
    return model;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Dataset(org.apache.jena.query.Dataset) Model(org.apache.jena.rdf.model.Model) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Prefix(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.Prefix) BufferedWriter(java.io.BufferedWriter)

Example 2 with Prefix

use of de.uni_koblenz.aggrimm.icp.crypto.sign.graph.Prefix in project webofneeds by researchstudio-sat.

the class ModelConverter method modelToGraphCollection.

private static GraphCollection modelToGraphCollection(String name, Model model, Map<String, String> pm) {
    // Convert each subj pred obj in Jena Statement into String and add to
    // SigningFramework's NamedGraph.
    // The simpler approach with just using Jena's writer and Signingframework's
    // reader to transform data between data structures won't work since
    // Signingframework has problems with recognizing the [] structure
    GraphCollection graphc = new GraphCollection();
    NamedGraph namedGraph = new NamedGraph(enclose(name, "<", ">"), 0, null);
    StmtIterator iterator = model.listStatements();
    while (iterator.hasNext()) {
        Statement stmt = iterator.nextStatement();
        String subjString = rdfNodeAsString(stmt.getSubject());
        String objString = rdfNodeAsString(stmt.getObject());
        String predString = enclose(stmt.getPredicate().asResource().getURI(), "<", ">");
        Triple gcTriple = new Triple(subjString, predString, objString);
        namedGraph.addTriple(gcTriple);
    }
    graphc.addGraph(namedGraph);
    for (String prefix : pm.keySet()) {
        graphc.addPrefix(new Prefix(prefix + ":", "<" + pm.get(prefix) + ">"));
    }
    return graphc;
}
Also used : GraphCollection(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection) Triple(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.Triple) StmtIterator(org.apache.jena.rdf.model.StmtIterator) NamedGraph(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.NamedGraph) Statement(org.apache.jena.rdf.model.Statement) Prefix(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.Prefix)

Aggregations

Prefix (de.uni_koblenz.aggrimm.icp.crypto.sign.graph.Prefix)2 GraphCollection (de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection)1 NamedGraph (de.uni_koblenz.aggrimm.icp.crypto.sign.graph.NamedGraph)1 Triple (de.uni_koblenz.aggrimm.icp.crypto.sign.graph.Triple)1 BufferedWriter (java.io.BufferedWriter)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Dataset (org.apache.jena.query.Dataset)1 Model (org.apache.jena.rdf.model.Model)1 Statement (org.apache.jena.rdf.model.Statement)1 StmtIterator (org.apache.jena.rdf.model.StmtIterator)1