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;
}
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);
}
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();
}
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);
}
}
}
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);
}
}
Aggregations