use of org.exist.xquery.AnalyzeContextInfo 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.AnalyzeContextInfo 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);
}
}
}
use of org.exist.xquery.AnalyzeContextInfo in project exist by eXist-db.
the class CallFunction method eval.
/* (non-Javadoc)
* @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
*/
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
final Sequence arg0 = getArgument(0).eval(contextSequence, contextItem);
if (arg0.getCardinality() != Cardinality.EXACTLY_ONE) {
throw new XPathException(this, "Expected exactly one item for first argument");
}
final Item item0 = arg0.itemAt(0);
if (item0.getType() != Type.FUNCTION_REFERENCE) {
throw new XPathException(this, "Type error: expected function, got " + Type.getTypeName(item0.getType()));
}
try (final FunctionReference ref = (FunctionReference) item0) {
// pass the remaining parameters to the function call
final List<Expression> params = new ArrayList<>(getArgumentCount() - 1);
for (int i = 1; i < getArgumentCount(); i++) {
params.add(getArgument(i));
}
ref.setArguments(params);
ref.analyze(new AnalyzeContextInfo(this, 0));
// Evaluate the function
return ref.eval(contextSequence);
}
}
use of org.exist.xquery.AnalyzeContextInfo in project exist by eXist-db.
the class FunUnordered method analyze.
public void analyze(AnalyzeContextInfo contextInfo) throws XPathException {
final AnalyzeContextInfo newContextInfo = new AnalyzeContextInfo(contextInfo);
newContextInfo.setParent(this);
newContextInfo.addFlag(UNORDERED);
super.analyze(newContextInfo);
}
Aggregations