01: /*
02: * Copyright 2002,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.core;
17:
18: import java.lang.reflect.InvocationTargetException;
19: import java.util.Map;
20:
21: import org.apache.commons.beanutils.BeanUtils;
22:
23: import org.apache.commons.jelly.JellyException;
24: import org.apache.commons.jelly.JellyTagException;
25: import org.apache.commons.jelly.MissingAttributeException;
26: import org.apache.commons.jelly.MapTagSupport;
27: import org.apache.commons.jelly.XMLOutput;
28: import org.apache.commons.jelly.impl.BeanSource;
29:
30: /**
31: * A tag which sets the bean properties on the given bean.
32: * So if you used it as follows, for example using the <j:new> tag.
33: * <pre>
34: * <j:new className="com.acme.Person" var="person"/>
35: * <j:setProperties object="${person}" name="James" location="${loc}"/>
36: * </pre>
37: * Then it would set the name and location properties on the bean denoted by
38: * the expression ${person}.
39: * <p>
40: * This tag can also be nested inside a bean tag such as the <useBean> tag
41: * or a JellySwing tag to set one or more properties, maybe inside some conditional
42: * logic.
43: *
44: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
45: * @version $Revision: 155420 $
46: */
47: public class SetPropertiesTag extends MapTagSupport {
48:
49: public SetPropertiesTag() {
50: }
51:
52: // Tag interface
53: //-------------------------------------------------------------------------
54: public void doTag(XMLOutput output)
55: throws MissingAttributeException, JellyTagException {
56: Map attributes = getAttributes();
57: Object bean = attributes.remove("object");
58: if (bean == null) {
59: // lets try find a parent bean
60: BeanSource tag = (BeanSource) findAncestorWithClass(BeanSource.class);
61: if (tag != null) {
62: try {
63: bean = tag.getBean();
64: } catch (JellyException e) {
65: throw new JellyTagException(e);
66: }
67: }
68: if (bean == null) {
69: throw new MissingAttributeException("bean");
70: }
71: }
72: setBeanProperties(bean, attributes);
73: }
74:
75: // Implementation methods
76: //-------------------------------------------------------------------------
77:
78: /**
79: * Sets the properties on the bean. Derived tags could implement some custom
80: * type conversion etc.
81: */
82: protected void setBeanProperties(Object bean, Map attributes)
83: throws JellyTagException {
84: try {
85: BeanUtils.populate(bean, attributes);
86: } catch (IllegalAccessException e) {
87: throw new JellyTagException(
88: "could not set the properties on a bean", e);
89: } catch (InvocationTargetException e) {
90: throw new JellyTagException(
91: "could not set the properties on a bean", e);
92: }
93: }
94: }
|