-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,170 additions
and
553 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/org/linkeddatafragments/datasource/AbstractRequestProcessorForTriplePatterns.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package org.linkeddatafragments.datasource; | ||
|
||
import org.apache.jena.rdf.model.Model; | ||
import org.apache.jena.rdf.model.Property; | ||
import org.apache.jena.rdf.model.RDFNode; | ||
import org.apache.jena.rdf.model.Resource; | ||
import org.linkeddatafragments.fragments.LinkedDataFragment; | ||
import org.linkeddatafragments.fragments.LinkedDataFragmentRequest; | ||
import org.linkeddatafragments.fragments.tpf.TriplePatternFragment; | ||
import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentImpl; | ||
import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentRequest; | ||
|
||
|
||
/** | ||
* Base class for implementations of {@link IFragmentRequestProcessor} that | ||
* process triple pattern based requests. | ||
* | ||
* @author <a href="http://olafhartig.de">Olaf Hartig</a> | ||
*/ | ||
public abstract class AbstractRequestProcessorForTriplePatterns | ||
implements IFragmentRequestProcessor | ||
{ | ||
public final TriplePatternFragmentRequest request; | ||
public final long pageNumber; | ||
|
||
public AbstractRequestProcessorForTriplePatterns( | ||
final TriplePatternFragmentRequest request ) | ||
{ | ||
this.request = request; | ||
if ( request.isPageRequest() ) | ||
this.pageNumber = request.getPageNumber(); | ||
else | ||
this.pageNumber = 1L; | ||
} | ||
|
||
@Override | ||
public void close() {} | ||
|
||
@Override | ||
public LinkedDataFragment createRequestedFragment() | ||
{ | ||
final long limit = LinkedDataFragmentRequest.TRIPLESPERPAGE; | ||
final long offset = limit * ( pageNumber - 1L ); | ||
|
||
return createFragment( request.getSubject(), | ||
request.getPredicate(), | ||
request.getObject(), | ||
offset, limit ); | ||
|
||
} | ||
|
||
protected LinkedDataFragment createFragment( final String subj, | ||
final String pred, | ||
final String obj, | ||
final long offset, | ||
final long limit ) | ||
{ | ||
final Resource s = FragmentRequestProcessorUtils.parseAsResource(subj); | ||
final Property p = FragmentRequestProcessorUtils.parseAsProperty(pred); | ||
final RDFNode o = FragmentRequestProcessorUtils.parseAsNode(obj); | ||
|
||
return createFragment( s, p, o, offset, limit ); | ||
} | ||
|
||
abstract protected LinkedDataFragment createFragment( final Resource subject, | ||
final Property predicate, | ||
final RDFNode object, | ||
final long offset, | ||
final long limit ); | ||
|
||
protected TriplePatternFragment createEmptyTriplePatternFragment() | ||
{ | ||
return new TriplePatternFragmentImpl( request.getFragmentURL(), | ||
request.getDatasetURL() ); | ||
} | ||
|
||
protected TriplePatternFragment createTriplePatternFragment( | ||
Model triples, long totalSize, final boolean isLastPage ) | ||
{ | ||
return new TriplePatternFragmentImpl( triples, | ||
totalSize, | ||
request.getFragmentURL(), | ||
request.getDatasetURL(), | ||
pageNumber, | ||
isLastPage ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/org/linkeddatafragments/datasource/FragmentRequestProcessorUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package org.linkeddatafragments.datasource; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.apache.jena.datatypes.TypeMapper; | ||
import org.apache.jena.rdf.model.Property; | ||
import org.apache.jena.rdf.model.RDFNode; | ||
import org.apache.jena.rdf.model.Resource; | ||
import org.apache.jena.rdf.model.ResourceFactory; | ||
import org.apache.jena.shared.InvalidPropertyURIException; | ||
|
||
import org.linkeddatafragments.util.CommonResources; | ||
|
||
|
||
|
||
/** | ||
* Utility functions for dealing with (fragment) requests. | ||
* | ||
* @author Ruben Verborgh | ||
* @author <a href="http://olafhartig.de">Olaf Hartig</a> | ||
*/ | ||
public class FragmentRequestProcessorUtils | ||
{ | ||
/** | ||
* Parses the given value as an RDF resource. | ||
* | ||
* @param value the value | ||
* @return the parsed value, or null if unspecified | ||
*/ | ||
public static Resource parseAsResource(String value) { | ||
RDFNode subject = parseAsNode(value); | ||
return subject == null || subject instanceof Resource | ||
? (Resource) subject | ||
: CommonResources.INVALID_URI; | ||
} | ||
|
||
/** | ||
* Parses the given value as an RDF property. | ||
* | ||
* @param value the value | ||
* @return the parsed value, or null if unspecified | ||
*/ | ||
public static Property parseAsProperty(String value) { | ||
RDFNode predicateNode = parseAsNode(value); | ||
if (predicateNode instanceof Resource) { | ||
try { | ||
return ResourceFactory.createProperty(((Resource) predicateNode).getURI()); | ||
} catch (InvalidPropertyURIException ex) { | ||
return CommonResources.INVALID_URI; | ||
} | ||
} | ||
return predicateNode == null ? null : CommonResources.INVALID_URI; | ||
} | ||
|
||
public final static TypeMapper TYPES = TypeMapper.getInstance(); | ||
public final static Pattern STRINGPATTERN | ||
= Pattern.compile("^\"(.*)\"(?:@(.*)|\\^\\^<?([^<>]*)>?)?$"); | ||
|
||
/** | ||
* Parses the given value as an RDF node. | ||
* | ||
* @param value the value | ||
* @return the parsed value, or null if unspecified | ||
*/ | ||
public static RDFNode parseAsNode(String value) { | ||
// nothing or empty indicates an unknown | ||
if (value == null || value.isEmpty()) { | ||
return null; | ||
} | ||
// find the kind of entity based on the first character | ||
char firstChar = value.charAt(0); | ||
switch (firstChar) { | ||
// variable or blank node indicates an unknown | ||
case '?': | ||
case '_': | ||
return null; | ||
// angular brackets indicate a URI | ||
case '<': | ||
return ResourceFactory.createResource(value.substring(1, value.length() - 1)); | ||
// quotes indicate a string | ||
case '"': | ||
Matcher matcher = STRINGPATTERN.matcher(value); | ||
if (matcher.matches()) { | ||
String body = matcher.group(1); | ||
String lang = matcher.group(2); | ||
String type = matcher.group(3); | ||
if (lang != null) { | ||
return ResourceFactory.createLangLiteral(body, lang); | ||
} | ||
if (type != null) { | ||
return ResourceFactory.createTypedLiteral(body, TYPES.getSafeTypeByName(type)); | ||
} | ||
return ResourceFactory.createPlainLiteral(body); | ||
} | ||
return CommonResources.INVALID_URI; | ||
// assume it's a URI without angular brackets | ||
default: | ||
return ResourceFactory.createResource(value); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,36 @@ | ||
package org.linkeddatafragments.datasource; | ||
|
||
import org.apache.jena.rdf.model.Property; | ||
import org.apache.jena.rdf.model.RDFNode; | ||
import org.apache.jena.rdf.model.Resource; | ||
import java.io.Closeable; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.linkeddatafragments.config.ConfigReader; | ||
import org.linkeddatafragments.fragments.LinkedDataFragmentRequest; | ||
|
||
/** | ||
* A data source of Basic Linked Data Fragments. | ||
* A data source of Linked Data Fragments. | ||
* | ||
* @author Ruben Verborgh | ||
* @author <a href="http://olafhartig.de">Olaf Hartig</a> | ||
*/ | ||
public interface IDataSource { | ||
/** | ||
* Gets a page of the Basic Linked Data Fragment matching the specified triple pattern. | ||
* @param subject the subject (null to match any subject) | ||
* @param predicate the predicate (null to match any predicate) | ||
* @param object the object (null to match any object) | ||
* @param offset the triple index at which to start the page | ||
* @param limit the number of triples on the page | ||
* @return the first page of the fragment | ||
*/ | ||
public TriplePatternFragment getFragment(Resource subject, Property predicate, | ||
RDFNode object, long offset, long limit); | ||
public interface IDataSource extends Closeable { | ||
|
||
public String getTitle(); | ||
|
||
public String getDescription(); | ||
|
||
/** | ||
* Returns a data source specific processor for the given request of a | ||
* Linked Data Fragment. | ||
*/ | ||
IFragmentRequestProcessor getRequestProcessor( | ||
final HttpServletRequest request, | ||
final ConfigReader config ); | ||
|
||
/** | ||
* Returns a data source specific processor for the given request of a | ||
* Linked Data Fragment. | ||
*/ | ||
IFragmentRequestProcessor getRequestProcessor( | ||
final LinkedDataFragmentRequest request ); | ||
} |
15 changes: 15 additions & 0 deletions
15
src/org/linkeddatafragments/datasource/IFragmentRequestProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.linkeddatafragments.datasource; | ||
|
||
import java.io.Closeable; | ||
|
||
import org.linkeddatafragments.fragments.LinkedDataFragment; | ||
|
||
/** | ||
* Processes a single request sent to a Linked Data Fragments interface. | ||
* | ||
* @author <a href="http://olafhartig.de">Olaf Hartig</a> | ||
*/ | ||
public interface IFragmentRequestProcessor extends Closeable | ||
{ | ||
LinkedDataFragment createRequestedFragment(); | ||
} |
21 changes: 0 additions & 21 deletions
21
src/org/linkeddatafragments/datasource/TriplePatternFragment.java
This file was deleted.
Oops, something went wrong.
40 changes: 0 additions & 40 deletions
40
src/org/linkeddatafragments/datasource/TriplePatternFragmentBase.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.