List parameterExprs = getExpressionsForPropertiesOfNode(childNode);
currentExpr = new InvokeExpression(symtbl, currentExpr, methodName, parameterExprs);
}
else
{
throw new QueryCompilerSyntaxException("Dont support compilation of " + node);
}
childNode = childNode.getFirstChild();
}
return currentExpr;
}
else if (node.getNodeType() == Node.INVOKE)
{
Node currentNode = node;
List tupple = new ArrayList();
Expression currentExpr = null;
while (currentNode != null)
{
tupple.add(currentNode.getNodeValue());
if (currentNode.getNodeType() == Node.INVOKE)
{
String methodName = (String)tupple.get(tupple.size()-1);
List parameterExprs = getExpressionsForPropertiesOfNode(currentNode);
currentExpr = new InvokeExpression(symtbl, currentExpr, methodName, parameterExprs);
currentNode = currentNode.getFirstChild();
if (currentNode != null)
{
// Continue on along the chain
tupple = new ArrayList();
tupple.add(currentExpr); // Start from this expression
}
}
else
{
// TODO What node type is this that comes after an INVOKE?
currentNode = currentNode.getFirstChild();
}
}
return currentExpr;
}
else if (node.getNodeType() == Node.CREATOR)
{
Node currentNode = node.getFirstChild();
List tupple = new ArrayList();
boolean method = false;
while (currentNode != null)
{
tupple.add(currentNode.getNodeValue());
if (currentNode.getNodeType() == Node.INVOKE)
{
method = true;
break;
}
currentNode = currentNode.getFirstChild();
}
List parameterExprs = null;
if (method)
{
parameterExprs = getExpressionsForPropertiesOfNode(currentNode);
}
else
{
parameterExprs = new ArrayList();
}
return new CreatorExpression(symtbl, tupple, parameterExprs);
}
else if (node.getNodeType() == Node.LITERAL)
{
Node currentNode = node;
List tupple = new ArrayList();
Expression currentExpr = null;
while (currentNode != null)
{
tupple.add(currentNode.getNodeValue());
if (currentNode.getNodeType() == Node.INVOKE)
{
if (currentExpr == null && tupple.size() > 1)
{
// Start from Literal and invoke on that
currentExpr = new Literal(node.getNodeValue());
}
String methodName = (String)tupple.get(tupple.size()-1);
List parameterExprs = getExpressionsForPropertiesOfNode(currentNode);
currentExpr = new InvokeExpression(symtbl, currentExpr, methodName, parameterExprs);
currentNode = currentNode.getFirstChild();
tupple = new ArrayList();
}
else
{
currentNode = currentNode.getFirstChild();
}
}
if (currentExpr == null)
{
currentExpr = new Literal(node.getNodeValue());
}
return currentExpr;
}
else if (node.getNodeType() == Node.ARRAY)
{
Node currentNode = node;
List<Node> arrayElements = (List<Node>)node.getNodeValue();
// Check if this is an array literal
boolean literal = true;
Class type = null;
Iterator<Node> iter = arrayElements.iterator();
while (iter.hasNext())
{
Node element = iter.next();
if (type == null)
{
type = element.getNodeValue().getClass();
}
else if (type != element.getNodeValue().getClass())
{
// TODO Allow for mixed-mode arrays e.g "(1,3,4,this.longField)"
throw new QueryCompilerSyntaxException("Array elements are of inconsistent types in " + node);
}
if (element.getNodeType() == Node.IDENTIFIER)
{
literal = false;
break;
}
}
Expression currentExpr = null;
if (literal)
{
Object array = Array.newInstance(type, arrayElements.size());
iter = arrayElements.iterator();
int index = 0;
while (iter.hasNext())
{
Node element = iter.next();
Array.set(array, index++, element.getNodeValue());
}
currentExpr = new Literal(array);
}
else
{
// TODO Handle ArrayExpression
throw new NucleusUserException("Compilation of an array of expressions is not yet supported");
}
currentNode = currentNode.getFirstChild();
List tupple = new ArrayList();
while (currentNode != null)
{
tupple.add(currentNode.getNodeValue());
if (currentNode.getNodeType() == Node.INVOKE)
{
if (currentExpr == null && tupple.size() > 1)
{
// Start from Literal and invoke on that
currentExpr = new Literal(node.getNodeValue());
}
String methodName = (String)tupple.get(tupple.size()-1);
List parameterExprs = getExpressionsForPropertiesOfNode(currentNode);
currentExpr = new InvokeExpression(symtbl, currentExpr, methodName, parameterExprs);
currentNode = currentNode.getFirstChild();
tupple = new ArrayList();
}
else
{
currentNode = currentNode.getFirstChild();
}
}
return currentExpr;
}
else if (node.getNodeType() == Node.SUBQUERY)
{
List children = node.getChildNodes();
if (children.size() != 1)
{
throw new QueryCompilerSyntaxException("Invalid number of children for SUBQUERY node : " + node);
}
Node varNode = (Node)children.get(0);
VariableExpression subqueryExpr = new VariableExpression(symtbl, varNode.getNodeId());
Expression currentExpr = new SubqueryExpression((String)node.getNodeValue(), subqueryExpr);
return currentExpr;