<workflow name ="hello workflow">
<activities>
<start name="start" id="0001" nextIds="0002,003">
</start>
<task name="task" id="0002" nextIds="0004">
</task>
<task name="task1" id="0003" nextIds="0004">
</task>
<join name="join" id="0004" nextIds="0005">
</join>
<end name="end" id="0005" nextIds="">
</end>
</activities>
</workflow>
<digester-rules>
<pattern value="workflow">
<object-create-rule
classname="com.itcast.workflow.model.Workflow"/>
<set-properties-rule />
<set-next-rule methodname="setWorkflow" />
<pattern value="activities/start">
<object-create-rule
classname="com.itcast.workflow.model.Start"/>
<set-properties-rule />
<set-next-rule methodname="addActivity" />
</pattern>
<pattern value="activities/task">
<object-create-rule
classname="com.itcast.workflow.model.Task"/>
<set-properties-rule />
<set-next-rule methodname="addActivity" />
</pattern>
<pattern value="activities/join">
<object-create-rule
classname="com.itcast.workflow.model.Join"/>
<set-properties-rule />
<set-next-rule methodname="addActivity" />
</pattern>
<pattern value="activities/end">
<object-create-rule
classname="com.itcast.workflow.model.End"/>
<set-properties-rule />
<set-next-rule methodname="addActivity" />
</pattern>
</pattern>
</digester-rules>
import java.io.StringReader;
import java.util.List;
import org.apache.commons.digester.xmlrules.DigesterLoader;
import org.xml.sax.SAXException;
import com.itcast.workflow.model.Workflow;
this.workflow = workflow;
}
this.xml = xml;
}
public void startRead(){
try
{
// 创建digester对象并指定解析规则
Digester d = DigesterLoader.createDigester(rulesURL);
//回调机制
d.parse(new StringReader(xml)); //弹出栈
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
catch(SAXException se)
{
se.printStackTrace();
}
buildRelation();
}
private void buildRelation() {//建立关联
List activities = workflow.getActivities();
for(int i=0;i<activities.size();i++){
Activity activity = (Activity )activities.get(i);
String nextIds = activity.getNextIds();
if(nextIds != null){
String[] ids = nextIds.split(",");
for(int j=0;j<ids.length;j++){
Activity next = getActivityById(ids[j]);
if(next != null){
activity.addNext(next);
}
}
}
}
}
private Activity getActivityById(String id) {
Activity activity = null;
List activities = workflow.getActivities();
for(int i=0;i<activities.size();i++){
Activity a = (Activity )activities.get(i);
if(a.getId().equals(id)){
activity = a;
break;
}
}
return activity;
}
Workflow workflow = new Workflow();
return workflow;
}