File Source: MenuHelper.java
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.weblogger.ui.core.util.menu;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Hashtable;
24 import java.util.Iterator;
25 import java.util.List;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.roller.weblogger.config.WebloggerConfig;
29 import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
30 import org.apache.roller.weblogger.pojos.WeblogPermission;
31 import org.apache.roller.weblogger.pojos.User;
32 import org.apache.roller.weblogger.pojos.Weblog;
33 import org.jdom.Document;
34 import org.jdom.Element;
35 import org.jdom.JDOMException;
36 import org.jdom.input.SAXBuilder;
37
38
39 /**
40 * A helper class for dealing with UI menus.
41 */
/*
P/P * Method: void org.apache.roller.weblogger.ui.core.util.menu.MenuHelper()
*/
42 public class MenuHelper {
43
/*
P/P * Method: org.apache.roller.weblogger.ui.core.util.menu.MenuHelper__static_init
*
* Presumptions:
* java.lang.Class:getResourceAsStream(...)@53 != null
* java.lang.Class:getResourceAsStream(...)@56 != null
* org.apache.commons.logging.LogFactory:getLog(...)@44 != null
*
* Postconditions:
* (soft) log != null
* menus == &new Hashtable(MenuHelper__static_init#1)
* new Hashtable(MenuHelper__static_init#1) num objects == 1
*/
44 private static Log log = LogFactory.getLog(MenuHelper.class);
45
46 private static Hashtable menus = new Hashtable();
47
48
49 static {
50 try {
51 // parse menus and cache so we can efficiently reuse them
52 // TODO: there is probably a better way than putting the whole path
53 ParsedMenu editorMenu = unmarshall(MenuHelper.class.getResourceAsStream("/org/apache/roller/weblogger/ui/struts2/editor/editor-menu.xml"));
54 menus.put("editor", editorMenu);
55
56 ParsedMenu adminMenu = unmarshall(MenuHelper.class.getResourceAsStream("/org/apache/roller/weblogger/ui/struts2/admin/admin-menu.xml"));
57 menus.put("admin", adminMenu);
58
59 } catch (Exception ex) {
60 log.error("Error parsing menu configs", ex);
61 }
62 }
63
64
65 public static Menu getMenu(String menuId, String currentAction,
66 User user, Weblog weblog) {
67
/*
P/P * Method: Menu getMenu(String, String, User, Weblog)
*
* Preconditions:
* (soft) currentAction != null
* (soft) log != null
* (soft) menus != null
* (soft) user != null
* (soft) weblog != null
*
* Presumptions:
* menuConfig.tabs@75 != null
*
* Postconditions:
* return_value in Addr_Set{null,&new Menu(buildMenu#2)}
* new ArrayList(Menu#1) num objects <= 1
* new Menu(buildMenu#2) num objects <= 1
* new Menu(buildMenu#2).tabs == &new ArrayList(Menu#1)
*
* Test Vectors:
* menuId: Inverse{null}, Addr_Set{null}
* java.util.Hashtable:get(...)@75: Addr_Set{null}, Inverse{null}
*/
68 if(menuId == null) {
69 return null;
70 }
71
72 Menu menu = null;
73
74 // do we know the specified menu config?
75 ParsedMenu menuConfig = (ParsedMenu) menus.get(menuId);
76 if(menuConfig != null) {
77 menu = buildMenu(menuConfig, currentAction, user, weblog);
78 }
79
80 return menu;
81 }
82
83
84 private static Menu buildMenu(ParsedMenu menuConfig, String currentAction,
85 User user, Weblog weblog) {
86
/*
P/P * Method: Menu buildMenu(ParsedMenu, String, User, Weblog)
*
* Preconditions:
* log != null
* menuConfig != null
* menuConfig.tabs != null
* (soft) currentAction != null
* (soft) user != null
* (soft) weblog != null
*
* Presumptions:
* configTab.tabItems@95 != null
* configTabItem.subActions.length@133 <= 232-1
* java.util.Iterator:next(...)@133 != null
* java.util.Iterator:next(...)@95 != null
*
* Postconditions:
* return_value == &new Menu(buildMenu#2)
* new ArrayList(Menu#1) num objects == 1
* new Menu(buildMenu#2) num objects == 1
* return_value.tabs == &new ArrayList(Menu#1)
*
* Test Vectors:
* configTab.disabledProperty@95: Addr_Set{null}, Inverse{null}
* configTab.enabledProperty@95: Addr_Set{null}, Inverse{null}
* configTab.role@95: Addr_Set{null}, Inverse{null}
* configTabItem.disabledProperty@133: Addr_Set{null}, Inverse{null}
* configTabItem.enabledProperty@133: Addr_Set{null}, Inverse{null}
* configTabItem.role@133: Addr_Set{null}, Inverse{null}
* java.util.Iterator:hasNext(...)@132: {0}, {1}
* java.util.Iterator:hasNext(...)@94: {0}, {1}
* org.apache.roller.weblogger.pojos.User:hasRole(...)@110: {1}, {0}
* org.apache.roller.weblogger.pojos.User:hasRole(...)@147: {1}, {0}
*/
87 log.debug("creating menu for action - "+currentAction);
88
89 Menu tabMenu = new Menu();
90
91 // iterate over tabs from parsed config
92 ParsedTab configTab = null;
93 Iterator tabsIter = menuConfig.getTabs().iterator();
94 while(tabsIter.hasNext()) {
95 configTab = (ParsedTab) tabsIter.next();
96
97 log.debug("config tab = "+configTab.getName());
98
99 // does this tab have an enabledProperty?
100 boolean includeTab = true;
101 if(configTab.getEnabledProperty() != null) {
102 includeTab = getBooleanProperty(configTab.getEnabledProperty());
103 } else if(configTab.getDisabledProperty() != null) {
104 includeTab = ! getBooleanProperty(configTab.getDisabledProperty());
105 }
106
107 if(includeTab) {
108 // user roles check
109 if(configTab.getRole() != null) {
110 if(!user.hasRole(configTab.getRole())) {
111 includeTab = false;
112 }
113 }
114 }
115
116 if(includeTab) {
117 // weblog permissions check
118 includeTab = isPermitted(configTab.getPerm(), user, weblog);
119 }
120
121 if(includeTab) {
122 log.debug("tab allowed - "+configTab.getName());
123
124 // all checks passed, tab should be included
125 MenuTab tab = new MenuTab();
126 tab.setKey(configTab.getName());
127
128 // setup tab items
129 boolean firstItem = true;
130 ParsedTabItem configTabItem = null;
131 Iterator itemsIter = configTab.getTabItems().iterator();
132 while(itemsIter.hasNext()) {
133 configTabItem = (ParsedTabItem) itemsIter.next();
134
135 log.debug("config tab item = "+configTabItem.getName());
136
137 boolean includeItem = true;
138 if(configTabItem.getEnabledProperty() != null) {
139 includeItem = getBooleanProperty(configTabItem.getEnabledProperty());
140 } else if(configTabItem.getDisabledProperty() != null) {
141 includeItem = !getBooleanProperty(configTabItem.getDisabledProperty());
142 }
143
144 if(includeItem) {
145 // user roles check
146 if(configTabItem.getRole() != null) {
147 if(!user.hasRole(configTabItem.getRole())) {
148 includeItem = false;
149 }
150 }
151 }
152
153 if(includeItem) {
154 // weblog permissions check
155 includeItem = isPermitted(configTabItem.getPerm(), user, weblog);
156 }
157
158 if(includeItem) {
159 log.debug("tab item allowed - "+configTabItem.getName());
160
161 // all checks passed, item should be included
162 MenuTabItem tabItem = new MenuTabItem();
163 tabItem.setKey(configTabItem.getName());
164 tabItem.setAction(configTabItem.getAction());
165
166 // is this the selected item?
167 if(isSelected(currentAction, configTabItem)) {
168 tabItem.setSelected(true);
169 tab.setSelected(true);
170 }
171
172 // the url for the tab is the url of the first item of the tab
173 if(firstItem) {
174 tab.setAction(tabItem.getAction());
175 firstItem = false;
176 }
177
178 // add the item
179 tab.addItem(tabItem);
180 }
181 }
182
183 // add the tab
184 tabMenu.addTab(tab);
185 }
186 }
187
188 return tabMenu;
189 }
190
191
192 private static boolean isPermitted(String perm, User user, Weblog weblog) {
193
194 // convert permissions string to short
/*
P/P * Method: bool isPermitted(String, User, Weblog)
*
* Preconditions:
* (soft) weblog != null
*
* Presumptions:
* init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.ADMIN)
* init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.AUTHOR)
* init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.LIMITED)
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* perm: Inverse{null}, Addr_Set{null}
* java.lang.String:equals(...)@198: {0}, {1}
* java.lang.String:equals(...)@200: {0}, {1}
* java.lang.String:equals(...)@202: {0}, {1}
*/
195 short permMask = -1;
196 if(perm == null) {
197 return true;
198 } else if("limited".equals(perm)) {
199 permMask = WeblogPermission.LIMITED;
200 } else if("author".equals(perm)) {
201 permMask = WeblogPermission.AUTHOR;
202 } else if("admin".equals(perm)) {
203 permMask = WeblogPermission.ADMIN;
204 } else {
205 // unknown perm
206 return false;
207 }
208
209 return weblog.hasUserPermissions(user, permMask);
210 }
211
212 /** Check enabled property, prefers runtime properties */
213 private static boolean getBooleanProperty(String propertyName) {
/*
P/P * Method: bool getBooleanProperty(String)
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* org.apache.roller.weblogger.config.WebloggerRuntimeConfig:getProperty(...)@214: Addr_Set{null}, Inverse{null}
*/
214 if (WebloggerRuntimeConfig.getProperty(propertyName) != null) {
215 return WebloggerRuntimeConfig.getBooleanProperty(propertyName);
216 }
217 return WebloggerConfig.getBooleanProperty(propertyName);
218 }
219
220 private static boolean isSelected(String currentAction, ParsedTabItem tabItem) {
221
/*
P/P * Method: bool isSelected(String, ParsedTabItem)
*
* Preconditions:
* currentAction != null
* tabItem != null
* init'ed(tabItem.action)
* (soft) init'ed(tabItem.subActions)
* (soft) tabItem.subActions.length <= 232-1
* (soft) init'ed(tabItem.subActions[...])
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* tabItem.subActions: Addr_Set{null}, Inverse{null}
* tabItem.subActions.length: {0}, {1..232-1}
* java.lang.String:equals(...)@222: {0}, {1}
* java.lang.String:equals(...)@230: {0}, {1}
*/
222 if(currentAction.equals(tabItem.getAction())) {
223 return true;
224 }
225
226 // an item is also considered selected if it's subforwards are the current action
227 String[] subActions = tabItem.getSubActions();
228 if(subActions != null && subActions.length > 0) {
229 for(int i=0; i < subActions.length; i++) {
230 if(currentAction.equals(subActions[i])) {
231 return true;
232 }
233 }
234 }
235
236 return false;
237 }
238
239
240 /**
241 * Unmarshall the given input stream into our defined
242 * set of Java objects.
243 **/
244 private static ParsedMenu unmarshall(InputStream instream)
245 throws IOException, JDOMException {
246
/*
P/P * Method: ParsedMenu unmarshall(InputStream)
*
* Preconditions:
* instream != null
*
* Presumptions:
* java.util.Iterator:next(...)@259 != null
* org.jdom.Document:getRootElement(...)@255 != null
* org.jdom.Element:getChildren(...)@256 != null
* org.jdom.input.SAXBuilder:build(...)@253 != null
*
* Postconditions:
* return_value == &new ParsedMenu(unmarshall#1)
* new ArrayList(ParsedMenu#1) num objects == 1
* new ParsedMenu(unmarshall#1) num objects == 1
* return_value.tabs == &new ArrayList(ParsedMenu#1)
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@258: {0}, {1}
*/
247 if(instream == null)
248 throw new IOException("InputStream is null!");
249
250 ParsedMenu config = new ParsedMenu();
251
252 SAXBuilder builder = new SAXBuilder();
253 Document doc = builder.build(instream);
254
255 Element root = doc.getRootElement();
256 List menus = root.getChildren("menu");
257 Iterator iter = menus.iterator();
258 while (iter.hasNext()) {
259 Element e = (Element) iter.next();
260 config.addTab(elementToParsedTab(e));
261 }
262
263 return config;
264 }
265
266
267 private static ParsedTab elementToParsedTab(Element element) {
268
/*
P/P * Method: ParsedTab elementToParsedTab(Element)
*
* Preconditions:
* element != null
*
* Presumptions:
* java.util.Iterator:next(...)@280 != null
* org.jdom.Element:getChildren(...)@277 != null
*
* Postconditions:
* return_value == &new ParsedTab(elementToParsedTab#1)
* new ArrayList(ParsedTab#1) num objects == 1
* new ParsedTab(elementToParsedTab#1) num objects == 1
* init'ed(return_value.disabledProperty)
* init'ed(return_value.enabledProperty)
* init'ed(return_value.name)
* init'ed(return_value.perm)
* init'ed(return_value.role)
* return_value.tabItems == &new ArrayList(ParsedTab#1)
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@279: {0}, {1}
*/
269 ParsedTab tab = new ParsedTab();
270
271 tab.setName(element.getAttributeValue("name"));
272 tab.setPerm(element.getAttributeValue("perms"));
273 tab.setRole(element.getAttributeValue("roles"));
274 tab.setEnabledProperty(element.getAttributeValue("enabledProperty"));
275 tab.setDisabledProperty(element.getAttributeValue("disabledProperty"));
276
277 List menuItems = element.getChildren("menu-item");
278 Iterator iter = menuItems.iterator();
279 while (iter.hasNext()) {
280 Element e = (Element) iter.next();
281 tab.addItem(elementToParsedTabItem(e));
282 }
283
284 return tab;
285 }
286
287
288 private static ParsedTabItem elementToParsedTabItem(Element element) {
289
/*
P/P * Method: ParsedTabItem elementToParsedTabItem(Element)
*
* Preconditions:
* element != null
*
* Postconditions:
* java.lang.String:split(...)._tainted == 0
* return_value == &new ParsedTabItem(elementToParsedTabItem#1)
* new ParsedTabItem(elementToParsedTabItem#1) num objects == 1
* init'ed(return_value.action)
* init'ed(return_value.disabledProperty)
* init'ed(return_value.enabledProperty)
* init'ed(return_value.name)
* init'ed(return_value.perm)
* init'ed(return_value.role)
* return_value.subActions in Addr_Set{null,&java.lang.String:split(...)}
*
* Test Vectors:
* org.jdom.Element:getAttributeValue(...)@295: Addr_Set{null}, Inverse{null}
*/
290 ParsedTabItem tabItem = new ParsedTabItem();
291
292 tabItem.setName(element.getAttributeValue("name"));
293 tabItem.setAction(element.getAttributeValue("action"));
294
295 String subActions = element.getAttributeValue("subactions");
296 if(subActions != null) {
297 tabItem.setSubActions(subActions.split(","));
298 }
299
300 tabItem.setPerm(element.getAttributeValue("perms"));
301 tabItem.setRole(element.getAttributeValue("roles"));
302 tabItem.setEnabledProperty(element.getAttributeValue("enabledProperty"));
303 tabItem.setDisabledProperty(element.getAttributeValue("disabledProperty"));
304
305 return tabItem;
306 }
307
308 }
SofCheck Inspector Build Version : 2.18479
| MenuHelper.java |
2009-Jan-02 14:24:54 |
| MenuHelper.class |
2009-Sep-04 03:12:44 |