예제 #1
0
  @Override
  public void endRDF() throws RDFHandlerException {
    final SesameRDFParser serialiser = new SesameRDFParser();
    try {
      Object output = JsonLdProcessor.fromRDF(model, serialiser);

      final JSONLDMode mode = getWriterConfig().get(JSONLDSettings.JSONLD_MODE);

      final JsonLdOptions opts = new JsonLdOptions();
      // opts.addBlankNodeIDs =
      // getWriterConfig().get(BasicParserSettings.PRESERVE_BNODE_IDS);
      opts.setUseRdfType(getWriterConfig().get(JSONLDSettings.USE_RDF_TYPE));
      opts.setUseNativeTypes(getWriterConfig().get(JSONLDSettings.USE_NATIVE_TYPES));
      // opts.optimize = getWriterConfig().get(JSONLDSettings.OPTIMIZE);

      if (mode == JSONLDMode.EXPAND) {
        output = JsonLdProcessor.expand(output, opts);
      }
      // TODO: Implement inframe in JSONLDSettings
      final Object inframe = null;
      if (mode == JSONLDMode.FLATTEN) {
        output = JsonLdProcessor.frame(output, inframe, opts);
      }
      if (mode == JSONLDMode.COMPACT) {
        final Map<String, Object> ctx = new LinkedHashMap<String, Object>();
        addPrefixes(ctx, model.getNamespaces());
        final Map<String, Object> localCtx = new HashMap<String, Object>();
        localCtx.put("@context", ctx);

        output = JsonLdProcessor.compact(output, localCtx, opts);
      }
      if (getWriterConfig().get(BasicWriterSettings.PRETTY_PRINT)) {
        JSONUtils.writePrettyPrint(writer, output);
      } else {
        JSONUtils.write(writer, output);
      }

    } catch (final JsonLdError e) {
      throw new RDFHandlerException("Could not render JSONLD", e);
    } catch (final JsonGenerationException e) {
      throw new RDFHandlerException("Could not render JSONLD", e);
    } catch (final JsonMappingException e) {
      throw new RDFHandlerException("Could not render JSONLD", e);
    } catch (final IOException e) {
      throw new RDFHandlerException("Could not render JSONLD", e);
    }
  }
  @Test
  public void triplesTest() throws IOException, JsonLdError {
    final InputStream in =
        getClass().getClassLoader().getResourceAsStream("testfiles/product.jsonld");
    final Object input = JSONUtils.fromInputStream(in);

    final ClerezzaTripleCallback callback = new ClerezzaTripleCallback();

    final MGraph graph = (MGraph) JsonLdProcessor.toRDF(input, callback);

    for (final Triple t : graph) {
      System.out.println(t);
    }
    assertEquals("Graph size", 13, graph.size());
  }
  @Test
  public void curiesInContextTest() throws IOException, JsonLdError {
    final InputStream in =
        getClass().getClassLoader().getResourceAsStream("testfiles/curies-in-context.jsonld");
    final Object input = JSONUtils.fromInputStream(in);

    final ClerezzaTripleCallback callback = new ClerezzaTripleCallback();

    final MGraph graph = (MGraph) JsonLdProcessor.toRDF(input, callback);

    for (final Triple t : graph) {
      System.out.println(t);
      assertTrue(
          "Predicate got fully expanded", t.getPredicate().getUnicodeString().startsWith("http"));
    }
    assertEquals("Graph size", 3, graph.size());
  }