/** * Injects a document at the specified URL for offline use. */ public Builder injectDocument(String url, CharSequence content) { documentLoader.addInjectedDoc(url, content.toString()); return this; }
/** * Supports "reverse injection" from an existing remote document. * <p> * Generally the document contents are already parsed JSON content, however this method will also accept * document contents represented as a string of JSON data. */ public Builder injectDocument(RemoteDocument document) { String url = document.getDocumentUrl(); if (document.getDocument() instanceof CharSequence) { // Avoid unnecessary JSON serialization return injectDocument(url, (CharSequence) document.getDocument()); } else { try { documentLoader.addInjectedDoc(url, JsonUtils.toString(document.getDocument())); return this; } catch (IOException e) { throw new JsonLdError(JsonLdError.Error.LOADING_INJECTED_CONTEXT_FAILED, url, e); } } }
@Test public void injectContext() throws Exception { final Object jsonObject = JsonUtils.fromString( "{ \"@context\":\"http://nonexisting.example.com/thing\", \"pony\":5 }"); final JsonLdOptions options = new JsonLdOptions(); // Verify fails to find context by default try { JsonLdProcessor.expand(jsonObject, options); fail("Expected exception to occur"); } catch (final JsonLdError e) { // Success } // Inject context final DocumentLoader dl = new DocumentLoader(); dl.addInjectedDoc("http://nonexisting.example.com/thing", "{ \"@context\": { \"pony\":\"http://nonexisting.example.com/thing/pony\" } }"); options.setDocumentLoader(dl); // Execute final List<Object> expand = JsonLdProcessor.expand(jsonObject, options); // Verify result final Object v = ((Map<Object, Object>) ((List<Object>) ((Map<Object, Object>) expand .get(0)).get("http://nonexisting.example.com/thing/pony")).get(0)).get("@value"); assertEquals(5, v); }