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 javax.servlet.jsp.JspException;
022import javax.servlet.jsp.PageContext;
023import javax.servlet.jsp.tagext.BodyContent;
024import javax.servlet.jsp.tagext.BodyTagSupport;
025
026import org.apache.wiki.WikiContext;
027
028import org.apache.log4j.Logger;
029
030/**
031 *  Converts the body text into HTML content.
032 *
033 *  @since 2.0
034 */
035public class TranslateTag
036    extends BodyTagSupport
037{
038    private static final long serialVersionUID = 0L;
039    
040    static    Logger    log = Logger.getLogger( TranslateTag.class );
041
042    public final int doAfterBody()
043        throws JspException
044    {
045        try
046        {
047            WikiContext context = (WikiContext) pageContext.getAttribute( WikiTagBase.ATTR_CONTEXT,
048                                                                          PageContext.REQUEST_SCOPE );
049
050            //
051            //  Because the TranslateTag should not affect any of the real page attributes
052            //  we have to make a clone here.
053            //
054            
055            context = context.deepClone();
056            
057            //
058            //  Get the page data.
059            //
060            BodyContent bc = getBodyContent();
061            String wikiText = bc.getString();
062            bc.clearBody();
063
064            if( wikiText != null )
065            {
066                wikiText = wikiText.trim();
067            
068                String result = context.getEngine().textToHTML( context, wikiText );
069
070                getPreviousOut().write( result );
071            }
072        }
073        catch( Exception e )
074        {
075            log.error( "Tag failed", e );
076            throw new JspException( "Tag failed, check logs: "+e.getMessage() );
077        }
078
079        return SKIP_BODY;
080    }
081}