1 16 package org.apache.commons.jexl.parser; 17 18 import org.apache.commons.jexl.JexlContext; 19 20 26 public class ASTReference extends SimpleNode { 27 28 protected SimpleNode root; 29 30 35 public ASTReference(int id) { 36 super(id); 37 } 38 39 45 public ASTReference(Parser p, int id) { 46 super(p, id); 47 } 48 49 50 public Object jjtAccept(ParserVisitor visitor, Object data) { 51 return visitor.visit(this, data); 52 } 53 54 55 public Object value(JexlContext jc) throws Exception { 56 return execute(null, jc); 57 } 58 59 60 public void jjtClose() { 61 root = (SimpleNode) jjtGetChild(0); 62 } 63 64 75 public Object execute(Object obj, JexlContext jc) throws Exception { 76 Object o = root.value(jc); 77 78 81 for (int i = 1; i < jjtGetNumChildren(); i++) { 82 o = ((SimpleNode) jjtGetChild(i)).execute(o, jc); 83 84 if (o == null) { 87 String varName = getIdentifierToDepth(i); 88 o = jc.getVars().get(varName); 89 } 90 } 91 92 return o; 93 } 94 95 103 private String getIdentifierToDepth(int i) { 104 StringBuffer varName = new StringBuffer (); 105 for (int j = 0; j <= i; j++) { 106 SimpleNode node = (SimpleNode) jjtGetChild(j); 107 if (node instanceof ASTIdentifier) { 108 varName.append(((ASTIdentifier) node).getIdentifierString()); 109 if (j != i) { 110 varName.append('.'); 111 } 112 } 113 } 114 return varName.toString(); 115 } 116 117 124 public String getRootString() throws Exception { 125 if (root instanceof ASTIdentifier) { 126 return ((ASTIdentifier) root).getIdentifierString(); 127 } 128 129 if (root instanceof ASTArrayAccess) { 130 return ((ASTArrayAccess) root).getIdentifierString(); 131 } 132 133 throw new Exception ("programmer error : ASTReference : root not known" + root); 134 } 135 } 136 | Popular Tags |