KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > workflow > impl > WorkflowBuilder


1 /*
2  * Copyright 1999-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
18 /* $Id: WorkflowBuilder.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.workflow.impl;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28
29 import org.apache.lenya.workflow.Action;
30 import org.apache.lenya.workflow.Condition;
31 import org.apache.lenya.workflow.Event;
32 import org.apache.lenya.workflow.Workflow;
33 import org.apache.lenya.workflow.WorkflowException;
34 import org.apache.lenya.xml.DocumentHelper;
35 import org.apache.log4j.Category;
36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40
41 /**
42  * Utility class to build a workflow schema from a file.
43  */

44 public class WorkflowBuilder {
45
46     private static final Category log = Category.getInstance(WorkflowBuilder.class);
47
48     /**
49      * Ctor.
50      */

51     protected WorkflowBuilder() {
52     }
53
54     /**
55      * Builds a workflow schema from a file.
56      * @param file The file.
57      * @return A workflow schema implementation.
58      * @throws WorkflowException if the file does not represent a valid workflow schema.
59      */

60     public static WorkflowImpl buildWorkflow(File JavaDoc file) throws WorkflowException {
61         WorkflowImpl workflow;
62
63         try {
64             Document JavaDoc document = DocumentHelper.readDocument(file);
65             workflow = buildWorkflow(document);
66         } catch (Exception JavaDoc e) {
67             throw new WorkflowException(e);
68         }
69
70         return workflow;
71     }
72
73     /**
74      * Builds a workflow object from an XML document.
75      * @param document The XML document.
76      * @return A workflow implementation.
77      * @throws ParserConfigurationException when something went wrong.
78      * @throws SAXException when something went wrong.
79      * @throws IOException when something went wrong.
80      * @throws WorkflowException when something went wrong.
81      */

82     protected static WorkflowImpl buildWorkflow(Document JavaDoc document)
83         throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc, WorkflowException {
84
85         Element JavaDoc root = document.getDocumentElement();
86         StateImpl initialState = null;
87
88         Map JavaDoc states = new HashMap JavaDoc();
89         Map JavaDoc events = new HashMap JavaDoc();
90         Map JavaDoc variables = new HashMap JavaDoc();
91
92         // load states
93
NodeList JavaDoc stateElements = root.getElementsByTagNameNS(Workflow.NAMESPACE, STATE_ELEMENT);
94
95         for (int i = 0; i < stateElements.getLength(); i++) {
96             Element JavaDoc element = (Element JavaDoc) stateElements.item(i);
97             StateImpl state = buildState(element);
98             String JavaDoc id = state.getId();
99             states.put(id, state);
100
101             if (isInitialStateElement(element)) {
102                 initialState = state;
103             }
104         }
105
106         WorkflowImpl workflow = new WorkflowImpl(initialState);
107
108         // load variables
109
NodeList JavaDoc variableElements =
110             root.getElementsByTagNameNS(Workflow.NAMESPACE, VARIABLE_ELEMENT);
111
112         for (int i = 0; i < variableElements.getLength(); i++) {
113             Element JavaDoc element = (Element JavaDoc) variableElements.item(i);
114             BooleanVariableImpl variable = buildVariable(element);
115             variables.put(variable.getName(), variable);
116             workflow.addVariable(variable);
117         }
118
119         // load events
120
NodeList JavaDoc eventElements = root.getElementsByTagNameNS(Workflow.NAMESPACE, EVENT_ELEMENT);
121
122         for (int i = 0; i < eventElements.getLength(); i++) {
123             EventImpl event = buildEvent((Element JavaDoc) eventElements.item(i));
124             String JavaDoc id = event.getName();
125             events.put(id, event);
126             workflow.addEvent(event);
127         }
128
129         // load transitions
130
NodeList JavaDoc transitionElements =
131             root.getElementsByTagNameNS(Workflow.NAMESPACE, TRANSITION_ELEMENT);
132
133         for (int i = 0; i < transitionElements.getLength(); i++) {
134             TransitionImpl transition =
135                 buildTransition((Element JavaDoc) transitionElements.item(i), states, events, variables);
136             workflow.addTransition(transition);
137         }
138
139         return workflow;
140     }
141
142     /**
143      * Checks if a state element contains the initial state.
144      * @param element An XML element.
145      * @return A boolean value.
146      */

147     protected static boolean isInitialStateElement(Element JavaDoc element) {
148         String JavaDoc initialAttribute = element.getAttribute(INITIAL_ATTRIBUTE);
149
150         return (initialAttribute != null)
151             && (initialAttribute.equals("yes") || initialAttribute.equals("true"));
152     }
153
154     protected static final String JavaDoc STATE_ELEMENT = "state";
155     protected static final String JavaDoc TRANSITION_ELEMENT = "transition";
156     protected static final String JavaDoc EVENT_ELEMENT = "event";
157     protected static final String JavaDoc CONDITION_ELEMENT = "condition";
158     protected static final String JavaDoc ACTION_ELEMENT = "action";
159     protected static final String JavaDoc ID_ATTRIBUTE = "id";
160     protected static final String JavaDoc INITIAL_ATTRIBUTE = "initial";
161     protected static final String JavaDoc SOURCE_ATTRIBUTE = "source";
162     protected static final String JavaDoc DESTINATION_ATTRIBUTE = "destination";
163     protected static final String JavaDoc CLASS_ATTRIBUTE = "class";
164     protected static final String JavaDoc VARIABLE_ELEMENT = "variable";
165     protected static final String JavaDoc ASSIGNMENT_ELEMENT = "assign";
166     protected static final String JavaDoc VARIABLE_ATTRIBUTE = "variable";
167     protected static final String JavaDoc VALUE_ATTRIBUTE = "value";
168     protected static final String JavaDoc NAME_ATTRIBUTE = "name";
169     protected static final String JavaDoc SYNCHRONIZED_ATTRIBUTE = "synchronized";
170
171     /**
172      * Builds a state from an XML element.
173      * @param element An XML element.
174      * @return A state.
175      */

176     protected static StateImpl buildState(Element JavaDoc element) {
177         String JavaDoc id = element.getAttribute(ID_ATTRIBUTE);
178         StateImpl state = new StateImpl(id);
179
180         return state;
181     }
182
183     /**
184      * Builds a transition from an XML element.
185      * @param element An XML element.
186      * @param states A map from state IDs to states.
187      * @param events A map from event IDs to events.
188      * @param variables A map from variable names to variables.
189      * @return A transition.
190      * @throws WorkflowException when something went wrong.
191      */

192     protected static TransitionImpl buildTransition(
193         Element JavaDoc element,
194         Map JavaDoc states,
195         Map JavaDoc events,
196         Map JavaDoc variables)
197         throws WorkflowException {
198
199         if (log.isDebugEnabled()) {
200             log.debug("Building transition");
201         }
202
203         String JavaDoc sourceId = element.getAttribute(SOURCE_ATTRIBUTE);
204         String JavaDoc destinationId = element.getAttribute(DESTINATION_ATTRIBUTE);
205
206         StateImpl source = (StateImpl) states.get(sourceId);
207         StateImpl destination = (StateImpl) states.get(destinationId);
208
209         TransitionImpl transition = new TransitionImpl(source, destination);
210
211         // set event
212
Element JavaDoc eventElement =
213             (Element JavaDoc) element.getElementsByTagNameNS(Workflow.NAMESPACE, EVENT_ELEMENT).item(0);
214         String JavaDoc id = eventElement.getAttribute(ID_ATTRIBUTE);
215         Event event = (Event) events.get(id);
216         transition.setEvent(event);
217
218         if (log.isDebugEnabled()) {
219             log.debug(" Event: [" + event + "]");
220         }
221
222         // load conditions
223
NodeList JavaDoc conditionElements =
224             element.getElementsByTagNameNS(Workflow.NAMESPACE, CONDITION_ELEMENT);
225
226         for (int i = 0; i < conditionElements.getLength(); i++) {
227             Condition condition = buildCondition((Element JavaDoc) conditionElements.item(i));
228             transition.addCondition(condition);
229         }
230
231         // load assignments
232
NodeList JavaDoc assignmentElements =
233             element.getElementsByTagNameNS(Workflow.NAMESPACE, ASSIGNMENT_ELEMENT);
234
235         for (int i = 0; i < assignmentElements.getLength(); i++) {
236             BooleanVariableAssignmentImpl action =
237                 buildAssignment(variables, (Element JavaDoc) assignmentElements.item(i));
238             transition.addAction(action);
239         }
240
241         // load actions
242
NodeList JavaDoc actionElements =
243             element.getElementsByTagNameNS(Workflow.NAMESPACE, ACTION_ELEMENT);
244
245         for (int i = 0; i < actionElements.getLength(); i++) {
246             Action action = buildAction((Element JavaDoc) actionElements.item(i));
247             transition.addAction(action);
248         }
249
250         // set synchronization
251
if (element.hasAttribute(SYNCHRONIZED_ATTRIBUTE)) {
252             Boolean JavaDoc isSynchronized = Boolean.valueOf(element.getAttribute(SYNCHRONIZED_ATTRIBUTE));
253             transition.setSynchronized(isSynchronized.booleanValue());
254         }
255
256         return transition;
257     }
258
259     /**
260      * Builds an event from an XML element.
261      * @param element An XML element.
262      * @return An event.
263      */

264     protected static EventImpl buildEvent(Element JavaDoc element) {
265         String JavaDoc id = element.getAttribute(ID_ATTRIBUTE);
266         EventImpl event = new EventImpl(id);
267
268         return event;
269     }
270
271     /**
272      * Builds a condition from an XML element.
273      * @param element An XML element.
274      * @return A condition.
275      * @throws WorkflowException when something went wrong.
276      */

277     protected static Condition buildCondition(Element JavaDoc element) throws WorkflowException {
278         String JavaDoc className = element.getAttribute(CLASS_ATTRIBUTE);
279         String JavaDoc expression = DocumentHelper.getSimpleElementText(element);
280         Condition condition = ConditionFactory.createCondition(className, expression);
281
282         return condition;
283     }
284
285     /**
286      * Builds an action from an XML element.
287      * @param element An XML element.
288      * @return An action.
289      */

290     protected static Action buildAction(Element JavaDoc element) {
291         String JavaDoc id = element.getAttribute(ID_ATTRIBUTE);
292         Action action = new ActionImpl(id);
293
294         return action;
295     }
296
297     /**
298      * Builds a boolean variable from an XML element.
299      * @param element An XML element.
300      * @return A boolean variable.
301      */

302     protected static BooleanVariableImpl buildVariable(Element JavaDoc element) {
303         String JavaDoc name = element.getAttribute(NAME_ATTRIBUTE);
304         String JavaDoc value = element.getAttribute(VALUE_ATTRIBUTE);
305
306         return new BooleanVariableImpl(name, Boolean.getBoolean(value));
307     }
308
309     /**
310      * Builds an assignment object from an XML element.
311      * @param variables A map from variable names to variables.
312      * @param element An XML element.
313      * @return An assignment object.
314      * @throws WorkflowException when something went wrong.
315      */

316     protected static BooleanVariableAssignmentImpl buildAssignment(Map JavaDoc variables, Element JavaDoc element)
317         throws WorkflowException {
318         String JavaDoc variableName = element.getAttribute(VARIABLE_ATTRIBUTE);
319
320         String JavaDoc valueString = element.getAttribute(VALUE_ATTRIBUTE);
321         boolean value = Boolean.valueOf(valueString).booleanValue();
322
323         BooleanVariableImpl variable = (BooleanVariableImpl) variables.get(variableName);
324
325         return new BooleanVariableAssignmentImpl(variable, value);
326     }
327 }
328
Popular Tags