|
| 1 | +package org.linkeddatafragments.datasource; |
| 2 | + |
| 3 | +import com.hp.hpl.jena.rdf.model.Model; |
| 4 | +import com.hp.hpl.jena.rdf.model.ModelFactory; |
| 5 | +import com.hp.hpl.jena.rdf.model.Property; |
| 6 | +import com.hp.hpl.jena.rdf.model.RDFNode; |
| 7 | +import com.hp.hpl.jena.rdf.model.Resource; |
| 8 | +import com.hp.hpl.jena.rdf.model.StmtIterator; |
| 9 | +import com.hp.hpl.jena.rdf.model.impl.PropertyImpl; |
| 10 | +import com.hp.hpl.jena.rdf.model.impl.ResourceImpl; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +/** |
| 15 | + * An Index data source provides an overview of all available datasets. |
| 16 | + * |
| 17 | + * @author Miel Vander Sande |
| 18 | + */ |
| 19 | +public class IndexDataSource extends DataSource { |
| 20 | + |
| 21 | + final String RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; |
| 22 | + final String RDFS = "http://www.w3.org/2000/01/rdf-schema#"; |
| 23 | + final String DC = "http://purl.org/dc/terms/"; |
| 24 | + final String VOID = "http://rdfs.org/ns/void#"; |
| 25 | + |
| 26 | + private final Model model; |
| 27 | + |
| 28 | + public IndexDataSource(String baseUrl, HashMap<String, IDataSource> datasources) { |
| 29 | + super("Index", "List of all datasources"); |
| 30 | + |
| 31 | + this.model = ModelFactory.createDefaultModel(); |
| 32 | + |
| 33 | + for (Map.Entry<String, IDataSource> entry : datasources.entrySet()) { |
| 34 | + String datasourceName = entry.getKey(); |
| 35 | + IDataSource datasource = entry.getValue(); |
| 36 | + |
| 37 | + Resource datasourceUrl = new ResourceImpl(baseUrl + "/" + datasourceName); |
| 38 | + |
| 39 | + model.add(datasourceUrl, new PropertyImpl(RDF + "type"), VOID + "Dataset"); |
| 40 | + model.add(datasourceUrl, new PropertyImpl(RDFS + "label"), datasource.getTitle()); |
| 41 | + model.add(datasourceUrl, new PropertyImpl(DC + "title"), datasource.getTitle()); |
| 42 | + model.add(datasourceUrl, new PropertyImpl(DC + "description"), datasource.getDescription()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public TriplePatternFragment getFragment(Resource subject, Property predicate, RDFNode object, long offset, long limit) { |
| 48 | + StmtIterator listStatements = this.model.listStatements(subject, predicate, object); |
| 49 | + Model result = ModelFactory.createDefaultModel(); |
| 50 | + |
| 51 | + long index = 0; |
| 52 | + while (listStatements.hasNext() && index < offset) { |
| 53 | + listStatements.next(); |
| 54 | + index++; |
| 55 | + } |
| 56 | + |
| 57 | + while (listStatements.hasNext() && index < (offset + limit)) { |
| 58 | + result.add(listStatements.next()); |
| 59 | + } |
| 60 | + |
| 61 | + return new TriplePatternFragmentBase(result, result.size()); |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments