use of org.exist.xquery.PathExpr in project exist by eXist-db.
the class Compile method eval.
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
// get the query expression
final String expr = args[0].getStringValue();
if (expr.trim().isEmpty()) {
return new EmptySequence();
}
context.pushNamespaceContext();
logger.debug("eval: {}", expr);
// TODO(pkaminsk2): why replicate XQuery.compile here?
String error = null;
ErrorCodes.ErrorCode code = null;
int line = -1;
int column = -1;
final XQueryContext pContext = new XQueryContext(context.getBroker().getBrokerPool());
if (getArgumentCount() == 2 && args[1].hasOne()) {
pContext.setModuleLoadPath(args[1].getStringValue());
}
final XQueryLexer lexer = new XQueryLexer(pContext, new StringReader(expr));
final XQueryParser parser = new XQueryParser(lexer);
// shares the context of the outer expression
final XQueryTreeParser astParser = new XQueryTreeParser(pContext);
try {
parser.xpath();
if (parser.foundErrors()) {
logger.debug(parser.getErrorMessage());
throw new XPathException(this, "error found while executing expression: " + parser.getErrorMessage());
}
final AST ast = parser.getAST();
final PathExpr path = new PathExpr(pContext);
astParser.xpath(ast, path);
if (astParser.foundErrors()) {
throw astParser.getLastException();
}
path.analyze(new AnalyzeContextInfo());
} catch (final RecognitionException | TokenStreamException e) {
error = e.toString();
} catch (final XPathException e) {
line = e.getLine();
column = e.getColumn();
code = e.getCode();
error = e.getDetailMessage();
} catch (final Exception e) {
error = e.getMessage();
} finally {
context.popNamespaceContext();
pContext.reset(false);
}
if (isCalledAs("compile")) {
return error == null ? Sequence.EMPTY_SEQUENCE : new StringValue(error);
} else {
return response(pContext, error, code, line, column);
}
}
use of org.exist.xquery.PathExpr in project exist by eXist-db.
the class XUpdateProcessor method processQuery.
private Sequence processQuery(String select) throws SAXException {
XQueryContext context = null;
try {
context = new XQueryContext(broker.getBrokerPool());
context.setStaticallyKnownDocuments(documentSet);
Map.Entry<String, String> namespaceEntry;
for (Map.Entry<String, String> stringStringEntry : namespaces.entrySet()) {
namespaceEntry = stringStringEntry;
context.declareNamespace(namespaceEntry.getKey(), namespaceEntry.getValue());
}
Map.Entry<String, Object> entry;
for (Map.Entry<String, Object> stringObjectEntry : variables.entrySet()) {
entry = stringObjectEntry;
context.declareVariable(entry.getKey(), entry.getValue());
}
// TODO(pkaminsk2): why replicate XQuery.compile here?
final XQueryLexer lexer = new XQueryLexer(context, new StringReader(select));
final XQueryParser parser = new XQueryParser(lexer);
final XQueryTreeParser treeParser = new XQueryTreeParser(context);
parser.xpath();
if (parser.foundErrors()) {
throw new SAXException(parser.getErrorMessage());
}
final AST ast = parser.getAST();
if (LOG.isDebugEnabled()) {
LOG.debug("generated AST: {}", ast.toStringTree());
}
final PathExpr expr = new PathExpr(context);
treeParser.xpath(ast, expr);
if (treeParser.foundErrors()) {
throw new SAXException(treeParser.getErrorMessage());
}
expr.analyze(new AnalyzeContextInfo());
final Sequence seq = expr.eval(null, null);
return seq;
} catch (final RecognitionException | TokenStreamException e) {
LOG.warn("error while creating variable", e);
throw new SAXException(e);
} catch (final XPathException e) {
throw new SAXException(e);
} finally {
if (context != null) {
context.reset(false);
}
}
}
Aggregations