KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > ejbql > parser > SimpleNode


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.ejbql.parser;
20
21 import java.io.PrintWriter JavaDoc;
22 import java.io.Serializable JavaDoc;
23 import java.io.StringWriter JavaDoc;
24
25 import org.apache.cayenne.ejbql.EJBQLExpression;
26 import org.apache.cayenne.ejbql.EJBQLExpressionVisitor;
27
28 /**
29  * A base node for the EJBQL concrete nodes that satisfies JJTree requirements.
30  *
31  * @since 3.0
32  * @author Andrus Adamchik
33  */

34 public abstract class SimpleNode implements Node, Serializable JavaDoc, EJBQLExpression {
35
36     final int id;
37     SimpleNode parent;
38     SimpleNode[] children;
39     boolean not;
40     String JavaDoc text;
41
42     public SimpleNode(int id) {
43         this.id = id;
44     }
45
46     public String JavaDoc getText() {
47         return text;
48     }
49
50     public boolean isNegated() {
51         return not;
52     }
53
54     /**
55      * A recursive visit method that passes a visitor to this node and all its children,
56      * depth first.
57      */

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     /**
72      * Visits this node without recursion. Default implementation simply returns true.
73      * Subclasses override this method to call an appropriate visitor method.
74      */

75     protected boolean visitNode(EJBQLExpressionVisitor visitor) {
76         return true;
77     }
78
79     /**
80      * Recursively visits a child at the specified index. Subclasses override this method
81      * if they desire to implement callbacks after visiting each child.
82      */

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 JavaDoc getName() {
97         String JavaDoc 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 JavaDoc text) {
138         this.text = text;
139     }
140
141     public String JavaDoc toString() {
142         StringWriter JavaDoc buffer = new StringWriter JavaDoc();
143         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(buffer);
144         dump(pw, "", true);
145         pw.close();
146         buffer.flush();
147         return buffer.toString();
148     }
149
150     void dump(PrintWriter JavaDoc out, String JavaDoc 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