if (!deParameterizedIdentifier.equalsIgnoreCase(SUPPORTED_FORMAT)) {
logger.info("serialize() the format '" + deParameterizedIdentifier + "' is not supported by this implementation");
return;
}
JsonLd jsonLd = new JsonLd();
// If there is no namespace prefix map set, we use the namespaces
// known from the NamespaceEnum
if (this.namespacePrefixMap.isEmpty()) {
for (NamespaceEnum ns : NamespaceEnum.values()) {
logger.debug("Adding JSON-LD namespace " + ns.getPrefix() + ":" + ns.getNamespace());
this.namespacePrefixMap.put(ns.getNamespace(), ns.getPrefix());
}
}
jsonLd.setNamespacePrefixMap(this.namespacePrefixMap);
jsonLd.setUseTypeCoercion(this.useTypeCoercion);
Map<NonLiteral, String> subjects = createSubjectsMap(tc);
for (NonLiteral subject : subjects.keySet()) {
JsonLdResource resource = new JsonLdResource();
String strSubject = subject.toString();
if (subject instanceof UriRef) {
UriRef uri = (UriRef) subject;
strSubject = uri.getUnicodeString();
}
resource.setSubject(strSubject);
Iterator<Triple> triplesFromSubject = tc.filter(subject, null, null);
while (triplesFromSubject.hasNext()) {
Triple currentTriple = triplesFromSubject.next();
if (currentTriple.getPredicate().getUnicodeString().equals(RDF_NS_TYPE)) {
if (logger.isDebugEnabled()) {
logger.debug("serialize() adding rdf:type: \"a\":" + currentTriple.getObject());
}
resource.addType(((UriRef) currentTriple.getObject()).getUnicodeString());
} else {
if (logger.isDebugEnabled()) {
logger.debug("serializer() adding predicate " + currentTriple.getPredicate().toString() + " with object " + currentTriple.getObject().toString());
}
String property = currentTriple.getPredicate().getUnicodeString();
JsonLdProperty jldProperty = resource.getProperty(property);
if (jldProperty == null) {
jldProperty = new JsonLdProperty(property);
}
String strValue = currentTriple.getObject().toString();
JsonLdPropertyValue jldValue = new JsonLdPropertyValue();
if (currentTriple.getObject() instanceof PlainLiteral) {
PlainLiteral plain = (PlainLiteral) currentTriple.getObject();
if (plain.getLanguage() != null) {
jldValue.setLanguage(plain.getLanguage().toString());
}
strValue = plain.getLexicalForm();
}
else if (currentTriple.getObject() instanceof TypedLiteral) {
TypedLiteral typedObject = (TypedLiteral) currentTriple.getObject();
String type = typedObject.getDataType().getUnicodeString();
jldValue.setType(type);
strValue = typedObject.getLexicalForm();
}
else if (currentTriple.getObject() instanceof UriRef) {
UriRef uriRef = (UriRef) currentTriple.getObject();
jldValue.setType(JsonLdCommon.ID);
strValue = uriRef.getUnicodeString();
}
jldValue.setValue(convertValueType(strValue));
jldProperty.addValue(jldValue);
resource.putProperty(jldProperty);
}
}
jsonLd.put(resource.getSubject(), resource);
}
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(serializedGraph,"utf-8"));
writer.write(jsonLd.toString(this.indentation));
writer.flush();
} catch (IOException ioe) {
logger.error(ioe.getMessage());
throw new RuntimeException(ioe.getMessage());
}