package com.videtek.vacp.common;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.InputStream;
import java.util.*;
/**
* @author hehaifeng
* @date 2018-4-23
* @version 1.0
*
* 工具类:主要用于字典值和国标值的转换
*/
public class DictionaryTransformUtil {
/**
* xml 配置文件根元素
*/
private static Element root;
/**
* 用于存放一级目录名和目录内容的
* key值:一级目录名,value值:该一级目录下的二级目录名
*/
private static Map<String,Set<String>> allClass;
/**
* 日志对象,用于记录该工具类的运行情况
*/
public static final Logger logger = LoggerFactory.getLogger(DictionaryTransformUtil.class);
/**
* 构造方法
*/
public DictionaryTransformUtil() {
}
/**
*
* @param bigType 一级分类(人,骑车人,汽车,通用)
* @param type 二级分类
* @param source 国标码
* @return 转换之后的编码
*/
public static String transformDictionary(String bigType,String type, String source) {
// 重要对象参数验证
if (root==null||allClass==null||allClass.size()==0){
logger.info("配置文件初始化失败!");
return null;
}
// 转换查询参数验证
if (StringUtils.isBlank(bigType)||StringUtils.isBlank(type)||StringUtils.isBlank(source)) {
logger.info("转换参数为空");
return null;
}
// 如果Map集合中包含一级分类,而且该一级分类中也包含二级分类
if (allClass.containsKey(bigType)&&allClass.get(bigType).contains(type)){
// 在配置文件中,先找到一级分类标签,再找到二级分类标签,然后获取二级分类标签里的所有元素
List<Element> smallType = null;
//如果配置文件配置不当,有可能出现空指针异常
try{
smallType = root.element(bigType).element(type).elements();
}catch (Exception e){
e.printStackTrace();
logger.error(e.getMessage());
}
// 进行参数验证
if(smallType==null || smallType.size()==0){
return null;
}
// 遍历该二级分类里的所有元素
for (Element e:smallType) {
// 获取元素上 decode 属性的值
String code = e.attributeValue("decode").trim();
// 获取元素上 soure属性的值
String soure = e.attributeValue("soure").trim();
// 如果code soure 都不为空,而且 soure等于 source 那就返回code的值
if (StringUtils.isNotBlank(code)&&StringUtils.isNotBlank(soure)&&source.equals(soure)){
return code;
}
}
}
// 如果是其他条件,则返回null值表示转换失败
return null;
}
/**
* 类加载时读取并初始化静态资源
*/
static {
// 读取配置文件
SAXReader reader = new SAXReader();
Document doc = null;
try {
// 直接写路径容易读取文件失败,这种写法就可以保证Servlet容器中 resources目录下的文件读取成功
InputStream xml = DictionaryTransformUtil.class.getResourceAsStream("/dictionaryTransform.xml");
doc = reader.read(xml);
} catch (Exception e) {
e.printStackTrace();
logger.error("读取配置文件失败:"+e.getMessage());
}
// 获取文件根元素
root = doc.getRootElement();
// 获取根元素下的 items 元素
Element items = root.element("items");
if (items==null){
logger.error("没有在配置文件里找到 items 标签");
}else {
// 获取 items 元素里的 所有下一级元素(一级分类),不允许重复
Set<Element> bigClass = new HashSet<Element>(items.elements());
// 初始化一个map集合,长度为items里元素的个数(一级分类的个数)
allClass = new HashMap<>(bigClass.size());
// 遍历这些一级分类
for (Element e:bigClass) {
//给map集合赋值,key值就items里的元素名(一级分类),value值就是每个items里的子分类(二级分类)
allClass.put(e.getName(),new HashSet(Arrays.asList(e.getTextTrim().split(","))));
}
}
}
/**
* 配置文件示例:
* <?xml version="1.0" encoding="UTF-8"?>
* <transformation>
*
* <!--总类别-->
* <items>
* 一级分类
* <!-- 汽车-->
* <vehicle>
* <!-- 多个分类之间用逗号隔开 -->
* </vehicle>
* <!-- 行人-->
* <person>
* <!-- 多个分类之间用逗号隔开 -->
* </person>
* <!--骑车人-->
* <bicycle>
*
* </bicycle>
* <!-- 可以共用的类型-->
* <common>
* driverCount
* </common>
* </items>
*
* <!--decode对应字典表(tb_sys_dictionary_value)里的value_code, soure 是国标值-->
*
* <!--公用的-->
* <common>
*
* <!--驾驶人数-->
* <driverCount>
* <!-- 0,0人,0-->
* <item decode="0" soure="0" />
* <!--1,1人,1-->
* <item decode="1" soure="1" />
* <!--2,2人,2-->
* <item decode="2" soure="2" />
* <!--3,更多人,3-->
* <item decode="3" soure="3" />
* </driverCount>
* </common>
*
* </transformation>
*/
}
操作xml文件,实现一一对应关系数值的高效转换
最新推荐文章于 2024-04-18 20:14:04 发布