JbpmUtil.java

package com.huike.leave.service.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class JbpmUtil {

private SAXReader reader = new SAXReader();  
private Document document = null;
private Element rootElement = null;
private Element childElement = null;
private Attribute attribute = null;
private String attribute_value = null;

private String node_name_property = null;

public JbpmUtil(String filepath){
this.parseXML(filepath);
this.getRootElement();
}

public void parseXML(String filepath) {
try {
document = reader.read(new File(filepath));
if(filepath == null)
p("SAXReader解析时出现异常,可能指定路径下的文件不存在");
} catch (DocumentException e) {
p("SAXReader解析时出现异常,可能指定路径下的文件不存在");
e.printStackTrace();
}  
}

public Element getRootElement(){
rootElement = document.getRootElement();
return rootElement;
}

public String getRootElementKey(){
return rootElement.attributeValue("name");
}
// public  String getRootElementKey(String attributeName) {
// return  rootElement.attributeValue(attributeName);
// }

// public Element getProcessChildElement(String nodeName){
// childElement = rootElement.element(nodeName);
// return childElement;
// }
// @SuppressWarnings({ "rawtypes" })
// public List<Element> getChildElementAsList(Element parentElement){
// List<Element> elementAsList = new ArrayList<Element>();
// for(Iterator it=parentElement.elementIterator();it.hasNext();)
// {  
// childElement = (Element) it.next();
// elementAsList.add(childElement);
// }
// return elementAsList;  
// }

/**
* 获取根节点下第一层子节点指定的的属性名的对应值
* @param nodeName
* @param attributeName
* @return
* @throws Exception
*/
public String getProperty(String nodeName,String attributeName)throws Exception{
Element currentElement = rootElement.element(nodeName);
if(currentElement == null)
{
new Exception("文件中不存在指定的节点名");
System.out.println("文件中不存在指定的节点名");
}
else{
attribute = currentElement.attribute(attributeName);
}

    if(attribute == null)
    {
    System.out.println("指定的属性在此节点中不存在,请检查传入的属性名与节点名是否匹配");
    new Exception("指定的属性在此节点中不存在,请检查传入的节点名与属性名是否匹配");
    }
    else{
    attribute_value = attribute.getValue();
    return attribute_value;
    }
    return attribute_value;
}

// /**
// * 处理第二层子节点下只有一个元素
// */
// public String getProperty(String firstLevelNodeName,String firstLevelChildNodeName,String attributeName){
// Element start = rootElement.element(firstLevelNodeName);
// Element transition = start.element(firstLevelChildNodeName);
// Attribute a = transition.attribute(attributeName);
// return a.getValue();
// }
// /**
// * 处理第二层子节点下有多个元素
// */
// @SuppressWarnings({ "unchecked", "rawtypes" })
// public List getProperties (String firstLevelChildNodeName,String secondLevelChildNodeName,String attributeName){
// Element task = rootElement.element(firstLevelChildNodeName);
// List task_transitionAsList = task.elements(secondLevelChildNodeName);
// Element transition = null;
// Attribute attribute = null;
// String s = null;
// List list = new ArrayList();
// for(int i=0; i<task_transitionAsList.size();i++)
// {
// transition = (Element) task_transitionAsList.get(i);
// attribute = transition.attribute(attributeName);
// s = attribute.getText();
// list.add(s);
// }
// return list;
// }

// public String getNameProperty(String nodeName){
// Element currentElement = rootElement.element(nodeName);
//     attribute = currentElement.attribute("name");
//     attribute_value = attribute.getValue();
// return node_name_property;
// }

// @SuppressWarnings("rawtypes")
// public List getCurrentNodeInformation(String nodeName){
// return  rootElement.element(nodeName).attributes();
// }
/**
* 根据元素的name属性值获取对应的元素
* @param name
* @return
*/
@SuppressWarnings("rawtypes")
public Element getElementByNameAttributeValue(String name){
Iterator it = rootElement.elementIterator();
Element e = null;
Attribute a = null;
List<Element> el = new ArrayList<Element>();
while(it.hasNext())
{
e = (Element)it.next();
a = e.attribute("name");
if(a.getText().equals(name))
{
el.add(e);
if(el.size() == 1)
{
return el.get(0);
}
}
if(el.size()==0)
System.out.println("根元素下的子元素中存在具有 name属性值为"+name+"的元素");
}
return el.get(0);
}

// @SuppressWarnings("rawtypes")
// public String getTransitionToProperty(String currentActivityName, String decisionInfo){
// Element currentElment = this.getElementByNameAttributeValue(currentActivityName);
// List transitionAsList = currentElment.elements();
// Element e = null;
// String toValue = null ;
// for(int i=0; i<transitionAsList.size();i++)
// {
// e = (Element)transitionAsList.get(i);
// toValue = e.attribute("name").getText().toString();
// p(toValue);
// p(decisionInfo);
// if(decisionInfo.equals(toValue))
// {
// toValue = e.attribute("to").getText().toString();
// return toValue;
// }
// }
// return toValue;
// }
public static void p(Object o){
System.out.println(o.toString());
}
}

你可能感兴趣的:(java)