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