📄 menuloader.java
字号:
/* * Copyright (C) butor.com. All rights reserved. * * This software is published under the terms of the GNU Library General * Public License (GNU LGPL), a copy of which has been included with this * distribution in the LICENSE.txt file. */package org.butor.web.menu;import java.io.IOException;import java.lang.reflect.Constructor;import java.util.ArrayList;import java.util.Collection;import java.util.Locale;import org.butor.config.Config;import org.butor.config.ConfigFactory;import org.butor.config.IConfig;import org.butor.config.lowlevel.IProperties;import org.butor.log.Log;import org.butor.messageResources.MessageResourcesService;import org.butor.security.ISecurityChecker;import org.butor.web.listener.ButorContextListener;/** * Load the menu from the configuration file and build the menu hierarchy. * * @author Nguyen The Hung */public class MenuLoader extends GenericMenuLoader { private static final String PROPERTY_MENU_HELPER = "helper"; private static final String PROPERTY_MENU_HELPER_CLASS_NAME = "helper_class_name"; private static final String PROPERTY_MENU_LIST = "menu_list"; private static final String PROPERTY_MENU_PREFIX = "menu."; private static final String PROPERTY_MENU_PARENT_NAME = "parent_name"; private static final String PROPERTY_MENU_SEQUENCE = "sequence"; private static final String PROPERTY_MENU_FUNCTION_CODE = "function_code"; private static final String PROPERTY_MENU_FUNCTION_LIST = "function_list"; private static final String PROPERTY_MENU_LABEL_KEY = "label_key"; private static final String PROPERTY_MENU_ACTION = "action"; private static final String PROPERTY_MENU_TITLED_ACTION = "titled_action"; private static final String PROPERTY_MENU_IMAGE_URL = "image_url"; private static final String PROPERTY_MENU_TARGET = "target"; private static final String PROPERTY_MENU_IMAGE_OVER_URL = "image_over_url"; private static final String PROPERTY_MENU_IMAGE_CLICK_URL = "image_click_url"; private static final String PROPERTY_MENU_IMAGE_DEPEND_ON_LANGUAGE = "img_depend_on_lang"; private static final String PROPERTY_MENU_ITEM_HEIGHT = "height"; private static final String PROPERTY_MENU_ITEM_WIDTH = "width"; private static final String PROPERTY_MENU_ITEM_TAG = "tag"; protected Locale f_locale; protected ISecurityChecker f_securityChecker = null; /** * Name of the menu */ protected String f_id; /** * Menu helper class name */ protected String f_helperName = null; /** * Menu helper class instance */ protected IMenuHelper f_helper = null; /** * Menu helper config property list */ protected IProperties f_helperPropertyList = null; /** * Constructor for MenuLoader. * @param locale java.util.Locale * @param menuId java.lang.String * @param securityChecker Security provider to check menu item access rights. */ public MenuLoader(Locale locale, String menuId, ISecurityChecker securityChecker) { super(); f_locale = locale; f_id = menuId; f_securityChecker = securityChecker; init(); build(); } /** * Load the menu from the configuration file. */ protected void init() { IConfig config; Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_INFO, "init()", ""); if (f_id == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "init()", "name = [null]."); return; } if (f_locale == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "init()", "locale = [null]."); return; } IProperties menu_list = Config.getPropertyList(PROPERTY_MENU_LIST); if (menu_list == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "init()", "Property 'menu_list' not found."); return; } String menu_file = menu_list.getProperty(f_id, null); if (menu_file == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "init()", "Property not found [" + f_id + "]."); return; } try { config = ConfigFactory.loadConfigFile(menu_file, ButorContextListener.getContextDir(), ConfigFactory.getAlternateConfDir(), ConfigFactory.getLogDir(), ButorContextListener.getConfigSuffixes()); } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "init()", e); return; } // any helper for this menu? initHelper(config); IProperties menuListProp = config.getPropertyList(PROPERTY_MENU_LIST); if (menuListProp == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "init()", "Property 'menu_list' not found in file " + menu_file); return; } // Clear the list of nodes. clear(); for (int i = 0; i < menuListProp.size(); i++) { IProperties menu = config.getPropertyList(PROPERTY_MENU_PREFIX + menuListProp.getProperty(i)); if (menu != null) { String labelKey = menu.getProperty(PROPERTY_MENU_LABEL_KEY, null); ArrayList secFunctions = new ArrayList(); // Security function code String secFnc = menu.getProperty(PROPERTY_MENU_FUNCTION_CODE, null); if ( secFnc != null && secFnc.trim().length() != 0 ) { secFunctions.add(secFnc.trim()); } // Sec function list String secFncCodeListName = menu.getProperty(PROPERTY_MENU_FUNCTION_LIST, null); if (secFncCodeListName != null && secFncCodeListName.trim().length() > 0) { IProperties secFncCodeList = config.getPropertyList(secFncCodeListName.trim()); for (int k = 0; k < secFncCodeList.size(); k++) { secFnc = (String)secFncCodeList.getProperty(k); if (secFnc != null && secFnc.trim().length() > 0) { secFunctions.add(secFnc.trim()); } } } String label = MessageResourcesService.getMessage(f_locale, labelKey); MenuItem node = new MenuItem((String)menuListProp.getProperty(i), menu.getProperty(PROPERTY_MENU_PARENT_NAME, null), menu.getPropertyInt(PROPERTY_MENU_SEQUENCE, 0), (String[])secFunctions.toArray(new String[0]), labelKey, label, menu.getProperty(PROPERTY_MENU_ACTION, null), menu.getProperty(PROPERTY_MENU_IMAGE_URL, null), menu.getProperty(PROPERTY_MENU_TARGET, null), menu.getProperty(PROPERTY_MENU_IMAGE_OVER_URL, null), menu.getProperty(PROPERTY_MENU_IMAGE_CLICK_URL, null), new Boolean(menu.getProperty(PROPERTY_MENU_IMAGE_DEPEND_ON_LANGUAGE, null)).booleanValue(), new Integer(menu.getProperty(PROPERTY_MENU_ITEM_WIDTH, "120")).intValue(), new Integer(menu.getProperty(PROPERTY_MENU_ITEM_HEIGHT, "20")).intValue()); node.setTitledAction(menu.getPropertyBoolean(PROPERTY_MENU_TITLED_ACTION, false)); node.setTag(menu.getProperty(PROPERTY_MENU_ITEM_TAG, null)); add(node); } else { Log.logStr(this, Log.LOG_TYPE_WARN, "init", PROPERTY_MENU_PREFIX + menuListProp.getProperty(i) + " item not defined"); } } } /** * Return whether the node should be displayed. * @param node org.butor.wak.menu.IBranch * @return boolean */ protected boolean shouldDisplayNode(IBranch node) { if (f_securityChecker == null) { Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_INFO, "shouldDisplayNode()", "User Profile = [null]. OK can pass!"); return true; } IMenuItem item = (IMenuItem)node; String[] functionCodes = item.getFunctionCodes(); if (functionCodes == null || functionCodes.length == 0) { Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_INFO, "shouldDisplayNode()", "Security function code = [null]. OK can pass!"); return true; } // Check all functions codes for(int i = 0; i < functionCodes.length; i++) { if (f_securityChecker.access(functionCodes[i])) { return true; } } return false; } /** * Return the menu object. * @return org.butor.wak.menu.ButorMenu */ public ButorMenu getMenu() { Log.logStr(this, Log.LOG_TYPE_INFO, "getMenu()", "..."); Collection menus = getHierarchy(); MenuItem menuItems[] = new MenuItem[menus.size()]; menus.toArray(menuItems); ButorMenu menu = new ButorMenu(f_id, menuItems, f_locale); if (f_helper != null) { Log.logStr(this, Log.LOG_TYPE_INFO, "getMenu()", "Applying helper on menu [" +menu.getId() +"]!"); f_helper.help(menu, f_helperPropertyList); } Log.logStr(this, Log.LOG_TYPE_INFO, "getMenu()", "Done"); return menu; } /** * Get the helper instance * @return */ public IMenuHelper getHelper() { return f_helper; } /** * Get the helper name * @return */ public String getHelperName() { return f_helperName; } protected void initHelper(IConfig config) { // any helper for this menu ? f_helperPropertyList = config.getPropertyList(PROPERTY_MENU_HELPER); if (f_helperPropertyList == null) { Log.logStr(this, Log.LOG_TYPE_INFO, "initHelper()", "There is no menu helper provided."); return; } f_helperName = f_helperPropertyList.getProperty(PROPERTY_MENU_HELPER_CLASS_NAME, null); if (f_helperName == null) { Log.logStr(this, Log.LOG_TYPE_INFO, "initHelper()", "There is no menu helper provided."); } else { Log.logStr(this, Log.LOG_TYPE_INFO, "initHelper()", "Got menu helper name [" +f_helperName +"]"); // instanciate the helper class. Exception err = null; try { Class cls = Class.forName(f_helperName); Class[] args = null; // constructor with empty signature Constructor cc = cls.getConstructor(args); Object instance = cc.newInstance(args); if (instance instanceof IMenuHelper) { f_helper = (IMenuHelper)instance; } else { err = new Exception("Helper not an intanceof IMenuHelper!"); } } catch (Exception e) { err = e; } if (err != null) { Log.logException(this, Log.LOG_TYPE_ERROR, "initHelper()", err); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -