1 19 package org.apache.cayenne.ejbql.parser; 20 21 import java.io.PrintWriter ; 22 import java.io.Serializable ; 23 import java.io.StringWriter ; 24 25 import org.apache.cayenne.ejbql.EJBQLExpression; 26 import org.apache.cayenne.ejbql.EJBQLExpressionVisitor; 27 28 34 public abstract class SimpleNode implements Node, Serializable , EJBQLExpression { 35 36 final int id; 37 SimpleNode parent; 38 SimpleNode[] children; 39 boolean not; 40 String text; 41 42 public SimpleNode(int id) { 43 this.id = id; 44 } 45 46 public String getText() { 47 return text; 48 } 49 50 public boolean isNegated() { 51 return not; 52 } 53 54 58 public void visit(EJBQLExpressionVisitor visitor) { 59 60 if (visitNode(visitor)) { 61 62 int len = getChildrenCount(); 63 for (int i = 0; i < len; i++) { 64 if (!visitChild(visitor, i)) { 65 break; 66 } 67 } 68 } 69 } 70 71 75 protected boolean visitNode(EJBQLExpressionVisitor visitor) { 76 return true; 77 } 78 79 83 protected boolean visitChild(EJBQLExpressionVisitor visitor, int childIndex) { 84 children[childIndex].visit(visitor); 85 return true; 86 } 87 88 public EJBQLExpression getChild(int index) { 89 return (EJBQLExpression) jjtGetChild(index); 90 } 91 92 public int getChildrenCount() { 93 return jjtGetNumChildren(); 94 } 95 96 public String getName() { 97 String className = getClass().getName(); 98 int i = className.lastIndexOf("EJBQL"); 99 return i >= 0 ? className.substring(i + 5) : className; 100 } 101 102 public void jjtOpen() { 103 } 104 105 public void jjtClose() { 106 } 107 108 public void jjtSetParent(Node parent) { 109 this.parent = (SimpleNode) parent; 110 } 111 112 public Node jjtGetParent() { 113 return this.parent; 114 } 115 116 public void jjtAddChild(Node n, int i) { 117 if (children == null) { 118 children = new SimpleNode[i + 1]; 119 } 120 else if (i >= children.length) { 121 SimpleNode c[] = new SimpleNode[i + 1]; 122 System.arraycopy(children, 0, c, 0, children.length); 123 children = c; 124 } 125 126 children[i] = (SimpleNode) n; 127 } 128 129 public Node jjtGetChild(int i) { 130 return children[i]; 131 } 132 133 public int jjtGetNumChildren() { 134 return (children == null) ? 0 : children.length; 135 } 136 137 public void setText(String text) { 138 this.text = text; 139 } 140 141 public String toString() { 142 StringWriter buffer = new StringWriter (); 143 PrintWriter pw = new PrintWriter (buffer); 144 dump(pw, "", true); 145 pw.close(); 146 buffer.flush(); 147 return buffer.toString(); 148 } 149 150 void dump(PrintWriter out, String prefix, boolean text) { 151 out.println(prefix 152 + getName() 153 + (text && this.text != null ? " [" + this.text + "]" : "")); 154 if (children != null) { 155 for (int i = 0; i < children.length; ++i) { 156 SimpleNode n = (SimpleNode) children[i]; 157 if (n != null) { 158 n.dump(out, prefix + " ", text); 159 } 160 } 161 } 162 } 163 } 164 | Popular Tags |