|
| 1 | +package com.github.jsonldjava.clerezza; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +import org.apache.clerezza.rdf.core.BNode; |
| 8 | +import org.apache.clerezza.rdf.core.Language; |
| 9 | +import org.apache.clerezza.rdf.core.MGraph; |
| 10 | +import org.apache.clerezza.rdf.core.NonLiteral; |
| 11 | +import org.apache.clerezza.rdf.core.Resource; |
| 12 | +import org.apache.clerezza.rdf.core.UriRef; |
| 13 | +import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl; |
| 14 | +import org.apache.clerezza.rdf.core.impl.SimpleMGraph; |
| 15 | +import org.apache.clerezza.rdf.core.impl.TripleImpl; |
| 16 | +import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl; |
| 17 | + |
| 18 | +import com.github.jsonldjava.core.JsonLdTripleCallback; |
| 19 | +import com.github.jsonldjava.core.RDFDataset; |
| 20 | + |
| 21 | +public class ClerezzaTripleCallback implements JsonLdTripleCallback { |
| 22 | + |
| 23 | + private MGraph mGraph = new SimpleMGraph(); |
| 24 | + private Map<String, BNode> bNodeMap = new HashMap<String, BNode>(); |
| 25 | + |
| 26 | + public void setMGraph(MGraph mGraph) { |
| 27 | + this.mGraph = mGraph; |
| 28 | + bNodeMap = new HashMap<String, BNode>(); |
| 29 | + } |
| 30 | + |
| 31 | + public MGraph getMGraph() { |
| 32 | + return mGraph; |
| 33 | + } |
| 34 | + |
| 35 | + private void triple(String s, String p, String o, String graph) { |
| 36 | + if (s == null || p == null || o == null) { |
| 37 | + // TODO: i don't know what to do here!!!! |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + final NonLiteral subject = getNonLiteral(s); |
| 42 | + final UriRef predicate = new UriRef(p); |
| 43 | + final NonLiteral object = getNonLiteral(o); |
| 44 | + mGraph.add(new TripleImpl(subject, predicate, object)); |
| 45 | + } |
| 46 | + |
| 47 | + private void triple(String s, String p, String value, String datatype, String language, |
| 48 | + String graph) { |
| 49 | + final NonLiteral subject = getNonLiteral(s); |
| 50 | + final UriRef predicate = new UriRef(p); |
| 51 | + Resource object; |
| 52 | + if (language != null) { |
| 53 | + object = new PlainLiteralImpl(value, new Language(language)); |
| 54 | + } else { |
| 55 | + if (datatype != null) { |
| 56 | + object = new TypedLiteralImpl(value, new UriRef(datatype)); |
| 57 | + } else { |
| 58 | + object = new PlainLiteralImpl(value); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + mGraph.add(new TripleImpl(subject, predicate, object)); |
| 63 | + } |
| 64 | + |
| 65 | + private NonLiteral getNonLiteral(String s) { |
| 66 | + if (s.startsWith("_:")) { |
| 67 | + return getBNode(s); |
| 68 | + } else { |
| 69 | + return new UriRef(s); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private BNode getBNode(String s) { |
| 74 | + if (bNodeMap.containsKey(s)) { |
| 75 | + return bNodeMap.get(s); |
| 76 | + } else { |
| 77 | + final BNode result = new BNode(); |
| 78 | + bNodeMap.put(s, result); |
| 79 | + return result; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Object call(RDFDataset dataset) { |
| 85 | + for (String graphName : dataset.graphNames()) { |
| 86 | + final List<RDFDataset.Quad> quads = dataset.getQuads(graphName); |
| 87 | + if ("@default".equals(graphName)) { |
| 88 | + graphName = null; |
| 89 | + } |
| 90 | + for (final RDFDataset.Quad quad : quads) { |
| 91 | + if (quad.getObject().isLiteral()) { |
| 92 | + triple(quad.getSubject().getValue(), quad.getPredicate().getValue(), quad |
| 93 | + .getObject().getValue(), quad.getObject().getDatatype(), quad |
| 94 | + .getObject().getLanguage(), graphName); |
| 95 | + } else { |
| 96 | + triple(quad.getSubject().getValue(), quad.getPredicate().getValue(), quad |
| 97 | + .getObject().getValue(), graphName); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return getMGraph(); |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments