KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.opensymphony.workflow.util.XMLizable;
10
11 import org.w3c.dom.Element JavaDoc;
12 import org.w3c.dom.Node JavaDoc;
13 import org.w3c.dom.NodeList JavaDoc;
14
15 import java.io.PrintWriter JavaDoc;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19
20
21 /**
22  * Date: May 12, 2004
23  * Time: 9:04:25 AM
24  *
25  * @author hani
26  */

27 public class ConditionsDescriptor extends AbstractDescriptor implements Validatable {
28     //~ Instance fields ////////////////////////////////////////////////////////
29

30     private List JavaDoc conditions = new ArrayList JavaDoc();
31     private String JavaDoc type;
32
33     //~ Constructors ///////////////////////////////////////////////////////////
34

35     public ConditionsDescriptor() {
36     }
37
38     public ConditionsDescriptor(Element JavaDoc element) {
39         type = element.getAttribute("type");
40
41         NodeList JavaDoc children = element.getChildNodes();
42         int size = children.getLength();
43
44         for (int i = 0; i < size; i++) {
45             Node JavaDoc child = children.item(i);
46
47             if (child instanceof Element JavaDoc) {
48                 if ("condition".equals(child.getNodeName())) {
49                     ConditionDescriptor condition = new ConditionDescriptor((Element JavaDoc) child);
50                     conditions.add(condition);
51                 } else if ("conditions".equals(child.getNodeName())) {
52                     ConditionsDescriptor condition = new ConditionsDescriptor((Element JavaDoc) child);
53                     conditions.add(condition);
54                 }
55             }
56         }
57     }
58
59     //~ Methods ////////////////////////////////////////////////////////////////
60

61     public void setConditions(List JavaDoc conditions) {
62         this.conditions = conditions;
63     }
64
65     public List JavaDoc getConditions() {
66         return conditions;
67     }
68
69     public void setType(String JavaDoc type) {
70         this.type = type;
71     }
72
73     public String JavaDoc getType() {
74         return type;
75     }
76
77     public void validate() throws InvalidWorkflowDescriptorException {
78         ValidationHelper.validate(conditions);
79
80         if ((conditions.size() > 1) && (type == null)) {
81             throw new InvalidWorkflowDescriptorException("Conditions must have AND or OR type specified");
82         }
83     }
84
85     public void writeXML(PrintWriter JavaDoc out, int indent) {
86         if (conditions.size() > 0) {
87             XMLUtil.printIndent(out, indent++);
88
89             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<conditions");
90
91             if (conditions.size() > 1) {
92                 sb.append(" type=\"").append(type).append("\"");
93             }
94
95             sb.append(">");
96             out.println(sb.toString());
97
98             for (int i = 0; i < conditions.size(); i++) {
99                 XMLizable condition = (XMLizable) conditions.get(i);
100                 condition.writeXML(out, indent);
101             }
102
103             XMLUtil.printIndent(out, --indent);
104             out.println("</conditions>");
105         }
106     }
107 }
108
Popular Tags