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