From 6b59e2b55ff0fd77a6c6329bece0423523cf9bc8 Mon Sep 17 00:00:00 2001 From: Andy Seaborne Date: Tue, 8 May 2018 12:04:09 +0100 Subject: [PATCH] JENA-1545: ParserProfileWrapper --- .../jena/riot/system/ParserProfile.java | 8 +- .../jena/riot/system/ParserProfileStd.java | 59 +++++---- .../riot/system/ParserProfileWrapper.java | 119 ++++++++++++++++++ 3 files changed, 157 insertions(+), 29 deletions(-) create mode 100644 jena-arq/src/main/java/org/apache/jena/riot/system/ParserProfileWrapper.java diff --git a/jena-arq/src/main/java/org/apache/jena/riot/system/ParserProfile.java b/jena-arq/src/main/java/org/apache/jena/riot/system/ParserProfile.java index 9fc2bcfb14c..412c0194ae8 100644 --- a/jena-arq/src/main/java/org/apache/jena/riot/system/ParserProfile.java +++ b/jena-arq/src/main/java/org/apache/jena/riot/system/ParserProfile.java @@ -49,9 +49,9 @@ public interface ParserProfile { /** Create a triple */ public Triple createTriple(Node subject, Node predicate, Node object, long line, long col); - /** Create a Quad */ + /** Create a quad */ public Quad createQuad(Node graph, Node subject, Node predicate, Node object, long line, long col); - + /** Create a URI Node */ public Node createURI(String uriStr, long line, long col); @@ -85,7 +85,9 @@ public interface ParserProfile { /* Return the prefix map, if any, used for mapping tokens into Nodes. */ public PrefixMap getPrefixMap(); - /** Get the error handler used by this {@code ParserProfile} */ + /** Get the {@link ErrorHandler error handler} used by this {@code ParserProfile} */ public ErrorHandler getErrorHandler(); + /** Get the {@link FactoryRDF factory for RDF terms, triples and quads} */ + public FactoryRDF getFactorRDF(); } diff --git a/jena-arq/src/main/java/org/apache/jena/riot/system/ParserProfileStd.java b/jena-arq/src/main/java/org/apache/jena/riot/system/ParserProfileStd.java index e893f15eb10..5267bac47cf 100644 --- a/jena-arq/src/main/java/org/apache/jena/riot/system/ParserProfileStd.java +++ b/jena-arq/src/main/java/org/apache/jena/riot/system/ParserProfileStd.java @@ -45,8 +45,8 @@ public class ParserProfileStd implements ParserProfile private final Context context; private IRIResolver resolver; private final PrefixMap prefixMap; - private final boolean strictMode; - private final boolean checking; + private final boolean strictMode; + private final boolean checking; public ParserProfileStd(FactoryRDF factory, ErrorHandler errorHandler, IRIResolver resolver, PrefixMap prefixMap, @@ -60,6 +60,11 @@ public ParserProfileStd(FactoryRDF factory, ErrorHandler errorHandler, this.strictMode = strictMode; } + @Override + public FactoryRDF getFactorRDF() { + return factory; + } + @Override public ErrorHandler getErrorHandler() { return errorHandler; @@ -99,6 +104,7 @@ public IRI makeIRI(String uriStr, long line, long col) { return iri; } + /** Create a triple - this operation call {@link #checkTriple} if checking is enabled. */ @Override public Triple createTriple(Node subject, Node predicate, Node object, long line, long col) { if ( checking ) @@ -106,6 +112,22 @@ public Triple createTriple(Node subject, Node predicate, Node object, long line, return factory.createTriple(subject, predicate, object); } + protected void checkTriple(Node subject, Node predicate, Node object, long line, long col) { + if ( subject == null || (!subject.isURI() && !subject.isBlank()) ) { + errorHandler.error("Subject is not a URI or blank node", line, col); + throw new RiotException("Bad subject: " + subject); + } + if ( predicate == null || (!predicate.isURI()) ) { + errorHandler.error("Predicate not a URI", line, col); + throw new RiotException("Bad predicate: " + predicate); + } + if ( object == null || (!object.isURI() && !object.isBlank() && !object.isLiteral()) ) { + errorHandler.error("Object is not a URI, blank node or literal", line, col); + throw new RiotException("Bad object: " + object); + } + } + + /** Create a quad - this operation call {@link #checkTriple} if checking is enabled. */ @Override public Quad createQuad(Node graph, Node subject, Node predicate, Node object, long line, long col) { if ( checking ) @@ -113,6 +135,15 @@ public Quad createQuad(Node graph, Node subject, Node predicate, Node object, lo return factory.createQuad(graph, subject, predicate, object); } + protected void checkQuad(Node graph, Node subject, Node predicate, Node object, long line, long col) { + // Allow blank nodes - syntax may restrict more. + if ( graph != null && !graph.isURI() && !graph.isBlank() ) { + errorHandler.error("Graph name is not a URI or blank node: " + FmtUtils.stringForNode(graph), line, col); + throw new RiotException("Bad graph name: " + graph); + } + checkTriple(subject, predicate, object, line, col); + } + @Override public Node createURI(String x, long line, long col) { // Special cases that don't resolve. @@ -246,28 +277,4 @@ private String expandPrefixedName(String prefix, String localPart, Token token) } return expansion; } - - private void checkTriple(Node subject, Node predicate, Node object, long line, long col) { - if ( subject == null || (!subject.isURI() && !subject.isBlank()) ) { - errorHandler.error("Subject is not a URI or blank node", line, col); - throw new RiotException("Bad subject: " + subject); - } - if ( predicate == null || (!predicate.isURI()) ) { - errorHandler.error("Predicate not a URI", line, col); - throw new RiotException("Bad predicate: " + predicate); - } - if ( object == null || (!object.isURI() && !object.isBlank() && !object.isLiteral()) ) { - errorHandler.error("Object is not a URI, blank node or literal", line, col); - throw new RiotException("Bad object: " + object); - } - } - - private void checkQuad(Node graph, Node subject, Node predicate, Node object, long line, long col) { - // Allow blank nodes - syntax may restrict more. - if ( graph != null && !graph.isURI() && !graph.isBlank() ) { - errorHandler.error("Graph name is not a URI or blank node: " + FmtUtils.stringForNode(graph), line, col); - throw new RiotException("Bad graph name: " + graph); - } - checkTriple(subject, predicate, object, line, col); - } } \ No newline at end of file diff --git a/jena-arq/src/main/java/org/apache/jena/riot/system/ParserProfileWrapper.java b/jena-arq/src/main/java/org/apache/jena/riot/system/ParserProfileWrapper.java new file mode 100644 index 00000000000..66dbd8c09f4 --- /dev/null +++ b/jena-arq/src/main/java/org/apache/jena/riot/system/ParserProfileWrapper.java @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jena.riot.system; + +import org.apache.jena.datatypes.RDFDatatype; +import org.apache.jena.graph.Node; +import org.apache.jena.graph.Triple; +import org.apache.jena.iri.IRI; +import org.apache.jena.riot.tokens.Token; +import org.apache.jena.sparql.core.Quad; + +public class ParserProfileWrapper implements ParserProfile +{ + private final ParserProfile other; + protected ParserProfile get() { return other; } + + public ParserProfileWrapper(ParserProfile other) { + this.other = other; + } + + @Override + public FactoryRDF getFactorRDF() { + return get().getFactorRDF(); + } + + @Override + public ErrorHandler getErrorHandler() { + return get().getErrorHandler(); + } + + @Override + public boolean isStrictMode() { + return get().isStrictMode(); + } + + @Override + public String resolveIRI(String uriStr, long line, long col) { + return get().resolveIRI(uriStr, line, col); + } + + @Override + public IRI makeIRI(String uriStr, long line, long col) { + return get().makeIRI(uriStr, line, col); + } + + @Override + public void setIRIResolver(IRIResolver resolver) { get().setIRIResolver(resolver); } + + @Override + public Triple createTriple(Node subject, Node predicate, Node object, long line, long col) { + return get().createTriple(subject, predicate, object, line, col); + } + + @Override + public Quad createQuad(Node graph, Node subject, Node predicate, Node object, long line, long col) { + return get().createQuad(graph, subject, predicate, object, line, col); + } + + @Override + public Node createURI(String uriStr, long line, long col) { + return get().createURI(uriStr, line, col); + } + + @Override + public Node createTypedLiteral(String lexical, RDFDatatype datatype, long line, long col) { + return get().createTypedLiteral(lexical, datatype, line, col); + } + + @Override + public Node createLangLiteral(String lexical, String langTag, long line, long col) { + return get().createLangLiteral(lexical, langTag, line, col); + } + + @Override + public Node createStringLiteral(String lexical, long line, long col) { + return get().createStringLiteral(lexical, line, col); + } + + @Override + public Node createBlankNode(Node scope, String label, long line, long col) { + return get().createBlankNode(scope, label, line, col); + } + + @Override + public Node createBlankNode(Node scope, long line, long col) { + return get().createBlankNode(scope, line, col); + } + + @Override + public Node createNodeFromToken(Node scope, Token token, long line, long col) { + return get().createNodeFromToken(scope, token, line, col); + } + + @Override + public Node create(Node currentGraph, Token token) { + return get().create(currentGraph, token); + } + + @Override + public PrefixMap getPrefixMap() { + return get().getPrefixMap(); + } +} \ No newline at end of file