package com.ankaibei.workFlow.common.flow.util;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.jdom.Document;

import org.jdom.Element;

import org.jdom.JDOMException;

import org.jdom.Namespace;

import org.jdom.input.SAXBuilder;

import org.xml.sax.InputSource;

public class BpmnGetElement {

    public Map<String, String> getUserTasks(String xmlDoc) {
//创建一个新的字符串
        StringReader read = new StringReader(xmlDoc);
//创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
        InputSource source = new InputSource(read);
//创建一个新的SAXBuilder
        SAXBuilder sb = new SAXBuilder();
        try {
//通过输入源构造一个Document
            Document doc = sb.build(source);
//取的根元素
            Element root = doc.getRootElement();
            //输出根元素的名称
            System.out.println("根元素==:" + root.getName());
//得到根元素所有子元素的集合
            List jiedian = root.getChildren();
            //第一个子标签为内容流程
            Element process = (Element) jiedian.get(0);
            List processChildren = process.getChildren();
//获得XML中的命名空间（XML中未定义可不写）
            Map<String, String> params = new HashMap<>();
            Element et = null;
            for (int i = 0; i < processChildren.size(); i++) {
                //循环依次得到子元素
                et = (Element) processChildren.get(i);
                if (et.getName().equals("userTask")) {
                    String id = et.getAttribute("id").toString();
                    String name = et.getAttribute("name").toString();
                    String idString = id.substring(16, id.length() - 2);
                    String nameString = name.substring(18, name.length() - 2);
                    //[Attribute: name="提交申请"]
                    params.put(nameString, idString);
                }
            }
            /**//*
             * 如要取<row>下的子元素的名称
             */
            //et = (Element) jiedian.get(0);
            //List zjiedian = et.getChildren();
            //for (int j = 0; j < zjiedian.size(); j++) {
            //    Element xet = (Element) zjiedian.get(j);
            //    System.out.println(xet.getName());
            //}
            return params;
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
