// the current expression context
exprContext = args[argCount++];
}
// get the query expression
final Item expr = args[argCount++].itemAt(0);
Source querySource;
if (Type.subTypeOf(expr.getType(), Type.ANY_URI)) {
querySource = loadQueryFromURI(expr);
} else {
final String queryStr = expr.getStringValue();
if("".equals(queryStr.trim())) {
return new EmptySequence();
}
querySource = new StringSource(queryStr);
}
NodeValue contextInit = null;
if(isCalledAs("eval-with-context")) {
// set the context initialization param for later use
contextInit = (NodeValue) args[argCount++].itemAt(0);
}
// should the compiled query be cached?
boolean cache = false;
if(argCount < getArgumentCount()) {
cache = ((BooleanValue)args[argCount].itemAt(0)).effectiveBooleanValue();
}
// save some context properties
evalContext.pushNamespaceContext();
final LocalVariable mark = evalContext.markLocalVariables(false);
final DocumentSet oldDocs = evalContext.getStaticallyKnownDocuments();
if(exprContext != null) {
evalContext.setStaticallyKnownDocuments(exprContext.getDocumentSet());
}
if(evalContext.isProfilingEnabled(2)) {
evalContext.getProfiler().start(this, "eval: " + expr);
}
// fixme! - hook for debugger here /ljo
final Sequence sequence = null;
final XQuery xqueryService = evalContext.getBroker().getXQueryService();
final XQueryContext innerContext;
if (contextInit != null) {
// eval-with-context: initialize a new context
innerContext = xqueryService.newContext(evalContext.getAccessContext());
initContextSequence = initContext(contextInit.getNode(), innerContext);
} else {
// use the existing outer context
// TODO: check if copying the static context would be sufficient???
innerContext = evalContext.copyContext();
innerContext.setShared(true);
//innerContext = context;
}
//set module load path
if(Type.subTypeOf(expr.getType(), Type.ANY_URI)) {
String uri = null;
final Object key = querySource.getKey();
if(key instanceof XmldbURI) {
uri = XmldbURI.EMBEDDED_SERVER_URI.append(((XmldbURI)key).removeLastSegment()).toString();
// } else if (key instanceof URL) {
// TODO: uri = ((URL) key).getParent()
} else if (key instanceof String && querySource instanceof FileSource) {
uri = ((FileSource) querySource).getFile().getParent();
}
if (uri != null) {
innerContext.setModuleLoadPath(uri);
}
}
//bind external vars?
if(isCalledAs("eval") && getArgumentCount() == 3) {
if(!args[2].isEmpty()) {
final Sequence externalVars = args[2];
for(int i = 0; i < externalVars.getItemCount(); i++) {
final Item varName = externalVars.itemAt(i);
if(varName.getType() == Type.QNAME) {
final Item varValue = externalVars.itemAt(++i);
innerContext.declareVariable(((QNameValue)varName).getQName(), varValue);
}
}
}
}