01: /*
02: * @(#)BeanPropertyAction.java 1.2 04/12/06
03: *
04: * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.xml.action;
10:
11: import pnuts.xml.DigestAction;
12: import pnuts.beans.BeanUtil;
13: import java.util.List;
14:
15: /**
16: * This action sets a Bean property of the object on the stack top,
17: * using <em>keyword</em> and the content of the element.
18: * <ul>
19: * <li>If a List object is on the stack top, it creates an instance of
20: * the class passed to the constructor, and push it onto the stack.
21: * <li>Regardless of the type of the stack top object, if
22: * the element has non-empty content, this action assigns the content's
23: * value to the Bean property whose name is the <em>keyword</em>. If the
24: * Bean property is of a primitive type, the content's value is
25: * automatically converted to the appropriate value.
26: * </ul>
27: */
28: public class BeanPropertyAction extends DigestAction {
29: static Object[] NOARGS = new Object[] {};
30: static Class[] NOTYPES = new Class[] {};
31:
32: Class cls;
33:
34: protected BeanPropertyAction() {
35: }
36:
37: public BeanPropertyAction(Class cls) {
38: this .cls = cls;
39: }
40:
41: /**
42: * Sets the bean property "key".
43: * A subclass may override this method to convert the string to an appropriate type.
44: *
45: * @param bean the target object
46: * @param key the property name
47: * @param text the value
48: */
49: protected void setBeanProperty(Object bean, String key, String text)
50: throws Exception {
51: BeanHelper.setBeanProperty(bean, key, text);
52: }
53:
54: public void end(String path, String key, String text, Object top)
55: throws Exception {
56: if (top instanceof List) {
57: Object bean = cls.newInstance();
58: ((List) top).add(bean);
59: setBeanProperty(bean, key, text);
60: push(getStackTopPath(), bean);
61: } else {
62: setBeanProperty(top, key, text);
63: }
64: }
65: }
|