01: /*
02: * @(#)TextAction.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 java.util.Map;
12: import java.util.HashMap;
13: import java.util.List;
14: import pnuts.xml.DigestAction;
15: import pnuts.beans.BeanUtil;
16:
17: /**
18: * This action adds a map entry of {<em>keyword</em> => the content of the
19: * element} to the object on the stack top, in a way that depends on the type of the object.
20: * <ul>
21: * <li>If a List object is on the stack top, a Map object with
22: * a {<em>keyword</em> => content} entry is created and pushed onto the stack.
23: * <li>If a Map object is on the stack top, a map entry of
24: * {<em>keyword</em> => content} is added to the Map object.
25: * <li>Otherwise, this action assigns the element's content to the Bean
26: * property whose name is the <em>keyword</em>.
27: * If the property is of a primitive type, the content is automatically
28: * converted to the appropriate value.
29: * </ul>
30: *
31: */
32: public class TextAction extends DigestAction {
33:
34: protected void setBeanProperty(Object bean, String propertyName,
35: String text) throws Exception {
36: BeanHelper.setBeanProperty(bean, propertyName, text);
37: }
38:
39: public void end(String path, String key, String text, Object top)
40: throws Exception {
41: if (top instanceof Map) {
42: ((Map) top).put(key, text);
43: } else if (top instanceof List) {
44: HashMap m = new HashMap();
45: m.put(key, text);
46: ((List) top).add(m);
47: push(getStackTopPath(), m);
48: } else {
49: setBeanProperty(top, key, text);
50: }
51: }
52: }
|