View Javadoc

1   /*
2    * Copyright 1999-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package org.apache.commons.workflow.core;
18  
19  
20  import java.util.EmptyStackException;
21  import org.apache.commons.workflow.Block;
22  import org.apache.commons.workflow.BlockState;
23  import org.apache.commons.workflow.Context;
24  import org.apache.commons.workflow.Descriptor;
25  import org.apache.commons.workflow.Step;
26  import org.apache.commons.workflow.StepException;
27  import org.apache.commons.workflow.base.BaseBlock;
28  
29  
30  /**
31   * <p>Evaluate properties specified by the associated Descriptors, and
32   * execute the nested Steps if and only if <strong>ANY</strong> of them
33   * evaluate to a positive result.  To avoid non-deterministic evaluation
34   * stack behavior, all of the specified Descriptors are always
35   * evaluated exactly once.</p>
36   *
37   * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
38   * @author Craig R. McClanahan
39   */
40  
41  public class IfAnyStep extends IfStep {
42  
43  
44      // ----------------------------------------------------------= Constructors
45  
46  
47      /**
48       * Construct a default instance of this Step.
49       */
50      public IfAnyStep() {
51  
52          super();
53  
54      }
55  
56  
57      /**
58       * Construct an instance of this Step with the specified identifier.
59       *
60       * @param id Step identifier
61       */
62      public IfAnyStep(String id) {
63  
64          this(id, null);
65  
66      }
67  
68  
69      /**
70       * Construct a fully configured instance of this Step.
71       *
72       * @param id Step identifier of this step
73       * @param descriptor Initial descriptor to be added
74       */
75      public IfAnyStep(String id, Descriptor descriptor) {
76  
77          super();
78          setId(id);
79          if (descriptor != null)
80              addDescriptor(descriptor);
81  
82      }
83  
84  
85      // --------------------------------------------------------- Public Methods
86  
87  
88      /**
89       * Render a string representation of this Step.
90       */
91      public String toString() {
92  
93          StringBuffer sb = new StringBuffer("<core:ifAny");
94          if (getId() != null) {
95              sb.append(" id=\"");
96              sb.append(getId());
97              sb.append("\"");
98          }
99          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 }