Refine search
@Override public void setup(Context context) throws IOException { jsonLdContextFile = context.getConfiguration().get("jsonld.context.file"); jsonLdContextURL = context.getConfiguration().get("jsonld.context.url"); if(jsonLdContextFile != null) { InputStream in = new FileInputStream(new File(jsonLdContextFile)); jsonLdContext = JsonUtils.fromInputStream(in); in.close(); } else if(jsonLdContextURL != null) { InputStream in = new URL(jsonLdContextURL).openStream(); jsonLdContext = JsonUtils.fromInputStream(in); in.close(); } else { throw new IOException("No context provided"); } } @Override
try { final long start = System.currentTimeMillis(); Object output = JsonLdProcessor.fromRDF(tc, serializer); output = JsonLdProcessor.expand(output, opts); output = JsonLdProcessor.flatten(output, inframe, opts); logger.debug(" - prettyPrint: {}", prettyPrint); if (prettyPrint) { JsonUtils.writePrettyPrint(writer, output); } else { JsonUtils.write(writer, output);
@Override public void map(Writable key, Text value, Context context) throws IOException, InterruptedException { try { String valueString = value.toString(); JSONObject obj = new JSONObject(valueString); Object outobj = JsonLdProcessor.compact(JsonUtils.fromString(valueString), jsonLdContext, new JsonLdOptions("")); if(outobj instanceof Map && jsonLdContextURL != null) { Map outjsonobj = (Map) outobj; outjsonobj.put("@context", jsonLdContextURL); } outputText.set(JsonUtils.toString(outobj)); if (obj.has("uri")) { context.write(new Text(obj.getString("uri")), outputText); } else if (obj.has("@id")) { context.write(new Text(obj.getString("@id")), outputText); } else { context.write(new Text(obj.toString()), outputText); } }catch(Exception e) { LOG.error("something is wrong", e); } } }
@SuppressWarnings("unchecked") private String doCompact(final InputStream input, final Object context) throws IOException { try { final List<Map<String, Object>> document = ((List<Map<String, Object>>)fromInputStream(input)).stream() .filter(filterExport::test) .collect(Collectors.toList()); return JsonUtils.toString( JsonLdProcessor.compact(document, context, options)); } catch (final JsonLdError ex) { throw new RuntimeException("Error converting JsonLd", ex); } }
/** * Return the current prefixes as a JSON-LD string. * * @return the current prefixes as a JSON-LD string * @throws IOException on any error */ public String getContextString() throws IOException { try { Object compact = JsonLdProcessor.compact( JsonUtils.fromString("{}"), context.getPrefixes(false), new JsonLdOptions()); return JsonUtils.toPrettyString(compact); } catch (Exception e) { throw new IOException(jsonldContextCreationError, e); } }
@Ignore("Schema.org started to redirect from HTTP to HTTPS which breaks the Java HttpURLConnection API") @Test public void fromURLSchemaOrgNoApacheHttpClient() throws Exception { final URL url = new URL("http://schema.org/"); final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.addRequestProperty("Accept", "application/ld+json"); final StringWriter output = new StringWriter(); try (final InputStream directStream = urlConn.getInputStream();) { IOUtils.copy(directStream, output, Charset.forName("UTF-8")); } final Object context = JsonUtils.fromReader(new StringReader(output.toString())); assertTrue(context instanceof Map); assertFalse(((Map<?, ?>) context).isEmpty()); }
final java.net.URLConnection conn = new java.net.URL(manifestURL).openConnection(); this.etag = conn.getHeaderField("ETag"); } else { try { Map<String, Object> rval = (Map<String, Object>) JsonLdProcessor .fromRDF(manifestFile, new JsonLdOptions(manifestURL) { rval = JsonLdProcessor.frame(rval, frame, new JsonLdOptions(manifestURL)); this.manifest = JsonLdProcessor.compact(rval, frame.get("@context"), new JsonLdOptions(manifestURL)); this.tests = (List<Map<String, Object>>) Obj.get(this.manifest, "mf:entries", "@list"); final Object rval = JsonUtils.fromString(manifestFile); if (rval instanceof Map) { this.manifest = (Map<String, Object>) rval;
final String protocol = url.getProtocol(); in = url.openStream(); } else { final HttpUriRequest request = new HttpGet(url.toExternalForm()); return fromInputStream(in); } finally { try {
/** * Fallback method directly using the {@link java.net.HttpURLConnection} * class for cases where servers do not interoperate correctly with Apache * HTTPClient. * * @param url * The URL to access. * @return The result, after conversion from JSON to a Java Object. * @throws JsonParseException * If there was a JSON related error during parsing. * @throws IOException * If there was an IO error during parsing. */ public static Object fromURLJavaNet(java.net.URL url) throws JsonParseException, IOException { final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.addRequestProperty("Accept", ACCEPT_HEADER); final StringWriter output = new StringWriter(); try (final InputStream directStream = urlConn.getInputStream();) { IOUtils.copy(directStream, output, StandardCharsets.UTF_8); } finally { output.flush(); } final Object context = JsonUtils.fromReader(new StringReader(output.toString())); return context; }
final File f = new File(cl.getResource(TEST_DIR).toURI()); final List<File> manifestfiles = Arrays.asList(f.listFiles(new FilenameFilter() { @Override .fromInputStream(manifestfile);
final SesameRDFParser serialiser = new SesameRDFParser(); try { Object output = JsonLdProcessor.fromRDF(model, serialiser); final JsonLdOptions opts = new JsonLdOptions(); opts.setUseRdfType(getWriterConfig().get(JSONLDSettings.USE_RDF_TYPE)); opts.setUseNativeTypes(getWriterConfig().get(JSONLDSettings.USE_NATIVE_TYPES)); output = JsonLdProcessor.expand(output, opts); output = JsonLdProcessor.flatten(output, inframe, opts); JsonUtils.writePrettyPrint(writer, output); } else { JsonUtils.write(writer, output);
input = JsonUtils.fromInputStream(inputStream); } else if (inputType.equals("nt") || inputType.equals("nq")) { final List<String> inputLines = new ArrayList<String>(); final String expectType = expectFile.substring(expectFile.lastIndexOf(".") + 1); if (expectType.equals("jsonld")) { expect = JsonUtils.fromInputStream(expectStream); } else if (expectType.equals("nt") || expectType.equals("nq")) { final List<String> expectLines = new ArrayList<String>(); options.setExpandContext(JsonUtils.fromInputStream(contextStream)); result = JsonLdProcessor.expand(input, options); } else if (testType.contains("jld:CompactTest")) { final InputStream contextStream = cl .getResourceAsStream(TEST_DIR + "/" + test.get("context")); final Object contextJson = JsonUtils.fromInputStream(contextStream); result = JsonLdProcessor.compact(input, contextJson, options); } else if (testType.contains("jld:FlattenTest")) { if (test.containsKey("context")) { final InputStream contextStream = cl .getResourceAsStream(TEST_DIR + "/" + test.get("context")); final Object contextJson = JsonUtils.fromInputStream(contextStream); result = JsonLdProcessor.flatten(input, contextJson, options); .getResourceAsStream(TEST_DIR + "/" + test.get("frame")); final Map<String, Object> frameJson = (Map<String, Object>) JsonUtils .fromInputStream(frameStream);
@Test public void testFrame0002() throws IOException, JsonLdError { final Object frame = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0002-frame.jsonld")); final Object in = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0002-in.jsonld")); final JsonLdOptions opts = new JsonLdOptions(); opts.setCompactArrays(false); final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts); final Object out = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0002-out.jsonld")); assertEquals(out, frame2); }
public void parse(final InputStream in, final String sourceURI) throws JsonLdError, IOException { _callback = new JsonLDtripleCallback(sourceURI); final JsonLdOptions options = new JsonLdOptions(sourceURI); JsonLdProcessor .toRDF(JsonUtils.fromInputStream(in), _callback, options); }
@Test public void fromRdfWithNamespaceLexicographicallyShortestChosen() throws Exception { final RDFDataset inputRdf = new RDFDataset(); inputRdf.setNamespace("aat", "http://vocab.getty.edu/aat/"); inputRdf.setNamespace("aat_rev", "http://vocab.getty.edu/aat/rev/"); inputRdf.addTriple("http://vocab.getty.edu/aat/rev/5001065997", JsonLdConsts.RDF_TYPE, "http://vocab.getty.edu/aat/datatype"); final JsonLdOptions options = new JsonLdOptions(); options.useNamespaces = true; final Object fromRDF = JsonLdProcessor.compact(new JsonLdApi(options).fromRDF(inputRdf), inputRdf.getContext(), options); final RDFDataset rdf = (RDFDataset) JsonLdProcessor.toRDF(fromRDF, options); // System.out.println(rdf.getNamespaces()); assertEquals("http://vocab.getty.edu/aat/", rdf.getNamespace("aat")); assertEquals("http://vocab.getty.edu/aat/rev/", rdf.getNamespace("aat_rev")); final String toJSONLD = JsonUtils.toPrettyString(fromRDF); // System.out.println(toJSONLD); assertTrue("The lexicographically shortest URI was not chosen", toJSONLD.contains("aat:rev/")); }
Object jsonObject = JsonUtils.fromInputStream(new ByteArrayInputStream(obj.toString().getBytes())); JsonLdOptions options = new JsonLdOptions(); options.setExpandContext(context); options.format = JsonLdConsts.APPLICATION_NQUADS; String rdfString = (String)JsonLdProcessor.toRDF(jsonObject, options); return rdfString;
Object output = JsonLdProcessor.fromRDF(triplesDocument.toString()); JsonLdOptions jlo = new JsonLdOptions(); String result = JsonUtils.toString(JsonLdProcessor.compact(output, null, jlo)); reusableOutputValue.set(result); String id = key.toString().trim();
Object obj = getObj(c, parameters, dataStreams); if (obj == null) return null; Object jsonObject = JsonUtils.fromInputStream(new ByteArrayInputStream(obj.toString().getBytes())); context = jsonObject; JsonLdOptions options = new JsonLdOptions(); Object compact = JsonLdProcessor.compact(jsonObject, context, options); return new JSONObject(JsonUtils.toString(compact)); } catch (IOException ex) { Logger.getLogger(CruncherJsonLdCompact.class.getName()).log(Level.SEVERE, null, ex);
@Override public Object resolve(Context c, Map<String, String[]> parameters, Map<String, InputStream> dataStreams) throws JSONException { try { Object obj = getObj(c, parameters, dataStreams); if (obj == null) return null; // Read the file into an Object (The type of this object will be a List, Map, String, Boolean, // Number or null depending on the root object in the file). Object jsonObject = JsonUtils.fromInputStream(new ByteArrayInputStream(obj.toString().getBytes())); // Create an instance of JsonLdOptions with the standard JSON-LD options JsonLdOptions options = new JsonLdOptions(); // Call whichever JSONLD function you want! (e.g. compact) Object compact = JsonLdProcessor.expand(jsonObject,options); // Print out the result (or don't, it's your call!) return new JSONArray(JsonUtils.toString(compact)); } catch (IOException ex) { Logger.getLogger(CruncherJsonLdExpand.class.getName()).log(Level.SEVERE, null, ex); } catch (JsonLdError ex) { Logger.getLogger(CruncherJsonLdExpand.class.getName()).log(Level.SEVERE, null, ex); } return null; }
@Override public void parse(Graph target, InputStream serializedGraph, String formatIdentifier, IRI baseUri) { // The callback will add parsed triples to the target MGraph final ClerezzaTripleCallback ctc = new ClerezzaTripleCallback(); ctc.setMGraph(target); Object input; int startSize = 0; if (logger.isDebugEnabled()) { startSize = target.size(); } final long start = System.currentTimeMillis(); try { input = JsonUtils.fromInputStream(serializedGraph, "UTF-8"); } catch (final IOException e) { logger.error("Unable to read from the parsed input stream", e); throw new RuntimeException(e.getMessage(), e); } try { JsonLdProcessor.toRDF(input, ctc); } catch (final JsonLdError e) { logger.error("Unable to parse JSON-LD from the parsed input stream", e); throw new RuntimeException(e.getMessage(), e); } if (logger.isDebugEnabled()) { logger.debug(" - parsed {} triples in {}ms", target.size() - startSize, System.currentTimeMillis() - start); } }