KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > pagestack > PageStackTag


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.pagestack;
14
15 import java.util.Iterator JavaDoc;
16
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.jsp.JspTagException JavaDoc;
19 import javax.servlet.jsp.jstl.core.LoopTagSupport;
20 import javax.servlet.jsp.jstl.fmt.LocaleSupport;
21
22 import com.tonbeller.tbutils.res.Resources;
23 import com.tonbeller.wcf.controller.RequestContext;
24 import com.tonbeller.wcf.expr.ExprUtils;
25 import com.tonbeller.wcf.token.RequestToken;
26
27 /**
28  * @author av
29  */

30 public class PageStackTag extends LoopTagSupport {
31   String JavaDoc page;
32   String JavaDoc pageId;
33   String JavaDoc title;
34   String JavaDoc key;
35   boolean clear;
36   String JavaDoc bundle;
37
38   Iterator JavaDoc iter;
39
40   protected void prepare() throws JspTagException JavaDoc {
41     String JavaDoc s = makeTitle();
42     PageStack stack = PageStack.instance(pageContext.getSession());
43     if (clear)
44       stack.clear();
45     if (page != null && s != null) {
46       String JavaDoc path = addContextPath(page);
47       Page curPg = new Page(pageId, path, page, s);
48       curPg.setRequestToken(RequestToken.instance(pageContext.getSession()));
49       stack.setCurrentPage(curPg);
50     }
51     iter = stack.iterator();
52   }
53
54   private String JavaDoc addContextPath(String JavaDoc path) {
55     // prepend context
56
if (path.startsWith("/")) {
57       HttpServletRequest JavaDoc hsr = (HttpServletRequest JavaDoc) pageContext.getRequest();
58       path = hsr.getContextPath() + path;
59     }
60     return path;
61   }
62
63   protected Object JavaDoc next() throws JspTagException JavaDoc {
64     return iter.next();
65   }
66
67   protected boolean hasNext() throws JspTagException JavaDoc {
68     return iter.hasNext();
69   }
70   
71   private String JavaDoc makeTitle() {
72     // use resbundle?
73
if (key == null)
74       return eval(title);
75     
76     // bundle given?
77
if (bundle != null) {
78       RequestContext context = RequestContext.instance();
79       Resources res = context.getResources(bundle);
80       return res.getOptionalString(key, key);
81     }
82     
83     // JSTL Locale support
84
return LocaleSupport.getLocalizedMessage(pageContext, key);
85   }
86
87   public void setTitle(String JavaDoc title) {
88     this.title = title;
89   }
90
91   public void setClear(boolean clear) {
92     this.clear = clear;
93   }
94
95   public void setPage(String JavaDoc page) {
96     this.page = page;
97   }
98
99   public void setPageId(String JavaDoc pageId) {
100     this.pageId = pageId;
101   }
102
103   public void setKey(String JavaDoc key) {
104     this.key = key;
105   }
106
107   public void setBundle(String JavaDoc bundle) {
108     this.bundle = bundle;
109   }
110   
111   private String JavaDoc eval(String JavaDoc el) {
112     if (ExprUtils.isExpression(el))
113       return String.valueOf(ExprUtils.getModelReference(pageContext, el));
114     return el;
115   }
116
117 }
Popular Tags