package com.zcxk.process.activiti;


import com.zcxk.process.activiti.model.SCTransferElement;
import org.dom4j.*;

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

public class SCElementParserHandler {
    private List<SCTransferElement> transferElements = new ArrayList<SCTransferElement>();
    SCTransferElement currentElement = null;

    public List<SCTransferElement> getTransferElements() {
        return transferElements;
    }

    // /process/extensionElements/transfer
    public void parserXML(String xmlStr) throws DocumentException {
        Document document = DocumentHelper.parseText(xmlStr);
        Element root = document.getRootElement();
        Element extensionElements = root.element("process").element("extensionElements");
        if(extensionElements != null){
            List<Element> elements = extensionElements.elements("transfer");
            for (Iterator<Element> it = elements.iterator(); it.hasNext(); ) {
                Element element = it.next();
                if (element.getName().equals("transfer")) {
                    currentElement = new SCTransferElement();
                } else continue;
                List<Attribute> attributes = element.attributes();
                for (int i = 0; i < attributes.size(); i++) {
                    Attribute attribute = attributes.get(i);
                    if ("id".equals(attribute.getName())) {
                        currentElement.setId(attribute.getText());
                    } else if ("taskRef".equals(attribute.getName())) {
                        currentElement.setTaskRef(attribute.getText());
                    } else if ("usersRange".equals(attribute.getName())) {
                        currentElement.setUsersRange(attribute.getText());
                    }
                }
                if (currentElement != null) {
                    transferElements.add(currentElement);
                    currentElement = null;
                }
            }
        }
    }
}