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.exceptions.ProviderException;
023import org.apache.wiki.ui.TemplateManager;
024import org.apache.wiki.util.TextUtil;
025
026import javax.servlet.ServletException;
027import javax.servlet.jsp.JspException;
028import java.io.IOException;
029
030/**
031 *  Includes an another JSP page, making sure that we actually pass the WikiContext correctly.
032 *
033 *  @since 2.0
034 */
035// FIXME: Perhaps unnecessary?
036public class IncludeTag extends WikiTagBase {
037
038    private static final long serialVersionUID = 0L;
039    private static final Logger log = Logger.getLogger( IncludeTag.class );
040    
041    protected String m_page;
042
043    @Override
044    public void initTag() {
045        super.initTag();
046        m_page = null;
047    }
048
049    public void setPage( final String page )
050    {
051        m_page = page;
052    }
053
054    public String getPage()
055    {
056        return m_page;
057    }
058
059    @Override
060    public final int doWikiStartTag() throws IOException, ProviderException {
061        return SKIP_BODY;
062    }
063
064    @Override
065    public final int doEndTag() throws JspException {
066        try {
067            final String page = m_wikiContext.getEngine().getManager( TemplateManager.class ).findJSP( pageContext,
068                                                                                                 m_wikiContext.getTemplate(),
069                                                                                                 m_page );
070
071            if( page == null ) {
072                pageContext.getOut().println( "No template file called '" + TextUtil.replaceEntities( m_page ) + "'" );
073            } else {
074                pageContext.include( page );
075            }
076        } catch( final ServletException e ) {
077            log.warn( "Including failed, got a servlet exception from sub-page. Rethrowing the exception to the JSP engine.", e );
078            throw new JspException( e.getMessage() );
079        } catch( final IOException e ) {
080            log.warn( "I/O exception - probably the connection was broken. Rethrowing the exception to the JSP engine.", e );
081            throw new JspException( e.getMessage() );
082        }
083
084        return EVAL_PAGE;
085    }
086
087}