001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.core.tags.menu;
020:
021: import java.io.InputStream;
022: import java.util.Hashtable;
023: import java.util.Vector;
024: import javax.servlet.ServletContext;
025: import javax.servlet.http.HttpServletRequest;
026: import org.apache.commons.digester.Digester;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.roller.RollerException;
030: import org.apache.roller.pojos.WebsiteData;
031: import org.apache.roller.ui.core.BasePageModel;
032: import org.xml.sax.SAXException;
033:
034: /**
035: * Menu model for roller.
036: */
037: public class RollerMenuModel extends BaseRollerMenu implements
038: MenuModel {
039:
040: private static Log mLogger = LogFactory
041: .getLog(RollerMenuModel.class);
042:
043: private String mMenuId = null;
044:
045: /** Vector of MenuImpl objects */
046: private Vector mMenus = new Vector();
047:
048: /** Key used to store selected menu in request */
049: public static final String MENU_KEY = "rmk";
050:
051: /** Key used to store selected menu item in request */
052: public static final String MENU_ITEM_KEY = "rmik";
053:
054: /** Construct menu model based on menu XML */
055: public RollerMenuModel(String menuId, String config,
056: ServletContext ctx) {
057: try {
058: mMenuId = menuId;
059:
060: Digester digester = new Digester();
061: digester.push(this );
062: //digester.setDebug(99);
063: //digester.setClassLoader( getClass().getClassLoader() );
064: //digester.setValidating(true);
065:
066: String menuPath = "menu-bar/menu";
067: String menuItemPath = "menu-bar/menu/menu-item";
068:
069: digester.addObjectCreate(menuPath,
070: "org.apache.roller.ui.core.tags.menu.MenuImpl");
071: digester.addSetProperties(menuPath);
072: digester.addSetNext(menuPath, "addMenu",
073: "org.apache.roller.ui.core.tags.menu.Menu");
074:
075: digester.addObjectCreate(menuItemPath,
076: "org.apache.roller.ui.core.tags.menu.MenuItemImpl");
077: digester.addSetProperties(menuItemPath);
078: digester.addSetNext(menuItemPath, "addItem",
079: "org.apache.roller.ui.core.tags.menu.MenuItemImpl");
080:
081: InputStream input = ctx.getResourceAsStream(config);
082: try {
083: digester.parse(input);
084:
085: if (getMenus() != null) {
086: Vector menus = getMenus();
087: for (int i = 0; i < menus.size(); i++) {
088: MenuImpl menu = (MenuImpl) menus.elementAt(i);
089: menu.setMenuId(mMenuId);
090: Vector menuItems = menu.getMenuItems();
091: if (menuItems != null) {
092: for (int j = 0; j < menuItems.size(); j++) {
093: MenuItemImpl item = (MenuItemImpl) menuItems
094: .elementAt(j);
095: item.setMenuId(mMenuId);
096: }
097: }
098: }
099: }
100: } catch (SAXException e) {
101: mLogger.error("Unexpected exception", e);
102: } finally {
103: if (input != null) {
104: try {
105: input.close();
106: } catch (Exception e) {
107: mLogger.error("Unexpected exception", e);
108: }
109: ;
110: }
111: }
112: } catch (Exception e) {
113: mLogger.error("Unexpected exception", e);
114: }
115: }
116:
117: public Vector getMenus() {
118: return mMenus;
119: }
120:
121: public Menu getSelectedMenu(HttpServletRequest req)
122: throws RollerException {
123: MenuImpl def = null;
124: MenuImpl selected = null;
125: for (int i = 0; i < mMenus.size(); i++) {
126: MenuImpl menu = (MenuImpl) mMenus.elementAt(i);
127: if (menu.isSelected(req)) {
128: selected = menu;
129: break;
130: }
131: if (def == null) {
132: def = menu;
133: }
134: }
135: if (selected != null) {
136: return selected;
137: } else {
138: return def;
139: }
140: }
141:
142: public void addMenu(Menu menu) {
143: mMenus.addElement(menu);
144: }
145:
146: /** Create params based on incoming request */
147: static Hashtable createParams(HttpServletRequest req) {
148: Hashtable params = new Hashtable();
149: try {
150: WebsiteData website = getRequestedWeblog(req);
151: BasePageModel pageModel = (BasePageModel) req
152: .getAttribute("model");
153: if (website == null && pageModel != null) {
154: website = pageModel.getWebsite();
155: }
156: if (website != null) {
157: params.put("weblog", website.getHandle());
158: }
159: } catch (Exception e) {
160: mLogger.error("ERROR getting user in menu model", e);
161: }
162: return params;
163: }
164:
165: }
|