|
| 1 | +package com.github.jsonldjava.clerezza; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.OutputStream; |
| 5 | +import java.io.OutputStreamWriter; |
| 6 | +import java.io.Writer; |
| 7 | +import java.nio.charset.Charset; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.Dictionary; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.LinkedHashMap; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import org.apache.clerezza.rdf.core.TripleCollection; |
| 15 | +import org.apache.clerezza.rdf.core.serializedform.SerializingProvider; |
| 16 | +import org.apache.clerezza.rdf.core.serializedform.SupportedFormat; |
| 17 | +import org.apache.felix.scr.annotations.Activate; |
| 18 | +import org.apache.felix.scr.annotations.Component; |
| 19 | +import org.apache.felix.scr.annotations.ConfigurationPolicy; |
| 20 | +import org.apache.felix.scr.annotations.Deactivate; |
| 21 | +import org.apache.felix.scr.annotations.Property; |
| 22 | +import org.apache.felix.scr.annotations.PropertyOption; |
| 23 | +import org.apache.felix.scr.annotations.Service; |
| 24 | +import org.osgi.service.component.ComponentContext; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +import com.github.jsonldjava.core.JsonLdError; |
| 29 | +import com.github.jsonldjava.core.JsonLdOptions; |
| 30 | +import com.github.jsonldjava.core.JsonLdProcessor; |
| 31 | +import com.github.jsonldjava.utils.JsonUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * A {@link org.apache.clerezza.rdf.core.serializedform.SerializingProvider} for |
| 35 | + * JSON-LD (application/ld+json) based on the java-jsonld library |
| 36 | + * |
| 37 | + * @author Rupert Westenthaler |
| 38 | + */ |
| 39 | +@Component(immediate = true, policy = ConfigurationPolicy.OPTIONAL) |
| 40 | +@Service |
| 41 | +@SupportedFormat(value = { "application/ld+json", "application/json" }) |
| 42 | +public class ClerezzaJsonLdSerializingProvider implements SerializingProvider { |
| 43 | + |
| 44 | + private final Logger logger = LoggerFactory.getLogger(getClass()); |
| 45 | + |
| 46 | + private static final Charset UTF8 = Charset.forName("UTF-8"); |
| 47 | + |
| 48 | + private static final String MODE_EXPAND = "expand"; |
| 49 | + private static final String MODE_FLATTEN = "flatten"; |
| 50 | + private static final String MODE_COMPACT = "compact"; |
| 51 | + |
| 52 | + @Property(value = "", options = { |
| 53 | + @PropertyOption(value = "%mode.option.none", name = ""), |
| 54 | + @PropertyOption(value = "%mode.option.flatten", name = "flatten"), |
| 55 | + @PropertyOption(value = "%mode.option.compact", name = "compact"), |
| 56 | + @PropertyOption(value = "%mode.option.expand", name = MODE_EXPAND) }) |
| 57 | + private static final String PROP_MODE = "mode"; |
| 58 | + |
| 59 | + @Property(boolValue = false) |
| 60 | + private static final String PROP_USE_RDF_TYPE = "useRdfTye"; |
| 61 | + |
| 62 | + @Property(boolValue = false) |
| 63 | + private static final String PROP_USE_NATIVE_TYPES = "useNativeTypes"; |
| 64 | + |
| 65 | + @Property(boolValue = true) |
| 66 | + private static final String PROP_PRETTY_PRINT = "prettyPrint"; |
| 67 | + |
| 68 | + // TODO: make configurable or read the whole prefix.cc list from a file and |
| 69 | + // search for really used namespaces while parsing the triples in the |
| 70 | + // ClerezzaRDFParser |
| 71 | + private static Map<String, String> DEFAULT_NAMESPACES; |
| 72 | + static { |
| 73 | + // core ontologies, top from prefixcc and some stanbol specific |
| 74 | + final Map<String, String> ns = new LinkedHashMap<String, String>(); |
| 75 | + // core schemas |
| 76 | + ns.put("xsd", "http://www.w3.org/2001/XMLSchema#"); |
| 77 | + ns.put("owl", "http://www.w3.org/2002/07/owl#"); |
| 78 | + ns.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); |
| 79 | + ns.put("rdfs", "http://www.w3.org/2000/01/rdf-schema#"); |
| 80 | + // well known ontologies |
| 81 | + ns.put("skos", "http://www.w3.org/2004/02/skos/core#"); |
| 82 | + ns.put("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#"); |
| 83 | + ns.put("dc", "http://purl.org/dc/elements/1.1/"); |
| 84 | + ns.put("foaf", "http://xmlns.com/foaf/0.1/"); |
| 85 | + ns.put("ma", "http://www.w3.org/ns/ma-ont#"); |
| 86 | + // big datasets |
| 87 | + ns.put("dbo", "http://dbpedia.org/ontology/"); |
| 88 | + ns.put("dbp", "http://dbpedia.org/property/"); |
| 89 | + ns.put("yago", "http://yago-knowledge.org/resource/"); |
| 90 | + ns.put("fb", "http://rdf.freebase.com/ns/"); |
| 91 | + ns.put("geonames", "http://www.geonames.org/ontology#"); |
| 92 | + // stanbol specific |
| 93 | + ns.put("fise", "http://fise.iks-project.eu/ontology/"); |
| 94 | + ns.put("enhancer", "http://stanbol.apache.org/ontology/enhancer/enhancer#"); |
| 95 | + ns.put("entityhub", "http://stanbol.apache.org/ontology/entityhub/entityhub#"); |
| 96 | + |
| 97 | + DEFAULT_NAMESPACES = Collections.unmodifiableMap(ns); |
| 98 | + } |
| 99 | + |
| 100 | + private JsonLdOptions opts = null; |
| 101 | + private String mode; |
| 102 | + |
| 103 | + private boolean prettyPrint; |
| 104 | + |
| 105 | + @Override |
| 106 | + public void serialize(OutputStream serializedGraph, TripleCollection tc, String formatIdentifier) { |
| 107 | + final ClerezzaRDFParser serializer = new ClerezzaRDFParser(); |
| 108 | + try { |
| 109 | + final long start = System.currentTimeMillis(); |
| 110 | + Object output = JsonLdProcessor.fromRDF(tc, serializer); |
| 111 | + |
| 112 | + if (MODE_EXPAND.equalsIgnoreCase(mode)) { |
| 113 | + logger.debug(" - mode: {}", MODE_EXPAND); |
| 114 | + output = JsonLdProcessor.expand(output, opts); |
| 115 | + } |
| 116 | + if (MODE_FLATTEN.equalsIgnoreCase(mode)) { |
| 117 | + logger.debug(" - mode: {}", MODE_FLATTEN); |
| 118 | + // TODO: Allow inframe config |
| 119 | + final Object inframe = null; |
| 120 | + output = JsonLdProcessor.flatten(output, inframe, opts); |
| 121 | + } |
| 122 | + if (MODE_COMPACT.equalsIgnoreCase(mode)) { |
| 123 | + logger.debug(" - mode: {}", MODE_COMPACT); |
| 124 | + // TODO: collect namespaces used in the triples in the |
| 125 | + // ClerezzaRDFParser |
| 126 | + final Map<String, Object> localCtx = new HashMap<String, Object>(); |
| 127 | + localCtx.put("@context", DEFAULT_NAMESPACES); |
| 128 | + output = JsonLdProcessor.compact(output, localCtx, opts); |
| 129 | + } |
| 130 | + final Writer writer = new OutputStreamWriter(serializedGraph, UTF8); |
| 131 | + logger.debug(" - prettyPrint: {}", prettyPrint); |
| 132 | + if (prettyPrint) { |
| 133 | + JsonUtils.writePrettyPrint(writer, output); |
| 134 | + } else { |
| 135 | + JsonUtils.write(writer, output); |
| 136 | + } |
| 137 | + if (logger.isDebugEnabled()) { |
| 138 | + logger.debug(" - serialized {} triples in {}ms", serializer.getCount(), |
| 139 | + System.currentTimeMillis() - start); |
| 140 | + } |
| 141 | + } catch (final JsonLdError e) { |
| 142 | + throw new RuntimeException(e.getMessage(), e); |
| 143 | + } catch (final IOException e) { |
| 144 | + throw new RuntimeException(e.getMessage(), e); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Activate |
| 149 | + protected void activate(ComponentContext ctx) { |
| 150 | + opts = new JsonLdOptions(); |
| 151 | + @SuppressWarnings("unchecked") |
| 152 | + final Dictionary<String, Object> config = ctx.getProperties(); |
| 153 | + // boolean properties |
| 154 | + opts.setUseRdfType(getState(config.get(PROP_USE_RDF_TYPE), false)); |
| 155 | + opts.setUseNativeTypes(getState(config.get(PROP_USE_NATIVE_TYPES), false)); |
| 156 | + prettyPrint = getState(config.get(PROP_PRETTY_PRINT), true); |
| 157 | + // parse the string mode |
| 158 | + final Object value = config.get(PROP_MODE); |
| 159 | + mode = value == null ? null : value.toString(); |
| 160 | + } |
| 161 | + |
| 162 | + @Deactivate |
| 163 | + protected void deactivate(ComponentContext ctx) { |
| 164 | + opts = null; |
| 165 | + mode = null; |
| 166 | + prettyPrint = false; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @param value |
| 171 | + */ |
| 172 | + private boolean getState(Object value, boolean defaultState) { |
| 173 | + if (value instanceof Boolean) { |
| 174 | + return ((Boolean) value).booleanValue(); |
| 175 | + } else if (value != null) { |
| 176 | + return Boolean.parseBoolean(value.toString()); |
| 177 | + } else { |
| 178 | + return defaultState; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | +} |
| 183 | + |
0 commit comments