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    @Override
051    public void setPageContext( final PageContext arg0 ) {
052        super.setPageContext( arg0 );
053        initTag();
054    }
055
056    /**
057     *  This method is called when the tag is encountered within a new request, but before the setXXX() methods are called.
058     *  The default implementation does nothing.
059     *  @since 2.3.92
060     */
061    public void initTag() {
062        m_wikiContext = null;
063    }
064    
065    public int doStartTag() throws JspException {
066        try {
067            m_wikiContext = ( Context )pageContext.getAttribute( Context.ATTR_CONTEXT, PageContext.REQUEST_SCOPE );
068            if( m_wikiContext == null ) {
069                throw new JspException("WikiContext may not be NULL - serious internal problem!");
070            }
071
072            return doWikiStartTag();
073        } catch( final Exception e ) {
074            LOG.error( "Tag failed", e );
075            throw new JspException( "Tag failed, check logs: "+e.getMessage() );
076        }
077    }
078
079    /**
080     *  This method is allowed to do pretty much whatever he wants.
081     *  We then catch all mistakes.
082     */
083    public abstract int doWikiStartTag() throws Exception;
084
085    public int doEndTag() throws JspException {
086        return EVAL_PAGE;
087    }
088
089    @Override
090    public void doCatch( final Throwable th ) throws Throwable {
091        LOG.error( th.getMessage(), th );
092    }
093
094    public void doFinally()
095    {
096        m_wikiContext = null;
097    }
098
099    public void setId( final String id)
100    {
101        super.setId( TextUtil.replaceEntities( id ) );
102    }
103
104}