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.Engine;
024import org.apache.wiki.api.exceptions.PluginException;
025import org.apache.wiki.plugin.PluginManager;
026
027import javax.servlet.jsp.JspException;
028import javax.servlet.jsp.tagext.BodyContent;
029import java.io.IOException;
030import java.util.Map;
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 = LogManager.getLogger( PluginTag.class );
048    
049    private String m_plugin;
050    private String m_args;
051
052    private boolean m_evaluated;
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( final 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( final 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( final String plugin, final String args, final String body ) throws PluginException, IOException {
096        final Engine engine = m_wikiContext.getEngine();
097        final PluginManager pm  = engine.getManager( PluginManager.class );
098
099        m_evaluated = true;
100
101        final Map<String, String> argmap = pm.parseArgs( args );
102        
103        if( body != null ) 
104        {
105            argmap.put( "_body", body );
106        }
107
108        return pm.execute( m_wikiContext, plugin, argmap );
109    }
110    
111    /**
112     *  {@inheritDoc}
113     */
114    @Override
115    public int doEndTag()
116        throws JspException
117    {
118        if( !m_evaluated )
119        {
120            try
121            {
122                pageContext.getOut().write( executePlugin( m_plugin, m_args, null ) );
123            }
124            catch( final Exception e )
125            {
126                log.error( "Failed to insert plugin", e );
127                throw new JspException( "Tag failed, check logs: "+e.getMessage() );
128            }
129        }
130        return EVAL_PAGE;
131    }
132    
133    /**
134     *  {@inheritDoc}
135     */
136    @Override
137    public int doAfterBody()
138        throws JspException
139    {
140        try
141        {
142            final BodyContent bc = getBodyContent();
143            
144            getPreviousOut().write( executePlugin( m_plugin, m_args, (bc != null) ? bc.getString() : null) );
145        }
146        catch( final Exception e )
147        {
148            log.error( "Failed to insert plugin", e );
149            throw new JspException( "Tag failed, check logs: "+e.getMessage() );
150        }
151        
152        return SKIP_BODY;
153    }
154}