View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.beanshell;
17  
18  import bsh.EvalError;
19  import bsh.Interpreter;
20  
21  import java.util.Iterator;
22  
23  import org.apache.commons.jelly.JellyContext;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /*** Integrates BeanShell's interpreter with Jelly's JellyContext
29    *
30    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31    * @version $Revision: 155420 $
32    */
33  public class JellyInterpreter extends Interpreter {
34  
35      /*** The Log to which logging calls will be made. */
36      private static final Log log = LogFactory.getLog( JellyInterpreter.class );
37  
38      private JellyContext context;
39  
40      public JellyInterpreter() {
41      }
42  
43      public JellyContext getJellyContext() {
44          return context;
45      }
46  
47      public void setJellyContext(JellyContext context) throws EvalError {
48          this.context = context;
49  
50          // now pass in all the variables
51          for ( Iterator iter = context.getVariableNames(); iter.hasNext(); ) {
52              String name = (String) iter.next();
53              Object value = context.getVariable(name);
54              name = convertVariableName(name);
55              if (name != null) {
56                  set( name, value );
57              }
58          }
59  
60          // lets pass in the Jelly context
61          set( "context", context );
62      }
63  
64  /*
65  
66      // the following code doesn't work - it seems that
67      // all variables must be passed into the Interpreter
68      // via set() method
69  
70      public Object get(String name) throws EvalError {
71          if ( context != null ) {
72              Object answer = context.getVariable( name );
73              if ( answer != null ) {
74                  return answer;
75              }
76          }
77          return super.get( name );
78      }
79  */
80  
81      /***
82       * Converts variables to a beanshell allowable format or hides names that
83       * can't be converted, by returning null.
84       * For now lets just turn '.' into '_'
85       */
86      protected String convertVariableName(String name) {
87          return name.replace('.', '_');
88      }
89  }