use of org.exist.xquery.value.Sequence in project exist by eXist-db.
the class UserDefinedFunction method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
// context.expressionStart(this);
context.stackEnter(this);
// make sure reset state is called after query has finished
hasBeenReset = false;
// Save the local variable stack
final LocalVariable mark = context.markLocalVariables(true);
if (closureVariables != null) {
context.restoreStack(closureVariables);
}
Sequence result = null;
try {
QName varName;
LocalVariable var;
int j = 0;
for (int i = 0; i < parameters.size(); i++, j++) {
varName = parameters.get(i);
var = new LocalVariable(varName);
var.setValue(currentArguments[j]);
if (contextDocs != null) {
var.setContextDocs(contextDocs[i]);
}
context.declareVariableBinding(var);
Cardinality actualCardinality;
if (currentArguments[j].isEmpty()) {
actualCardinality = Cardinality.EMPTY_SEQUENCE;
} else if (currentArguments[j].hasMany()) {
actualCardinality = Cardinality._MANY;
} else {
actualCardinality = Cardinality.EXACTLY_ONE;
}
if (!getSignature().getArgumentTypes()[j].getCardinality().isSuperCardinalityOrEqualOf(actualCardinality)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "Invalid cardinality for parameter $" + varName + ". Expected " + getSignature().getArgumentTypes()[j].getCardinality().getHumanDescription() + ", got " + currentArguments[j].getItemCount());
}
}
result = body.eval(null, null);
return result;
} finally {
// restore the local variable stack
context.popLocalVariables(mark, result);
context.stackLeave(this);
// context.expressionEnd(this);
}
}
use of org.exist.xquery.value.Sequence in project exist by eXist-db.
the class ValueComparison method nodeSetCompare.
protected Sequence nodeSetCompare(NodeSet nodes, Sequence contextSequence) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().message(this, Profiler.OPTIMIZATION_FLAGS, "OPTIMIZATION CHOICE", "nodeSetCompare");
}
final NodeSet result = new ExtArrayNodeSet();
final Collator collator = getCollator(contextSequence);
if (contextSequence != null && !contextSequence.isEmpty()) {
for (final NodeProxy current : nodes) {
ContextItem context = current.getContext();
if (context == null) {
throw new XPathException(this, ErrorCodes.XPDY0002, "Context is missing for node set comparison");
}
do {
final AtomicValue lv = current.atomize();
final Sequence rs = getRight().eval(context.getNode().toSequence());
if (!rs.hasOne()) {
throw new XPathException(this, ErrorCodes.XPTY0004, "Type error: sequence with less or more than one item is not allowed here");
}
if (compareAtomic(collator, lv, rs.itemAt(0).atomize(), StringTruncationOperator.NONE, relation)) {
result.add(current);
}
} while ((context = context.getNextDirect()) != null);
}
} else {
final Sequence rs = getRight().eval(null);
if (!rs.hasOne()) {
throw new XPathException(this, ErrorCodes.XPTY0004, "Type error: sequence with less or more than one item is not allowed here");
}
final AtomicValue rv = rs.itemAt(0).atomize();
for (final NodeProxy current : nodes) {
final AtomicValue lv = current.atomize();
if (compareAtomic(collator, lv, rv, StringTruncationOperator.NONE, Comparison.EQ)) {
result.add(current);
}
}
}
return result;
}
use of org.exist.xquery.value.Sequence in project exist by eXist-db.
the class VariableDeclaration method eval.
@Override
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
}
context.pushInScopeNamespaces(false);
try {
final Module myModule = findDeclaringModule(context.getRootModules(qname.getNamespaceURI()));
context.pushDocumentContext();
try {
context.prologEnter(this);
if (expression.isPresent()) {
// normal variable declaration or external var with default value
final Sequence seq = expression.get().eval(contextSequence, null);
final Variable var;
if (myModule != null) {
var = myModule.declareVariable(qname, seq);
var.setSequenceType(sequenceType);
var.checkType();
} else {
var = new VariableImpl(qname);
var.setValue(seq);
var.setSequenceType(sequenceType);
var.checkType();
context.declareGlobalVariable(var);
}
if (context.getProfiler().isEnabled()) {
// Note : that we use seq but we return Sequence.EMPTY_SEQUENCE
context.getProfiler().end(this, "", seq);
}
} else {
// external variable without default
final Variable external = context.resolveGlobalVariable(qname);
if (external == null) {
// is not specified, then a dynamic error is raised [err:XPDY0002]
throw new XPathException(ErrorCodes.XPDY0002, "no value specified for external variable " + qname);
}
external.setSequenceType(sequenceType);
if (myModule != null) {
// declare on module
myModule.declareVariable(external);
}
}
} finally {
context.popDocumentContext();
}
} finally {
context.popInScopeNamespaces();
}
return null;
}
use of org.exist.xquery.value.Sequence in project exist by eXist-db.
the class OpAnd method eval.
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
Sequence result;
if (getLength() == 0) {
result = Sequence.EMPTY_SEQUENCE;
} else {
if (contextItem != null) {
contextSequence = contextItem.toSequence();
}
boolean doOptimize = optimize;
if (contextSequence != null && !contextSequence.isPersistentSet()) {
doOptimize = false;
}
final Expression left = getLeft();
final Expression right = getRight();
// setContextId(getExpressionId());
if (doOptimize && contextSequence != null) {
contextSequence.setSelfAsContext(getContextId());
}
final Sequence ls = left.eval(contextSequence, null);
doOptimize = doOptimize && (ls.isPersistentSet() || ls.isEmpty());
if (doOptimize) {
if (inPredicate) {
NodeSet lr = ls.toNodeSet();
lr = lr.getContextNodes(getContextId());
if (lr.isEmpty()) {
return NodeSet.EMPTY_SET;
}
final Sequence rs = right.eval(lr, null);
final NodeSet rr = rs.toNodeSet();
result = rr.getContextNodes(getContextId());
} else {
final Sequence rs = right.eval(contextSequence, null);
final boolean rl = ls.effectiveBooleanValue();
if (!rl) {
result = BooleanValue.FALSE;
} else {
final boolean rr = rs.effectiveBooleanValue();
result = (rl && rr) ? BooleanValue.TRUE : BooleanValue.FALSE;
}
}
} else {
boolean rl = ls.effectiveBooleanValue();
// Immediately return false if the left operand is false
if (!rl) {
result = BooleanValue.FALSE;
} else {
final Sequence rs = right.eval(contextSequence, null);
final boolean rr = rs.effectiveBooleanValue();
result = (rl && rr) ? BooleanValue.TRUE : BooleanValue.FALSE;
}
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.Sequence in project exist by eXist-db.
the class GrammarTooling method eval.
/**
* @see org.exist.xquery.BasicFunction#eval(Sequence[], Sequence)
*/
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
final GrammarPool grammarpool = (GrammarPool) config.getProperty(XMLReaderObjectFactory.GRAMMAR_POOL);
if (isCalledAs("clear-grammar-cache")) {
final Sequence result = new ValueSequence();
final int before = countTotalNumberOfGrammar(grammarpool);
LOG.debug("Clearing {} grammars", before);
clearGrammarPool(grammarpool);
final int after = countTotalNumberOfGrammar(grammarpool);
LOG.debug("Remained {} grammars", after);
final int delta = before - after;
result.add(new IntegerValue(delta));
return result;
} else if (isCalledAs("show-grammar-cache")) {
context.pushDocumentContext();
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
final NodeImpl result = writeReport(grammarpool, builder);
return result;
} finally {
context.popDocumentContext();
}
} else if (isCalledAs("pre-parse-grammar")) {
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
// Setup for XML schema support only
final XMLGrammarPreparser parser = new XMLGrammarPreparser();
parser.registerPreparser(TYPE_XSD, null);
final List<Grammar> allGrammars = new ArrayList<>();
// iterate through the argument sequence and parse url
for (final SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
String url = i.nextItem().getStringValue();
// Fix database urls
if (url.startsWith("/")) {
url = "xmldb:exist://" + url;
}
LOG.debug("Parsing {}", url);
// parse XSD grammar
try {
if (url.endsWith(".xsd")) {
final InputStream is = new URL(url).openStream();
final XMLInputSource xis = new XMLInputSource(null, url, url, is, null);
final Grammar schema = parser.preparseGrammar(TYPE_XSD, xis);
is.close();
allGrammars.add(schema);
} else {
throw new XPathException(this, "Only XMLSchemas can be preparsed.");
}
} catch (final Exception ex) {
LOG.debug(ex);
throw new XPathException(this, ex);
}
}
LOG.debug("Successfully parsed {} grammars.", allGrammars.size());
// Send all XSD grammars to grammarpool
Grammar[] grammars = new Grammar[allGrammars.size()];
grammars = allGrammars.toArray(grammars);
grammarpool.cacheGrammars(TYPE_XSD, grammars);
// Construct result to end user
final ValueSequence result = new ValueSequence();
for (final Grammar one : grammars) {
result.add(new StringValue(one.getGrammarDescription().getNamespace()));
}
return result;
} else {
// oh oh
LOG.error("function not found error");
throw new XPathException(this, "function not found");
}
}
Aggregations