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    *      http://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      private static final long serialVersionUID = 1L;
38      /** The parent node. */
39      private JexlNode parent;
40      /** The array of children nodes. */
41      private JexlNode[] children;
42      /** The node type id. */
43      protected final int id;
44      /** Volatile value so it can be used as a last evaluation cache. */
45      private transient volatile Object value;
46  
47      /**
48       * Creates a SimpleNode instance.
49       * @param i the node type identifier
50       */
51      public SimpleNode(final int i) {
52          id = i;
53      }
54  
55      /**
56       * Constructs a SimpleNode instance.
57       *
58       * @param p not used.
59       * @param i the node type identifier
60       * @deprecated Use {@link #SimpleNode(int)}.
61       */
62      @Deprecated
63      public SimpleNode(final Parser p, final int i) {
64          this(i);
65      }
66  
67      /**
68       * Accepts the visitor on all this node's children.
69       *
70       * @param visitor the visitor
71       * @param data contextual data
72       * @return result of visit
73       **/
74      public Object childrenAccept(final ParserVisitor visitor, final Object data) {
75          if (children != null) {
76              for (final JexlNode child : children) {
77                  child.jjtAccept(visitor, data);
78              }
79          }
80          return data;
81      }
82  
83      /* Override this method if you want to customize how the JexlNode dumps
84      out its children. */
85      public void dump(final String prefix) {
86          dumpOut(toString(prefix));
87          if (children != null) {
88              for (final SimpleNode n : children) {
89                  if (n != null) {
90                      n.dump(prefix + " ");
91                  }
92              }
93          }
94      }
95  
96      /**
97       * Override to dump output somewhere.
98       * @param str the string to output
99       */
100     protected void dumpOut(final String str) {
101         // override to obtain an output
102     }
103 
104     @Override
105     public int getId() {
106         return id;
107     }
108 
109     /**
110      * Accepts the visitor.
111      *
112      * @param visitor the visitor
113      * @param data contextual data
114      * @return result of visit
115      **/
116     @Override
117     public Object jjtAccept(final ParserVisitor visitor, final Object data) {
118         return visitor.visit(this, data);
119     }
120 
121     /**
122      * Adds a child node.
123      *
124      * @param n the child node
125      * @param i the child offset
126      */
127     @Override
128     public void jjtAddChild(final Node n, final int i) {
129         if (children == null) {
130             children = new JexlNode[i + 1];
131         } else if (i >= children.length) {
132             final JexlNode[] c = new JexlNode[i + 1];
133             System.arraycopy(children, 0, c, 0, children.length);
134             children = c;
135         }
136         children[i] = (JexlNode) n;
137     }
138 
139     @Override
140     public void jjtClose() {
141     }
142 
143     /**
144      * Gets a child of this node.
145      *
146      * @param i the child offset
147      * @return the child node
148      */
149     @Override
150     public JexlNode jjtGetChild(final int i) {
151         return children[i];
152     }
153 
154     /**
155      * Gets this node number of children.
156      *
157      * @return the number of children
158      */
159     @Override
160     public int jjtGetNumChildren() {
161         return children == null ? 0 : children.length;
162     }
163 
164     /**
165      * Gets this node's parent.
166      *
167      * @return the parent node
168      */
169     @Override
170     public JexlNode jjtGetParent() {
171         return parent;
172     }
173 
174     /**
175      * Gets this node value.
176      *
177      * @return value
178      */
179     public Object jjtGetValue() {
180         return value;
181     }
182 
183     @Override
184     public void jjtOpen() {
185     }
186 
187     // For use by ASTJexlScript only
188     void jjtSetChildren(final JexlNode[] jexlNodes) {
189         children = jexlNodes;
190     }
191 
192     /**
193      * Sets this node's parent.
194      * @param n the parent
195      */
196     @Override
197     public void jjtSetParent(final Node n) {
198         parent = (JexlNode) n;
199     }
200 
201     /**
202      * Sets this node value.
203      *
204      * @param value this node value.
205      */
206     public void jjtSetValue(final Object value) {
207         this.value = value;
208     }
209 
210     /* You can override these two methods in subclasses of SimpleNode to
211     customize the way the JexlNode appears when the tree is dumped.  If
212     your output uses more than one line you should override
213     toString(String), otherwise overriding toString() is probably all
214     you need to do. */
215     @Override
216     public String toString() {
217         return ParserTreeConstants.jjtNodeName[id];
218     }
219 
220     public String toString(final String prefix) {
221         return prefix + toString();
222     }
223 }
224 
225 /* JavaCC - OriginalChecksum=7dff880883d088a37c1e3197e4b455a0 (do not edit this line) */