View Javadoc

1   package org.apache.commons.jelly.tags.ant;
2   
3   /*
4    * Copyright 2002,2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Hashtable;
20  import java.util.Iterator;
21  
22  import org.apache.commons.jelly.JellyContext;
23  import org.apache.commons.jelly.expression.Expression;
24  
25  /*** Implementation of a Commons Grant <code>propsHandler</code>
26   *  to resolve through Jelly's context.
27   *
28   *  @author <a href="mailto:bob@eng.werken.com">Bob McWhirter</a>
29   *  @author <a href="mailto:stephenh@chase3000.com">Stephen Haberman</a>
30   */
31  public class JellyPropsHandler extends DefaultPropsHandler {
32  
33      /*** The JellyContext. */
34      private JellyContext context;
35  
36      /*** Simple constructor with the context to be used.
37       *
38       *  @param context The context to be used.
39       */
40      public JellyPropsHandler(JellyContext context) {
41          this.context = context;
42      }
43  
44      /*** Set an ant property.
45       *
46       *  @param name The property name.
47       *  @param value The property value.
48       */
49      public void setProperty(String name, String value) {
50          this.context.setVariable(name, value);
51      }
52  
53      /*** Retrieve an ant property.
54       *
55       *  @param name The property name.
56       *
57       *  @return The property value.
58       */
59      public String getProperty(String name) {
60          if (name == null) {
61              return null;
62          }
63          Object value = this.context.getVariable(name);
64          if (value == null) {
65              return null;
66          }
67          else {
68              if ( value instanceof Expression )
69              {
70                  return ( ( Expression ) value ).evaluateAsString( context );
71              }
72              else
73              {
74                  return value.toString();
75              }
76          }
77      }
78  
79      /*** Retrieve all ant properties.
80       *
81       *  @return A <code>Hashtable</code> of all properties.
82       */
83      public Hashtable getProperties() {
84          Hashtable h = new Hashtable();
85          for (Iterator i = this.context.getVariableNames(); i.hasNext(); ) {
86              String name = (String) i.next();
87              String value = getProperty( name );
88              if (value != null) {
89                  h.put(name, value);
90              }
91          }
92          return h;
93      }
94  
95  }