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: import com.opensymphony.workflow.util.XMLizable;
010:
011: import org.w3c.dom.Element;
012: import org.w3c.dom.Node;
013: import org.w3c.dom.NodeList;
014:
015: import java.io.PrintWriter;
016:
017: import java.util.ArrayList;
018: import java.util.List;
019:
020: /**
021: * Date: May 12, 2004
022: * Time: 9:04:25 AM
023: *
024: * @author hani
025: */
026: public class ConditionsDescriptor extends AbstractDescriptor implements
027: Validatable {
028: //~ Instance fields ////////////////////////////////////////////////////////
029:
030: private List conditions = new ArrayList();
031: private String type;
032:
033: //~ Constructors ///////////////////////////////////////////////////////////
034:
035: /**
036: * @deprecated use {@link DescriptorFactory} instead
037: */
038: ConditionsDescriptor() {
039: }
040:
041: /**
042: * @deprecated use {@link DescriptorFactory} instead
043: */
044: ConditionsDescriptor(Element element) {
045: type = element.getAttribute("type");
046:
047: NodeList children = element.getChildNodes();
048: int size = children.getLength();
049:
050: for (int i = 0; i < size; i++) {
051: Node child = children.item(i);
052:
053: if (child instanceof Element) {
054: if ("condition".equals(child.getNodeName())) {
055: ConditionDescriptor condition = DescriptorFactory
056: .getFactory().createConditionDescriptor(
057: (Element) child);
058: conditions.add(condition);
059: } else if ("conditions".equals(child.getNodeName())) {
060: ConditionsDescriptor condition = DescriptorFactory
061: .getFactory().createConditionsDescriptor(
062: (Element) child);
063: conditions.add(condition);
064: }
065: }
066: }
067: }
068:
069: //~ Methods ////////////////////////////////////////////////////////////////
070:
071: public void setConditions(List conditions) {
072: this .conditions = conditions;
073: }
074:
075: public List getConditions() {
076: return conditions;
077: }
078:
079: public void setType(String type) {
080: this .type = type;
081: }
082:
083: public String getType() {
084: return type;
085: }
086:
087: public void validate() throws InvalidWorkflowDescriptorException {
088: ValidationHelper.validate(conditions);
089:
090: if (conditions.size() == 0) {
091: AbstractDescriptor desc = getParent();
092:
093: if ((desc != null)
094: && (desc instanceof ConditionalResultDescriptor)) {
095: throw new InvalidWorkflowDescriptorException(
096: "Conditional result from "
097: + ((ActionDescriptor) desc.getParent())
098: .getName()
099: + " to "
100: + ((ConditionalResultDescriptor) desc)
101: .getDestination()
102: + " must have at least one condition");
103: }
104: }
105:
106: if ((conditions.size() > 1) && (type == null)) {
107: throw new InvalidWorkflowDescriptorException(
108: "Conditions must have AND or OR type specified");
109: }
110: }
111:
112: public void writeXML(PrintWriter out, int indent) {
113: if (conditions.size() > 0) {
114: XMLUtil.printIndent(out, indent++);
115:
116: StringBuffer sb = new StringBuffer("<conditions");
117:
118: if (conditions.size() > 1) {
119: sb.append(" type=\"").append(type).append('\"');
120: }
121:
122: sb.append('>');
123: out.println(sb.toString());
124:
125: for (int i = 0; i < conditions.size(); i++) {
126: XMLizable condition = (XMLizable) conditions.get(i);
127: condition.writeXML(out, indent);
128: }
129:
130: XMLUtil.printIndent(out, --indent);
131: out.println("</conditions>");
132: }
133: }
134: }
|