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;
022import java.util.Map;
023
024import javax.servlet.jsp.JspException;
025import javax.servlet.jsp.tagext.BodyContent;
026
027import org.apache.log4j.Logger;
028import org.apache.wiki.WikiEngine;
029import org.apache.wiki.api.engine.PluginManager;
030import org.apache.wiki.api.exceptions.PluginException;
031
032/**
033 *  Inserts any Wiki plugin.  The body of the tag becomes then
034 *  the body for the plugin.
035 *  <P><B>Attributes</B></P>
036 *  <UL>
037 *    <LI>plugin - name of the plugin you want to insert.
038 *    <LI>args   - An argument string for the tag.
039 *  </UL>
040 *
041 *  @since 2.0
042 */
043public class PluginTag
044    extends WikiBodyTag
045{
046    private static final long serialVersionUID = 0L;
047    private static final Logger log = Logger.getLogger( PluginTag.class );
048    
049    private String m_plugin;
050    private String m_args;
051
052    private boolean m_evaluated = false;
053
054    /**
055     *  {@inheritDoc}
056     */
057    @Override
058    public void release()
059    {
060        super.release();
061        m_plugin = m_args = null;
062        m_evaluated = false;
063    }
064    
065    /**
066     *  Set the name of the plugin to execute.
067     *  
068     *  @param p Name of the plugin.
069     */
070    public void setPlugin( String p )
071    {
072        m_plugin = p;
073    }
074
075    /**
076     *  Set the argument string to the plugin.
077     *  
078     *  @param a Arguments string.
079     */
080    public void setArgs( String a )
081    {
082        m_args = a;
083    }
084    
085    /**
086     *  {@inheritDoc}
087     */
088    @Override
089    public int doWikiStartTag() throws JspException, IOException
090    {
091        m_evaluated = false;
092        return EVAL_BODY_BUFFERED;
093    }
094
095    private String executePlugin( String plugin, String args, String body )
096        throws PluginException, IOException
097    {
098        WikiEngine engine = m_wikiContext.getEngine();
099        PluginManager pm  = engine.getPluginManager();
100
101        m_evaluated = true;
102
103        Map<String, String> argmap = pm.parseArgs( args );
104        
105        if( body != null ) 
106        {
107            argmap.put( "_body", body );
108        }
109
110        String result = pm.execute( m_wikiContext, plugin, argmap );
111
112        return result;        
113    }
114    
115    /**
116     *  {@inheritDoc}
117     */
118    @Override
119    public int doEndTag()
120        throws JspException
121    {
122        if( !m_evaluated )
123        {
124            try
125            {
126                pageContext.getOut().write( executePlugin( m_plugin, m_args, null ) );
127            }
128            catch( Exception e )
129            {
130                log.error( "Failed to insert plugin", e );
131                throw new JspException( "Tag failed, check logs: "+e.getMessage() );
132            }
133        }
134        return EVAL_PAGE;
135    }
136    
137    /**
138     *  {@inheritDoc}
139     */
140    @Override
141    public int doAfterBody()
142        throws JspException
143    {
144        try
145        {
146            BodyContent bc = getBodyContent();
147            
148            getPreviousOut().write( executePlugin( m_plugin, m_args, (bc != null) ? bc.getString() : null) );
149        }
150        catch( Exception e )
151        {
152            log.error( "Failed to insert plugin", e );
153            throw new JspException( "Tag failed, check logs: "+e.getMessage() );
154        }
155        
156        return SKIP_BODY;
157    }
158}