⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hpdlparser.java

📁 java实现的可配置的工作流引擎,采用jsp+javabean实现
💻 JAVA
字号:
package com.hongsoft.agile.hpdl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.hongsoft.agile.xmlParser.XMLParserException;
import com.hongsoft.agile.xmlParser.XmlFilterNull;

/**
 * @author Administrator To change the template for this generated type comment go to Window>Preferences>Java>Code
 *         Generation>Code and Comments
 */
public class HpdlParser {
    private long lastModified = 0;

    private Document doc;

    private ProcessesObject po;

    public HpdlParser(String file) throws HpdlException {
        try {
            SAXBuilder builder = new SAXBuilder();
            XmlFilterNull format = new XmlFilterNull();
            builder.setXMLFilter(format);
            doc = builder.build(new File(file));
        } catch (Exception e) {
            e.printStackTrace();
            throw new HpdlException("打开流程定义文件失败!");
        }
    }

    /**
     * @return
     */
    public long getLastModified() {
        return lastModified;
    }

    /**
     * @param l
     */
    public void setLastModified(long l) {
        lastModified = l;
    }

    /**
     * 将整个HPDL文件解析为一个PackageObject对象
     *
     * @return
     */
    public ProcessesObject parsePackage() throws HpdlException {
        ProcessesObject po = new ProcessesObject();
        Element e = doc.getRootElement();
        if (e == null)
            throw new HpdlException("流程定义必须以processes作为根元素!");
        List list = e.getChildren();
        for (int i = 0; i < list.size(); i++) {
            Element process = (Element) list.get(i);
            po.getProcessObjectList().add(parseProcess(process));

        }
        // 向本对象中加入packageObject对象
        setPo(po);
        return po;
    }

    /**
     * 解析其中的一个流程
     *
     * @param process
     * @return
     * @throws XMLParserException
     */
    public ProcessObject parseProcess(Element process) throws HpdlException {
        boolean isThereStart = false;
        boolean isThereEnd = false;
        Map activityMap = new HashMap();
        ProcessObject po = new ProcessObject();
        if (process.getAttributeValue("id") == null || process.getAttributeValue("name") == null)
            throw new HpdlException("process的id和name属性必须定义!");
        else {
            po.setProcessDefId(process.getAttributeValue("id"));
            po.setProcessName(process.getAttributeValue("name"));
        }
        if (process.getAttributeValue("parentProcessId") == null)
            po.setParentProcessDefId("");
        else
            po.setParentProcessDefId(process.getAttributeValue("parentProcessId"));
        List list = process.getChildren();
        for (int i = 0; i < list.size(); i++) {
            Element activity = (Element) list.get(i);
            po.getActivityObjectList().add(parseActivity(process, activity));
            if ("start".equalsIgnoreCase(activity.getAttributeValue("id")))
                isThereStart = true;
            else if ("end".equalsIgnoreCase(activity.getAttributeValue("id")))
                isThereEnd = true;
            else if (activityMap.containsKey(activity.getAttributeValue("id")))
                throw new HpdlException("流程定义中的所有活动名称都不能重复!您可能重复定义了活动名称!");
            else
                activityMap.put(activity.getAttributeValue("id"), "");
        }
        if (isThereStart == false)
            throw new HpdlException(process.getAttributeValue("id") + "流程没有定义start活动!");
        if (isThereEnd == false)
            throw new HpdlException(process.getAttributeValue("id") + "流程没有定义end活动!");
        return po;
    }

    /**
     * 解析其中的一个活动
     *
     * @param process
     * @param activity
     * @return
     * @throws XMLParserException
     */
    public ActivityObject parseActivity(Element process, Element activity) throws HpdlException {
        ActivityObject ao = new ActivityObject();
        if (activity.getAttributeValue("id") == null || activity.getAttributeValue("name") == null)
            throw new HpdlException("activity的id和name属性必须定义!");
        else {
            ao.setActivityDefId(activity.getAttributeValue("id"));
            ao.setActivityDefName(activity.getAttributeValue("name"));
        }
        if (activity.getAttributeValue("correspondingSubProcessId") == null)
            ao.setCorrespondingSubProcessDefId("");
        else
            ao.setCorrespondingSubProcessDefId(activity.getAttributeValue("correspondingSubProcessId"));
        if (activity.getAttributeValue("joinType") == null)
            ao.setJoinType("and");
        else
            ao.setJoinType(activity.getAttributeValue("joinType"));
        List list = activity.getChildren();
        for (int i = 0; i < list.size(); i++) {
            Element condition = (Element) list.get(i);
            DriveTypeObject co = new DriveTypeObject();
            co.setId(condition.getAttributeValue("id"));
            String type = condition.getAttributeValue("drivedType");
            if (type == null || type.equalsIgnoreCase("auto"))
                co.setDrivedType("auto");
            else if (type.equalsIgnoreCase("manual"))
                co.setDrivedType("manual");
            else
                throw new HpdlException("drivedType只能配置为auto/manual,或者不配置!");
            ao.getNextActivityList().add(co);
            co = null;
        }
        ao.setPreActivityList(getPreActivityList(process, ao.getActivityDefId()));
        return ao;
    }

    /**
     * 获取一个活动的所有前置活动
     *
     * @param Process
     * @param activityId
     * @return
     * @throws XMLParserException
     */
    public List getPreActivityList(Element process, String activityId) throws HpdlException {
        List preList = new ArrayList();
        List activityList = process.getChildren();
        for (int i = 0; i < activityList.size(); i++) {
            Element activity = (Element) activityList.get(i);
            String fromId = activity.getAttributeValue("id");
            List conditionList = activity.getChildren();
            for (int j = 0; j < conditionList.size(); j++) {
                Element condition = (Element) conditionList.get(j);
                // 这个conditon是到本活动的condition
                if (activityId.equalsIgnoreCase(condition.getAttributeValue("id"))) {
                    DriveTypeObject co = new DriveTypeObject();
                    co.setId(fromId);
                    String type = condition.getAttributeValue("drivedType");
                    if (type == null || type.equalsIgnoreCase("auto"))
                        co.setDrivedType("auto");
                    else if (type.equalsIgnoreCase("manual"))
                        co.setDrivedType("manual");
                    else
                        throw new HpdlException("drivedType只能配置为auto/manual,或者不配置!");
                    preList.add(co);
                    co = null;
                }
                condition = null;
            }
            activity = null;
        }
        return preList;
    }

    /**
     * @return Returns the po.
     */
    public ProcessesObject getPo() {
        return po;
    }

    /**
     * @param po The po to set.
     */
    public void setPo(ProcessesObject po) {
        this.po = po;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -