1 package net.sf.tomp.xtcl.command;
2
3 import net.sf.tomp.general.Parametrized;
4 import net.sf.tomp.xtcl.Command;
5 import net.sf.tomp.xtcl.Context;
6
7 import java.io.PrintStream;
8
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.Map;
12
13 /***
14 * Base class for (almost all) built-in Commands in XTCL. Provides utility
15 * instance methods such as done, initParameters, and static methods:
16 * listMap,...
17 *
18 * @author tomp
19 */
20 public class AbstractCommand implements Command {
21 /***
22 * Do-nothing execution of an empty command.
23 *
24 * @param c
25 * Context this command will be executed in.
26 * @return always 0 == success, OK
27 * @throws Exception
28 * only if I/O error occurs when reporting the result to user.
29 */
30 public int execute(Context c) throws Exception {
31 return done(c, 0);
32 }
33
34 /***
35 * Executed always after the command itself. Prints the OK, ERR on the
36 * Context's std. out.
37 *
38 * @param c
39 * Context
40 * @param i
41 * result of previously called Command
42 * @return same as i
43 * @throws Exception
44 * if I/O error occurs when reporting the result to user.
45 *
46 */
47 public int done(Context c, int i) throws Exception {
48 PrintStream out = c.getOut();
49
50 if ((out != null) && c.isVerbose()) {
51 if (i == 0) {
52 out.print("OK " + this);
53 } else {
54 out.print("ERR " + i + " " + this);
55 }
56 }
57
58 return i;
59 }
60
61 /***
62 * DOCUMENT ME!
63 *
64 * @param params
65 * contains formal parameters pairs (key->value)
66 * the values are used as keys to refer objects in the context
67 * values of such references are set to Parametrized f and
68 * put to realParams
69 *
70 * @param realParams
71 * here the real parameter pairs are put
72 *
73 * @param f
74 * here the real parameter pairs are set
75 *
76 * @param context
77 * context to get the real params from
78 *
79 * @return 0 means OK, 1 means error: when strictParsing is on and
80 * a parameter reference is not found in the Context
81 */
82 protected static int initParameters(Map params, Map realParams, Parametrized f,
83 Context context) {
84 for (Iterator i = params.keySet().iterator(); i.hasNext();) {
85 String k = (String) i.next();
86 Object v = params.get(k);
87
88 if (v instanceof String && ((String) v).startsWith("$")) {
89 v = context.get(v);
90
91 if ((v == null) && context.isStrictPassing()) {
92 return 1;
93 }
94 }
95
96 if (f != null) {
97 f.setParameter(k, v);
98 }
99
100 realParams.put(k, v);
101 }
102
103 return 0;
104 }
105
106 /***
107 * Prettyprints an object array to a String
108 *
109 * @param os
110 * the array to be formatted
111 *
112 * @return resulting String
113 */
114 public static String listArray(Object[] os) {
115 StringBuffer sb = new StringBuffer();
116
117 for (int i = 0; i < os.length; i++) {
118 if (i > 0) {
119 sb.append(", ");
120 }
121
122 sb.append(os[i]);
123 }
124
125 return sb.toString();
126 }
127
128 /***
129 * As listArray but for Lists
130 *
131 * @param m
132 * the List to be formatted
133 * @return the resulting String
134 */
135 public static String listList(List m) {
136 StringBuffer sb = new StringBuffer();
137
138 for (Iterator i = m.iterator(); i.hasNext();) {
139 sb.append(i.next());
140
141 if (i.hasNext()) {
142 sb.append(", ");
143 }
144 }
145
146 return sb.toString();
147 }
148
149 /***
150 * Prettyprints a Map
151 *
152 * @param m
153 * the Map
154 * @return the resulting String
155 */
156 public static String listMap(Map m) {
157 StringBuffer sb = new StringBuffer();
158
159 for (Iterator i = m.entrySet().iterator(); i.hasNext();) {
160 Map.Entry e = (Map.Entry) i.next();
161 Object k = e.getKey();
162 Object v = e.getValue();
163
164 sb.append(k + "=" + v);
165
166 if (i.hasNext()) {
167 sb.append(", ");
168 }
169 }
170
171 return sb.toString();
172 }
173 }
174
175
176
177
178
179
180
181
182
183
184