KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > tags > menu > RollerMenuModel


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.core.tags.menu;
20
21 import java.io.InputStream JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Vector JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import org.apache.commons.digester.Digester;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.roller.RollerException;
30 import org.apache.roller.pojos.WebsiteData;
31 import org.apache.roller.ui.core.BasePageModel;
32 import org.xml.sax.SAXException JavaDoc;
33
34
35 /**
36  * Menu model for roller.
37  */

38 public class RollerMenuModel extends BaseRollerMenu implements MenuModel {
39     
40     private static Log mLogger = LogFactory.getLog(RollerMenuModel.class);
41     
42     private String JavaDoc mMenuId = null;
43     
44     /** Vector of MenuImpl objects */
45     private Vector JavaDoc mMenus = new Vector JavaDoc();
46     
47     /** Key used to store selected menu in request */
48     public static final String JavaDoc MENU_KEY = "rmk";
49     
50     /** Key used to store selected menu item in request */
51     public static final String JavaDoc MENU_ITEM_KEY = "rmik";
52     
53     
54     /** Construct menu model based on menu XML */
55     public RollerMenuModel( String JavaDoc menuId, String JavaDoc config, ServletContext JavaDoc ctx ) {
56         try {
57             mMenuId = menuId;
58             
59             Digester digester = new Digester();
60             digester.push(this);
61             //digester.setDebug(99);
62
//digester.setClassLoader( getClass().getClassLoader() );
63
//digester.setValidating(true);
64

65             String JavaDoc menuPath = "menu-bar/menu";
66             String JavaDoc menuItemPath = "menu-bar/menu/menu-item";
67             
68             digester.addObjectCreate( menuPath,
69                     "org.apache.roller.ui.core.tags.menu.MenuImpl");
70             digester.addSetProperties( menuPath );
71             digester.addSetNext( menuPath,"addMenu",
72                     "org.apache.roller.ui.core.tags.menu.Menu");
73             
74             digester.addObjectCreate( menuItemPath,
75                     "org.apache.roller.ui.core.tags.menu.MenuItemImpl");
76             digester.addSetProperties( menuItemPath );
77             digester.addSetNext( menuItemPath, "addItem",
78                     "org.apache.roller.ui.core.tags.menu.MenuItemImpl");
79             
80             InputStream JavaDoc input = ctx.getResourceAsStream(config);
81             try {
82                 digester.parse(input);
83                 
84                 if ( getMenus() != null ) {
85                     Vector JavaDoc menus = getMenus();
86                     for (int i=0; i<menus.size(); i++) {
87                         MenuImpl menu = (MenuImpl)menus.elementAt(i);
88                         menu.setMenuId( mMenuId );
89                         Vector JavaDoc menuItems = menu.getMenuItems();
90                         if ( menuItems != null ) {
91                             for (int j=0; j<menuItems.size(); j++ ) {
92                                 MenuItemImpl item =
93                                         (MenuItemImpl)menuItems.elementAt(j);
94                                 item.setMenuId( mMenuId );
95                             }
96                         }
97                     }
98                 }
99             } catch (SAXException JavaDoc e) {
100                 mLogger.error("Unexpected exception",e);
101             } finally {
102                 if ( input!=null ) {
103                     try { input.close(); } catch (Exception JavaDoc e) { mLogger.error("Unexpected exception",e); };
104                 }
105             }
106         } catch (Exception JavaDoc e) {
107             mLogger.error("Unexpected exception",e);
108         }
109     }
110     
111     
112     public Vector JavaDoc getMenus() {
113         return mMenus;
114     }
115     
116     
117     public Menu getSelectedMenu( HttpServletRequest JavaDoc req ) throws RollerException {
118         MenuImpl def = null;
119         MenuImpl selected = null;
120         for ( int i=0; i<mMenus.size(); i++ ) {
121             MenuImpl menu = (MenuImpl)mMenus.elementAt(i);
122             if ( menu.isSelected( req ) ) {
123                 selected = menu;
124                 break;
125             }
126             if (def == null) {
127                 def = menu;
128             }
129         }
130         if ( selected != null ) {
131             return selected;
132         } else {
133             return def;
134         }
135     }
136     
137     
138     public void addMenu( Menu menu ) {
139         mMenus.addElement( menu );
140     }
141     
142     
143     /** Create params based on incoming request */
144     static Hashtable JavaDoc createParams(HttpServletRequest JavaDoc req) {
145         Hashtable JavaDoc params = new Hashtable JavaDoc();
146         try {
147             WebsiteData website = getRequestedWeblog(req);
148             BasePageModel pageModel = (BasePageModel)req.getAttribute("model");
149             if (website == null && pageModel != null) {
150                 website = pageModel.getWebsite();
151             }
152             if (website != null) {
153                 params.put("weblog", website.getHandle());
154             }
155         } catch (Exception JavaDoc e) {
156             mLogger.error("ERROR getting user in menu model", e);
157         }
158         return params;
159     }
160     
161 }
162
Popular Tags