001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (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
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.tags;
021:
022: import javax.servlet.jsp.JspException;
023: import javax.servlet.jsp.PageContext;
024: import javax.servlet.jsp.tagext.TagSupport;
025: import javax.servlet.jsp.tagext.TryCatchFinally;
026:
027: import org.apache.log4j.Logger;
028:
029: import com.ecyrd.jspwiki.TextUtil;
030: import com.ecyrd.jspwiki.WikiContext;
031:
032: /**
033: * Base class for JSPWiki tags. You do not necessarily have
034: * to derive from this class, since this does some initialization.
035: * <P>
036: * This tag is only useful if you're having an "empty" tag, with
037: * no body content.
038: *
039: * @author Janne Jalkanen
040: * @since 2.0
041: */
042: public abstract class WikiTagBase extends TagSupport implements
043: TryCatchFinally {
044: public static final String ATTR_CONTEXT = "jspwiki.context";
045:
046: static Logger log = Logger.getLogger(WikiTagBase.class);
047:
048: protected WikiContext m_wikiContext;
049:
050: /**
051: * This method calls the parent setPageContext() but it also
052: * provides a way for a tag to initialize itself before
053: * any of the setXXX() methods are called.
054: */
055: public void setPageContext(PageContext arg0) {
056: super .setPageContext(arg0);
057:
058: initTag();
059: }
060:
061: /**
062: * This method is called when the tag is encountered within a new request,
063: * but before the setXXX() methods are called.
064: * The default implementation does nothing.
065: * @since 2.3.92
066: */
067: public void initTag() {
068: m_wikiContext = null;
069: return;
070: }
071:
072: public int doStartTag() throws JspException {
073: try {
074: m_wikiContext = (WikiContext) pageContext.getAttribute(
075: ATTR_CONTEXT, PageContext.REQUEST_SCOPE);
076:
077: if (m_wikiContext == null) {
078: throw new JspException(
079: "WikiContext may not be NULL - serious internal problem!");
080: }
081:
082: return doWikiStartTag();
083: } catch (Exception e) {
084: log.error("Tag failed", e);
085: throw new JspException("Tag failed, check logs: "
086: + e.getMessage());
087: }
088: }
089:
090: /**
091: * This method is allowed to do pretty much whatever he wants.
092: * We then catch all mistakes.
093: */
094: public abstract int doWikiStartTag() throws Exception;
095:
096: public int doEndTag() throws JspException {
097: return EVAL_PAGE;
098: }
099:
100: public void doCatch(Throwable arg0) throws Throwable {
101: }
102:
103: public void doFinally() {
104: m_wikiContext = null;
105: }
106:
107: public void setId(String id) {
108: super.setId(TextUtil.replaceEntities(id));
109: }
110:
111: }
|