KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > taglib > AbstractTag


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.taglib;
24
25 import java.io.*;
26 import java.util.*;
27 import javax.servlet.http.*;
28 import javax.servlet.jsp.*;
29 import javax.servlet.jsp.tagext.*;
30 import org.meshcms.core.*;
31 import org.meshcms.util.*;
32 import com.opensymphony.module.sitemesh.*;
33 import com.opensymphony.module.sitemesh.util.*;
34
35 /**
36  * This class works as a base for all tags of the MeshCMS custom tags library.
37  */

38 public abstract class AbstractTag extends TagSupport implements RequestConstants {
39   /**
40    * Name of the page context attribute that stores the descriptions of the
41    * modules contained in a page.
42    */

43   public static final String JavaDoc PAGE_MODULES = "page_modules";
44
45   WebSite webSite;
46   HttpServletRequest request;
47   Path pagePath;
48   UserInfo userInfo;
49   String JavaDoc cp, ap, afp;
50   boolean isEdit;
51
52   /**
53    * Initializes some variables, then executes <code>writeTag()</code> if the
54    * page is being viewed or <code>writeEditTag()</code> if it is being edited.
55    *
56    * @return the value returned by {@link #getStartTagReturnValue}
57    */

58   public int doStartTag() throws JspException {
59     request = (HttpServletRequest) pageContext.getRequest();
60     webSite = (WebSite) request.getAttribute("webSite");
61     pagePath = webSite.getRequestedPath(request);
62     userInfo = (UserInfo)
63         pageContext.getAttribute("userInfo", PageContext.SESSION_SCOPE);
64     cp = request.getContextPath();
65     ap = "/" + webSite.getAdminPath();
66     afp = cp + ap;
67     isEdit = HitFilter.ACTION_EDIT.equals(request.getParameter(HitFilter.ACTION_NAME)) &&
68              userInfo != null && userInfo.canWrite(webSite, pagePath);
69
70     try {
71       if (isEdit) {
72         writeEditTag();
73       } else {
74         writeTag();
75       }
76     } catch (IOException ex) {
77       throw new JspException(ex);
78       // pageContext.getServletContext().log("Can't write", ex);
79
}
80
81     return getStartTagReturnValue();
82   }
83
84   /**
85    * Defines the return value of <code>doStartTag()</code>. This method can be
86    * overridden by subclasses to change that value. The default implementation
87    * returns SKIP_BODY.
88    *
89    * @see #doStartTag
90    */

91   public int getStartTagReturnValue() {
92     return SKIP_BODY;
93   }
94
95   /**
96    * Writes the contents of the tag. Subclasses will use this method to write
97    * to the page.
98    */

99   public abstract void writeTag() throws IOException, JspException;
100
101   /**
102    * Writes the contents of the tag when the page is being edited. The default
103    * implementation calls <code>writeTag()</code>. Subclasses can override it
104    * when they behave differently while editing.
105    */

106   public void writeEditTag() throws IOException, JspException {
107     writeTag();
108   }
109
110   /**
111    * Copied from com.opensymphony.module.sitemesh.taglib.AbstactTag for
112    * compatibility with SiteMesh
113    */

114   protected Page getPage() {
115     Page p = (Page) pageContext.getAttribute(PAGE, PageContext.PAGE_SCOPE);
116
117     if (p == null) {
118       p = (Page) pageContext.getAttribute(PAGE, PageContext.REQUEST_SCOPE);
119
120       if (p == null) {
121         pageContext.removeAttribute(PAGE, PageContext.PAGE_SCOPE);
122       } else {
123         pageContext.setAttribute(PAGE, p, PageContext.PAGE_SCOPE);
124       }
125
126       pageContext.removeAttribute(PAGE, PageContext.REQUEST_SCOPE);
127     }
128
129     if (p == null) {
130       // No page? Weird! Better to block the cache at least.
131
WebUtils.setBlockCache(request);
132     }
133
134     return p;
135   }
136
137   /**
138    * Copied from com.opensymphony.module.sitemesh.taglib.AbstactTag for
139    * compatibility with SiteMesh
140    */

141   protected Writer getOut() {
142     return OutputConverter.getWriter(pageContext.getOut());
143   }
144
145   ModuleDescriptor getModuleDescriptor(String JavaDoc location, String JavaDoc name) {
146     ModuleDescriptor md;
147
148     if (name == null) {
149       Map pageModules = (Map) pageContext.getAttribute(PAGE_MODULES);
150
151       if (pageModules == null) {
152         pageModules = new HashMap();
153         String JavaDoc[] modules = Utils.tokenize
154             (getPage().getProperty(PageAssembler.MODULES_PARAM), ";");
155
156         if (modules != null) {
157           for (int i = 0; i < modules.length; i++) {
158             ModuleDescriptor md0 = new ModuleDescriptor(modules[i]);
159
160             if (md0.isValid()) {
161               pageModules.put(md0.getLocation(), md0);
162             }
163           }
164         }
165
166         pageContext.setAttribute(PAGE_MODULES, pageModules, PageContext.PAGE_SCOPE);
167       }
168
169       md = (ModuleDescriptor) pageModules.get(location);
170     } else {
171       md = new ModuleDescriptor(name);
172       md.setLocation(location);
173     }
174
175     return md;
176   }
177 }
178
Popular Tags