package weaver.interfaces.htsc.job.biz;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.*;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by K1810006 on 2020/11/24.
 */
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class RulesExpression {

    @XmlElementWrapper
    @XmlElement(name = "expression")
    List<Expression> rulesExpression;

    public List<Expression> getRulesExpression() {
        return rulesExpression;
    }

    public void setRulesExpression(List<Expression> rulesExpression) {
        this.rulesExpression = rulesExpression;
    }


    /**
     * 将对象直接转换成String类型的 XML输出
     *
     * @param obj
     * @return
     */
    public static String convertToXml(Object obj) {
        // 创建输出流
        StringWriter sw = new StringWriter();
        try {
            // 利用jdk中自带的转换类实现
            JAXBContext context = JAXBContext.newInstance(obj.getClass());

            Marshaller marshaller = context.createMarshaller();
            // 格式化xml输出的格式
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            // 将对象转换成输出流形式的xml
            marshaller.marshal(obj, sw);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return sw.toString();
    }


    public static void main(String[] args) {
        try{
            Expression exp1 = new Expression();
            Expression exp2 = new Expression();
            List exps = new ArrayList();
            exps.add(exp1);
            exps.add(exp2);

            RulesExpression rulesExp = new RulesExpression();
            rulesExp.setRulesExpression(exps);

            String xml = convertToXml(rulesExp);
            System.out.println(xml);

        }catch (Exception e){


        }

    }
}
