修复xml形式读取窗体元素时读取错误的问题

This commit is contained in:
彭宇琦 2021-02-06 19:42:55 +08:00
parent 4c647ad192
commit 5feb22981a
2 changed files with 28 additions and 31 deletions

View File

@ -55,13 +55,7 @@
<version>3.17</version>
</dependency>
<!-- xml解析工具
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>-->
<!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
<!-- xml解析工具 -->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>

View File

@ -59,7 +59,7 @@ public class XmlLocation extends AbstractLocation {
try {
dom = new SAXReader().read(xmlFile);
} catch (DocumentException e) {
throw new IncorrectFileException("xml文件异常文件位置" + xmlFile.getAbsolutePath());
throw new IncorrectFileException("xml文件异常文件位置" + xmlFile.getAbsolutePath(), e);
}
}
@ -76,12 +76,9 @@ public class XmlLocation extends AbstractLocation {
public ArrayList<ByType> findElementByTypeList(String name) {
ArrayList<ByType> byTypeList = new ArrayList<ByType>();
//查询并存储元素下的子元素
@SuppressWarnings("unchecked")
ArrayList<Object> lableElementList = new ArrayList<>(getElementLabelElement(name).elements());
ArrayList<Element> lableElementList = new ArrayList<>(getElementLabelElement(name).elements());
lableElementList.stream()
//强转为Element类型
.map(lable -> (Element)lable)
//获取标签名称
.map(lable -> lable.getName())
//将名称转换为ByType枚举
@ -90,7 +87,6 @@ public class XmlLocation extends AbstractLocation {
.filter(lable -> lable != null)
//存储标签
.forEach(byTypeList::add);
;
return byTypeList;
}
@ -98,25 +94,32 @@ public class XmlLocation extends AbstractLocation {
@Override
public ArrayList<String> findValueList(String name) {
ArrayList<String> valueList = new ArrayList<>();
//查询元素
Element element = getElementLabelElement(name);
//遍历元素下所有的定位标签并将其转换为相应的ByType枚举存储至byTypeList中
for (Object byElement : element.elements()) {
//判断元素是否启用若元素未启用则下一个循环
String isUserText = ((Element) byElement).attributeValue("is_user");
if (isUserText != null && !Boolean.valueOf(isUserText)) {
continue;
}
//判断元素是否启用模板若启用模板则获取模板内容并将定位内容进行转换
String tempId = ((Element) byElement).attributeValue("temp_id");
String value = tempId != null ?
getTemplateValue(tempId, toByType(((Element) byElement).getName())) :
((Element)byElement).getText();
valueList.add(replaceValue(((Element) byElement), value));
}
//查询元素遍历元素下所有的定位标签并过滤掉元素标签
getElementLabelElement(name).elements().stream().filter(ele -> !"element".equals(ele.getName()))
//过滤不启用的标签
.filter(ele -> {
return Optional.ofNullable(ele.attributeValue("is_user"))
.filter(t -> !t.isEmpty())
.map(t -> {
try {
return Boolean.valueOf(t).booleanValue();
} catch (Exception e) {
return true;
}
}).orElse(true);
//根据值或模板将定位内容转译并存储至valueList
}).forEach(ele -> {
String value = "";
String tempId = Optional.ofNullable(ele.attributeValue("temp_id")).orElse("");
if (tempId.isEmpty()) {
value = ele.getText();
} else {
value = getTemplateValue(tempId, toByType(ele.getName()));
}
valueList.add(replaceValue(ele, value));
});
return valueList;
}