01: /*
02: * @(#)BeanAction.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.Map;
14: import java.util.HashMap;
15: import java.util.List;
16: import java.util.Iterator;
17: import org.xml.sax.Attributes;
18:
19: /**
20: * This action creates an instance of the class, which was passed to the
21: * constructor, and adds it to the object on the stack top, in a way that
22: * depends on the type of the object. Then it pushes the object on the stack.
23: * <ul>
24: * <li>If a List object is on the stack top, the created object is added to the List.
25: * <li>If a Map object is on the stack top, a map entry of {<em>keyword</em> => the
26: * created object} is added to the Map.
27: * <li>Otherwise, this action assigns the created object to the Bean property whose name
28: * is the <em>keyword</em>. The type of the Bean property must match the type of the created object.
29: * </ul>
30: * If the element has one or more attributes, this action assigns the
31: * attribute's value to the Bean property, whose name is the name of the
32: * attribute. If the Bean property is of a primitive type, the attribute's
33: * value is automatically converted to the appropriate value.
34: */
35: public class BeanAction extends DigestAction {
36: static Object[] NOARGS = new Object[] {};
37: static Class[] NOTYPES = new Class[] {};
38:
39: Class cls;
40:
41: protected BeanAction() {
42: }
43:
44: public BeanAction(Class cls) {
45: this .cls = cls;
46: }
47:
48: /**
49: * Defines bean properties from the [qName->value] mapping in the specified Attibutes.
50: *
51: * @param bean the target object
52: * @param attributeMap a [qName->value] mapping.
53: */
54: protected void setAttributes(Object bean, Map attributeMap)
55: throws Exception {
56: for (Iterator it = attributeMap.entrySet().iterator(); it
57: .hasNext();) {
58: Map.Entry entry = (Map.Entry) it.next();
59: setBeanProperty(bean, (String) entry.getKey(),
60: (String) entry.getValue());
61: }
62: }
63:
64: /**
65: * Defines a bean property
66: *
67: * @param bean the target object
68: * @param name the property name
69: * @param text a string
70: */
71: protected void setBeanProperty(Object bean, String name, String text)
72: throws Exception {
73: BeanHelper.setBeanProperty(bean, name, text);
74: }
75:
76: public void start(String path, String key, Map attributeMap,
77: Object top) throws Exception {
78: Object bean = cls.newInstance();
79: setAttributes(bean, attributeMap);
80: if (top instanceof List) {
81: ((List) top).add(bean);
82: } else if (top instanceof Map) {
83: ((Map) top).put(key, bean);
84: push(path, bean);
85: } else {
86: BeanUtil.setProperty(top, key, bean);
87: push(path, bean);
88: }
89: }
90: }
|