001    /*
002     * Copyright 1999-2001,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */ 
016    
017    package org.apache.commons.workflow.core;
018    
019    
020    import java.util.EmptyStackException;
021    import org.apache.commons.workflow.Block;
022    import org.apache.commons.workflow.BlockState;
023    import org.apache.commons.workflow.Context;
024    import org.apache.commons.workflow.Descriptor;
025    import org.apache.commons.workflow.Step;
026    import org.apache.commons.workflow.StepException;
027    import org.apache.commons.workflow.base.BaseBlock;
028    
029    
030    /**
031     * <p>Evaluate properties specified by the associated Descriptors, and
032     * execute the nested Steps if and only if <strong>ANY</strong> of them
033     * evaluate to a positive result.  To avoid non-deterministic evaluation
034     * stack behavior, all of the specified Descriptors are always
035     * evaluated exactly once.</p>
036     *
037     * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
038     * @author Craig R. McClanahan
039     */
040    
041    public class IfAnyStep extends IfStep {
042    
043    
044        // ----------------------------------------------------------= Constructors
045    
046    
047        /**
048         * Construct a default instance of this Step.
049         */
050        public IfAnyStep() {
051    
052            super();
053    
054        }
055    
056    
057        /**
058         * Construct an instance of this Step with the specified identifier.
059         *
060         * @param id Step identifier
061         */
062        public IfAnyStep(String id) {
063    
064            this(id, null);
065    
066        }
067    
068    
069        /**
070         * Construct a fully configured instance of this Step.
071         *
072         * @param id Step identifier of this step
073         * @param descriptor Initial descriptor to be added
074         */
075        public IfAnyStep(String id, Descriptor descriptor) {
076    
077            super();
078            setId(id);
079            if (descriptor != null)
080                addDescriptor(descriptor);
081    
082        }
083    
084    
085        // --------------------------------------------------------- Public Methods
086    
087    
088        /**
089         * Render a string representation of this Step.
090         */
091        public String toString() {
092    
093            StringBuffer sb = new StringBuffer("<core:ifAny");
094            if (getId() != null) {
095                sb.append(" id=\"");
096                sb.append(getId());
097                sb.append("\"");
098            }
099            sb.append(">");
100            Descriptor descriptors[] = findDescriptors();
101            for (int i = 0; i < descriptors.length; i++)
102                sb.append(descriptors[i]);
103            Step steps[] = getSteps();
104            for (int i = 0; i < steps.length; i++)
105                sb.append(steps[i]);
106            sb.append("</core:ifAny>");
107            return (sb.toString());
108    
109        }
110    
111    
112        // ------------------------------------------------------ Protected Methods
113    
114    
115        /**
116         * Evaluate the condition specified by the Descriptors associated with
117         * this Block, and return the resulting boolean value.
118         *
119         * @param context Context within which to evaluate the descriptors
120         */
121        protected boolean evaluate(Context context) {
122    
123            boolean condition = false;
124            Descriptor descriptors[] = findDescriptors();
125            for (int i = 0; i < descriptors.length; i++) {
126                if (descriptors[i].positive(context))
127                    condition = true;
128            }
129            return (condition);
130    
131        }
132    
133    
134    }