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.jexl2.parser;
18
19 import org.apache.commons.jexl2.JexlEngine;
20
21 /**
22 * Enhanced script to allow parameters declaration.
23 */
24 public class ASTJexlScript extends JexlNode {
25 /** The script scope. */
26 private JexlEngine.Scope scope = null;
27
28 public ASTJexlScript(int id) {
29 super(id);
30 }
31
32 public ASTJexlScript(Parser p, int id) {
33 super(p, id);
34 }
35
36 @Override
37 public Object jjtAccept(ParserVisitor visitor, Object data) {
38 return visitor.visit(this, data);
39 }
40
41 /**
42 * Sets the parameters and registers
43 * @param theScope the scope
44 */
45 public void setScope(JexlEngine.Scope theScope) {
46 this.scope = theScope;
47 }
48
49 /**
50 * Gets this script scope.
51 */
52 public JexlEngine.Scope getScope() {
53 return scope;
54 }
55
56 /**
57 * Creates an array of arguments by copying values up to the number of parameters.
58 * @param values the argument values
59 * @return the arguments array
60 */
61 public JexlEngine.Frame createFrame(Object... values) {
62 return scope != null? scope.createFrame(values) : null;
63 }
64
65 /**
66 * Gets the (maximum) number of arguments this script expects.
67 * @return the number of parameters
68 */
69 public int getArgCount() {
70 return scope != null? scope.getArgCount() : 0;
71 }
72
73 /**
74 * Gets this script registers, i.e. parameters and local variables.
75 * @return the register names
76 */
77 public String[] getRegisters() {
78 return scope != null? scope.getRegisters() : null;
79 }
80
81 /**
82 * Gets this script parameters, i.e. registers assigned before creating local variables.
83 * @return the parameter names
84 */
85 public String[] getParameters() {
86 return scope != null? scope.getParameters() : null;
87 }
88
89 /**
90 * Gets this script local variable, i.e. registers assigned to local variables.
91 * @return the parameter names
92 */
93 public String[] getLocalVariables() {
94 return scope != null? scope.getLocalVariables() : null;
95 }
96 }