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