admin
2022-05-19 56364722d3ed70d48ec41f567a4e59e5ccbbb868
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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);
    }
 
}