All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jsonldjava.impl.ClerezzaTripleCallback Maven / Gradle / Ivy

There is a newer version: 0.12.0
Show newest version
package com.github.jsonldjava.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.clerezza.rdf.core.*;
import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
import org.apache.clerezza.rdf.core.impl.TripleImpl;
import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl;

import com.github.jsonldjava.core.JSONLDTripleCallback;
import com.github.jsonldjava.utils.Obj;

public class ClerezzaTripleCallback implements JSONLDTripleCallback {

    private MGraph mGraph = new SimpleMGraph();
    private Map bNodeMap = new HashMap();

    public void setMGraph(MGraph mGraph) {
        this.mGraph = mGraph;
        bNodeMap = new HashMap();
    }

    public MGraph getMGraph() {
        return mGraph;
    }

    private void triple(String s, String p, String o, String graph) {
        if (s == null || p == null || o == null) {
            // TODO: i don't know what to do here!!!!
            return;
        }

        NonLiteral subject = getNonLiteral(s);
		UriRef predicate = new UriRef(p);
		NonLiteral object = getNonLiteral(o);
		mGraph.add(new TripleImpl(subject, predicate, object));
    }

    private void triple(String s, String p, String value, String datatype, String language, String graph) {
        NonLiteral subject = getNonLiteral(s);
		UriRef predicate = new UriRef(p);
		Resource object;
		if (language != null) {
			object = new PlainLiteralImpl(value, new Language(language)); 
		} else {
			if (datatype != null) {
				object = new TypedLiteralImpl(value, new UriRef(datatype));
			} else {
				object = new PlainLiteralImpl(value);
			}
		}
      
		mGraph.add(new TripleImpl(subject, predicate, object));
    }

	private NonLiteral getNonLiteral(String s) {
		if (s.startsWith("_:")) {
			return getBNode(s);
		} else {
			return new UriRef(s);
		}
	}

	private BNode getBNode(String s) {
		if (bNodeMap.containsKey(s)) {
			return bNodeMap.get(s);
		} else {
			BNode result = new BNode();
			bNodeMap.put(s, result);
			return result;
		}
	}

	@Override
	public Object call(Map dataset) {
		for (String graphName : dataset.keySet()) {
			List> triples = (List>) dataset.get(graphName);
			if ("@default".equals(graphName)) {
				graphName = null;
			}
			for (Map triple : triples) {
				if ("literal".equals(Obj.get(triple, "object", "type"))) {
					triple((String)Obj.get(triple, "subject", "value"), (String)Obj.get(triple, "predicate", "value"), 
							(String)Obj.get(triple, "object", "value"), (String)Obj.get(triple, "object", "datatype"), (String)Obj.get(triple, "object", "language"), graphName);
				} else {
					triple((String)Obj.get(triple, "subject", "value"), (String)Obj.get(triple, "predicate", "value"), (String)Obj.get(triple, "object", "value"), graphName);
				}
			}
		}

		return getMGraph();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy