001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.tags; 020 021import org.apache.log4j.Logger; 022import org.apache.wiki.api.core.Context; 023import org.apache.wiki.util.TextUtil; 024 025import javax.servlet.jsp.JspException; 026import javax.servlet.jsp.PageContext; 027import javax.servlet.jsp.tagext.TagSupport; 028import javax.servlet.jsp.tagext.TryCatchFinally; 029 030 031/** 032 * Base class for JSPWiki tags. You do not necessarily have to derive from this class, since this does some initialization. 033 * <P> 034 * This tag is only useful if you're having an "empty" tag, with no body content. 035 * 036 * @since 2.0 037 */ 038public abstract class WikiTagBase extends TagSupport implements TryCatchFinally { 039 040 private static final long serialVersionUID = -1409836349293777141L; 041 private static final Logger log = Logger.getLogger( WikiTagBase.class ); 042 043 protected Context m_wikiContext; 044 045 /** 046 * This method calls the parent setPageContext() but it also provides a way for a tag to initialize itself before 047 * any of the setXXX() methods are called. 048 */ 049 public void setPageContext( final PageContext arg0 ) { 050 super.setPageContext( arg0 ); 051 initTag(); 052 } 053 054 /** 055 * This method is called when the tag is encountered within a new request, but before the setXXX() methods are called. 056 * The default implementation does nothing. 057 * @since 2.3.92 058 */ 059 public void initTag() { 060 m_wikiContext = null; 061 } 062 063 public int doStartTag() throws JspException { 064 try { 065 m_wikiContext = ( Context )pageContext.getAttribute( Context.ATTR_CONTEXT, PageContext.REQUEST_SCOPE ); 066 if( m_wikiContext == null ) { 067 throw new JspException("WikiContext may not be NULL - serious internal problem!"); 068 } 069 070 return doWikiStartTag(); 071 } catch( final Exception e ) { 072 log.error( "Tag failed", e ); 073 throw new JspException( "Tag failed, check logs: "+e.getMessage() ); 074 } 075 } 076 077 /** 078 * This method is allowed to do pretty much whatever he wants. 079 * We then catch all mistakes. 080 */ 081 public abstract int doWikiStartTag() throws Exception; 082 083 public int doEndTag() throws JspException { 084 return EVAL_PAGE; 085 } 086 087 public void doCatch( final Throwable th ) throws Throwable { 088 log.error( th.getMessage(), th ); 089 } 090 091 public void doFinally() 092 { 093 m_wikiContext = null; 094 } 095 096 public void setId( final String id) 097 { 098 super.setId( TextUtil.replaceEntities( id ) ); 099 } 100 101}