下面的代码中,因为我是使用
for (Iterator<Element> i = el.elements().iterator(); i.hasNext(); )
迭代器遍历根节点的所有子节点的flag,但是因为linux基线有 <initcmd> 节点,导致flag为空,
在传入service中进行数据库操作时抛出异常未被处理导致程序被挂起,错误信息不输出到console,直接打印到了页面。
解决方法就是try catch处理掉异常并返回false即可
public boolean isFound(String xcwIndex) {
try {
XconfigLinuxExample example = new XconfigLinuxExample();
XconfigLinuxExample.Criteria criteria = example.createCriteria();
criteria.andXcwindexEqualTo(xcwIndex); long count = xconfigLinuxMapper.countByExample(example);
return count > 0;
}catch (Exception e){
// System.out.println("xcwIndex为空,当次未遍历到flag");
return false;
}
}
public void getSysTypeByUuid(String uuid) throws IOException, DocumentException { switch (uuid){
case "66c221be-6ab2-ef53-1589-fe16877914e2": //windows
importDataXML("66c221be-6ab2-ef53-1589-fe16877914e2");break;
case "66c221be-6ab2-ef53-1589-fe16877914f4": //linux linux系都需要去除 <initcmd> 的干扰
linuxBaseline("66c221be-6ab2-ef53-1589-fe16877914f4");break;
}
}
public void linuxBaseline(String uuid) throws DocumentException, IOException { JSONObject jsonObject = new JSONObject();
String result = null; CompTools compTools = new CompTools(); String path = "C:\\RM-ROOT\\upload\\assetsConfResultXml\\" + uploadedFileName; // 创建 dom4j 解析器
SAXReader reader = new SAXReader();
Document document = reader.read(new File(path));
Element root = document.getRootElement(); List<Element> elements = root.elements();
String flag;
String xcwValue;
Integer noneComplianceConfCount = 0;
for (Element el :
elements) {
for (Iterator<Element> i = el.elements().iterator(); i.hasNext(); ) {
Element element = (Element) i.next(); flag = element.attributeValue("flag");
if (xconfigLinuxService.isFound(flag)) {
XconfigLinux allByXcwIndex = xconfigLinuxService.getAllByXcwIndex(flag);
xcwValue = element.element("cmd").element("value").getText();
result = compTools.linuxComp(allByXcwIndex.getCindex().toString(), flag, xconfigLinuxService, xcwValue);
jsonObject.put(flag, result);
if ("1".equals(result)){
noneComplianceConfCount+=1;
} } else {
// System.out.println("没找到:"+flag);
} }
}
assetsService.saveAssetsData(OriginalFilename,jsonObject,noneComplianceConfCount,uuid);
}