KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > tags > core > SetPropertiesTag


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.core;
17
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19 import java.util.Map JavaDoc;
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  * &lt;j:new className="com.acme.Person" var="person"/&gt;
35  * &lt;j:setProperties object="${person}" name="James" location="${loc}"/&gt;
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 &lt;useBean&gt; 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) throws MissingAttributeException, JellyTagException {
55         Map JavaDoc attributes = getAttributes();
56         Object JavaDoc bean = attributes.remove( "object" );
57         if ( bean == null ) {
58             // lets try find a parent bean
59
BeanSource tag = (BeanSource) findAncestorWithClass(BeanSource.class);
60             if (tag != null) {
61                 try {
62                     bean = tag.getBean();
63                 } catch (JellyException e) {
64                     throw new JellyTagException(e);
65                 }
66             }
67             if (bean == null) {
68                 throw new MissingAttributeException("bean");
69             }
70         }
71         setBeanProperties(bean, attributes);
72     }
73
74     // Implementation methods
75
//-------------------------------------------------------------------------
76

77     /**
78      * Sets the properties on the bean. Derived tags could implement some custom
79      * type conversion etc.
80      */

81     protected void setBeanProperties(Object JavaDoc bean, Map JavaDoc attributes) throws JellyTagException {
82         try {
83             BeanUtils.populate(bean, attributes);
84         } catch (IllegalAccessException JavaDoc e) {
85             throw new JellyTagException("could not set the properties on a bean",e);
86         } catch (InvocationTargetException JavaDoc e) {
87             throw new JellyTagException("could not set the properties on a bean",e);
88         }
89     }
90 }
91
Popular Tags