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 javax.servlet.jsp.JspException; 022import javax.servlet.jsp.PageContext; 023import javax.servlet.jsp.tagext.TagSupport; 024import javax.servlet.jsp.tagext.TryCatchFinally; 025 026import org.apache.log4j.Logger; 027 028import org.apache.wiki.WikiContext; 029import org.apache.wiki.util.TextUtil; 030 031/** 032 * Base class for JSPWiki tags. You do not necessarily have 033 * to derive from this class, since this does some initialization. 034 * <P> 035 * This tag is only useful if you're having an "empty" tag, with 036 * no body content. 037 * 038 * @since 2.0 039 */ 040public abstract class WikiTagBase 041 extends TagSupport 042 implements TryCatchFinally 043{ 044 private static final long serialVersionUID = -1409836349293777141L; 045 046 public static final String ATTR_CONTEXT = "jspwiki.context"; 047 048 private static final Logger log = Logger.getLogger( WikiTagBase.class ); 049 050 protected WikiContext m_wikiContext; 051 052 /** 053 * This method calls the parent setPageContext() but it also 054 * provides a way for a tag to initialize itself before 055 * any of the setXXX() methods are called. 056 */ 057 public void setPageContext(PageContext arg0) 058 { 059 super.setPageContext(arg0); 060 061 initTag(); 062 } 063 064 /** 065 * This method is called when the tag is encountered within a new request, 066 * but before the setXXX() methods are called. 067 * The default implementation does nothing. 068 * @since 2.3.92 069 */ 070 public void initTag() 071 { 072 m_wikiContext = null; 073 return; 074 } 075 076 public int doStartTag() 077 throws JspException 078 { 079 try 080 { 081 m_wikiContext = (WikiContext) pageContext.getAttribute( ATTR_CONTEXT, 082 PageContext.REQUEST_SCOPE ); 083 084 if( m_wikiContext == null ) 085 { 086 throw new JspException("WikiContext may not be NULL - serious internal problem!"); 087 } 088 089 return doWikiStartTag(); 090 } 091 catch( Exception e ) 092 { 093 log.error( "Tag failed", e ); 094 throw new JspException( "Tag failed, check logs: "+e.getMessage() ); 095 } 096 } 097 098 /** 099 * This method is allowed to do pretty much whatever he wants. 100 * We then catch all mistakes. 101 */ 102 public abstract int doWikiStartTag() throws Exception; 103 104 public int doEndTag() 105 throws JspException 106 { 107 return EVAL_PAGE; 108 } 109 110 public void doCatch( Throwable th ) throws Throwable 111 { 112 log.error( th.getMessage(), th ); 113 } 114 115 public void doFinally() 116 { 117 m_wikiContext = null; 118 } 119 120 public void setId(String id) 121 { 122 super.setId( TextUtil.replaceEntities( id ) ); 123 } 124 125}