Java Code Examples for com.github.jsonldjava.core.JsonLdProcessor#expand()

The following examples show how to use com.github.jsonldjava.core.JsonLdProcessor#expand() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JSONLDOAAnnotationMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OAAnnotation oaAnnotation, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaAnnotation.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 2
Source File: JSONLDW3CStatisticsPageMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CStatisticsPage w3cStatisticsPage, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cStatisticsPage.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 3
Source File: JSONLDOABatchOperationMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OABatchOperation oaBatchOperation, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaBatchOperation.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 4
Source File: JSONLDOAAnnotationPageMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OAAnnotationPage oaAnnotationPage, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaAnnotationPage.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 5
Source File: JSONLDW3CAnnotationCollectionMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CAnnotationCollection w3cAnnotationCollection, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cAnnotationCollection.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 6
Source File: JSONLDW3CAnnotationPageMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CAnnotationPage w3cAnnotationPage, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cAnnotationPage.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 7
Source File: JSONLDOAStatisticsPageMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OAStatisticsPage oaStatisticsPage, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaStatisticsPage.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 8
Source File: JSONLDOAAnnotationHistoryMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(OAAnnotationHistory oaAnnotationHistory, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = oaAnnotationHistory.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    JSONLDProfile.Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(JSONLDProfile.Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(JSONLDProfile.Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(JSONLDProfile.Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 9
Source File: JSONLDW3CAnnotationMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CAnnotation w3cAnnotation, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cAnnotation.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 10
Source File: JSONLDW3CBatchOperationMessageConverter.java    From elucidate-server with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected String getStringRepresentation(W3CBatchOperation w3cBatchOperation, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = w3cBatchOperation.getJsonMap();

    JSONLDProfile jsonLdProfile = getJsonLdProfile(contentType, defaultContexts);

    Format format = jsonLdProfile.getFormats().get(0);
    if (format.equals(Format.COMPACTED)) {
        jsonMap = JsonLdProcessor.compact(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else if (format.equals(Format.EXPANDED)) {
        List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
        jsonMap = (Map<String, Object>) jsonList.get(0);
    } else if (format.equals(Format.FLATTENED)) {
        jsonMap = (Map<String, Object>) JsonLdProcessor.flatten(jsonMap, jsonLdProfile.getContexts(), jsonLdOptions);
    } else {
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes());
    }

    jsonMap = reorderJsonAttributes(jsonMap);
    return JsonUtils.toPrettyString(jsonMap);
}
 
Example 11
Source File: AbstractSchemaValidatorTest.java    From elucidate-server with MIT License 5 votes vote down vote up
protected void validateJson(String jsonFileName) throws IOException, ProcessingException, JsonLdError {

        JsonNode schema = getSchema();
        assertNotNull(schema);

        String jsonStr = getJson(jsonFileName);
        assertNotNull(jsonStr);

        Object jsonObj = JsonUtils.fromString(jsonStr);
        List<Object> expandedJson = JsonLdProcessor.expand(jsonObj);
        jsonStr = JsonUtils.toString(expandedJson);
        JsonNode json = JsonLoader.fromString(jsonStr);

        JsonValidator jsonValidator = JsonSchemaFactory.byDefault().getValidator();
        ProcessingReport processingReport = jsonValidator.validate(schema, json);
        assertNotNull(processingReport);
        if (!processingReport.isSuccess()) {

            ArrayNode jsonArray = JsonNodeFactory.instance.arrayNode();
            assertNotNull(jsonArray);

            Iterator<ProcessingMessage> iterator = processingReport.iterator();
            while (iterator.hasNext()) {
                ProcessingMessage processingMessage = iterator.next();
                jsonArray.add(processingMessage.asJson());
            }

            String errorJson = JsonUtils.toPrettyString(jsonArray);
            assertNotNull(errorJson);

            fail(errorJson);
        }
    }
 
Example 12
Source File: JSONLDW3CAnnotationCollectionMessageConverter.java    From elucidate-server with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected W3CAnnotationCollection getObjectRepresentation(String jsonStr, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = (Map<String, Object>) JsonUtils.fromString(jsonStr);
    List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
    jsonMap = (Map<String, Object>) jsonList.get(0);

    W3CAnnotationCollection w3cAnnotationCollection = new W3CAnnotationCollection();
    w3cAnnotationCollection.setJsonMap(jsonMap);
    return w3cAnnotationCollection;
}
 
Example 13
Source File: JSONLDW3CBatchOperationMessageConverter.java    From elucidate-server with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected W3CBatchOperation getObjectRepresentation(String jsonStr, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = (Map<String, Object>) JsonUtils.fromString(jsonStr);
    List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
    jsonMap = (Map<String, Object>) jsonList.get(0);

    W3CBatchOperation w3cBatchOperation = new W3CBatchOperation();
    w3cBatchOperation.setJsonMap(jsonMap);
    return w3cBatchOperation;
}
 
Example 14
Source File: JSONLDW3CAnnotationMessageConverter.java    From elucidate-server with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected W3CAnnotation getObjectRepresentation(String jsonStr, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = (Map<String, Object>) JsonUtils.fromString(jsonStr);
    List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
    jsonMap = (Map<String, Object>) jsonList.get(0);

    W3CAnnotation w3cAnnotation = new W3CAnnotation();
    w3cAnnotation.setJsonMap(jsonMap);
    return w3cAnnotation;
}
 
Example 15
Source File: JSONLDOAAnnotationHistoryMessageConverter.java    From elucidate-server with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected OAAnnotationHistory getObjectRepresentation(String jsonStr, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = (Map<String, Object>) JsonUtils.fromString(jsonStr);
    List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
    jsonMap = (Map<String, Object>) jsonList.get(0);

    OAAnnotationHistory oaAnnotationHistory = new OAAnnotationHistory();
    oaAnnotationHistory.setJsonMap(jsonMap);
    return oaAnnotationHistory;
}
 
Example 16
Source File: JSONLDOAAnnotationCollectionMessageConverter.java    From elucidate-server with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected OAAnnotationCollection getObjectRepresentation(String jsonStr, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = (Map<String, Object>) JsonUtils.fromString(jsonStr);
    List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
    jsonMap = (Map<String, Object>) jsonList.get(0);

    OAAnnotationCollection oaAnnotationCollection = new OAAnnotationCollection();
    oaAnnotationCollection.setJsonMap(jsonMap);
    return oaAnnotationCollection;
}
 
Example 17
Source File: JSONLDW3CAnnotationHistoryMessageConverter.java    From elucidate-server with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected W3CAnnotationHistory getObjectRepresentation(String jsonStr, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = (Map<String, Object>) JsonUtils.fromString(jsonStr);
    List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
    jsonMap = (Map<String, Object>) jsonList.get(0);

    W3CAnnotationHistory w3cAnnotationHistory = new W3CAnnotationHistory();
    w3cAnnotationHistory.setJsonMap(jsonMap);
    return w3cAnnotationHistory;
}
 
Example 18
Source File: JSONLDOABatchOperationMessageConverter.java    From elucidate-server with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected OABatchOperation getObjectRepresentation(String jsonStr, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = (Map<String, Object>) JsonUtils.fromString(jsonStr);
    List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
    jsonMap = (Map<String, Object>) jsonList.get(0);

    OABatchOperation oaBatchOperation = new OABatchOperation();
    oaBatchOperation.setJsonMap(jsonMap);
    return oaBatchOperation;
}
 
Example 19
Source File: JSONLDOAAnnotationMessageConverter.java    From elucidate-server with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected OAAnnotation getObjectRepresentation(String jsonStr, MediaType contentType) throws Exception {
    Map<String, Object> jsonMap = (Map<String, Object>) JsonUtils.fromString(jsonStr);
    List<Object> jsonList = JsonLdProcessor.expand(jsonMap, jsonLdOptions);
    jsonMap = (Map<String, Object>) jsonList.get(0);

    OAAnnotation oaAnnotation = new OAAnnotation();
    oaAnnotation.setJsonMap(jsonMap);
    return oaAnnotation;
}
 
Example 20
Source File: JSONLDWriter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void endRDF() throws RDFHandlerException {
	checkWritingStarted();
	final JSONLDInternalRDFParser serialiser = new JSONLDInternalRDFParser();
	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);
		WriterConfig writerConfig = getWriterConfig();
		opts.setCompactArrays(writerConfig.get(JSONLDSettings.COMPACT_ARRAYS));
		opts.setProduceGeneralizedRdf(writerConfig.get(JSONLDSettings.PRODUCE_GENERALIZED_RDF));
		opts.setUseRdfType(writerConfig.get(JSONLDSettings.USE_RDF_TYPE));
		opts.setUseNativeTypes(writerConfig.get(JSONLDSettings.USE_NATIVE_TYPES));
		// opts.optimize = getWriterConfig().get(JSONLDSettings.OPTIMIZE);

		if (writerConfig.get(JSONLDSettings.HIERARCHICAL_VIEW)) {
			output = JSONLDHierarchicalProcessor.fromJsonLdObject(output);
		}

		if (baseURI != null && writerConfig.get(BasicWriterSettings.BASE_DIRECTIVE)) {
			opts.setBase(baseURI);
		}
		if (mode == JSONLDMode.EXPAND) {
			output = JsonLdProcessor.expand(output, opts);
		}
		// TODO: Implement inframe in JSONLDSettings
		final Object inframe = null;
		if (mode == JSONLDMode.FLATTEN) {
			output = JsonLdProcessor.flatten(output, inframe, opts);
		}
		if (mode == JSONLDMode.COMPACT) {
			final Map<String, Object> ctx = new LinkedHashMap<>();
			addPrefixes(ctx, model.getNamespaces());
			final Map<String, Object> localCtx = new HashMap<>();
			localCtx.put(JsonLdConsts.CONTEXT, ctx);

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

	} catch (JsonLdError | IOException e) {
		throw new RDFHandlerException("Could not render JSONLD", e);
	}
}