Skip to content

Commit

Permalink
Merge pull request #4 from LinkedDataFragments/master
Browse files Browse the repository at this point in the history
merging in latest changes from origin
  • Loading branch information
hartig committed Dec 23, 2015
2 parents a524f1a + a904459 commit 29a2efe
Show file tree
Hide file tree
Showing 22 changed files with 1,065 additions and 647 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package org.linkeddatafragments.datasource;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.linkeddatafragments.fragments.LinkedDataFragment;
import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentRequest;
import org.linkeddatafragments.util.CommonResources;

import com.hp.hpl.jena.datatypes.TypeMapper;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;

/**
* Base class for implementations of {@link IFragmentRequestProcessor} that
* process {@link TriplePatternFragmentRequest}s based on the Jena API.
*
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
* @author Ruben Verborgh
*/
abstract public class AbstractJenaBasedRequestProcessorForTriplePatterns
extends AbstractRequestProcessorForTriplePatterns
{
abstract static protected class Worker
extends AbstractRequestProcessorForTriplePatterns.Worker
{
public Worker( final TriplePatternFragmentRequest request )
{
super( request );
}

@Override
protected LinkedDataFragment createFragment( final String subject,
final String predicate,
final String object,
final long offset,
final long limit )
throws IllegalArgumentException
{
final Resource s = parseAsResource( subject );
final Property p = parseAsProperty( predicate );
final RDFNode o = parseAsNode( object );

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 )
throws IllegalArgumentException;

/**
* Parses the given value as an RDF resource.
*
* @param value the value
* @return the parsed value, or null if unspecified
*/
public 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 Property parseAsProperty( String value )
{
RDFNode predicateNode = parseAsNode( value );
if ( predicateNode instanceof Resource ) {
final String uri = ( (Resource) predicateNode ).getURI();
return ResourceFactory.createProperty( uri );
}
else if ( predicateNode == null ) {
return null;
}
else {
return 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 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 );
}
else if ( type != null ) {
return ResourceFactory.createTypedLiteral(
body, TYPES.getSafeTypeByName(type) );
}
else {
return ResourceFactory.createPlainLiteral( body );
}
}
else {
return CommonResources.INVALID_URI;
}

// assume it's a URI without angular brackets
default:
return ResourceFactory.createResource( value );
}
}

} // end of class Worker

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.linkeddatafragments.datasource;

import org.linkeddatafragments.fragments.LinkedDataFragment;
import org.linkeddatafragments.fragments.LinkedDataFragmentRequest;

/**
* Base class for implementations of {@link IFragmentRequestProcessor}.
*
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
*/
abstract public class AbstractRequestProcessor
implements IFragmentRequestProcessor
{
@Override
public void close() {}

@Override
final public LinkedDataFragment createRequestedFragment(
final LinkedDataFragmentRequest request )
throws IllegalArgumentException
{
return getWorker( request ).createRequestedFragment();
}

abstract protected Worker getWorker(
final LinkedDataFragmentRequest request )
throws IllegalArgumentException;


abstract static protected class Worker
{
public final LinkedDataFragmentRequest request;

public Worker( final LinkedDataFragmentRequest request )
{
this.request = request;
}

abstract public LinkedDataFragment createRequestedFragment()
throws IllegalArgumentException;

} // end of class Worker

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,82 +7,91 @@
import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentRequest;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;

/**
* Base class for implementations of {@link IFragmentRequestProcessor} that
* process triple pattern based requests.
* process {@link TriplePatternFragmentRequest}s.
*
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
*/
public abstract class AbstractRequestProcessorForTriplePatterns
implements IFragmentRequestProcessor
extends AbstractRequestProcessor
{
public final TriplePatternFragmentRequest request;
public final long pageNumber;

public AbstractRequestProcessorForTriplePatterns(
final TriplePatternFragmentRequest request )
@Override
protected Worker getWorker( final LinkedDataFragmentRequest request )
throws IllegalArgumentException
{
this.request = request;
if ( request.isPageRequest() )
this.pageNumber = request.getPageNumber();
if ( request instanceof TriplePatternFragmentRequest )
return getWorker( (TriplePatternFragmentRequest) request );
else
this.pageNumber = 1L;
throw new IllegalArgumentException( request.getClass().getName() );
}

@Override
public void close() {}
abstract protected Worker getWorker(
final TriplePatternFragmentRequest request )
throws IllegalArgumentException;

@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 );

}
abstract static protected class Worker
extends AbstractRequestProcessor.Worker
{
public Worker( final TriplePatternFragmentRequest request )
{
super( request );
}

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);
@Override
public LinkedDataFragment createRequestedFragment()
throws IllegalArgumentException
{
final long limit = LinkedDataFragmentRequest.TRIPLESPERPAGE;
final long offset;
if ( request.isPageRequest() )
offset = limit * ( request.getPageNumber() - 1L );
else
offset = 0L;

return createFragment( s, p, o, offset, limit );
}
final TriplePatternFragmentRequest tpfRequest =
(TriplePatternFragmentRequest) request;

abstract protected LinkedDataFragment createFragment( final Resource subject,
final Property predicate,
final RDFNode object,
final long offset,
final long limit );
return createFragment( tpfRequest.getSubject(),
tpfRequest.getPredicate(),
tpfRequest.getObject(),
offset, limit );
}

protected TriplePatternFragment createEmptyTriplePatternFragment()
{
return new TriplePatternFragmentImpl( request.getFragmentURL(),
request.getDatasetURL() );
}
abstract protected LinkedDataFragment createFragment( final String subj,
final String pred,
final String obj,
final long offset,
final long limit )
throws IllegalArgumentException;

protected TriplePatternFragment createTriplePatternFragment(
Model triples, long totalSize, final boolean isLastPage )
{
return new TriplePatternFragmentImpl( triples,
totalSize,
request.getFragmentURL(),
request.getDatasetURL(),
pageNumber,
isLastPage );
}
protected TriplePatternFragment createEmptyTriplePatternFragment()
{
return new TriplePatternFragmentImpl( request.getFragmentURL(),
request.getDatasetURL() );
}

protected TriplePatternFragment createTriplePatternFragment(
final Model triples,
final long totalSize,
final boolean isLastPage )
{
final long pageNumber;
if ( request.isPageRequest() )
pageNumber = request.getPageNumber();
else
pageNumber = 1L;

return new TriplePatternFragmentImpl( triples,
totalSize,
request.getFragmentURL(),
request.getDatasetURL(),
pageNumber,
isLastPage );
}

} // end of class Worker

}
19 changes: 6 additions & 13 deletions src/org/linkeddatafragments/datasource/DataSource.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package org.linkeddatafragments.datasource;

import javax.servlet.http.HttpServletRequest;

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

/**
*
Expand All @@ -32,21 +30,16 @@ public String getTitle() {
};

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

return getRequestProcessor( r );
return new TPFRequestParser();
}

@Override
Expand Down
Loading

0 comments on commit 29a2efe

Please sign in to comment.