1 56 package org.objectstyle.cayenne.exp; 57 58 import java.util.HashMap ; 59 import java.util.Map ; 60 61 67 class ASTCompiler { 68 69 73 static ASTNode compile(Expression expression) throws ExpressionException { 74 ExpressionParser handler = new ExpressionParser(); 75 expression.traverse(handler); 76 return handler.finishParsing(expression); 77 } 78 79 static final class ExpressionParser extends TraversalHelper { 80 85 ASTNode currentNode; 86 ASTNode startNode; 87 Map astMap = new HashMap (); 88 89 ASTNode finishParsing(Expression expression) { 90 processNode((ASTNode) astMap.get(expression)); 93 94 return startNode; 95 } 96 97 public void startNode(Expression node, Expression parentNode) { 98 ASTNode parentAST = (ASTNode) astMap.get(parentNode); 101 astMap.put(node, ASTNode.buildExpressionNode(node, parentAST)); 102 } 103 104 public void finishedChild( 105 Expression node, 106 int childIndex, 107 boolean hasMoreChildren) { 108 109 int type = node.getType(); 112 if (type == Expression.OBJ_PATH 113 || type == Expression.LIST 114 || (childIndex == 1 115 && (type == Expression.LIKE 116 || type == Expression.LIKE_IGNORE_CASE 117 || type == Expression.NOT_LIKE 118 || type == Expression.NOT_LIKE_IGNORE_CASE))) { 119 return; 120 } 121 122 Object child = node.getOperand(childIndex); 124 125 ASTNode newAST; 127 if (child instanceof Expression) { 128 newAST = (ASTNode) astMap.get(child); 129 } 130 else { 131 ASTNode parentAST = (ASTNode) astMap.get(node); 132 newAST = ASTNode.buildObjectNode(child, parentAST); 133 } 134 135 processNode(newAST); 136 } 137 138 void processNode(ASTNode newAST) { 139 if (startNode == null) { 140 startNode = newAST; 141 currentNode = newAST; 142 } 143 else { 144 currentNode.setNextNode(newAST); 145 currentNode = newAST; 146 } 147 } 148 } 149 } 150 | Popular Tags |