|
| 1 | +package org.linkeddatafragments.datasource; |
| 2 | + |
| 3 | +import java.util.regex.Matcher; |
| 4 | +import java.util.regex.Pattern; |
| 5 | + |
| 6 | +import org.linkeddatafragments.fragments.LinkedDataFragment; |
| 7 | +import org.linkeddatafragments.fragments.tpf.TriplePatternFragmentRequest; |
| 8 | +import org.linkeddatafragments.util.CommonResources; |
| 9 | + |
| 10 | +import com.hp.hpl.jena.datatypes.TypeMapper; |
| 11 | +import com.hp.hpl.jena.rdf.model.Property; |
| 12 | +import com.hp.hpl.jena.rdf.model.RDFNode; |
| 13 | +import com.hp.hpl.jena.rdf.model.Resource; |
| 14 | +import com.hp.hpl.jena.rdf.model.ResourceFactory; |
| 15 | + |
| 16 | +/** |
| 17 | + * Base class for implementations of {@link IFragmentRequestProcessor} that |
| 18 | + * process {@link TriplePatternFragmentRequest}s based on the Jena API. |
| 19 | + * |
| 20 | + * @author <a href="http://olafhartig.de">Olaf Hartig</a> |
| 21 | + * @author Ruben Verborgh |
| 22 | + */ |
| 23 | +abstract public class AbstractJenaBasedRequestProcessorForTriplePatterns |
| 24 | + extends AbstractRequestProcessorForTriplePatterns |
| 25 | +{ |
| 26 | + abstract static protected class Worker |
| 27 | + extends AbstractRequestProcessorForTriplePatterns.Worker |
| 28 | + { |
| 29 | + public Worker( final TriplePatternFragmentRequest request ) |
| 30 | + { |
| 31 | + super( request ); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + protected LinkedDataFragment createFragment( final String subject, |
| 36 | + final String predicate, |
| 37 | + final String object, |
| 38 | + final long offset, |
| 39 | + final long limit ) |
| 40 | + throws IllegalArgumentException |
| 41 | + { |
| 42 | + final Resource s = parseAsResource( subject ); |
| 43 | + final Property p = parseAsProperty( predicate ); |
| 44 | + final RDFNode o = parseAsNode( object ); |
| 45 | + |
| 46 | + return createFragment( s, p, o, offset, limit ); |
| 47 | + } |
| 48 | + |
| 49 | + abstract protected LinkedDataFragment createFragment( |
| 50 | + final Resource subject, |
| 51 | + final Property predicate, |
| 52 | + final RDFNode object, |
| 53 | + final long offset, |
| 54 | + final long limit ) |
| 55 | + throws IllegalArgumentException; |
| 56 | + |
| 57 | + /** |
| 58 | + * Parses the given value as an RDF resource. |
| 59 | + * |
| 60 | + * @param value the value |
| 61 | + * @return the parsed value, or null if unspecified |
| 62 | + */ |
| 63 | + public Resource parseAsResource( String value ) |
| 64 | + { |
| 65 | + RDFNode subject = parseAsNode( value ); |
| 66 | + return subject == null || subject instanceof Resource |
| 67 | + ? (Resource) subject |
| 68 | + : CommonResources.INVALID_URI; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Parses the given value as an RDF property. |
| 73 | + * |
| 74 | + * @param value the value |
| 75 | + * @return the parsed value, or null if unspecified |
| 76 | + */ |
| 77 | + public Property parseAsProperty( String value ) |
| 78 | + { |
| 79 | + RDFNode predicateNode = parseAsNode( value ); |
| 80 | + if ( predicateNode instanceof Resource ) { |
| 81 | + final String uri = ( (Resource) predicateNode ).getURI(); |
| 82 | + return ResourceFactory.createProperty( uri ); |
| 83 | + } |
| 84 | + else if ( predicateNode == null ) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + else { |
| 88 | + return CommonResources.INVALID_URI; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public final static TypeMapper TYPES = TypeMapper.getInstance(); |
| 93 | + public final static Pattern STRINGPATTERN |
| 94 | + = Pattern.compile("^\"(.*)\"(?:@(.*)|\\^\\^<?([^<>]*)>?)?$"); |
| 95 | + |
| 96 | + /** |
| 97 | + * Parses the given value as an RDF node. |
| 98 | + * |
| 99 | + * @param value the value |
| 100 | + * @return the parsed value, or null if unspecified |
| 101 | + */ |
| 102 | + public RDFNode parseAsNode( String value ) |
| 103 | + { |
| 104 | + // nothing or empty indicates an unknown |
| 105 | + if ( value == null || value.isEmpty() ) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + // find the kind of entity based on the first character |
| 110 | + char firstChar = value.charAt(0); |
| 111 | + switch ( firstChar ) |
| 112 | + { |
| 113 | + // variable or blank node indicates an unknown |
| 114 | + case '?': |
| 115 | + case '_': |
| 116 | + return null; |
| 117 | + |
| 118 | + // angular brackets indicate a URI |
| 119 | + case '<': |
| 120 | + return ResourceFactory.createResource( |
| 121 | + value.substring(1, value.length() - 1) ); |
| 122 | + |
| 123 | + // quotes indicate a string |
| 124 | + case '"': |
| 125 | + Matcher matcher = STRINGPATTERN.matcher( value ); |
| 126 | + if ( matcher.matches() ) { |
| 127 | + String body = matcher.group(1); |
| 128 | + String lang = matcher.group(2); |
| 129 | + String type = matcher.group(3); |
| 130 | + if ( lang != null ) { |
| 131 | + return ResourceFactory.createLangLiteral( |
| 132 | + body, lang ); |
| 133 | + } |
| 134 | + else if ( type != null ) { |
| 135 | + return ResourceFactory.createTypedLiteral( |
| 136 | + body, TYPES.getSafeTypeByName(type) ); |
| 137 | + } |
| 138 | + else { |
| 139 | + return ResourceFactory.createPlainLiteral( body ); |
| 140 | + } |
| 141 | + } |
| 142 | + else { |
| 143 | + return CommonResources.INVALID_URI; |
| 144 | + } |
| 145 | + |
| 146 | + // assume it's a URI without angular brackets |
| 147 | + default: |
| 148 | + return ResourceFactory.createResource( value ); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + } // end of class Worker |
| 153 | + |
| 154 | +} |
0 commit comments