use of org.apache.jena.shacl.parser.ShaclParseException in project jena by apache.
the class shacl_parse method exec.
private void exec(String fn, boolean multipleFiles) {
Shapes shapes;
PrintStream out = System.out;
PrintStream err = System.err;
try {
Graph g = parseFile(fn);
shapes = Shapes.parse(g);
} catch (RiotException ex) {
/*ErrorHandler logged this */
return;
} catch (ShaclParseException | ShaclcParseException ex) {
// Errors parsing SHACL Compact Syntax.
if (multipleFiles)
err.println(fn + " : ");
err.println(ex.getMessage());
return;
}
boolean outputByPrev = false;
if (printText) {
outputByPrev = printText(out, err, shapes);
}
if (printCompact) {
if (outputByPrev) {
out.println("- - - - - - - -");
outputByPrev = false;
}
outputByPrev = printCompact(out, err, shapes);
}
if (printRDF) {
if (outputByPrev) {
out.println("- - - - - - - -");
outputByPrev = false;
}
outputByPrev = printRDF(out, err, shapes);
}
}
use of org.apache.jena.shacl.parser.ShaclParseException in project jena by apache.
the class ShLib method parseQueryString.
/**
* Parse a string to produce a {@link Query}.
* All {@link Query} should go through this function to allow of inserting default prefixes.
*/
public static Query parseQueryString(String queryString) {
try {
Query query = new Query();
// The SHACL spec does not define any default prefixes.
// But for identified practical reasons some may be added such as:
// query.getPrefixMapping().setNsPrefix("owl", OWL.getURI());
// query.getPrefixMapping().setNsPrefix("rdf", RDF.getURI());
// query.getPrefixMapping().setNsPrefix("rdfs", RDFS.getURI());
// query.getPrefixMapping().setNsPrefix("sh", SHACL.getURI());
// query.getPrefixMapping().setNsPrefix("xsd", XSD.getURI());
QueryFactory.parse(query, queryString, null, Syntax.defaultQuerySyntax);
return query;
} catch (QueryParseException ex) {
throw new ShaclParseException("Bad query: " + ex.getMessage());
}
}
use of org.apache.jena.shacl.parser.ShaclParseException in project jena by apache.
the class ClosedConstraint method ignoredProperties.
private static List<Node> ignoredProperties(Graph shapesGraph, Node shNode) {
List<Node> ignoredProperties = null;
if (G.contains(shapesGraph, shNode, SHACL.ignoredProperties, null)) {
Node ignored = G.getOneSP(shapesGraph, shNode, SHACL.ignoredProperties);
if (ignored != null) {
ignoredProperties = GraphList.members(GNode.create(shapesGraph, ignored));
ignoredProperties.forEach(p -> {
if (!p.isURI())
throw new ShaclParseException("Only URIs allowed in sh:ignoredProperties at " + displayStr(shNode));
});
}
}
return ignoredProperties;
}
use of org.apache.jena.shacl.parser.ShaclParseException in project jena by apache.
the class ShLib method prefixes.
public static String prefixes(Graph shapesGraph, Node sparqlNode) {
StringJoiner prefixesSJ = new StringJoiner("\n");
QueryExecution qExec = QueryExecutionFactory.create(prefixesQuery, DatasetGraphFactory.wrap(shapesGraph));
ResultSet rs = qExec.execSelect();
Map<String, String> seen = new HashMap<>();
while (rs.hasNext()) {
Binding binding = rs.nextBinding();
Node nPrefix = binding.get(varPrefix);
// Strictly, namespace must be xsd:anyURI.
// We accept any literal, and also URIs
// which makes [ sh:prefix "ex" ; sh:namespace ex: ] work.
Node nNamespace = binding.get(varNamespace);
String prefix = nPrefix.getLiteralLexicalForm();
String ns;
if (nNamespace.isLiteral())
ns = nNamespace.getLiteralLexicalForm();
else if (nNamespace.isURI())
ns = nNamespace.getURI();
else
throw new ShaclParseException("sh:namespace is not a literal or URI");
if (seen.containsKey(prefix)) {
if (seen.get(prefix).equals(ns))
continue;
}
prefixesSJ.add("PREFIX " + prefix + ": <" + ns + ">");
seen.put(prefix, ns);
}
return prefixesSJ.toString();
}
use of org.apache.jena.shacl.parser.ShaclParseException in project jena by apache.
the class ShLib method extractSPARQLQueryString.
public static String extractSPARQLQueryString(Graph shapesGraph, Node sparqlNode) {
// XXX Optimize prefixes acquisition in case of use from more than one place.
String prefixes = prefixes(shapesGraph, sparqlNode);
Node selectNode = G.getOneSP(shapesGraph, sparqlNode, SHACL.select);
if (!Util.isSimpleString(selectNode))
throw new ShaclParseException("Not a string for sh:select: " + ShLib.displayStr(selectNode));
String selectQuery = selectNode.getLiteralLexicalForm();
String qs = prefixes + "\n" + selectQuery;
return qs;
}
Aggregations