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