01: /*
02: * @(#)SimpleCallAction.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 java.util.Map;
13: import java.util.HashMap;
14: import java.util.ArrayList;
15:
16: /**
17: * This action does some computation with the element's attributes as
18: * the parameters. A subclass defines a concrete implementation.
19: */
20: public abstract class SimpleCallAction extends DigestAction {
21:
22: private String[] parameterNames;
23:
24: /**
25: * Subclass should override this method.
26: *
27: * @param args the arguments.
28: */
29: protected abstract void call(Object[] args);
30:
31: /**
32: * Constructor
33: */
34: public SimpleCallAction() {
35: }
36:
37: /**
38: * Constructor
39: *
40: * @param parameterNames the parameter names
41: */
42: public SimpleCallAction(String[] parameterNames) {
43: setParameterNames(parameterNames);
44: }
45:
46: /**
47: * Sets the parameter names
48: *
49: * @param parameterNames the parameter names
50: */
51: public void setParameterNames(String[] parameterNames) {
52: this .parameterNames = parameterNames;
53: }
54:
55: /**
56: * Gets the parameter names
57: *
58: */
59: public String[] getParameterNames() {
60: return this .parameterNames;
61: }
62:
63: /**
64: * Calls call(Object[]) method, assuming the parameter names are given to the constructor.
65: * The actual parameters are marshaled from the attributes, in the order of the parameter names.
66: *
67: * @param attributeMap the attributes of the start element
68: */
69: protected void callWithNamedParameters(Map attributeMap) {
70: ArrayList args = new ArrayList();
71: for (int i = 0; i < parameterNames.length; i++) {
72: args.add(attributeMap.get(parameterNames[i]));
73: }
74: call(args.toArray());
75: }
76:
77: /**
78: * Calls call(Object[]) method with a single argument, a Map object, which contains
79: * { qualified_name => value } mappings.
80: *
81: * @param attributeMap the attributes of the start element
82: */
83: protected void call(Map attributeMap) {
84: call(new Object[] { attributeMap });
85: }
86:
87: public void start(String path, String key, Map attributeMap,
88: Object top) throws Exception {
89: if (parameterNames != null) {
90: callWithNamedParameters(attributeMap);
91: } else {
92: call(attributeMap);
93: }
94: }
95: }
|