|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package ec.edu.cedia.redi.ldclient.provider.json; |
| 19 | + |
| 20 | +import com.jayway.jsonpath.Configuration; |
| 21 | +import com.jayway.jsonpath.JsonPath; |
| 22 | +import com.jayway.jsonpath.Option; |
| 23 | +import com.jayway.jsonpath.ReadContext; |
| 24 | +import ec.edu.cedia.redi.ldclient.provider.json.mappers.JsonPathValueMapper; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import org.apache.marmotta.ldclient.api.provider.DataProvider; |
| 30 | +import org.apache.marmotta.ldclient.exception.DataRetrievalException; |
| 31 | +import org.apache.marmotta.ldclient.services.provider.AbstractHttpProvider; |
| 32 | +import org.openrdf.model.Model; |
| 33 | +import org.openrdf.model.Resource; |
| 34 | +import org.openrdf.model.URI; |
| 35 | +import org.openrdf.model.Value; |
| 36 | +import org.openrdf.model.ValueFactory; |
| 37 | +import org.openrdf.model.impl.ValueFactoryImpl; |
| 38 | +import org.openrdf.model.vocabulary.RDF; |
| 39 | + |
| 40 | +/** |
| 41 | + * |
| 42 | + * @author Xavier Sumba <xavier.sumba93@ucuenca.ec> |
| 43 | + */ |
| 44 | +public abstract class AbstractJSONDataProvider extends AbstractHttpProvider implements DataProvider { |
| 45 | + |
| 46 | + protected static final String CHARSET = "UTF-8"; |
| 47 | + |
| 48 | + protected abstract List<String> getTypes(URI resource); |
| 49 | + |
| 50 | + protected abstract Map<String, JsonPathValueMapper> getMappings(String resource, String requestUrl); |
| 51 | + |
| 52 | + protected Configuration getConfiguration() { |
| 53 | + return Configuration.defaultConfiguration() |
| 54 | + .addOptions(Option.ALWAYS_RETURN_LIST) |
| 55 | + .addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL) |
| 56 | + .addOptions(Option.SUPPRESS_EXCEPTIONS); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected List<String> parseResponse(String resource, String requestUrl, Model triples, InputStream in, String contentType) throws DataRetrievalException { |
| 61 | + ReadContext ctx = JsonPath.parse(in, getConfiguration()); |
| 62 | + ValueFactory vf = ValueFactoryImpl.getInstance(); |
| 63 | + |
| 64 | + URI subject = vf.createURI(resource); |
| 65 | + for (Map.Entry<String, JsonPathValueMapper> mapping : getMappings(resource, requestUrl).entrySet()) { |
| 66 | + URI predicate = vf.createURI(mapping.getKey()); |
| 67 | + |
| 68 | + List a = ctx.read(mapping.getValue().getPath()); |
| 69 | + for (Object o : a) { |
| 70 | + String value; |
| 71 | + if (o == null) { |
| 72 | + continue; |
| 73 | + } else { |
| 74 | + value = String.valueOf(o); |
| 75 | + } |
| 76 | + |
| 77 | + List<Value> objects = mapping.getValue().map(resource, value, vf); |
| 78 | + for (Value object : objects) { |
| 79 | + triples.add(subject, predicate, object); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + URI ptype = vf.createURI(RDF.NAMESPACE + "type"); |
| 85 | + for (String typeUri : getTypes(subject)) { |
| 86 | + Resource type_resource = vf.createURI(typeUri); |
| 87 | + triples.add(vf.createStatement(subject, ptype, type_resource)); |
| 88 | + } |
| 89 | + return Collections.emptyList(); |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments