package com.unis.crk.utils;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

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

/**
 * @program: crk
 * @Date: 2023-08-03 13:01
 * @Author: TangSong
 * @Description:
 */
public class Xml {

    private Document doc;
    private Element node;
    private Element root;


    public Element getRoot() {
        return root;
    }
    public void setRoot(Element root) {
        this.root = root;
    }

    public Xml(String xml)
    {
        StringReader read=new StringReader(xml);
        //创建新的输入源SAX   解析器将使用   InputSource   对象来确定如何读取   XML   输入
        InputSource source=new InputSource(read);
        //创建一个新的SAXBuilder
        SAXBuilder sb=new SAXBuilder();
        //通过输入源构造一个Document
        try
        {
            doc=sb.build(source);
            //取的根元素
            root=doc.getRootElement();
        }catch (Exception e) {
            e.printStackTrace();
            doc=null;
        }
    }
    public Xml(FileInputStream f)
    {
        SAXBuilder sb=new SAXBuilder();// 建立构造器
        try {
            doc=sb.build(f);
            root=doc.getRootElement();// 获得根节点
        } catch (JDOMException e) {
            doc=null;
            e.printStackTrace();
        } catch (IOException e) {
            doc=null;
            e.printStackTrace();
        }// 读入指定文件

    }

    public boolean initElement(String Name)
    {
        try
        {
            node=root.getChild(Name);//root.getElementsByTagName(Name);
            return true;
        }
        catch(Exception er)
        {
            return false;
        }
    }
    public int getCount()
    {
        if(node==null)
            return root.getChildren().size();
        else
            return node.getChildren().size();
    }

    public String getAttribute(int i,String AttName)
    {
        Element e;
        if(node==null)
        {
            e=(Element)root.getChildren().get(i);// root.getChildNodes().item(i);
        }
        else
        {
            e=(Element)node.getChildren().get(i);
        }
        return e.getAttributeValue(AttName);
    }

    public String getText(int i)
    {
        String tmp="";
        if(node==null)
        {
            tmp=((Element)root.getChildren().get(i)).getValue();
            if(tmp==null)tmp="";
            return tmp;
        }
        else
        {
            tmp=((Element)node.getChildren().get(i)).getValue();
            if(tmp==null)tmp="";
            return tmp;
        }
    }
    public Element getElement(int i)
    {
        Element tmp=null;
        if(node==null)
        {
            tmp=(Element)root.getChildren().get(i);
            if(tmp==null)tmp=null;
            return tmp;
        }
        else
        {
            tmp=(Element)node.getChildren().get(i);
            if(tmp==null)tmp=null;
            return tmp;
        }
    }
    public String getText(String name)
    {
        List lt=root.getChildren();
        for(int i=0;i<lt.size();i++)
        {
            Element item=(Element)lt.get(i);
            if(item.getName().equals(name))
            {
                return item.getValue();
            }
        }
        return "";
    }
    public Map<String,String> getText()
    {
        Map<String,String> map = new HashMap<String, String>();
        List lt=root.getChildren();
        for(int i=0;i<lt.size();i++)
        {
            Element item=(Element)lt.get(i);
            map.put(item.getAttributeValue("id").toUpperCase(), item.getTextTrim());
        }
        return map;
    }

    public String getText(String RootName,String name)
    {
        List lt=root.getChildren();
        for(int i=0;i<lt.size();i++)
        {
            Element item=(Element)lt.get(i);
            if(item.getName().equals(RootName))
            {
                List sublt=item.getChildren();
                for(int j=0;j<sublt.size();j++)
                {
                    Element subitem=(Element)sublt.get(j);
                    if(subitem.getName().equals(name))
                    {
                        return subitem.getValue();
                    }
                }
            }
        }
        return "";
    }

}
