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.core.Context;
023import org.apache.wiki.render.RenderingManager;
024
025import javax.servlet.jsp.JspException;
026import javax.servlet.jsp.PageContext;
027import javax.servlet.jsp.tagext.BodyContent;
028import javax.servlet.jsp.tagext.BodyTagSupport;
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    private static final Logger log = Logger.getLogger( TranslateTag.class );
041
042    @Override
043    public final int doAfterBody() throws JspException {
044        try {
045            Context context = (Context) pageContext.getAttribute( Context.ATTR_CONTEXT, PageContext.REQUEST_SCOPE );
046
047            //  Because the TranslateTag should not affect any of the real page attributes we have to make a clone here.
048            context = context.deepClone();
049            
050            //  Get the page data.
051            final BodyContent bc = getBodyContent();
052            String wikiText = bc.getString();
053            bc.clearBody();
054
055            if( wikiText != null ) {
056                wikiText = wikiText.trim();
057                final String result = context.getEngine().getManager( RenderingManager.class ).textToHTML( context, wikiText );
058                getPreviousOut().write( result );
059            }
060        } catch( final Exception e ) {
061            log.error( "Tag failed", e );
062            throw new JspException( "Tag failed, check logs: " + e.getMessage() );
063        }
064
065        return SKIP_BODY;
066    }
067
068}