%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.dynabean.SetTag |
|
|
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 | 6 | 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 | 21 | public SetTag() { |
54 | 21 | } |
55 | ||
56 | // Tag interface |
|
57 | //------------------------------------------------------------------------- |
|
58 | public void doTag(XMLOutput output) throws JellyTagException { |
|
59 | 21 | Object answer = null; |
60 | 21 | if ( value != null ) { |
61 | 21 | answer = value.evaluate(context); |
62 | } |
|
63 | else { |
|
64 | 0 | 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 | 21 | if (( var != null ) |
70 | && ( property != null) |
|
71 | && ( target == null )) |
|
72 | { |
|
73 | // Get object from context |
|
74 | 21 | if ( scope != null ) { |
75 | 0 | target = context.getVariable(var, scope); |
76 | } |
|
77 | else { |
|
78 | 21 | target = context.getVariable(var); |
79 | } |
|
80 | ||
81 | 21 | if (target != null) { |
82 | 21 | var = null; |
83 | } |
|
84 | } |
|
85 | ||
86 | 21 | if ( var != null ) { |
87 | ||
88 | ||
89 | 0 | if ( scope != null ) { |
90 | 0 | context.setVariable(var, scope, answer); |
91 | } |
|
92 | else { |
|
93 | 0 | context.setVariable(var, answer); |
94 | } |
|
95 | } |
|
96 | else { |
|
97 | 21 | if ( target == null ) { |
98 | 0 | throw new JellyTagException( "Either a 'var' or a 'target' attribute must be defined for this tag" ); |
99 | } |
|
100 | 21 | if ( property == null ) { |
101 | 0 | throw new JellyTagException( "You must define a 'property' attribute if you specify a 'target'" ); |
102 | } |
|
103 | 21 | setPropertyValue( target, property, answer ); |
104 | } |
|
105 | 21 | } |
106 | ||
107 | // Properties |
|
108 | //------------------------------------------------------------------------- |
|
109 | /** Sets the variable name to define for this expression |
|
110 | */ |
|
111 | public void setVar(String var) { |
|
112 | 21 | this.var = class="keyword">var; |
113 | 21 | } |
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 | 0 | this.scope = scope; |
124 | 0 | } |
125 | ||
126 | /** Sets the expression to evaluate. */ |
|
127 | public void setValue(Expression value) { |
|
128 | 21 | this.value = value; |
129 | 21 | } |
130 | ||
131 | /** Sets the target object on which to set a property. */ |
|
132 | public void setTarget(Object target) { |
|
133 | 0 | this.target = target; |
134 | 0 | } |
135 | ||
136 | /** Sets the name of the property to set on the target object. */ |
|
137 | public void setProperty(String property) { |
|
138 | 21 | this.property = property; |
139 | 21 | } |
140 | ||
141 | // Implementation methods |
|
142 | //------------------------------------------------------------------------- |
|
143 | protected void setPropertyValue( Object target, String property, Object value ) { |
|
144 | try { |
|
145 | 21 | if ( target instanceof Map ) { |
146 | 0 | Map map = (Map) target; |
147 | 0 | map.put( property, value ); |
148 | } |
|
149 | else { |
|
150 | 21 | BeanUtils.setProperty( target, property, value ); |
151 | } |
|
152 | } |
|
153 | 0 | catch (Exception e) { |
154 | 0 | log.error( "Failed to set the property: " + property + " on bean: " + target + " to value: " + value + " due to exception: " + e, e ); |
155 | 21 | } |
156 | 21 | } |
157 | ||
158 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |