001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.jexl2.parser; 018 019 import org.apache.commons.jexl2.JexlEngine; 020 021 /** 022 * Enhanced script to allow parameters declaration. 023 */ 024 public class ASTJexlScript extends JexlNode { 025 /** The script scope. */ 026 private JexlEngine.Scope scope = null; 027 028 public ASTJexlScript(int id) { 029 super(id); 030 } 031 032 public ASTJexlScript(Parser p, int id) { 033 super(p, id); 034 } 035 036 @Override 037 public Object jjtAccept(ParserVisitor visitor, Object data) { 038 return visitor.visit(this, data); 039 } 040 041 /** 042 * Sets the parameters and registers 043 * @param theScope the scope 044 */ 045 public void setScope(JexlEngine.Scope theScope) { 046 this.scope = theScope; 047 } 048 049 /** 050 * Gets this script scope. 051 */ 052 public JexlEngine.Scope getScope() { 053 return scope; 054 } 055 056 /** 057 * Creates an array of arguments by copying values up to the number of parameters. 058 * @param values the argument values 059 * @return the arguments array 060 */ 061 public JexlEngine.Frame createFrame(Object... values) { 062 return scope != null? scope.createFrame(values) : null; 063 } 064 065 /** 066 * Gets the (maximum) number of arguments this script expects. 067 * @return the number of parameters 068 */ 069 public int getArgCount() { 070 return scope != null? scope.getArgCount() : 0; 071 } 072 073 /** 074 * Gets this script registers, i.e. parameters and local variables. 075 * @return the register names 076 */ 077 public String[] getRegisters() { 078 return scope != null? scope.getRegisters() : null; 079 } 080 081 /** 082 * Gets this script parameters, i.e. registers assigned before creating local variables. 083 * @return the parameter names 084 */ 085 public String[] getParameters() { 086 return scope != null? scope.getParameters() : null; 087 } 088 089 /** 090 * Gets this script local variable, i.e. registers assigned to local variables. 091 * @return the parameter names 092 */ 093 public String[] getLocalVariables() { 094 return scope != null? scope.getLocalVariables() : null; 095 } 096 }