001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.menu;
024:
025: import java.util.ArrayList;
026: import java.util.List;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpSession;
030:
031: import org.apache.log4j.Logger;
032:
033: import biz.hammurapi.config.Context;
034: import biz.hammurapi.web.RequestContext;
035:
036: public class MenuNavigator implements Context {
037: private static final Logger logger = Logger
038: .getLogger(MenuNavigator.class);
039:
040: private static final String PIPE = "|";
041: private HttpServletRequest request;
042:
043: public MenuNavigator(HttpServletRequest request) {
044: this .request = request;
045: }
046:
047: /**
048: * Tries request first, then session.
049: * @return Menu
050: */
051: private Menu getMenu() {
052: Menu ret = (Menu) request.getAttribute(Menu.class.getName());
053: if (ret == null) {
054: HttpSession session = request.getSession(false);
055: if (session != null) {
056: ret = (Menu) session.getAttribute(Menu.class.getName());
057: }
058: }
059: return ret;
060: }
061:
062: /**
063: * Splits string at pipes
064: * @param str
065: * @return
066: */
067: public static String[] splitAtPipe(String str) {
068: String[] ret = { null, null };
069: if (!Menu.isBlank(str)) {
070: int pidx = str.indexOf(PIPE);
071: if (pidx == -1) {
072: ret[0] = str;
073: } else {
074: if (pidx > 0) {
075: ret[0] = str.substring(0, pidx);
076: }
077: if (pidx < str.length() - 1) {
078: ret[1] = str.substring(pidx + 1);
079: }
080: }
081: }
082:
083: return ret;
084: }
085:
086: public Object get(String path) {
087: Context rc = new RequestContext(request);
088:
089: if ("formHandler".equals(path)) { // Shortcut to formHandler
090: Menu matched = getMatched();
091: return matched == null ? null : matched.get("formHandler",
092: rc);
093: }
094:
095: int idx = path.indexOf(PIPE);
096: String iPath = idx == -1 ? path : path.substring(0, idx);
097: int hidx = iPath.lastIndexOf("#");
098:
099: Menu startMenu = getMatched();
100: if (startMenu == null) {
101: startMenu = getMenu();
102: }
103:
104: Menu toRender = null;
105: if (hidx == -1) {
106: toRender = startMenu.findItem(iPath);
107: } else if (hidx == 0) {
108: toRender = startMenu.findItem("path:.");
109: } else {
110: toRender = startMenu.findItem(iPath.substring(0, hidx));
111: }
112:
113: if (toRender == null) {
114: logger.info("Item not found: "
115: + (hidx == -1 ? iPath : iPath.substring(0, hidx)));
116: } else if (hidx != -1) {
117: String actionName = iPath.substring(hidx + 1);
118: int menuId = toRender.getId();
119: toRender = toRender.getAction(actionName);
120: if (toRender == null) {
121: logger.info("Action not found '" + actionName
122: + "' in menu " + menuId);
123: }
124: }
125:
126: if (idx == -1) {
127: return toRender == null ? "" : toRender.get("link", rc);
128: }
129:
130: String[] token = splitAtPipe(path.substring(idx + 1));
131: String format = token[0];
132: List tokens = new ArrayList();
133: for (String[] tkn = splitAtPipe(token[1]); tkn[0] != null; tkn = splitAtPipe(tkn[1])) {
134: tokens.add(tkn[0]);
135: }
136:
137: String[] ta = (String[]) tokens.toArray(new String[tokens
138: .size()]);
139:
140: if (toRender == null) {
141: if ("label".equals(format) && ta.length > 0) {
142: return ta[0];
143: }
144:
145: if ("href".equals(format)) {
146: return "#";
147: }
148:
149: return "";
150: }
151:
152: String ret = (String) toRender.get(format, ta, rc);
153: if (ret == null) {
154: logger.info("Invalid format: " + path.substring(idx + 1));
155: }
156: return ret == null ? "" : ret;
157: }
158:
159: private Menu getMatched() {
160: Menu ret = (Menu) request
161: .getAttribute(MenuFilter.MENU_MATCHED_ATTRIBUTE);
162: if (ret == null) {
163: ret = (Menu) request.getSession().getAttribute(
164: MenuFilter.MENU_MATCHED_ATTRIBUTE);
165: }
166: return ret;
167: }
168:
169: }
|