⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webmenumanager.java

📁 struts+spring+hibernate自创框架
💻 JAVA
字号:
package com.pegasus.framework.web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.pegasus.framework.acl.pojo.WebUser;
import com.pegasus.framework.castor.menu.MenuItem;
import com.pegasus.framework.castor.menu.WebMenu;
import com.pegasus.framework.util.FileUtil;

public class WebMenuManager {
	private static final String filename = "config/webMenuBar.xml";

	protected static final Log logger = LogFactory.getLog(WebMenuManager.class);

	private static WebMenu menuData;
	static {
		init();
	}

	public static synchronized void init() {
		try {
			loadXML();
		} catch (Exception e) {
			logger.warn(e);
		}
	}

	public static synchronized void loadXML() throws Exception {
		menuData = loadXML(filename);
	}

	/**
	 * load xml
	 * 
	 * @param filename
	 * xml file name
	 * @return WebMenu object
	 * @throws Exception
	 * FileNotFoundException
	 */
	public static synchronized WebMenu loadXML(String filename) throws Exception {
		java.net.URL url = WebMenuManager.class.getClassLoader().getResource(filename);
		return loadXML(url);
	}

	/**
	 * load xml
	 * 
	 * @param url
	 * xml url
	 * @return WebMenu Object
	 * @throws Exception
	 * exception
	 */
	public static synchronized WebMenu loadXML(URL url) throws Exception {
		logger.warn("load xml file[" + url.getFile() + "]");
		String xmlString = FileUtil.readFile(url.getFile());
		logger.warn("xmlString" + xmlString);
		return unmarshal(xmlString);
	}

	/**
	 * unmarshal xml string to WebMenu object
	 * 
	 * @param xmlString
	 * xml string
	 * @return WebMenu object
	 * @throws org.exolab.castor.xml.MarshalException
	 * castor exception
	 * @throws org.exolab.castor.xml.ValidationException
	 * castor exception
	 * @throws IOException
	 */
	public static WebMenu unmarshal(String xmlString) throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException, IOException {
		BufferedReader reader = new BufferedReader(new StringReader(xmlString));
		try {
			WebMenu webMenu = WebMenu.unmarshal(reader);
			return webMenu;
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			reader.close();
		}
	}

	/**
	 * marshal WebMenu object to xml string
	 * 
	 * @param webMenu
	 * data
	 * @return xml string
	 * @throws org.exolab.castor.xml.MarshalException
	 * castor exception
	 * @throws org.exolab.castor.xml.ValidationException
	 * castor exception
	 * @throws IOException
	 * IOException
	 */
	public static String marshal(WebMenu webMenu) throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException, IOException {
		StringWriter writer = new StringWriter();
		try {
			webMenu.marshal(writer);
			return writer.toString();
		} finally {
			writer.close();
		}
	}

	/**
	 * @return webmenu application data
	 */
	public static WebMenu getMenuData() {
		return menuData;
	}

	public synchronized static WebMenu getUserWebMenu(WebUser user) {

		WebMenu userMenuData = new WebMenu();
		userMenuData.setId(menuData.getId());
		userMenuData.setCaption(menuData.getCaption());
		userMenuData.setDescription(menuData.getDescription());
		userMenuData.setIcon(menuData.getIcon());
		userMenuData.setTarget(menuData.getTarget());
		userMenuData.setUrl(menuData.getUrl());
		if (user == null)
			return userMenuData;
		for (int index = 0; index < menuData.getMenuItemCount(); index++) {
			MenuItem menuItem = menuData.getMenuItem(index);
			MenuItem item = new MenuItem();
			item.setCaption(menuItem.getCaption());
			item.setDescription(menuItem.getDescription());
			item.setIcon1(menuItem.getIcon1());
			item.setIcon2(menuItem.getIcon2());
			item.setId(menuItem.getId());
			item.setTarget(menuItem.getTarget());
			item.setUrl(menuItem.getUrl());
			userMenuData.addMenuItem(item);
			initUserWebMenu(user, menuItem, item);
		}
		return userMenuData;
	}

	/**
	 * @param user
	 * user
	 * @param src
	 * item
	 * @param dest
	 * item
	 */
	private synchronized static void initUserWebMenu(WebUser user, MenuItem src, MenuItem dest) {
		if (user == null)
			return;
		if (src == null)
			return;
		for (int index = 0; index < src.getMenuItemCount(); index++) {
			MenuItem menuItem = src.getMenuItem(index);
			MenuItem item = new MenuItem();
			item.setCaption(menuItem.getCaption());
			item.setDescription(menuItem.getDescription());
			item.setIcon1(menuItem.getIcon1());
			item.setIcon2(menuItem.getIcon2());
			item.setId(menuItem.getId());
			item.setTarget(menuItem.getTarget());
			item.setUrl(menuItem.getUrl());
			dest.addMenuItem(item);
			initUserWebMenu(user, menuItem, item);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -