Skip to content

Commit

Permalink
Merged master
Browse files Browse the repository at this point in the history
  • Loading branch information
Miel Vander Sande committed Dec 16, 2015
2 parents ca04b19 + 09e861e commit 967e0f0
Show file tree
Hide file tree
Showing 27 changed files with 1,170 additions and 553 deletions.
2 changes: 1 addition & 1 deletion WebContent/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<servlet>
<display-name>BasicLdfServlet</display-name>
<servlet-name>BasicLdfServlet</servlet-name>
<servlet-class>org.linkeddatafragments.servlet.TriplePatternFragmentServlet</servlet-class>
<servlet-class>org.linkeddatafragments.servlet.LinkedDataFragmentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BasicLdfServlet</servlet-name>
Expand Down
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 );
}

}
42 changes: 28 additions & 14 deletions src/org/linkeddatafragments/datasource/DataSource.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package org.linkeddatafragments.datasource;

import javax.servlet.http.HttpServletRequest;

import org.linkeddatafragments.config.ConfigReader;
import org.linkeddatafragments.fragments.LinkedDataFragmentRequest;
import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentRequest;
import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentRequestImpl;

/**
*
* @author mielvandersande
Expand All @@ -8,20 +15,6 @@
public abstract class DataSource implements IDataSource {
protected String title;
protected String description;

/**
*
* @param offset
* @param limit
*/
protected void checkBoundaries(long offset, long limit) {
if (offset < 0) {
throw new IndexOutOfBoundsException("offset");
}
if (limit < 1) {
throw new IllegalArgumentException("limit");
}
}

public DataSource(String title, String description) {
this.title = title;
Expand All @@ -37,4 +30,25 @@ public String getDescription() {
public String getTitle() {
return this.title;
};

/**
* This implementation assumes that the given request is a
* {@link TriplePatternFragmentRequest}.
*
* Data sources for other types of {@link LinkedDataFragmentRequest}s must
* override this method accordingly.
*/
@Override
public IFragmentRequestProcessor getRequestProcessor(
final HttpServletRequest request,
final ConfigReader config )
{
final TriplePatternFragmentRequest r =
new TriplePatternFragmentRequestImpl( request, config );

return getRequestProcessor( r );
}

@Override
public void close() {}
}
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);
}
}

}
42 changes: 26 additions & 16 deletions src/org/linkeddatafragments/datasource/IDataSource.java
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 );
}
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 src/org/linkeddatafragments/datasource/TriplePatternFragment.java

This file was deleted.

This file was deleted.

Loading

0 comments on commit 967e0f0

Please sign in to comment.