Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unset prefixes #136

Merged
merged 7 commits into from
Apr 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions jena-arq/src/main/java/org/apache/jena/query/ARQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.jena.query;

import org.apache.jena.riot.RIOT ;
import org.apache.jena.riot.system.RiotLib ;
import org.apache.jena.sparql.SystemARQ ;
import org.apache.jena.sparql.algebra.optimize.TransformOrderByDistinctApplication ;
import org.apache.jena.sparql.core.assembler.AssemblerUtils ;
Expand Down Expand Up @@ -251,6 +252,14 @@ public static void enableBlankNodeResultLabels(boolean val)

public static final Symbol serviceAllowed = Service.serviceAllowed ;

/** If set to true, the parsers will convert undefined prefixes to a URI
* according to the fixup function {@link RiotLib#fixupPrefixes}.
* Normally, unset (which equates to false).
*
* @see RiotLib#isPrefixIRI
*/
public static final Symbol fixupUndefinedPrefixes = SystemARQ.allocSymbol("fixupPrefixes") ;

/**
* A Long value that specifies the number of bindings (or triples for CONSTRUCT queries) to be stored in memory by sort
* operations or hash tables before switching to temporary disk files. The value defaults to -1, which will always
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.jena.graph.NodeFactory ;
import org.apache.jena.graph.Triple ;
import org.apache.jena.iri.IRI ;
import org.apache.jena.query.ARQ ;
import org.apache.jena.riot.RiotException ;
import org.apache.jena.riot.SysRIOT ;
import org.apache.jena.riot.tokens.Token ;
Expand Down Expand Up @@ -217,8 +218,11 @@ private static Node create(ParserProfile pp, Node currentGraph, Token token) {

private static String expandPrefixedName(ParserProfile pp, String prefix, String localPart, Token token) {
String expansion = pp.getPrologue().getPrefixMap().expand(prefix, localPart) ;
if (expansion == null)
if (expansion == null) {
if ( ARQ.isTrue(ARQ.fixupUndefinedPrefixes) )
return RiotLib.fixupPrefixIRI(prefix, localPart) ;
pp.getHandler().fatal("Undefined prefix: " + prefix, token.getLine(), token.getColumn()) ;
}
return expansion ;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ private void checkQuad(Node graph, Node subject, Node predicate, Node object, lo

@Override
public Node createURI(String x, long line, long col) {
if ( ! RiotLib.isBNodeIRI(x) )
if ( RiotLib.isBNodeIRI(x) ) {}
else if ( RiotLib.isPrefixIRI(x) ) {}
else
x = resolveIRI(x, line, col) ;
return super.createURI(x, line, col) ;
}
Expand Down
40 changes: 36 additions & 4 deletions jena-arq/src/main/java/org/apache/jena/riot/system/RiotLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.io.OutputStream ;
import java.io.Writer ;
import java.util.* ;
import java.util.function.Function ;
import java.util.function.Predicate ;

import org.apache.jena.atlas.io.IndentedWriter ;
import org.apache.jena.atlas.iterator.Iter ;
Expand All @@ -38,7 +40,10 @@
import org.apache.jena.graph.NodeFactory ;
import org.apache.jena.graph.Triple ;
import org.apache.jena.query.ARQ ;
import org.apache.jena.riot.* ;
import org.apache.jena.riot.Lang ;
import org.apache.jena.riot.RDFLanguages ;
import org.apache.jena.riot.SysRIOT ;
import org.apache.jena.riot.WriterDatasetRIOT ;
import org.apache.jena.riot.lang.LabelToNode ;
import org.apache.jena.riot.tokens.Token ;
import org.apache.jena.riot.tokens.Tokenizer ;
Expand Down Expand Up @@ -73,12 +78,39 @@ public static Node createIRIorBNode(String iri)
return NodeFactory.createURI(iri) ;
}

/** Test whether */
public static boolean isBNodeIRI(String iri)
{
/** Test whether a IRI is a ARQ-encoded blank node. */
public static boolean isBNodeIRI(String iri) {
return skolomizedBNodes && iri.startsWith(bNodeLabelStart) ;
}

private static final String URI_PREFIX_FIXUP = "::";

// These two must be in-step.
/** Function applied to undefined prefixes to convert to a URI string */
public static final Function<String,String> fixupPrefixes = (x) -> URI_PREFIX_FIXUP.concat(x) ;

/** Function to test for undefined prefix URIs*/
public static final Predicate<String> testFixupedPrefixURI = (x) -> x.startsWith(URI_PREFIX_FIXUP) ;

/** Test whether a IRI is a ARQ-encoded blank node. */
public static boolean isPrefixIRI(String iri) {
return testFixupedPrefixURI.test(iri) ;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, just a style thing, maybe personal, but I would rather read:

/** Function to test for undefined prefix URIs*/  
    public static final Predicate<String> testFixupedPrefixURI  = RiotLib::isPrefixIRI ;

/** Test whether a IRI is a ARQ-encoded blank node. */
    public static boolean isPrefixIRI(String iri) {
        return iri.startsWith("::") ;
    }

or even just factor out testFixupedPrefixURI for the method reference RiotLib::isPrefixIRI.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to leave the possibility of making it configurable. // These two must be in-step.

At the moment, they are final, but the structure is in place for different especially in dev (when tweaking the final is possible).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so you want to keep a "handle" on the test, to possibly swap in different tests, hence the Predicate is primary? That makes sense, cool.

}

/** Convert an prefix name (qname) to an IRI, for when the prerix is nor defined.
* @see ARQ#fixupUndefinedPrefixes
*/
public static String fixupPrefixIRI(String prefix, String localPart) {
return fixupPrefixIRI(prefix+":"+localPart) ;
}

/** Convert an prefix name (qname) to an IRI, for when the prerix is nor defined.
* @see ARQ#fixupUndefinedPrefixes
*/
public static String fixupPrefixIRI(String prefixedName) {
return fixupPrefixes.apply(prefixedName) ;
}

private static ParserProfile profile = profile(RDFLanguages.TURTLE, null, ErrorHandlerFactory.errorHandlerStd) ;
static {
PrefixMap pmap = profile.getPrologue().getPrefixMap() ;
Expand Down
Loading