Search in sources :

Example 1 with ShaclParseException

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);
    }
}
Also used : PrintStream(java.io.PrintStream) ShaclParseException(org.apache.jena.shacl.parser.ShaclParseException) Graph(org.apache.jena.graph.Graph) ShaclcParseException(org.apache.jena.shacl.compact.reader.ShaclcParseException) Shapes(org.apache.jena.shacl.Shapes)

Example 2 with ShaclParseException

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());
    }
}
Also used : ShaclParseException(org.apache.jena.shacl.parser.ShaclParseException)

Example 3 with ShaclParseException

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;
}
Also used : ShaclParseException(org.apache.jena.shacl.parser.ShaclParseException) Node(org.apache.jena.graph.Node) GNode(org.apache.jena.sparql.util.graph.GNode)

Example 4 with ShaclParseException

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();
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) ShaclParseException(org.apache.jena.shacl.parser.ShaclParseException) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node)

Example 5 with ShaclParseException

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;
}
Also used : ShaclParseException(org.apache.jena.shacl.parser.ShaclParseException) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node)

Aggregations

ShaclParseException (org.apache.jena.shacl.parser.ShaclParseException)6 Node (org.apache.jena.graph.Node)4 RDFNode (org.apache.jena.rdf.model.RDFNode)2 PrintStream (java.io.PrintStream)1 Graph (org.apache.jena.graph.Graph)1 Shapes (org.apache.jena.shacl.Shapes)1 ShaclcParseException (org.apache.jena.shacl.compact.reader.ShaclcParseException)1 Binding (org.apache.jena.sparql.engine.binding.Binding)1 GNode (org.apache.jena.sparql.util.graph.GNode)1