View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jexl3.parser;
18  
19  /**
20   * A class originally generated by JJTree with the following JavaCCOptions:
21   * MULTI=true,NODE_USES_PARSER=true,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=
22   *
23   * Works around issue https://javacc.dev.java.net/issues/show_bug.cgi?id=227
24   * As soon as this issue if fixed and the maven plugin uses the correct version of Javacc, this
25   * class can go away.
26   *
27   * The technical goal is to ensure every reference made in the parser was to a JexlNode; unfortunately,
28   * as in javacc 4.1, it still uses a SimpleNode reference in the generated ParserVisitor.
29   * Besides, there is no need to keep the parser around in the node.
30   *
31   * The functional goal is to a allow a <em>volatile</em> value in the node
32   * so it can serve as a last evaluation cache even in multi-threaded executions.
33   */
34  public class SimpleNode implements Node {
35  
36      /**
37       */
38      private static final long serialVersionUID = 1L;
39  
40      /** The parent node. */
41      private JexlNode parent;
42  
43      /** The array of children nodes. */
44      private JexlNode[] children;
45  
46      /** The node type id. */
47      protected final int id;
48  
49      /** Volatile value so it can be used as a last evaluation cache. */
50      private transient volatile Object value;
51  
52      /**
53       * Creates a SimpleNode instance.
54       *
55       * @param i the node type identifier
56       */
57      public SimpleNode(final int i) {
58          id = i;
59      }
60  
61      /**
62       * Constructs a SimpleNode instance.
63       *
64       * @param p not used.
65       * @param i the node type identifier
66       * @deprecated Use {@link #SimpleNode(int)}.
67       */
68      @Deprecated
69      public SimpleNode(final Parser p, final int i) {
70          this(i);
71      }
72  
73      /**
74       * Accepts the visitor on all this node's children.
75       *
76       * @param visitor the visitor
77       * @param data contextual data
78       * @return result of visit
79       **/
80      public Object childrenAccept(final ParserVisitor visitor, final Object data) {
81          if (children != null) {
82              for (final JexlNode child : children) {
83                  child.jjtAccept(visitor, data);
84              }
85          }
86          return data;
87      }
88  
89      /* Override this method if you want to customize how the JexlNode dumps
90      out its children. */
91      public void dump(final String prefix) {
92          dumpOut(toString(prefix));
93          if (children != null) {
94              for (final SimpleNode n : children) {
95                  if (n != null) {
96                      n.dump(prefix + " ");
97                  }
98              }
99          }
100     }
101 
102     /**
103      * Override to dump output somewhere.
104      *
105      * @param str the string to output
106      */
107     protected void dumpOut(final String str) {
108         // override to obtain an output
109     }
110 
111     @Override
112     public int getId() {
113         return id;
114     }
115 
116     /**
117      * Accepts the visitor.
118      *
119      * @param visitor the visitor
120      * @param data contextual data
121      * @return result of visit
122      **/
123     @Override
124     public Object jjtAccept(final ParserVisitor visitor, final Object data) {
125         return visitor.visit(this, data);
126     }
127 
128     /**
129      * Adds a child node.
130      *
131      * @param n the child node
132      * @param i the child offset
133      */
134     @Override
135     public void jjtAddChild(final Node n, final int i) {
136         if (children == null) {
137             children = new JexlNode[i + 1];
138         } else if (i >= children.length) {
139             final JexlNode[] c = new JexlNode[i + 1];
140             System.arraycopy(children, 0, c, 0, children.length);
141             children = c;
142         }
143         children[i] = (JexlNode) n;
144     }
145 
146     @Override
147     public void jjtClose() {
148     }
149 
150     /**
151      * Gets a child of this node.
152      *
153      * @param i the child offset
154      * @return the child node
155      */
156     @Override
157     public JexlNode jjtGetChild(final int i) {
158         return children[i];
159     }
160 
161     /**
162      * Gets this node number of children.
163      *
164      * @return the number of children
165      */
166     @Override
167     public int jjtGetNumChildren() {
168         return children == null ? 0 : children.length;
169     }
170 
171     /**
172      * Gets this node's parent.
173      *
174      * @return the parent node
175      */
176     @Override
177     public JexlNode jjtGetParent() {
178         return parent;
179     }
180 
181     /**
182      * Gets this node value.
183      *
184      * @return value
185      */
186     public Object jjtGetValue() {
187         return value;
188     }
189 
190     @Override
191     public void jjtOpen() {
192     }
193 
194     // For use by ASTJexlScript only
195     void jjtSetChildren(final JexlNode[] jexlNodes) {
196         children = jexlNodes;
197     }
198 
199     /**
200      * Sets this node's parent.
201      *
202      * @param n the parent
203      */
204     @Override
205     public void jjtSetParent(final Node n) {
206         parent = (JexlNode) n;
207     }
208 
209     /**
210      * Sets this node value.
211      *
212      * @param value this node value.
213      */
214     public void jjtSetValue(final Object value) {
215         this.value = value;
216     }
217 
218     /* You can override these two methods in subclasses of SimpleNode to
219     customize the way the JexlNode appears when the tree is dumped.  If
220     your output uses more than one line you should override
221     toString(String), otherwise overriding toString() is probably all
222     you need to do. */
223     @Override
224     public String toString() {
225         return ParserTreeConstants.jjtNodeName[id];
226     }
227 
228     public String toString(final String prefix) {
229         return prefix + toString();
230     }
231 }
232 
233 /* JavaCC - OriginalChecksum=7dff880883d088a37c1e3197e4b455a0 (do not edit this line) */