KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > xml > ActivityReader


1 package org.jbpm.bpel.xml;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import org.w3c.dom.Element JavaDoc;
7
8 import org.jbpm.bpel.data.def.Snippet;
9 import org.jbpm.bpel.def.Activity;
10 import org.jbpm.bpel.def.CompositeActivity;
11 import org.jbpm.bpel.def.Flow;
12 import org.jbpm.bpel.def.Link;
13 import org.jbpm.bpel.def.Pick;
14 import org.jbpm.bpel.def.Receive;
15 import org.jbpm.bpel.def.Sequence;
16 import org.jbpm.bpel.xml.util.NodeUtil;
17 import org.jbpm.jpdl.xml.Problem;
18
19 /**
20  * @author Juan Cantú
21  * @version $Revision: 1.9 $ $Date: 2005/06/23 20:45:04 $
22  */

23 public abstract class ActivityReader {
24
25   protected BpelReader bpelReader;
26   
27   /**
28    * Loads the activity properties from the given DOM element
29    */

30   public Activity read(Element JavaDoc element, CompositeActivity parent) {
31     //create new instance
32
Activity activity = createActivity();
33
34     //load standard properties
35
readStandardProperties(element, activity, parent);
36
37     //activity specific properties
38
try {
39       readActivity(activity, element);
40     }
41     catch(BpelException e) {
42       /* fatal exceptions that ocurr inside activity readers are caught
43        * at this point and transformed in errors. This allows continuing with
44        * the parsing of the rest of the bpel document.*/

45       bpelReader.getProblemHandler().add(
46           new Problem( Problem.LEVEL_ERROR, "bpel activity is invalid.", e ) );
47     }
48     
49     return activity;
50   }
51
52   /**
53    * creates an new instance of the bpel activity represented by the parser
54    */

55   protected abstract Activity createActivity();
56   
57   /**
58    * subclasses must override this method to load the activity-specific properties
59    * from the given DOM element
60    */

61   protected void readActivity(Activity activity, Element JavaDoc element) {
62     //nothing done
63
}
64
65   protected void readStandardProperties(Element JavaDoc elem, Activity activity, CompositeActivity parent) {
66     activity.setName( NodeUtil.getAttribute(elem, BpelConstants.ATTR_NAME) );
67     
68     //set parent
69
parent.addNode( activity );
70     
71     //load activity links
72
readSources(activity, elem);
73     readTargets(activity, elem);
74     
75     //when initial, validate that its either a "start activity" or it has
76
//a control flow dependency
77
if( activity.isInitial() ) {
78       Collection JavaDoc targets = activity.getTargets();
79       if(!(activity instanceof Sequence || activity instanceof Flow ||
80            activity instanceof Pick ||activity instanceof Receive) &&
81            (targets == null || targets.size() == 0)) {
82         bpelReader.getProblemHandler().add(
83             new LocalizedProblem( Problem.LEVEL_ERROR, "activity can't be initial", elem ) );
84       }
85     }
86     
87     //suppress join failure
88
Boolean JavaDoc supJoin = bpelReader.readTBoolean(
89         elem.getAttributeNode(BpelConstants.ATTR_SUPPRESS_JOIN_FAILURE), null);
90     activity.setSuppressJoinFailure(supJoin);
91   }
92   
93   private void readSources(Activity activity, Element JavaDoc element) {
94     Element JavaDoc sourcesElem = NodeUtil.getElement(element, BpelConstants.NS_BPWS, BpelConstants.ELEM_SOURCES);
95     
96     if (sourcesElem != null) {
97       CompositeActivity parent = activity.getCompositeActivity();
98       Iterator JavaDoc sourceElemIt = NodeUtil.getElements(sourcesElem, BpelConstants.NS_BPWS, BpelConstants.ELEM_SOURCE);
99       while (sourceElemIt.hasNext()) {
100         Element JavaDoc sourceElem = (Element JavaDoc) sourceElemIt.next();
101         String JavaDoc linkName = sourceElem.getAttribute(BpelConstants.ATTR_LINK_NAME);
102         Link link = parent.findLink(linkName);
103         Element JavaDoc transitionElem = NodeUtil.getElement(sourceElem, BpelConstants.NS_BPWS, BpelConstants.ELEM_TRANSITION_CONDITION);
104         if(transitionElem != null) {
105           link.setTransitionCondition(bpelReader.readExpression(transitionElem, parent));
106         }
107         activity.addSource(link);
108       }
109     }
110   }
111   
112   private void readTargets(Activity activity, Element JavaDoc activityElement) {
113     Element JavaDoc targetsElem = NodeUtil.getElement(activityElement, BpelConstants.NS_BPWS, BpelConstants.ELEM_TARGETS);
114     if (targetsElem != null) {
115       CompositeActivity parent = activity.getCompositeActivity();
116       // targets
117
Iterator JavaDoc targetElemIt = NodeUtil.getElements(targetsElem, BpelConstants.NS_BPWS, BpelConstants.ELEM_TARGET);
118       while (targetElemIt.hasNext()) {
119         Element JavaDoc targetElem = (Element JavaDoc) targetElemIt.next();
120         String JavaDoc linkName = targetElem.getAttribute(BpelConstants.ATTR_LINK_NAME);
121         activity.addTarget(parent.findLink(linkName));
122       }
123       // join condition
124
Element JavaDoc conditionElem = NodeUtil.getElement(targetsElem, BpelConstants.NS_BPWS, BpelConstants.ELEM_JOIN_CONDITION);
125       if(conditionElem != null) {
126         activity.setJoinCondition(bpelReader.readExpression(conditionElem, parent,
127             Snippet.Use.JOIN_CONDITION));
128       }
129     }
130   }
131   
132   public BpelReader getBpelReader() {
133     return bpelReader;
134   }
135   
136   public void setBpelReader(BpelReader bpelReader) {
137     this.bpelReader = bpelReader;
138   }
139 }
Popular Tags