1 2 3 21 22 package org.apache.cayenne.exp.parser; 23 24 import java.io.PrintWriter ; 25 26 import org.apache.cayenne.ObjectId; 27 import org.apache.cayenne.Persistent; 28 import org.apache.cayenne.exp.Expression; 29 import org.apache.cayenne.exp.ExpressionException; 30 import org.apache.cayenne.util.Util; 31 32 42 public abstract class SimpleNode extends Expression implements Node { 43 44 protected Node parent; 45 protected Node[] children; 46 protected int id; 47 48 51 protected static void encodeScalarAsString(PrintWriter pw, Object scalar) { 52 boolean quote = scalar instanceof String ; 53 54 if (quote) { 55 pw.print('\"'); 56 } 57 58 if (scalar instanceof Persistent) { 61 ObjectId id = ((Persistent) scalar).getObjectId(); 62 if(id != null) { 63 scalar = id; 64 } 65 } 66 67 encodeAsEscapedString(pw, String.valueOf(scalar)); 68 if (quote) { 69 pw.print('\"'); 70 } 71 } 72 73 77 protected static void encodeAsEscapedString(PrintWriter pw, String source) { 78 int len = source.length(); 79 for (int i = 0; i < len; i++) { 80 char c = source.charAt(i); 81 82 switch (c) { 83 case '\n': 84 pw.print("\\n"); 85 continue; 86 case '\r': 87 pw.print("\\r"); 88 continue; 89 case '\t': 90 pw.print("\\t"); 91 continue; 92 case '\b': 93 pw.print("\\b"); 94 continue; 95 case '\f': 96 pw.print("\\f"); 97 continue; 98 case '\\': 99 pw.print("\\\\"); 100 continue; 101 case '\'': 102 pw.print("\\'"); 103 continue; 104 case '\"': 105 pw.print("\\\""); 106 continue; 107 default: 108 pw.print(c); 109 } 110 } 111 } 112 113 protected SimpleNode(int i) { 114 id = i; 115 } 116 117 protected abstract String getExpressionOperator(int index); 118 119 protected boolean pruneNodeForPrunedChild(Object prunedChild) { 120 return true; 121 } 122 123 126 public String expName() { 127 return ExpressionParserTreeConstants.jjtNodeName[id]; 128 } 129 130 134 protected void flattenTree() { 135 boolean shouldFlatten = false; 136 int newSize = 0; 137 138 for (int i = 0; i < children.length; i++) { 139 if (children[i].getClass() == getClass()) { 140 shouldFlatten = true; 141 newSize += children[i].jjtGetNumChildren(); 142 } 143 else { 144 newSize++; 145 } 146 } 147 148 if (shouldFlatten) { 149 Node[] newChildren = new Node[newSize]; 150 int j = 0; 151 152 for (int i = 0; i < children.length; ++i) { 153 Node c = children[i]; 154 if (c.getClass() == getClass()) { 155 for (int k = 0; k < c.jjtGetNumChildren(); ++k) 156 newChildren[j++] = c.jjtGetChild(k); 157 } 158 else { 159 newChildren[j++] = c; 160 } 161 } 162 163 if (j != newSize) { 164 throw new ExpressionException("Assertion error: " + j + " != " + newSize); 165 } 166 167 this.children = newChildren; 168 } 169 } 170 171 public void encodeAsString(PrintWriter pw) { 172 if (parent != null) { 173 pw.print("("); 174 } 175 176 if ((children != null) && (children.length > 0)) { 177 for (int i = 0; i < children.length; ++i) { 178 if (i > 0) { 179 pw.print(' '); 180 pw.print(getExpressionOperator(i)); 181 pw.print(' '); 182 } 183 184 ((SimpleNode) children[i]).encodeAsString(pw); 185 } 186 } 187 188 if (parent != null) { 189 pw.print(')'); 190 } 191 } 192 193 public Object getOperand(int index) { 194 Node child = jjtGetChild(index); 195 196 return unwrapChild(child); 200 } 201 202 protected Node wrapChild(Object child) { 203 return (child instanceof Node || child == null) ? (Node) child : new ASTScalar( 204 child); 205 } 206 207 protected Object unwrapChild(Node child) { 208 return (child instanceof ASTScalar) ? ((ASTScalar) child).getValue() : child; 209 } 210 211 public int getOperandCount() { 212 return jjtGetNumChildren(); 213 } 214 215 public void setOperand(int index, Object value) { 216 Node node = (value == null || value instanceof Node) 217 ? (Node) value 218 : new ASTScalar(value); 219 jjtAddChild(node, index); 220 221 if (node != null) { 223 node.jjtSetParent(this); 224 } 225 } 226 227 public void jjtOpen() { 228 229 } 230 231 public void jjtClose() { 232 233 } 234 235 public void jjtSetParent(Node n) { 236 parent = n; 237 } 238 239 public Node jjtGetParent() { 240 return parent; 241 } 242 243 public void jjtAddChild(Node n, int i) { 244 if (children == null) { 245 children = new Node[i + 1]; 246 } 247 else if (i >= children.length) { 248 Node c[] = new Node[i + 1]; 249 System.arraycopy(children, 0, c, 0, children.length); 250 children = c; 251 } 252 children[i] = n; 253 } 254 255 public Node jjtGetChild(int i) { 256 return children[i]; 257 } 258 259 public final int jjtGetNumChildren() { 260 return (children == null) ? 0 : children.length; 261 } 262 263 266 protected abstract Object evaluateNode(Object o) throws Exception ; 267 268 protected Object evaluateChild(int index, Object o) throws Exception { 269 return ((SimpleNode) jjtGetChild(index)).evaluate(o); 270 } 271 272 public Expression notExp() { 273 return new ASTNot(this); 274 } 275 276 public Object evaluate(Object o) { 277 try { 279 return evaluateNode(o); 280 } 281 catch (Throwable th) { 282 String string = this.toString(); 283 throw new ExpressionException( 284 "Error evaluating expression '" + string + "'", 285 string, 286 Util.unwindException(th)); 287 } 288 } 289 } 290 | Popular Tags |