dom4j

作者: hehehehe | 来源:发表于2021-01-31 10:43 被阅读0次
package com.ecarx.check.achievement;

import com.ecarx.check.model.IcsAchievementReport;
import com.ecarx.check.util.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * 类描述 <p>
 * @since 2020/4/13 14:37
 */
@SuppressWarnings("all")
public class AchieveCommonUtil {

    private static Logger logger = LoggerFactory.getLogger(AchieveCommonUtil.class);

    public static boolean checkLayerFieldMustExist(Element element, String field) throws BusinessException {
        if (element == null || StringUtils.isBlank(field)) {
            throw new BusinessException("数据参数有误");
        }
        Attribute attribute = element.attribute(field);
        if (attribute == null) {
            return false;
        } else {
            return true;
        }

    }

    /**
     * 字段值域检查: check_field_value(节点,字段,参数3,值域)
     * 值域类型:1-固定值域(暂未实现),2-范围值域  固定值域检查包含非空检查
     *
     * @param element minValue maxValue
     * @param field
     * @param param
     * @return
     */
    public static boolean checkFieldValue(Element element, String field, Map<String, Double> param) throws BusinessException {
        if (element == null || StringUtils.isBlank(field)) {
            throw new BusinessException("数据参数有误");
        }
        String attributeValue = element.attributeValue(field);
        if (StringUtils.isBlank(attributeValue)) {
            return false;
        }
        double minValue = param.get("minValue");
        double maxValue = param.get("maxValue");

        double value = Double.valueOf(attributeValue);
        if (value < maxValue && minValue < value) {
            return true;
        } else {
            return false;
        }

    }


    /**
     * 节点下存在节点检查:check_layer_child_exist(节点,“子节点”,参数3,个数)
     * <p>
     * 参数3定义:1-至少存在,2-只能存在,3-至多存在
     *
     * @param element
     * @param childField
     * @param type
     * @param num
     * @return
     */
    public static boolean checkLayerChildExist(Element element, String childField, int type, int num) throws BusinessException {
        if (element == null || StringUtils.isBlank(childField)) {
            throw new BusinessException("数据参数有误");
        }
        List<Node> nodes = element.selectNodes("OpenDRIVE:" + childField);
//        List<Node> nodes = element.selectNodes(childField);
        if (type == 1) {
            if (nodes.size() >= num) {
                return true;
            } else {
                return false;
            }
        } else if (type == 2) {
            if (nodes.size() == num) {
                return true;
            } else {
                return false;
            }
        } else if (type == 3) {
            if (nodes.size() <= num) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

    /**
     * 节点下存在节点检查:check_layer_child_exist(节点,“子节点”,参数3,个数)
     * <p>
     * 参数3定义:1-至少存在,2-只能存在,3-至多存在
     *
     * @param element
     * @param childField
     * @param type
     * @param num
     * @return
     */
    public static boolean checkLayerChildExist2(Element element, String childField, int type, int num) throws BusinessException {
        if (element == null || StringUtils.isBlank(childField)) {
            throw new BusinessException("数据参数有误");
        }
        List<Node> nodes = element.selectNodes(childField);
        if (type == 1) {
            if (nodes.size() >= num) {
                return true;
            } else {
                return false;
            }
        } else if (type == 2) {
            if (nodes.size() == num) {
                return true;
            } else {
                return false;
            }
        } else if (type == 3) {
            if (nodes.size() <= num) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }


    /**
     * 插件里报表创建
     *
     * @param chkId
     * @param errDesc
     * @param catalog
     * @param featureId
     * @return
     */
    public static IcsAchievementReport createReport(String chkId, String errDesc, String catalog, String featureId) {
        IcsAchievementReport icsAchievementReport = new IcsAchievementReport();
        icsAchievementReport.setErrorDesc(errDesc);
        icsAchievementReport.setCatalog(catalog);
        icsAchievementReport.setChkId(chkId);
        icsAchievementReport.setFeatureId(featureId);
        return icsAchievementReport;
    }

    public static Document xmlLoad(String filename) {
        Document document = null;
        SAXReader saxReader = new SAXReader();
        Map<String, String> map = new HashMap();
        map.put("OpenDRIVE", "http://www.opendrive.org");
        saxReader.getDocumentFactory().setXPathNamespaceURIs(map);
        try {
            document = saxReader.read(new File(filename));
        } catch (DocumentException e) {
            logger.error("xml load error", e);
        }
        return document;
    }

    public static Document xmlLoad2(String filename) {
        Document document = null;
        SAXReader saxReader = new SAXReader();
//        Map<String, String> map = new HashMap();
//        map.put("OpenDRIVE", "http://www.opendrive.org");
//        saxReader.getDocumentFactory().setXPathNamespaceURIs(map);
        try {
            document = saxReader.read(new File(filename));
        } catch (DocumentException e) {
            logger.error("xml load error", e);
        }
        return document;
    }
}

相关文章

网友评论

      本文标题:dom4j

      本文链接:https://www.haomeiwen.com/subject/lnoltltx.html