package com.ks.app.utils.xml;
|
|
import com.ks.app.dto.admin.PageNav;
|
import org.springframework.util.AntPathMatcher;
|
import org.w3c.dom.Document;
|
import org.w3c.dom.Node;
|
import org.w3c.dom.NodeList;
|
import org.xml.sax.SAXException;
|
import org.yeshi.utils.StringUtil;
|
|
import javax.xml.parsers.DocumentBuilder;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.ParserConfigurationException;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.List;
|
|
/**
|
* @author hxh
|
* @title: PageNavUtil
|
* @description: 页面导航帮助类
|
* @date 2022/5/18 16:35
|
*/
|
public class PageNavUtil {
|
|
/**
|
* @return java.util.List<com.yeshi.makemoney.app.dto.admin.PageNav>
|
* @author hxh
|
* @description 解析导航
|
* @date 16:49 2022/5/18
|
* @param: inputStream
|
**/
|
private static List<PageNav> parseNav(InputStream inputStream) throws ParserConfigurationException, IOException, SAXException {
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
//2.创建DocumentBuilder对象
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
Document d = builder.parse(inputStream);
|
Node root = d.getElementsByTagName("navs").item(0);
|
NodeList items = root.getChildNodes();
|
List<PageNav> list = new ArrayList<>();
|
for (int i = 0; i < items.getLength(); i++) {
|
if (items.item(i).getNodeName().equalsIgnoreCase("nav")) {
|
list.add(parseNav(items.item(i)));
|
}
|
}
|
return list;
|
}
|
|
private static PageNav parseNav(Node node) {
|
PageNav nav = new PageNav();
|
String name = node.getAttributes().getNamedItem("name") == null ? null : node.getAttributes().getNamedItem("name").getNodeValue();
|
String path = node.getAttributes().getNamedItem("path") == null ? null : node.getAttributes().getNamedItem("path").getNodeValue();
|
String icon = node.getAttributes().getNamedItem("icon") == null ? null : node.getAttributes().getNamedItem("icon").getNodeValue();
|
nav.setName(name);
|
nav.setPath(path);
|
nav.setIcon(icon);
|
if (node.getChildNodes() != null && node.getChildNodes().getLength() > 0) {
|
|
List<PageNav> children = new ArrayList<>();
|
for (int i = 0; i < node.getChildNodes().getLength(); i++) {
|
if (node.getChildNodes().item(i).getNodeName().equalsIgnoreCase("nav")) {
|
children.add(parseNav(node.getChildNodes().item(i)));
|
}
|
}
|
nav.setChildren(children);
|
}
|
return nav;
|
}
|
|
|
/**
|
* @return java.util.List<com.yeshi.makemoney.app.dto.admin.PageNav>
|
* @author hxh
|
* @description 获取页面导航
|
* @date 18:02 2022/5/18
|
* @param: pathList
|
**/
|
public static List<PageNav> getPageNavs(List<String> pathList) {
|
List<PageNav> pageNavs = null;
|
try {
|
pageNavs = PageNavUtil.parseNav(PageNavUtil.class.getClassLoader().getResourceAsStream("admin/nav.xml"));
|
} catch (ParserConfigurationException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} catch (SAXException e) {
|
e.printStackTrace();
|
}
|
|
for (PageNav nav : pageNavs) {
|
filter(nav, pathList);
|
}
|
|
//删除一级空目录
|
for (int i = 0; i < pageNavs.size(); i++) {
|
if (StringUtil.isNullOrEmpty(pageNavs.get(i).getPath()) && pageNavs.get(i).getChildren() == null) {
|
pageNavs.remove(i--);
|
}
|
}
|
return pageNavs;
|
}
|
|
/**
|
* @return void
|
* @author hxh
|
* @description 过滤掉没有在pathList中的导航选项
|
* @date 18:01 2022/5/18
|
* @param: root
|
* @param: pathList
|
**/
|
public static void filter(PageNav root, List<String> pathList) {
|
if (root == null || root.getChildren() == null || root.getChildren().size() == 0) {
|
return;
|
} else {
|
|
for (int i = 0; i < root.getChildren().size(); i++) {
|
filter(root.getChildren().get(i), pathList);
|
}
|
|
for (int i = 0; i < root.getChildren().size(); i++) {
|
PageNav child = root.getChildren().get(i);
|
if (!StringUtil.isNullOrEmpty(child.getPath()) && !pathMatch(child.getPath(), pathList)) {
|
root.getChildren().remove(i--);
|
}
|
}
|
if (root.getChildren().size() == 0) {
|
root.setChildren(null);
|
}
|
return;
|
}
|
}
|
|
private static boolean pathMatch(String path, List<String> pathList) {
|
AntPathMatcher matcher = new AntPathMatcher();
|
for (String regex : pathList) {
|
if (matcher.match(regex, path)) {
|
return true;
|
}
|
}
|
return false;
|
}
|
|
|
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
|
List<PageNav> result = getPageNavs(Arrays.asList(new String[]{"team/vip.html"}));
|
System.out.println(result);
|
}
|
|
}
|