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.ArrayList;
015: import java.util.List;
016:
017: /**
018: * @author <a href="mailto:plightbo@hotmail.com">Pat Lightbody</a>
019: */
020: public class RestrictionDescriptor extends AbstractDescriptor implements
021: Validatable {
022: //~ Instance fields ////////////////////////////////////////////////////////
023:
024: protected List conditions = new ArrayList();
025:
026: //~ Constructors ///////////////////////////////////////////////////////////
027:
028: public RestrictionDescriptor() {
029: }
030:
031: public RestrictionDescriptor(Element restriction) {
032: init(restriction);
033: }
034:
035: //~ Methods ////////////////////////////////////////////////////////////////
036:
037: /**
038: * @deprecated A restrict-to can only have one conditions element,
039: * please use {@link #getConditionsDescriptor()} instead.
040: */
041: public List getConditions() {
042: return conditions;
043: }
044:
045: public void setConditionsDescriptor(ConditionsDescriptor descriptor) {
046: if (conditions.size() == 1) {
047: conditions.set(0, descriptor);
048: } else {
049: conditions.add(descriptor);
050: }
051: }
052:
053: public ConditionsDescriptor getConditionsDescriptor() {
054: if (conditions.size() == 0) {
055: return null;
056: }
057:
058: return (ConditionsDescriptor) conditions.get(0);
059: }
060:
061: public void validate() throws InvalidWorkflowDescriptorException {
062: if (conditions.size() > 1) {
063: throw new InvalidWorkflowDescriptorException(
064: "A restrict-to element can only have one conditions child element");
065: }
066:
067: ValidationHelper.validate(conditions);
068: }
069:
070: public void writeXML(PrintWriter out, int indent) {
071: ConditionsDescriptor conditions = getConditionsDescriptor();
072:
073: List list = conditions.getConditions();
074:
075: if (list.size() == 0) {
076: return;
077: }
078:
079: XMLUtil.printIndent(out, indent++);
080: out.println("<restrict-to>");
081: conditions.writeXML(out, indent);
082: XMLUtil.printIndent(out, --indent);
083: out.println("</restrict-to>");
084: }
085:
086: protected void init(Element restriction) {
087: // set up condition - OPTIONAL
088: List conditionNodes = XMLUtil.getChildElements(restriction,
089: "conditions");
090: int length = conditionNodes.size();
091:
092: for (int i = 0; i < length; i++) {
093: Element condition = (Element) conditionNodes.get(i);
094: ConditionsDescriptor conditionDescriptor = DescriptorFactory
095: .getFactory().createConditionsDescriptor(condition);
096: conditionDescriptor.setParent(this);
097: this.conditions.add(conditionDescriptor);
098: }
099: }
100: }
|