List mapStack = null; // get the stack only when necessary - lazy inside the loop
int stackSize = 0;
if (needsMapStack) {
if (context == null) {
throw new PatternException("Need an invoke context to resolve " + this);
}
mapStack = context.getMapStack();
stackSize = mapStack.size();
}
Stack stack = new Stack();
for (Iterator i = tokens.iterator(); i.hasNext();) {
Token token = (Token) i.next();
Token last;
switch (token.getType()){
case TEXT:
if (stack.empty()) {
stack.push(new Token(EXPR, token.getStringValue()));
} else {
last = (Token)stack.peek();
if (last.hasType(EXPR)) {
last.merge(token);
} else {
stack.push(new Token(EXPR, token.getStringValue()));
}
}
break;
case CLOSE:
Token expr = (Token)stack.pop();
Token lastButOne = (Token)stack.pop();
Token result;
if (expr.hasType(COLON)) { // i.e. nothing was specified after the colon
stack.pop(); // Pop the OPEN
result = processModule(lastButOne, EMPTY_TOKEN, objectModel, context, mapStack, stackSize);
} else if (lastButOne.hasType(COLON)) {
Token module = (Token)stack.pop();
stack.pop(); // Pop the OPEN
result = processModule(module, expr, objectModel, context, mapStack, stackSize);
} else if (lastButOne.hasType(VariableExpressionTokenizer.TokenReciever.NEW_EXPRESSION)) {
stack.pop(); // Pop the OPEN
ExpressionFactory expressionFactory = null;
ObjectModel newObjectModel = null;
try {
expressionFactory = (ExpressionFactory)manager.lookup(ExpressionFactory.ROLE);
newObjectModel = (ObjectModel)manager.lookup(ObjectModel.ROLE);
result = processNewExpression(lastButOne, expressionFactory, newObjectModel);
} catch (ServiceException e) {
throw new PatternException("Cannot obtain necessary components to evaluate new expression '"
+ lastButOne.getStringValue() + "' in expression '" + this.originalExpr + "'", e);
} finally {
if (expressionFactory != null)
manager.release(expressionFactory);
if (newObjectModel != null)
manager.release(newObjectModel);
}
} else {
result = processVariable(expr, mapStack, stackSize);
}
if (stack.empty()) {
stack.push(result);
} else {
last = (Token)stack.peek();
if (last.hasType(EXPR)) {
last.merge(result);
} else {
stack.push(result);
}
}
break;
case OPEN:
case COLON:
case ANCHOR_VAR:
case THREADSAFE_MODULE:
case STATEFUL_MODULE:
case ROOT_SITEMAP_VARIABLE:
default: {
stack.push(token);
break;
}
}
}
if (stack.size() !=1) {
throw new PatternException("Evaluation error in expression: " + originalExpr);
}
return ((Token)stack.pop()).getStringValue();
}