liuxiaolong
2019-05-09 0d1d88cdb668e75ea8609417ac18ae19947e9525
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.basic.x01.systemMenu;
 
import org.apache.log4j.Logger;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
import framework.util.XmlUtil;
 
/**
 * 加载菜单
 * 
 * @company 北京贝思科技术有限公司
 * @author liuyajun, 8384503@qq.com
 * @date 2016年1月11日
 * @time 下午8:08:11
 */
public final class MenuLoader {
    protected MenuLoader(){
        
    }
    private Logger log = Logger.getLogger(MenuLoader.class);
    private int itemId = 1;
 
    /**
     * 
     * @param absMenuXml xml绝对文件路径
     * 
     * @throws Exception
     */
    protected synchronized void load(String absMenuXml) throws Exception {
        MenuUtil.menuActionMap.clear();
        MenuUtil.menuIdMap.clear();
        MenuUtil.menuList.clear();
        MenuUtil.menu1List.clear();
        MenuUtil.menu2List.clear();
        MenuUtil.menu3List.clear();
        
        
        itemId = 0;        //菜单ID从1开始
 
        Element root = XmlUtil.getXmlFileRoot(absMenuXml);
 
        NodeList items = root.getChildNodes();
 
        for (int i = 0, size=items.getLength(); i < size; i++) {
            Node actionNode = items.item(i);
            String nodeName = actionNode.getNodeName();
            
            String idPath="";
            String titlePath="";
            
            if (nodeName.equalsIgnoreCase("menu") 
                    || nodeName.equalsIgnoreCase("button")
                    //这一层不应该有必须权限
                    ) {
                parseItem(-1, itemId++, actionNode, 
                        idPath, titlePath, null);
            }
        }
    }
 
    private void parseItem(int parId, int id, 
            Node actionNode, String idPath, String titlePath,
            MenuItem parent) throws Exception {
        
        MenuItem menu = new MenuItem();
        NamedNodeMap attr = actionNode.getAttributes();
        
        menu.actionId = attr.getNamedItem("action").getNodeValue().trim();
        menu.title = attr.getNamedItem("title").getNodeValue().trim();
 
        if(menu.actionId.equals("") || menu.title.equals("")){
            //必须存在 actionId, title
            return;
        }
 
        menu.menu = actionNode.getNodeName().equalsIgnoreCase("menu");
        menu.button = actionNode.getNodeName().equalsIgnoreCase("button");
        menu.required = actionNode.getNodeName().equalsIgnoreCase("required");
        
        menu.id = id+"";
        menu.parId = parId>=0?(parId+""):"";
        menu.idPath = (idPath.equals("")?"":(idPath+","))+id;
        menu.parent = parent;
        menu.level = menu.idPath.length()-menu.idPath.replace(",", "").length()+1;
        
        if(menu.isRequired() && menu.getParent()==null){
            throw new Exception(menu.getTitle() + "必须权限一定要有上级");
        }
 
        if(menu.isMenu() && menu.getParent()!=null
                && ! menu.getParent().isMenu()){
            throw new Exception(menu.getTitle() +"菜单上层必须也是菜单");
        }
        
        if(parent !=null){
            if(menu.isMenu()){
                parent.subMenuList.add(menu);
            }else if(menu.isButton()){
                parent.subButtonList.add(menu);
            }
        }
        
        //title路径
        if(titlePath.trim().length()==0){
            menu.titlePath = menu.title;
        }else{
            menu.titlePath = titlePath + MenuUtil.TITLE_PATH_SPLIT
                    + menu.title;
        }
 
        menu.publicMenu = false;
        if(! menu.isRequired()){
            try {
                String publicMenu = attr.getNamedItem("public").getNodeValue().trim();
                menu.publicMenu = publicMenu.toLowerCase().equals("true");            
            } catch (Exception e) {
                
            }
        }
        //其他配置, other="a=1//b=2", key=value//key=value
        menu.other.clear();
        try {
            String otherString = attr.getNamedItem("other").getNodeValue().trim();
            String[] others=otherString.split("//");
            for(String s : others){
                if(s==null || s.trim().length()==0 || ! s.contains("=")){
                    continue;
                }
                int pos=s.indexOf("=");
                String key = s.substring(0, pos).trim();
                String val = s.substring(pos+1).trim();
                if(key.length()>0 && val.length()>0){
                    menu.other.put(key, val);
                }
            }
        } catch (Throwable e) {
            
        }
 
        //存储
        if(MenuUtil.menuActionMap.containsKey(menu.getActionId())){
            throw new Exception("存在相同action的权限");
        }
        
        MenuUtil.menuActionMap.put(menu.getActionId(), menu);
        MenuUtil.menuIdMap.put(menu.getId(), menu);
        MenuUtil.menuList.add(menu);
        if(menu.isMenu()){
            if(menu.getLevel()==1){
                MenuUtil.menu1List.add(menu);
            }else if(menu.getLevel()==2){
                MenuUtil.menu2List.add(menu);
            }else if(menu.getLevel()==3){
                MenuUtil.menu3List.add(menu);
            }
        }
        log.info("Menu: "+menu);
        
        if(menu.isRequired()){
            //必须权限,不需要处理下级,没有下级
            return;
        }
        
        //不是required,需要处理子节点
        NodeList children = actionNode.getChildNodes();
        for (int i = 0, size = children.getLength(); i < size; i++) {
            Node child = children.item(i);
            if (child.getNodeName().equalsIgnoreCase("menu")
                    || child.getNodeName().equalsIgnoreCase("button")
                    || child.getNodeName().equalsIgnoreCase("required")) {
                parseItem(Integer.valueOf(menu.getId()).intValue(), 
                        itemId++, child, 
                        menu.getIdPath(), menu.getTitlePath(),
                        menu);
            }
        }
    }
}