📄 bpeldocument.java
字号:
package org.bpel.model.document;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.bpel.model.node.*;
import org.bpel.model.parameter.*;
import org.dom4j.*;
import org.dom4j.io.*;
/*
* 根据模型节点生成BPEL文档
* @author jackkp
*
*/
public class BPELDocument {
//BPEL 主要由三部分组成:variables, partnerLinks, sequence;
//So we hava to produce these parts individually;
public BPELDocument() {
this.doc = null;
this.childNode = null;
}
public void createDocument() {
doc = DocumentHelper.createDocument();
Element proRoot = DocumentHelper.createElement("process");
doc.setRootElement(proRoot);
//还需要考虑当没有子节点时,或者整个bpel文档没有partnerLinks和variables子节点时,
//就没有必要生成这些节点
//往根节点中添加partnerLinks子节点和variables子节点
//create an element named "partnerLinks"
Element partLinkEle = proRoot.addElement("partnerLink");
Element variableEle = proRoot.addElement("variables");
//when the list of partnerLinks has next element;
ModelNode curNode;
if (childNode.size() <= 0) {
System.out.println("There is no childnode in the list");
return;
}
for (int i = 1; i <= childNode.size(); i++) {
curNode = childNode.get(i);
//add an new subelement named "partnerLink"
Element parEle = partLinkEle.addElement("partnerLink");
//add "name", "partnerLinkType" attributes to the partnerLink
PartnerLink pl = curNode.getPartnerLinkName();
VariablePara vp = curNode.getVariables();
if (pl.getPartnerLink() != null) {
parEle.addAttribute("name",
pl.getPartnerLink());
parEle.addAttribute("partnerLinkType",
pl.getPartnerLinkType());
}
//if "myRole" attribute isn't null;
if (pl.getMyRole() != null) {
//add "myRole" attribute to the partnerLink
parEle.addAttribute("myRole",
pl.getMyRole());
}
//if "partner" attribute isn't null;
if (pl.getPartnerRole() != null) {
//add "partnerRole" attribute to the partnerLink
parEle.addAttribute("partnerRole",
pl.getPartnerRole());
}
//Now process the variables subelement;
if (vp.getInputVariable() != null) {
Element inputEle = variableEle.addElement("variable");
inputEle.addAttribute("name", vp.getInputVariable());
inputEle.addAttribute("messageType", vp.getInputVarMT());
}
if (vp.getOutputVariable() != null) {
Element outputEle = variableEle.addElement("variable");
outputEle.addAttribute("name", vp.getOutputVariable());
outputEle.addAttribute("messageType", vp.getOutputVarMT());
}
if (vp.getVariableForMyRole() != null) {
Element myRoleVarEle = variableEle.addElement("variable");
myRoleVarEle.addAttribute("name", vp.getVariableForMyRole());
myRoleVarEle.addAttribute("messageType", vp.getVarForMyRoleMT());
}
//end when
}
//往根节点中添加variables子节点
//when the list of variables has next element
//add an new subelement named "variable"
//add "name", "messageType" attributes to the variable element
//end when
//往根节点中添加sequence子节点
//以下的处理算法需要依据模型生成的数据结构
//处理这个部分是必须用递归算法
//create an element named "sequence"
Element sequenceRoot = proRoot.addElement("sequence");
}
/*
* 递归生成文档
* 广度优先算法
*/
public void createSequence(ModelNode curNode, Element parEle){
//用递归算法生成表示流程部分代码
//the procession should be begin from the cotainer node
//begin from the first node of the list
//if node's partnerRole isn's null;
if (!curNode.isContainer()) {
if (curNode.getPartnerLinkName().getPartnerRole()!= null) {
//create "invoke" element
Element childInvoke = parEle.addElement("invoke");
//add "partnerLink", "portType",
childInvoke.addAttribute("partenerLink",
curNode.getPartnerLinkName().getPartnerLink());
childInvoke.addAttribute("portType",
curNode.getPartnerLinkName().getPortType());
//"operation", "inputVariable", "outputVariable" attributes;
childInvoke.addAttribute("operation",
curNode.getPartnerLinkName().getOperation());
childInvoke.addAttribute("inputVariable",
curNode.getVariables().getInputVariable());
childInvoke.addAttribute("outputVariable",
curNode.getVariables().getOutputVariable());
//if sourceOrTarget isn't null;
if (curNode.getLink().getLinkName() != null) {
//add "source" or "target" element;
Element link = childInvoke.addElement(curNode.getLink().isSourceOrTarget(),
curNode.getLink().getLinkName());
//add "linkName" attribute attributes
link.addAttribute("linkName",
curNode.getLink().getLinkName());
}
}
//if node's myRole is'n null;
if (curNode.getPartnerLinkName().getMyRole()!= null) {
//create "receive" element
Element childReceive = parEle.addElement("receive");
//add "partnerLink", "portType", "operation", "variable" attributes
childReceive.addAttribute("partnerLink",
curNode.getPartnerLinkName().getPartnerLink());
childReceive.addAttribute("portType",
curNode.getPartnerLinkName().getPortType());
childReceive.addAttribute("operation",
curNode.getPartnerLinkName().getOperation());
childReceive.addAttribute("variable",
curNode.getVariables().getVariableForMyRole());
if (curNode.getLink().getLinkName() != null) {
//add "source" or "target" element;
Element link = childReceive.addElement(curNode.getLink().isSourceOrTarget(),
curNode.getLink().getLinkName());
//add "linkName" attribute attributes
link.addAttribute("linkName",
curNode.getLink().getLinkName());
}
}
if (curNode.getRefNode().getNextNode() != null) {
createSequence(curNode.getRefNode().getNextNode(), parEle);
}
}else {
Element child = parEle.addElement(curNode.getName());
List<AttriSet> attList = curNode.getAttriList();
int size = attList.size();
for (int i = 1; i <= size; i++) {
String name = attList.get(i).getName();
String value = attList.get(i).getValue();
child.addAttribute(name, value);
}
int num;
if ((num = curNode.getRefNode().getChildNode().size()) != 0) {
for (int i = 1; i < num; i++) {
createSequence(curNode.getRefNode());
}
}
}
//if node's child isn't null
//createSequence(node's child, nodeEle)
//add this node to the its parentElement
//if node's next node isn't null;
//createSequence(node's nextNode, parentNode);
//end function
}
public void outputDocument() {
try{
XMLWriter xmlWriter = new XMLWriter();
if (doc == null) {
System.out.println("There is no child");
}
xmlWriter.write(doc);
}catch(IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(String args[]) {
BPELDocument pd = new BPELDocument();
pd.outputDocument();
}
private StartNode start;
private EndNode end;
private ArrayList<ModelNode> childNode;//包括开始和结束节点
private Link links;
private PartnerLink partnerLinks;
private VariablePara variables;
private Document doc;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -