Search in sources :

Example 16 with CmdException

use of org.apache.jena.cmd.CmdException in project jena by apache.

the class FusekiWebapp method configFromTemplate.

private static DataAccessPoint configFromTemplate(String templateFile, String datasetPath, boolean allowUpdate, Map<String, String> params) {
    DatasetDescriptionMap registry = new DatasetDescriptionMap();
    // ---- Setup
    if (params == null) {
        params = new HashMap<>();
        params.put(Template.NAME, datasetPath);
    } else {
        if (!params.containsKey(Template.NAME)) {
            Fuseki.configLog.warn("No NAME found in template parameters (added)");
            params.put(Template.NAME, datasetPath);
        }
    }
    // -- Logging
    Fuseki.configLog.info("Template file: " + templateFile);
    String dir = params.get(Template.DIR);
    if (dir != null) {
        if (!Objects.equals(dir, Names.memName) && !FileOps.exists(dir))
            throw new CmdException("Directory not found: " + dir);
    }
    // -- Logging
    datasetPath = DataAccessPoint.canonical(datasetPath);
    // DRY -- ActionDatasets (and others?)
    addGlobals(params);
    String str = TemplateFunctions.templateFile(templateFile, params, Lang.TTL);
    Lang lang = RDFLanguages.filenameToLang(str, Lang.TTL);
    StringReader sr = new StringReader(str);
    Model model = ModelFactory.createDefaultModel();
    RDFDataMgr.read(model, sr, datasetPath, lang);
    // ---- DataAccessPoint
    Statement stmt = getOne(model, null, FusekiVocab.pServiceName, null);
    if (stmt == null) {
        StmtIterator sIter = model.listStatements(null, FusekiVocab.pServiceName, (RDFNode) null);
        if (!sIter.hasNext())
            ServletOps.errorBadRequest("No name given in description of Fuseki service");
        sIter.next();
        if (sIter.hasNext())
            ServletOps.errorBadRequest("Multiple names given in description of Fuseki service");
        throw new InternalErrorException("Inconsistent: getOne didn't fail the second time");
    }
    Resource subject = stmt.getSubject();
    if (!allowUpdate) {
    // Opportunity for more sophisticated "read-only" mode.
    // 1 - clean model, remove "fu:serviceUpdate", "fu:serviceUpload", "fu:serviceReadGraphStore", "fu:serviceReadWriteGraphStore"
    // 2 - set a flag on DataAccessPoint
    }
    DataAccessPoint dap = FusekiConfig.buildDataAccessPoint(subject, registry);
    return dap;
}
Also used : CmdException(org.apache.jena.cmd.CmdException) StringReader(java.io.StringReader) DataAccessPoint(org.apache.jena.fuseki.server.DataAccessPoint) Lang(org.apache.jena.riot.Lang) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException) DatasetDescriptionMap(org.apache.jena.fuseki.build.DatasetDescriptionMap)

Example 17 with CmdException

use of org.apache.jena.cmd.CmdException in project jena by apache.

the class textindexdump method processModulesAndArgs.

@Override
protected void processModulesAndArgs() {
    super.processModulesAndArgs();
    // Two forms : with and without arg.
    // Maximises similarity with other tools.
    String file;
    if (super.contains(assemblerDescDecl)) {
        if (getValues(assemblerDescDecl).size() != 1)
            throw new CmdException("Multiple assembler descriptions given");
        if (getPositional().size() != 0)
            throw new CmdException("Additional assembler descriptions given");
        file = getValue(assemblerDescDecl);
    } else {
        if (getNumPositional() != 1)
            throw new CmdException("Multiple assembler descriptions given");
        file = getPositionalArg(0);
    }
    textIndex = (TextIndex) AssemblerUtils.build(file, TextVocab.textIndex);
}
Also used : CmdException(org.apache.jena.cmd.CmdException)

Example 18 with CmdException

use of org.apache.jena.cmd.CmdException in project jena by apache.

the class textindexer method processModulesAndArgs.

@Override
protected void processModulesAndArgs() {
    super.processModulesAndArgs();
    // Two forms : with and without arg.
    // Maximises similarity with other tools.
    String file;
    if (!super.contains(assemblerDescDecl) && getNumPositional() == 0)
        throw new CmdException("No assembler description given");
    if (super.contains(assemblerDescDecl)) {
        if (getValues(assemblerDescDecl).size() != 1)
            throw new CmdException("Multiple assembler descriptions given via --desc");
        if (getPositional().size() != 0)
            throw new CmdException("Additional assembler descriptions given");
        file = getValue(assemblerDescDecl);
    } else {
        if (getNumPositional() != 1)
            throw new CmdException("Multiple assembler descriptions given as positional arguments");
        file = getPositionalArg(0);
    }
    if (file == null)
        throw new CmdException("No dataset specified");
    // Assumes a single text dataset description in the assembler file.
    Dataset ds = TextDatasetFactory.create(file);
    if (ds == null)
        throw new CmdException("No dataset description found");
    // get index.
    dataset = (DatasetGraphText) (ds.asDatasetGraph());
    textIndex = dataset.getTextIndex();
    if (textIndex == null)
        throw new CmdException("Dataset has no text index");
    entityDefinition = textIndex.getDocDef();
}
Also used : CmdException(org.apache.jena.cmd.CmdException) Dataset(org.apache.jena.query.Dataset)

Example 19 with CmdException

use of org.apache.jena.cmd.CmdException in project jena by apache.

the class qexpr method main2.

public static void main2(String... argv) {
    CmdLineArgs cl = new CmdLineArgs(argv);
    ArgDecl helpDecl = new ArgDecl(ArgDecl.NoValue, "h", "help");
    cl.add(helpDecl);
    ArgDecl verboseDecl = new ArgDecl(ArgDecl.NoValue, "v", "verbose");
    cl.add(verboseDecl);
    ArgDecl versionDecl = new ArgDecl(ArgDecl.NoValue, "ver", "version", "V");
    cl.add(versionDecl);
    ArgDecl quietDecl = new ArgDecl(ArgDecl.NoValue, "q", "quiet");
    cl.add(quietDecl);
    ArgDecl reduceDecl = new ArgDecl(ArgDecl.NoValue, "reduce", "fold", "simplify");
    cl.add(reduceDecl);
    ArgDecl strictDecl = new ArgDecl(ArgDecl.NoValue, "strict");
    cl.add(strictDecl);
    ArgDecl printDecl = new ArgDecl(ArgDecl.HasValue, "print");
    cl.add(printDecl);
    try {
        cl.process();
    } catch (IllegalArgumentException ex) {
        System.err.println(ex.getMessage());
        usage(System.err);
        throw new CmdException();
    }
    if (cl.contains(helpDecl)) {
        usage();
        throw new TerminationException(0);
    }
    if (cl.contains(versionDecl)) {
        System.out.println("ARQ Version: " + ARQ.VERSION + " (Jena: " + Jena.VERSION + ")");
        throw new TerminationException(0);
    }
    // ==== General things
    boolean verbose = cl.contains(verboseDecl);
    boolean quiet = cl.contains(quietDecl);
    if (cl.contains(strictDecl))
        ARQ.setStrictMode();
    boolean actionCopySubstitute = cl.contains(reduceDecl);
    boolean actionPrintPrefix = false;
    boolean actionPrintSPARQL = false;
    boolean actionPrint = cl.contains(printDecl);
    for (String v : cl.getValues(printDecl)) {
        if (v.equalsIgnoreCase("prefix") || v.equalsIgnoreCase("op")) {
            actionPrintPrefix = true;
        } else if (v.equalsIgnoreCase("expr")) {
            actionPrintSPARQL = true;
        } else {
            System.err.println("Unknown print form: " + v);
            throw new TerminationException(0);
        }
    }
    for (int i = 0; i < cl.getNumPositional(); i++) {
        String exprStr = cl.getPositionalArg(i);
        exprStr = cl.indirect(exprStr);
        try {
            PrefixMapping pmap = PrefixMapping.Factory.create();
            pmap.setNsPrefixes(ARQConstants.getGlobalPrefixMap());
            pmap.setNsPrefix("", "http://example/");
            pmap.setNsPrefix("ex", "http://example/ns#");
            Expr expr = ExprUtils.parse(exprStr, pmap);
            if (actionPrint) {
                IndentedWriter iOut = IndentedWriter.stdout;
                if (actionPrintSPARQL) {
                    ExprUtils.fmtSPARQL(iOut, expr);
                    iOut.ensureStartOfLine();
                }
                if (actionPrintPrefix) {
                    WriterSSE.out(iOut, expr, new Prologue(pmap));
                    iOut.ensureStartOfLine();
                }
                iOut.flush();
                continue;
            }
            if (verbose)
                System.out.print(expr.toString() + " => ");
            try {
                if (actionCopySubstitute) {
                    Expr e = ExprLib.foldConstants(expr);
                    System.out.println(e);
                } else {
                    // Default action
                    ARQ.getContext().set(ARQConstants.sysCurrentTime, NodeFactoryExtra.nowAsDateTime());
                    FunctionEnv env = new ExecutionContext(ARQ.getContext(), null, null, null);
                    NodeValue r = expr.eval(null, env);
                    // System.out.println(r.asQuotedString()) ;
                    Node n = r.asNode();
                    String s = NodeFmtLib.displayStr(n);
                    System.out.println(s);
                }
            } catch (ExprEvalException ex) {
                System.out.println("Exception: " + ex.getMessage());
                throw new TerminationException(2);
            }
        } catch (QueryParseException ex) {
            System.err.println("Parse error: " + ex.getMessage());
            throw new TerminationException(2);
        }
    }
}
Also used : NodeValue(org.apache.jena.sparql.expr.NodeValue) CmdException(org.apache.jena.cmd.CmdException) Node(org.apache.jena.graph.Node) ArgDecl(org.apache.jena.cmd.ArgDecl) CmdLineArgs(org.apache.jena.cmd.CmdLineArgs) QueryParseException(org.apache.jena.query.QueryParseException) IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) FunctionEnv(org.apache.jena.sparql.function.FunctionEnv) PrefixMapping(org.apache.jena.shared.PrefixMapping) TerminationException(org.apache.jena.cmd.TerminationException) Prologue(org.apache.jena.sparql.core.Prologue) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) Expr(org.apache.jena.sparql.expr.Expr) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Example 20 with CmdException

use of org.apache.jena.cmd.CmdException in project jena by apache.

the class query method queryExec.

protected void queryExec(boolean timed, ResultsFormat fmt, PrintStream resultsDest) {
    try {
        Query query = getQuery();
        if (isVerbose()) {
            IndentedWriter out = new IndentedWriter(resultsDest, true);
            query.serialize(out);
            out.setLineNumbers(false);
            out.println();
            out.flush();
        }
        if (isQuiet())
            LogCtl.setError(SysRIOT.riotLoggerName);
        Dataset dataset = getDataset(query);
        // The default policy is to create an empty one - convenience for VALUES and BIND providing the data.
        if (dataset == null && !query.hasDatasetDescription()) {
            System.err.println("Dataset not specified in query nor provided on command line.");
            throw new TerminationException(1);
        }
        Transactional transactional = (dataset != null && dataset.supportsTransactions()) ? dataset : new TransactionalNull();
        Txn.executeRead(transactional, () -> {
            modTime.startTimer();
            try (QueryExecution qe = QueryExecutionFactory.create(query, dataset)) {
                try {
                    QueryExecUtils.executeQuery(query, qe, fmt, resultsDest);
                } catch (QueryCancelledException ex) {
                    IO.flush(resultsDest);
                    System.err.println("Query timed out");
                }
                long time = modTime.endTimer();
                if (timed) {
                    totalTime += time;
                    System.err.println("Time: " + modTime.timeStr(time) + " sec");
                }
            } catch (ResultSetException ex) {
                System.err.println(ex.getMessage());
                ex.printStackTrace(System.err);
            } catch (QueryException qEx) {
                // System.err.println(qEx.getMessage()) ;
                throw new CmdException("Query Exeception", qEx);
            }
        });
    } catch (ARQInternalErrorException intEx) {
        System.err.println(intEx.getMessage());
        if (intEx.getCause() != null) {
            System.err.println("Cause:");
            intEx.getCause().printStackTrace(System.err);
            System.err.println();
        }
        intEx.printStackTrace(System.err);
    } catch (JenaException | CmdException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new CmdException("Exception", ex);
    }
}
Also used : CmdException(org.apache.jena.cmd.CmdException) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) RiotException(org.apache.jena.riot.RiotException) CmdException(org.apache.jena.cmd.CmdException) ResultSetException(org.apache.jena.sparql.resultset.ResultSetException) NotFoundException(org.apache.jena.shared.NotFoundException) JenaException(org.apache.jena.shared.JenaException) TerminationException(org.apache.jena.cmd.TerminationException) RiotNotFoundException(org.apache.jena.riot.RiotNotFoundException) IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) JenaException(org.apache.jena.shared.JenaException) TerminationException(org.apache.jena.cmd.TerminationException) ResultSetException(org.apache.jena.sparql.resultset.ResultSetException) TransactionalNull(org.apache.jena.sparql.core.TransactionalNull) Transactional(org.apache.jena.sparql.core.Transactional)

Aggregations

CmdException (org.apache.jena.cmd.CmdException)35 Lang (org.apache.jena.riot.Lang)8 JenaException (org.apache.jena.shared.JenaException)5 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)5 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)4 RiotException (org.apache.jena.riot.RiotException)4 ArgDecl (org.apache.jena.cmd.ArgDecl)3 Graph (org.apache.jena.graph.Graph)3 Node (org.apache.jena.graph.Node)3 ModDataset (arq.cmdline.ModDataset)2 PrintStream (java.io.PrintStream)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Function (java.util.function.Function)2 Stream (java.util.stream.Stream)2 StreamOps (org.apache.jena.atlas.lib.StreamOps)2 LogCtl (org.apache.jena.atlas.logging.LogCtl)2 CmdGeneral (org.apache.jena.cmd.CmdGeneral)2 TerminationException (org.apache.jena.cmd.TerminationException)2 DataAccessPoint (org.apache.jena.fuseki.server.DataAccessPoint)2