Hi,

here is a Test class using jsonld-java that should do what you want (override 
the @context). It reads the jsonld file (that includes an @context defined as a 
URI) and the file defining the @context from files on my disk. Just replace 
with your own files.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.github.jsonldjava.utils.JsonUtils;

public class JsonLdReadTest {
@Test
public final void test() throws JsonGenerationException, IOException {
        // read the jsonld file (which contains a URI as value of @context)
        File f = new File("/Users/fps/Desktop/jsonldTest.json");
        Object jsonldObject = JsonUtils.fromInputStream(new FileInputStream(f));
        
        // read the context file
        // (here from disk, normally read file at URI defined in @context)
        File ctxf = new File("/Users/fps/Desktop/jsonldTestContext.json");
        Object jsonLdContext = JsonUtils.fromInputStream(new 
FileInputStream(ctxf));
        
        // jsonldObject is a Map. 
        // Replace value associated to @context
        ((Map) jsonldObject).put("@context", jsonLdContext);
        
        // the jsonld with the @context replaced
        String jsonld = JsonUtils.toString(jsonLdContext);
        System.out.println(jsonld);
        
        // no problem now to parse it
        Model m = parse(jsonld);
}

private Model parse(String jsonld) {
  Model m = ModelFactory.createDefaultModel();
  StringReader reader = new StringReader(jsonld);
  m.read(reader, null, "JSON-LD");
  return m;
}
}

(Note that this is not optimal: the json is parsed twice)
HTH

fps

> Le 7 janv. 2017 à 22:08, Grahame Grieve <[email protected]> 
> a écrit :
> 
> hi
> 
> I am trying to read a json-ld file into a Model so I can use IsoMatcher to
> compare it with another Model.
> 
> When I try the simple approach:
> 
>   Model mj = RDFDataMgr.loadModel([json-ld filename]);
> 
> I get the error LOADING_REMOTE_CONTEXT_FAILED returned. I don't know why;
> the file works in the json-ld playground. The source is actually
> http://build.fhir.org/account-example.jsonld. Unfortunately, jsonld-java
> suppresses the original exception.
> 
> But irrespective of that, I need to override the @context value and specify
> my own local context instead (I'm actually running the build that generates
> the content for build.fhir.org). I can't find out how to override the
> context. At
> http://stackoverflow.com/questions/41494678/how-can-i-override-the-context-when-loading-json-ld-in-jena
> , it was suggested that I use read(Model model, String uri, Context context)
> <https://jena.apache.org/documentation/javadoc/arq/org/apache/jena/riot/RDFDataMgr.html#read-org.apache.jena.rdf.model.Model-java.lang.String-org.apache.jena.sparql.util.Context->,
> but I've drawn a blank trying to find information about how to use the
> Context to do this.
> 
> Alternatively, I could use the loading method documented here:
> https://github.com/jsonld-java/jsonld-java#code-example - but I have no
> idea how to get to a Jena graph from there (and I haven't figured out how
> make the custom resolution work in my Eclipse context either)
> 
> How can I get to a Jena graph using a context defined in code somewhere?
> Grahame
> 
> 
> -- 
> -----
> http://www.healthintersections.com.au / [email protected]
> / +61 411 867 065

Reply via email to