KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > loader > ActionDescriptor


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.loader;
6
7 import com.opensymphony.workflow.InvalidWorkflowDescriptorException;
8 import com.opensymphony.workflow.util.Validatable;
9
10 import org.w3c.dom.Element JavaDoc;
11 import org.w3c.dom.Node JavaDoc;
12 import org.w3c.dom.NodeList JavaDoc;
13
14 import java.io.PrintWriter JavaDoc;
15
16 import java.util.*;
17
18
19 /**
20  * @author <a HREF="mailto:plightbo@hotmail.com">Pat Lightbody</a>
21  * @version $Revision: 1.13 $
22  */

23 public class ActionDescriptor extends AbstractDescriptor implements Validatable {
24     //~ Instance fields ////////////////////////////////////////////////////////
25

26     protected List JavaDoc conditionalResults = new ArrayList();
27     protected List JavaDoc postFunctions = new ArrayList();
28     protected List JavaDoc preFunctions = new ArrayList();
29     protected List JavaDoc validators = new ArrayList();
30     protected Map metaAttributes = new HashMap();
31     protected RestrictionDescriptor restriction;
32     protected ResultDescriptor unconditionalResult;
33     protected String JavaDoc name;
34     protected String JavaDoc view;
35     protected boolean autoExecute = false;
36     protected boolean finish = false;
37
38     //~ Constructors ///////////////////////////////////////////////////////////
39

40     public ActionDescriptor() {
41     }
42
43     public ActionDescriptor(Element JavaDoc action) {
44         init(action);
45     }
46
47     //~ Methods ////////////////////////////////////////////////////////////////
48

49     public void setAutoExecute(boolean autoExecute) {
50         this.autoExecute = autoExecute;
51     }
52
53     public boolean getAutoExecute() {
54         return autoExecute;
55     }
56
57     public List JavaDoc getConditionalResults() {
58         return conditionalResults;
59     }
60
61     public void setFinish(boolean finish) {
62         this.finish = finish;
63     }
64
65     public boolean isFinish() {
66         return finish;
67     }
68
69     public void setMetaAttributes(Map metaAttributes) {
70         this.metaAttributes = metaAttributes;
71     }
72
73     public Map getMetaAttributes() {
74         return metaAttributes;
75     }
76
77     public void setName(String JavaDoc name) {
78         this.name = name;
79     }
80
81     public String JavaDoc getName() {
82         return name;
83     }
84
85     public List JavaDoc getPostFunctions() {
86         return postFunctions;
87     }
88
89     public List JavaDoc getPreFunctions() {
90         return preFunctions;
91     }
92
93     public void setRestriction(RestrictionDescriptor restriction) {
94         this.restriction = restriction;
95     }
96
97     public RestrictionDescriptor getRestriction() {
98         return restriction;
99     }
100
101     public void setUnconditionalResult(ResultDescriptor unconditionalResult) {
102         this.unconditionalResult = unconditionalResult;
103     }
104
105     public ResultDescriptor getUnconditionalResult() {
106         return unconditionalResult;
107     }
108
109     public List JavaDoc getValidators() {
110         return validators;
111     }
112
113     public void setView(String JavaDoc view) {
114         this.view = view;
115     }
116
117     public String JavaDoc getView() {
118         return view;
119     }
120
121     public String JavaDoc toString() {
122         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
123
124         if (name != null) {
125             sb.append(name);
126         }
127
128         if ((view != null) && (view.length() > 0)) {
129             sb.append(" (").append(view).append(")");
130         }
131
132         return sb.toString();
133     }
134
135     public void validate() throws InvalidWorkflowDescriptorException {
136         ValidationHelper.validate(preFunctions);
137         ValidationHelper.validate(postFunctions);
138         ValidationHelper.validate(validators);
139         ValidationHelper.validate(conditionalResults);
140
141         if (restriction != null) {
142             restriction.validate();
143         }
144
145         if (unconditionalResult != null) {
146             unconditionalResult.validate();
147         }
148     }
149
150     public void writeXML(PrintWriter JavaDoc out, int indent) {
151         XMLUtil.printIndent(out, indent++);
152
153         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("<action id=\"");
154         buf.append(getId());
155         buf.append("\"");
156
157         if ((name != null) && (name.length() > 0)) {
158             buf.append(" name=\"");
159             buf.append(name);
160             buf.append("\"");
161         }
162
163         if ((view != null) && (view.length() > 0)) {
164             buf.append(" view=\"");
165             buf.append(view);
166             buf.append("\"");
167         }
168
169         if (finish) {
170             buf.append(" finish=\"true\"");
171         }
172
173         if (autoExecute) {
174             buf.append(" auto=\"true\"");
175         }
176
177         buf.append(">");
178         out.println(buf.toString());
179
180         Iterator iter = metaAttributes.entrySet().iterator();
181
182         while (iter.hasNext()) {
183             Map.Entry entry = (Map.Entry) iter.next();
184             XMLUtil.printIndent(out, indent);
185             out.print("<meta name=\"");
186             out.print(entry.getKey());
187             out.print("\">");
188             out.print(entry.getValue());
189             out.println("</meta>");
190         }
191
192         if (restriction != null) {
193             restriction.writeXML(out, indent);
194         }
195
196         if (validators.size() > 0) {
197             XMLUtil.printIndent(out, indent++);
198             out.println("<validators>");
199
200             for (int i = 0; i < validators.size(); i++) {
201                 ValidatorDescriptor validator = (ValidatorDescriptor) validators.get(i);
202                 validator.writeXML(out, indent);
203             }
204
205             XMLUtil.printIndent(out, --indent);
206             out.println("</validators>");
207         }
208
209         if (preFunctions.size() > 0) {
210             XMLUtil.printIndent(out, indent++);
211             out.println("<pre-functions>");
212
213             for (int i = 0; i < preFunctions.size(); i++) {
214                 FunctionDescriptor function = (FunctionDescriptor) preFunctions.get(i);
215                 function.writeXML(out, indent);
216             }
217
218             XMLUtil.printIndent(out, --indent);
219             out.println("</pre-functions>");
220         }
221
222         XMLUtil.printIndent(out, indent++);
223         out.println("<results>");
224
225         for (int i = 0; i < conditionalResults.size(); i++) {
226             ConditionalResultDescriptor result = (ConditionalResultDescriptor) conditionalResults.get(i);
227             result.writeXML(out, indent);
228         }
229
230         if (unconditionalResult != null) {
231             unconditionalResult.writeXML(out, indent);
232         }
233
234         XMLUtil.printIndent(out, --indent);
235         out.println("</results>");
236
237         if (postFunctions.size() > 0) {
238             XMLUtil.printIndent(out, indent++);
239             out.println("<post-functions>");
240
241             for (int i = 0; i < postFunctions.size(); i++) {
242                 FunctionDescriptor function = (FunctionDescriptor) postFunctions.get(i);
243                 function.writeXML(out, indent);
244             }
245
246             XMLUtil.printIndent(out, --indent);
247             out.println("</post-functions>");
248         }
249
250         XMLUtil.printIndent(out, --indent);
251         out.println("</action>");
252     }
253
254     protected void init(Element JavaDoc action) {
255         try {
256             setId(Integer.parseInt(action.getAttribute("id")));
257         } catch (Exception JavaDoc ex) {
258             throw new IllegalArgumentException JavaDoc("Invalid action id value " + action.getAttribute("id"));
259         }
260
261         this.name = action.getAttribute("name");
262         this.view = action.getAttribute("view");
263         this.autoExecute = "true".equals(action.getAttribute("auto"));
264         this.finish = "true".equals(action.getAttribute("finish"));
265
266         NodeList JavaDoc children = action.getChildNodes();
267
268         for (int i = 0; i < children.getLength(); i++) {
269             Node JavaDoc child = (Node JavaDoc) children.item(i);
270
271             if (child.getNodeName().equals("meta")) {
272                 Element JavaDoc meta = (Element JavaDoc) child;
273                 String JavaDoc value = XMLUtil.getText(meta);
274                 this.metaAttributes.put(meta.getAttribute("name"), value);
275             }
276         }
277
278         // set up validators - OPTIONAL
279
Element JavaDoc v = XMLUtil.getChildElement(action, "validators");
280
281         if (v != null) {
282             List JavaDoc validators = XMLUtil.getChildElements(v, "validator");
283
284             for (int k = 0; k < validators.size(); k++) {
285                 Element JavaDoc validator = (Element JavaDoc) validators.get(k);
286                 ValidatorDescriptor validatorDescriptor = new ValidatorDescriptor(validator);
287                 validatorDescriptor.setParent(this);
288                 this.validators.add(validatorDescriptor);
289             }
290         }
291
292         // set up pre-functions - OPTIONAL
293
Element JavaDoc pre = XMLUtil.getChildElement(action, "pre-functions");
294
295         if (pre != null) {
296             List JavaDoc preFunctions = XMLUtil.getChildElements(pre, "function");
297
298             for (int k = 0; k < preFunctions.size(); k++) {
299                 Element JavaDoc preFunction = (Element JavaDoc) preFunctions.get(k);
300                 FunctionDescriptor functionDescriptor = new FunctionDescriptor(preFunction);
301                 functionDescriptor.setParent(this);
302                 this.preFunctions.add(functionDescriptor);
303             }
304         }
305
306         // set up results - REQUIRED
307
Element JavaDoc resultsElememt = XMLUtil.getChildElement(action, "results");
308         List JavaDoc results = XMLUtil.getChildElements(resultsElememt, "result");
309
310         for (int k = 0; k < results.size(); k++) {
311             Element JavaDoc result = (Element JavaDoc) results.get(k);
312             ConditionalResultDescriptor conditionalResultDescriptor = new ConditionalResultDescriptor(result);
313             conditionalResultDescriptor.setParent(this);
314             this.conditionalResults.add(conditionalResultDescriptor);
315         }
316
317         Element JavaDoc unconditionalResult = XMLUtil.getChildElement(resultsElememt, "unconditional-result");
318         this.unconditionalResult = new ResultDescriptor(unconditionalResult);
319         this.unconditionalResult.setParent(this);
320
321         // set up post-functions - OPTIONAL
322
Element JavaDoc post = XMLUtil.getChildElement(action, "post-functions");
323
324         if (post != null) {
325             List JavaDoc postFunctions = XMLUtil.getChildElements(post, "function");
326
327             for (int k = 0; k < postFunctions.size(); k++) {
328                 Element JavaDoc postFunction = (Element JavaDoc) postFunctions.get(k);
329                 FunctionDescriptor functionDescriptor = new FunctionDescriptor(postFunction);
330                 functionDescriptor.setParent(this);
331                 this.postFunctions.add(functionDescriptor);
332             }
333         }
334
335         // set up restrict-to - OPTIONAL
336
Element JavaDoc restrictElement = XMLUtil.getChildElement(action, "restrict-to");
337
338         if (restrictElement != null) {
339             restriction = new RestrictionDescriptor(restrictElement);
340
341             if (restriction.getConditions().size() == 0) {
342                 restriction = null;
343             } else {
344                 restriction.setParent(this);
345             }
346         }
347     }
348 }
349
Popular Tags