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.dynabean;
17  
18  import java.util.Map;
19  
20  import org.apache.commons.beanutils.BeanUtils;
21  import org.apache.commons.jelly.JellyTagException;
22  import org.apache.commons.jelly.TagSupport;
23  import org.apache.commons.jelly.XMLOutput;
24  import org.apache.commons.jelly.expression.Expression;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /*** A tag which sets a variable from the result of an expression
29    *
30   * @author Theo Niemeijer
31   * @version 1.0
32    */
33  public class SetTag extends TagSupport {
34  
35      /*** The Log to which logging calls will be made. */
36      private static final Log log = LogFactory.getLog(SetTag.class);
37  
38      /*** The variable name to export. */
39      private String var;
40  
41      /*** The variable scope to export */
42      private String scope;
43  
44      /*** The expression to evaluate. */
45      private Expression value;
46  
47      /*** The target object on which to set a property. */
48      private Object target;
49  
50      /*** The name of the property to set on the target object. */
51      private String property;
52  
53      public SetTag() {
54      }
55  
56      // Tag interface
57      //-------------------------------------------------------------------------
58      public void doTag(XMLOutput output) throws JellyTagException {
59          Object answer = null;
60          if ( value != null ) {
61              answer = value.evaluate(context);
62          }
63          else {
64              answer = getBodyText();
65          }
66  
67          // Assume that if a var name and a property is given then
68          // var is the name of an object in the context
69          if (( var != null )
70          && ( property != null)
71          && ( target == null ))
72          {
73              // Get object from context
74              if ( scope != null ) {
75                 target = context.getVariable(var, scope);
76              }
77              else {
78                  target = context.getVariable(var);
79              }
80  
81              if (target != null) {
82                  var = null;
83              }
84          }
85  
86          if ( var != null ) {
87  
88  
89              if ( scope != null ) {
90                  context.setVariable(var, scope, answer);
91              }
92              else {
93                  context.setVariable(var, answer);
94              }
95          }
96          else {
97              if ( target == null ) {
98                  throw new JellyTagException( "Either a 'var' or a 'target' attribute must be defined for this tag" );
99              }
100             if ( property == null ) {
101                 throw new JellyTagException( "You must define a 'property' attribute if you specify a 'target'" );
102             }
103             setPropertyValue( target, property, answer );
104         }
105     }
106 
107     // Properties
108     //-------------------------------------------------------------------------
109     /*** Sets the variable name to define for this expression
110      */
111     public void setVar(String var) {
112         this.var = var;
113     }
114 
115     /***
116      * Sets the variable scope for this variable. For example setting this value to 'parent' will
117      * set this value in the parent scope. When Jelly is run from inside a Servlet environment
118      * then other scopes will be available such as 'request', 'session' or 'application'.
119      *
120      * Other applications may implement their own custom scopes.
121      */
122     public void setScope(String scope) {
123         this.scope = scope;
124     }
125 
126     /*** Sets the expression to evaluate. */
127     public void setValue(Expression value) {
128         this.value = value;
129     }
130 
131     /*** Sets the target object on which to set a property. */
132     public void setTarget(Object target) {
133         this.target = target;
134     }
135 
136     /*** Sets the name of the property to set on the target object. */
137     public void setProperty(String property) {
138         this.property = property;
139     }
140 
141     // Implementation methods
142     //-------------------------------------------------------------------------
143     protected void setPropertyValue( Object target, String property, Object value ) {
144         try {
145             if ( target instanceof Map ) {
146                 Map map = (Map) target;
147                 map.put( property, value );
148             }
149             else {
150                 BeanUtils.setProperty( target, property, value );
151             }
152         }
153         catch (Exception e) {
154             log.error( "Failed to set the property: " + property + " on bean: " + target + " to value: " + value + " due to exception: " + e, e );
155         }
156     }
157 
158 }