001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.workflow.loader;
006:
007: import com.opensymphony.workflow.InvalidWorkflowDescriptorException;
008: import com.opensymphony.workflow.util.Validatable;
009:
010: import org.w3c.dom.Element;
011:
012: import java.io.PrintWriter;
013:
014: import java.util.*;
015:
016: /**
017: * DOCUMENT ME!
018: */
019: public class ConditionDescriptor extends AbstractDescriptor implements
020: Validatable {
021: //~ Instance fields ////////////////////////////////////////////////////////
022:
023: protected Map args = new HashMap();
024:
025: /**
026: * The name field helps the editor identify the condition template used.
027: */
028: protected String name;
029: protected String type;
030: protected boolean negate = false;
031:
032: //~ Constructors ///////////////////////////////////////////////////////////
033:
034: /**
035: * @deprecated use {@link DescriptorFactory} instead
036: */
037: ConditionDescriptor() {
038: }
039:
040: /**
041: * @deprecated use {@link DescriptorFactory} instead
042: */
043: ConditionDescriptor(Element function) {
044: init(function);
045: }
046:
047: //~ Methods ////////////////////////////////////////////////////////////////
048:
049: public Map getArgs() {
050: return args;
051: }
052:
053: public void setName(String name) {
054: this .name = name;
055: }
056:
057: public String getName() {
058: return name;
059: }
060:
061: public void setNegate(boolean negate) {
062: this .negate = negate;
063: }
064:
065: public boolean isNegate() {
066: return negate;
067: }
068:
069: public void setType(String type) {
070: this .type = type;
071: }
072:
073: public String getType() {
074: return type;
075: }
076:
077: public void validate() throws InvalidWorkflowDescriptorException {
078: }
079:
080: public void writeXML(PrintWriter out, int indent) {
081: XMLUtil.printIndent(out, indent++);
082: out.println("<condition "
083: + (hasId() ? ("id=\"" + getId() + "\" ") : "")
084: + (((name != null) && (name.length() > 0)) ? ("name=\""
085: + getName() + "\" ") : "")
086: + (negate ? ("negate=\"true\" ") : "") + "type=\""
087: + type + "\">");
088:
089: Iterator iter = args.entrySet().iterator();
090:
091: while (iter.hasNext()) {
092: Map.Entry entry = (Map.Entry) iter.next();
093: XMLUtil.printIndent(out, indent);
094: out.print("<arg name=\"");
095: out.print(entry.getKey());
096: out.print("\">");
097:
098: if ("beanshell".equals(type) || "bsf".equals(type)) {
099: out.print("<![CDATA[");
100: out.print(entry.getValue());
101: out.print("]]>");
102: } else {
103: out.print(XMLUtil.encode(entry.getValue()));
104: }
105:
106: out.println("</arg>");
107: }
108:
109: XMLUtil.printIndent(out, --indent);
110: out.println("</condition>");
111: }
112:
113: protected void init(Element condition) {
114: type = condition.getAttribute("type");
115:
116: try {
117: setId(Integer.parseInt(condition.getAttribute("id")));
118: } catch (NumberFormatException e) {
119: }
120:
121: String n = condition.getAttribute("negate");
122:
123: if ("true".equalsIgnoreCase(n) || "yes".equalsIgnoreCase(n)) {
124: negate = true;
125: } else {
126: negate = false;
127: }
128:
129: if (condition.getAttribute("name") != null) {
130: name = condition.getAttribute("name");
131: }
132:
133: List args = XMLUtil.getChildElements(condition, "arg");
134:
135: for (int l = 0; l < args.size(); l++) {
136: Element arg = (Element) args.get(l);
137: this .args.put(arg.getAttribute("name"), XMLUtil
138: .getText(arg));
139: }
140: }
141: }
|