KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 import java.io.PrintWriter JavaDoc;
13
14 import java.util.*;
15
16
17 /**
18  * DOCUMENT ME!
19  *
20  * @author $author$
21  * @version $Revision: 1.7 $
22  */

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

26     protected Map args = new HashMap();
27
28     /**
29      * The name field helps the editor identify the condition template used.
30      */

31     protected String JavaDoc name;
32     protected String JavaDoc type;
33     protected boolean negate = false;
34
35     //~ Constructors ///////////////////////////////////////////////////////////
36

37     public ConditionDescriptor() {
38     }
39
40     public ConditionDescriptor(Element JavaDoc function) {
41         init(function);
42     }
43
44     //~ Methods ////////////////////////////////////////////////////////////////
45

46     public Map getArgs() {
47         return args;
48     }
49
50     public void setName(String JavaDoc name) {
51         this.name = name;
52     }
53
54     public String JavaDoc getName() {
55         return name;
56     }
57
58     public void setNegate(boolean negate) {
59         this.negate = negate;
60     }
61
62     public boolean isNegate() {
63         return negate;
64     }
65
66     public void setType(String JavaDoc type) {
67         this.type = type;
68     }
69
70     public String JavaDoc getType() {
71         return type;
72     }
73
74     public void validate() throws InvalidWorkflowDescriptorException {
75     }
76
77     public void writeXML(PrintWriter JavaDoc out, int indent) {
78         XMLUtil.printIndent(out, indent++);
79         out.println("<condition " + (hasId() ? ("id=\"" + getId() + "\" ") : "") + (((name != null) && (name.length() > 0)) ? ("name=\"" + getName() + "\" ") : "") + (negate ? ("negate=\"true\" ") : "") + "type=\"" + type + "\">");
80
81         Iterator iter = args.entrySet().iterator();
82
83         while (iter.hasNext()) {
84             Map.Entry entry = (Map.Entry) iter.next();
85             XMLUtil.printIndent(out, indent);
86             out.print("<arg name=\"");
87             out.print(entry.getKey());
88             out.print("\">");
89
90             if ("beanshell".equals(type) || "bsf".equals(type)) {
91                 out.print("<![CDATA[");
92                 out.print(entry.getValue());
93                 out.print("]]>");
94             } else {
95                 out.print(XMLUtil.encode(entry.getValue()));
96             }
97
98             out.println("</arg>");
99         }
100
101         XMLUtil.printIndent(out, --indent);
102         out.println("</condition>");
103     }
104
105     protected void init(Element JavaDoc condition) {
106         type = condition.getAttribute("type");
107
108         try {
109             setId(Integer.parseInt(condition.getAttribute("id")));
110         } catch (NumberFormatException JavaDoc e) {
111         }
112
113         String JavaDoc n = condition.getAttribute("negate");
114
115         if ("true".equalsIgnoreCase(n) || "yes".equalsIgnoreCase(n)) {
116             negate = true;
117         } else {
118             negate = false;
119         }
120
121         if (condition.getAttribute("name") != null) {
122             name = condition.getAttribute("name");
123         }
124
125         List args = XMLUtil.getChildElements(condition, "arg");
126
127         for (int l = 0; l < args.size(); l++) {
128             Element JavaDoc arg = (Element JavaDoc) args.get(l);
129             this.args.put(arg.getAttribute("name"), XMLUtil.getText(arg));
130         }
131     }
132 }
133
Popular Tags