1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.workflow.base;
18
19
20 import java.util.EmptyStackException;
21 import org.apache.commons.jxpath.JXPathContext;
22 import org.apache.commons.workflow.Context;
23 import org.apache.commons.workflow.Descriptor;
24
25
26
27
28
29
30
31
32
33 public class BaseDescriptor implements Descriptor {
34
35
36
37
38
39
40
41
42 public BaseDescriptor() {
43
44 super();
45
46 }
47
48
49
50
51
52
53
54 public BaseDescriptor(String xpath) {
55
56 this(null, null, xpath, null);
57
58 }
59
60
61
62
63
64
65
66
67 public BaseDescriptor(String xpath, Class type) {
68
69 this(null, null, xpath, type);
70
71 }
72
73
74
75
76
77
78
79
80 public BaseDescriptor(String name, String scope) {
81
82 this(name, scope, null, null);
83
84 }
85
86
87
88
89
90
91
92
93
94 public BaseDescriptor(String name, String scope, Class type) {
95
96 this(name, scope, null, type);
97
98 }
99
100
101
102
103
104
105
106
107
108
109 public BaseDescriptor(String name, String scope,
110 String xpath, Class type) {
111
112 super();
113 setName(name);
114 setScope(scope);
115 setXpath(xpath);
116 setType(type);
117
118 }
119
120
121
122
123
124
125
126
127 protected String name = null;
128
129
130
131
132
133 protected String scope = null;
134
135
136
137
138
139 protected Class type = null;
140
141
142
143
144
145 protected String xpath = null;
146
147
148
149
150
151
152
153
154
155 public String getName() {
156
157 return (this.name);
158
159 }
160
161
162
163
164
165
166
167
168 public void setName(String name) {
169
170 this.name = name;
171
172 }
173
174
175
176
177
178 public String getScope() {
179
180 return (this.scope);
181
182 }
183
184
185
186
187
188
189
190 public void setScope(String scope) {
191
192 this.scope = scope;
193
194 }
195
196
197
198
199
200 public Class getType() {
201
202 return (this.type);
203
204 }
205
206
207
208
209
210
211
212 public void setType(Class type) {
213
214 this.type = type;
215
216 }
217
218
219
220
221
222
223 public String getXpath() {
224
225 return (this.xpath);
226
227 }
228
229
230
231
232
233
234
235
236 public void setXpath(String xpath) {
237
238 this.xpath = xpath;
239
240 }
241
242
243
244
245
246
247
248
249
250
251
252 public Object get(Context context) {
253
254 if (xpath != null) {
255 JXPathContext jpc = context.getJXPathContext();
256 jpc.setLenient(true);
257 return (jpc.getValue("local/" + xpath));
258 } else if (name != null) {
259 if (scope == null)
260 return (context.get(name));
261 else {
262 int scopeId = context.getScopeId(scope);
263 return (context.get(name, scopeId));
264 }
265 } else {
266 try {
267 return (context.pop());
268 } catch (EmptyStackException e) {
269 return (null);
270 }
271 }
272
273 }
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293 public boolean positive(Context context) {
294
295 Object value = get(context);
296 if (value == null)
297 return (false);
298 else if (value instanceof Boolean)
299 return (((Boolean) value).booleanValue());
300 else if (value instanceof Byte)
301 return (((Byte) value).byteValue() != (byte) 0);
302 else if (value instanceof Character)
303 return (((Character) value).charValue() != (char) 0);
304 else if (value instanceof Double)
305 return (((Double) value).doubleValue() != (double) 0.0);
306 else if (value instanceof Float)
307 return (((Double) value).floatValue() != (float) 0.0);
308 else if (value instanceof Integer)
309 return (((Integer) value).intValue() != (int) 0);
310 else if (value instanceof Long)
311 return (((Long) value).longValue() != (long) 0);
312 else if (value instanceof Short)
313 return (((Short) value).shortValue() != (short) 0);
314 else if (value instanceof String)
315 return (((String) value).length() > 0);
316 else
317 return (true);
318
319
320 }
321
322
323
324
325
326
327
328
329
330 public void put(Context context, Object value) {
331
332 if (xpath != null) {
333 JXPathContext jpc = context.getJXPathContext();
334 jpc.setValue("local/" + xpath, value);
335 } else {
336 if (scope == null)
337 context.put(name, value);
338 else {
339 int scopeId = context.getScopeId(scope);
340 context.put(name, value, scopeId);
341 }
342 }
343
344 }
345
346
347
348
349
350
351
352
353 public void remove(Context context) {
354
355 if (xpath != null) {
356 throw new IllegalStateException("Cannot 'remove' throw xpath");
357 } else {
358 if (scope == null)
359 context.remove(name);
360 else {
361 int scopeId = context.getScopeId(scope);
362 context.remove(name, scopeId);
363 }
364 }
365
366 }
367
368
369
370
371
372 public String toString() {
373
374 StringBuffer sb = new StringBuffer("<descriptor");
375 if (xpath != null) {
376 sb.append(" xpath=\"");
377 sb.append(xpath);
378 sb.append("\"");
379 }
380 if (name != null) {
381 sb.append(" name=\"");
382 sb.append(name);
383 sb.append("\"");
384 }
385 if (scope != null) {
386 sb.append(" scope=\"");
387 sb.append(scope);
388 sb.append("\"");
389 }
390 sb.append("/>");
391 return (sb.toString());
392
393 }
394
395
396 }