01: /*
02: * @(#)MapAction.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 creates a Map object and adds it to the object on the
19: * 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, an another Map object of
22: * {<em>keyword</em> => the created Map object} is created and added to the List object.
23: * <li>If a Map object is on the stack top, a map entry of {<em>keyword</em> => the
24: * created Map object} is added to the Map object.
25: * <li>Otherwise, this action assigns the created Map object to the Bean
26: * property whose name is the <em>keyword</em>. In this case, the Bean property must be
27: * of java.util.Map type.
28: * </ul>
29: * If the element has one or more attributes, a map entry of {attribute's
30: * name=>the value} is added to the created Map object.
31: */
32: public class MapAction extends DigestAction {
33:
34: public void start(String path, String key, Map attributeMap,
35: Object top) throws Exception {
36: Map v = new HashMap(attributeMap);
37: if (top instanceof Map) {
38: ((Map) top).put(key, v);
39: push(path, v);
40: } else if (top instanceof List) {
41: Map m = new HashMap();
42: m.put(key, v);
43: ((List) top).add(m);
44: push(getStackTopPath(), m);
45: } else {
46: BeanUtil.setProperty(top, key, v);
47: push(path, v);
48: }
49: }
50:
51: public void end(String path, String key, String text, Object top)
52: throws Exception {
53: pop();
54: }
55: }
|