package com.huike.leave.service.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.jbpm.api.Configuration;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
import org.jbpm.api.task.Task;
import org.jbpm.pvm.internal.model.Activity;
import org.jbpm.pvm.internal.model.ExecutionImpl;
import org.jbpm.pvm.internal.model.ProcessDefinitionImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ParseXmlFile {
DocumentBuilderFactory documentBuilderFactory = null;
DocumentBuilder documentBuilder = null;
Document doc = null;
InputStream inputStream = null;
Element rootElement = null;
NodeList childNodes = null;
Node childNodes_item = null;
List<Node> first_layer_child_node_AsList = new ArrayList<Node>();
StringBuffer all_untreated_nodeNames_As_StringBuffer = new StringBuffer();
String[] nodeTypeNameAsArray = null;
List<String> first_layer_child_node_name_property_AsList = new ArrayList<String>();
List<String> first_layer_child_node_g_property_AsList = new ArrayList<String>();
List<String> first_layer_child_node_to_property_AsList = new ArrayList<String>();
List<String> transition_to_AsLsit = new ArrayList<String>();
List<String> transition_name_AsLsit = new ArrayList<String>();
Map<String,List<String>> transition_containt_to_name = new HashMap<String,List<String>>();
List<String> task_assignee_AsList = new ArrayList<String>();
List<String> decision_expr_AsList = new ArrayList<String>();
List<String> end_state_AsList = new ArrayList<String>();
String key = null;
/**
* 设计存放jpdl.xml文件中各节点信息及其对应关系的集合
* @throws ParserConfigurationException
* @throws SAXException
*/
List<Map<String,String>> process = new ArrayList<Map<String,String>>();
Map<String,Map<String,String>> process_task = new HashMap<String,Map<String,String>>();
Map<String,String> task = new HashMap<String,String>();
Map<String,String> decision = new HashMap<String,String>();
Map<String,String> transition = new HashMap<String,String>();
Map<String,String> transition_to = new HashMap<String,String>();
public ParseXmlFile(){
// this.ParseXmlFile();
}
/**
* 创建DocumentBuilderFactory(文档构建工厂)实例, 用于创建设DocumentBuilder(文档构建对象)
*/
public ParseXmlFile(String filePath) throws ParserConfigurationException, SAXException {
documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
documentBuilder = documentBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
e1.printStackTrace();
}
/**
* 获取要解析的jpdl.xml文件的输入流对象
*/
try {
inputStream = new FileInputStream(filePath);
} catch (FileNotFoundException e) {
new Exception("jpdl.xml file not found!");
e.printStackTrace();
}
/**
* 利用DocumentBuilder(文档构建对象)从jpdl.xml文件中解析出Document(文档对象)
*/
try {
doc = documentBuilder.parse(inputStream);
} catch (IOException e) {
new Exception("parse xml error!");
e.printStackTrace();
}
}
/**
* 获取根节点
* @return
* @throws SAXException
*/
public Element getRootElement() throws SAXException {
rootElement = doc.getDocumentElement();
if (rootElement.getAttribute("name") != null) {
key = rootElement.getAttribute("name");
System.out.println("<process name=\" "
+ rootElement.getAttribute("name")
+ "\" xmlns=\"http://jbpm.org/4.0/jpdl\">"); // 输出流程定义的key值
}
return rootElement;
}
/**
* 生成一个存放process节点下的第一层所有子节点的节点名的字符串数组 遍历所有节点
* ,并把各个节点的类型保存到一个数组中,方法其它方法调用(仅作测试用)
*
* @return String[] strArray 各个节点的类型,不包括根节点(process),一般是从start
* 开始,也可能从on开始
*/
/**
* 获取根元素下的子节点
*/
public String[] getChildNodesNameAsStringArray(Element rootElement) {
childNodes = rootElement.getChildNodes();
if (childNodes != null) {
for (int i = 0; i < childNodes.getLength(); i++) {
childNodes_item = childNodes.item(i);
first_layer_child_node_AsList.add(childNodes_item);
all_untreated_nodeNames_As_StringBuffer.append(childNodes_item
.getNodeName()); // 每遍历出一个节点类型名则附加到一起,因为此时遍历出的类型名包含了#text.....
// 进入第一层节点的处理,如start task decision end
if (childNodes_item.getNodeType() == Node.ELEMENT_NODE) {
}
}
nodeTypeNameAsArray = all_untreated_nodeNames_As_StringBuffer
.toString().split("#text");// 分离出含#text的类型名,并存入一个字符串数组
}
return nodeTypeNameAsArray;
}
/**
* 获取指定节点的属性值
*
* @return nodePropertyMap
* @param String
* nodeName
* @param Element
* rootElement
*/
public Map<String, String> getNodePropertyAsMap(String nodeName,
Element rootElement) {
Map<String, String> map = new HashMap<String, String>();
childNodes = rootElement.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
childNodes_item = childNodes.item(i);
String currentNodeName = null;
if(i%2 != 0)
{
currentNodeName = childNodes_item.getNodeName();
System.out.println(childNodes_item.getNodeName());
}
// currentNodeName = childNodes_item.getNodeName().split("#text")[0];
// System.out.println("currentNodeName" + i + currentNodeName);
if (childNodes_item.getNodeType() == Node.ELEMENT_NODE) {
if (currentNodeName.equals(nodeName)) {
// 获取start task decision 和节点name属性值
if (childNodes_item.getAttributes().getNamedItem("name") != null) {
first_layer_child_node_name_property_AsList
.add(childNodes_item.getAttributes()
.getNamedItem("name").getNodeValue());
System.out.println(" childNodes_item name = "
+ childNodes_item.getAttributes()
.getNamedItem("name").getNodeValue());
map.put("name", childNodes_item.getAttributes()
.getNamedItem("name").getNodeValue());
}
// 获取start task decision 和节点to属性值
if (childNodes_item.getAttributes().getNamedItem("to") != null) {
first_layer_child_node_to_property_AsList
.add(childNodes_item.getAttributes()
.getNamedItem("to").getNodeValue());
System.out.println(" childNodes_item to = "
+ childNodes_item.getAttributes()
.getNamedItem("to").getNodeValue());
map.put("to", childNodes_item.getAttributes()
.getNamedItem("to").getNodeValue());
}
// 获取task节点assignee属性值
if (childNodes_item.getAttributes()
.getNamedItem("assignee") != null) {
task_assignee_AsList.add(childNodes_item
.getAttributes().getNamedItem("assignee")
.getNodeValue());
System.out.println(" task_assignee_AsList assignee = "
+ childNodes_item.getAttributes()
.getNamedItem("assignee")
.getNodeValue());
map.put("assignee", childNodes_item.getAttributes()
.getNamedItem("assignee").getNodeValue());
}
// 获取decision节点expr属性值
if (childNodes_item.getAttributes().getNamedItem("expr") != null) {
decision_expr_AsList.add(childNodes_item
.getAttributes()
.getNamedItem("expr")
.getNodeValue()
.substring(
2,
childNodes_item.getAttributes()
.getNamedItem("expr")
.getNodeValue().length() - 1));
System.out.println(" decision_expr_AsList expr= "
+ childNodes_item
.getAttributes()
.getNamedItem("expr")
.getNodeValue()
.substring(
2,
childNodes_item.getAttributes()
.getNamedItem("expr")
.getNodeValue()
.length() - 1));
map.put("expr", childNodes_item.getAttributes()
.getNamedItem("expr").getNodeValue());
}
// 获取end节点state属性值
if (childNodes_item.getAttributes().getNamedItem("state") != null) {
end_state_AsList.add(childNodes_item
.getAttributes()
.getNamedItem("state")
.getNodeValue()
.substring(
2,
childNodes_item.getAttributes()
.getNamedItem("state")
.getNodeValue().length() - 1));
System.out.println(" end_state_AsList state= "
+ childNodes_item
.getAttributes()
.getNamedItem("state")
.getNodeValue()
.substring(
2,
childNodes_item.getAttributes()
.getNamedItem("state")
.getNodeValue()
.length() - 1));
}
// 获取 transition
// 节点的属性,从childNodes_item节点的第一个子节点开始,遍历其兄弟节点(Sibling),并取其to属性和name属性的值
for (Node node = childNodes_item.getFirstChild(); node != null; node = node
.getNextSibling()) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
if (node.getNodeName().equals("transition")) {
// 获取 transition 节点的to属性的值
String transition_to = node.getAttributes()
.getNamedItem("to").getNodeValue();
transition_to_AsLsit.add(transition_to);
// 获取 transition 节点的name属性的值
String transition_name = node.getAttributes()
.getNamedItem("name").getNodeValue();
transition_name_AsLsit.add(transition_name);
}
}
transition_containt_to_name.put("to",
transition_to_AsLsit);
transition_containt_to_name.put("name",
transition_name_AsLsit);
}
}
}
}
return map;
}
public static void main(String args[]) throws ParserConfigurationException, SAXException{
// new ParseXmlFile("build/classes/com/huike/leave/service/Order.jpdl.xml");
ParseXmlFile pxf = new ParseXmlFile("build/classes/com/huike/leave/service/Order.jpdl.xml");
// pxf.getNodePropertyAsMap("start", pxf.getRootElement());
// pxf.getNodePropertyAsMap("task", pxf.getRootElement());
}
// public void parseXmlFile() throws ParserConfigurationException, SAXException {
// ProcessEngine processEngine = new Configuration().buildProcessEngine();
// RepositoryService repositoryService = processEngine
// .getRepositoryService();
// ExecutionService executionService = processEngine.getExecutionService();
// TaskService taskService = processEngine.getTaskService();
//
// /**
// * 发布流程定义:获得流程定义deploymentId
// */
// String deploymentId = repositoryService
// .createDeployment()
// .addResourceFromClasspath(
// "com/huike/leave/service/Order.jpdl.xml").deploy();
//
// /**
// * 获得流程定义的ID
// */
// String id = repositoryService.createProcessDefinitionQuery()
// .deploymentId(deploymentId).uniqueResult().getId();
//
// /**
// * 获取jpdl.xml文件中process节点的name属性的值,即得到该流程的key,通过该key即可启动新流程
// * 注:但此方法对于开始之后执行任务的中要求动态分配所有者时,在启动时无法确定分配人的key,
// * 即不能确定要传的Map对象,如不传则无法启动成功 if(task_assignee_AsList.size()!=0)
// * System.out.println(task_assignee_AsList.get(0).substring(2,
// * task_assignee_AsList.get(0).length()-1));
// *
// * String owner = task_assignee_AsList.get(0).substring(2,
// * task_assignee_AsList.get(0).length()-1);
// *
// * Map<String,Object> map = new HashMap<String,Object>(); map.put(owner,
// * "testOwner"); ProcessInstance processInstance =
// * executionService.startProcessInstanceByKey(key,map);
// */
//
// /**
// * 启动流程实例(注:在jpdl.xml文件中,如果任务要求动态绑定所有者,则不能根据id直接启动流程实例
// */
// if (task_assignee_AsList.size() != 0)
// System.out.println(task_assignee_AsList.get(0).substring(2,
// task_assignee_AsList.get(0).length() - 1));
//
// String assignee = task_assignee_AsList.get(0).substring(2,
// task_assignee_AsList.get(0).length() - 1);
//
// /**
// * 获取
// */
// String decider = "manager";
// String decision_transition_name = "是";
// String true_or_no = decision_transition_name;
//
// Map<String, Object> map = new HashMap<String, Object>();
// map.put(assignee, "testOwner");
// map.put("time", new Date());
// map.put("leaveDay", 5);
// map.put("content", "test");
// map.put("position", "经理");
//
// map.put(decider, true_or_no);
// // map.put("manager", "是");
// // map.put("position", "普通员工");
// // map.put("manager", "否");
//
// ProcessInstance processInstance = executionService
// .startProcessInstanceByKey(key, map);
// List<Task> taskList = taskService.findPersonalTasks((String) map
// .get(assignee));
//
// if (taskList.size() > 0) {
// Task task = taskList.get(0);
// taskService.setVariables(task.getId(), map);
// taskService.completeTask(task.getId());
// System.out.println("申请任务完成,流程根据判断转到经理审核或老板审批");
// } else
// new Exception("apply error!");
//
// /**
// * 获取jbpm中各节点的类型
// */
// ExecutionImpl executionImpl = (ExecutionImpl) processInstance;
// ProcessDefinitionImpl processDefinitionImpl = executionImpl
// .getProcessDefinition();
// Map<String, Activity> activitiesMap = processDefinitionImpl
// .getActivitiesMap();
// Set<String> activitiesKeySet = activitiesMap.keySet();
//
// for (String str : activitiesKeySet) {
// System.out.println(activitiesMap.get(str).getType()
// + activitiesMap.get(str).getParent()
// + activitiesMap.get(str).getName());
// }
// }
}