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);
|
}
|
}
|
}
|
}
|