//# 0 errors, 73 messages
//#
/*
    //#VelocityRenderer.java:1:1: class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
 * Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  The ASF licenses this file to You
 * under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.  For additional information regarding
 * copyright in this work, please see the NOTICE file in the top level
 * directory of this distribution.
 */

package org.apache.roller.weblogger.ui.rendering.velocity;

import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.pojos.Template;
import org.apache.roller.weblogger.pojos.ThemeTemplate;
import org.apache.roller.weblogger.ui.rendering.Renderer;
import org.apache.roller.weblogger.ui.rendering.RenderingException;
import org.apache.roller.weblogger.ui.rendering.model.UtilitiesModel;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.context.Context;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;


/**
 * Renderer that renders using the Velocity template engine.
 */
public class VelocityRenderer implements Renderer {
    
    private static Log log = LogFactory.getLog(VelocityRenderer.class);
    //#VelocityRenderer.java:42: method: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer.org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer__static_init
    //#VelocityRenderer.java:42: Warning: method not available
    //#    -- call on Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer__static_init
    //#    unanalyzed callee: Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#output(org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer__static_init): __Descendant_Table[org/apache/roller/weblogger/ui/rendering/velocity/VelocityRenderer]
    //#output(org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer__static_init): __Dispatch_Table.render(Ljava/util/Map;Ljava/io/Writer;)V
    //#output(org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer__static_init): log
    //#post(org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer__static_init): __Descendant_Table[org/apache/roller/weblogger/ui/rendering/velocity/VelocityRenderer] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer__static_init): __Dispatch_Table.render(Ljava/util/Map;Ljava/io/Writer;)V == &render
    //#post(org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer__static_init): init'ed(log)
    //#VelocityRenderer.java:42: end of method: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer.org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer__static_init
    
    // the original template we are supposed to render
    private Template renderTemplate = null;
    
    // the velocity templates
    private org.apache.velocity.Template velocityTemplate = null;
    private org.apache.velocity.Template velocityDecorator = null;
    
    // a possible exception
    private Exception parseException = null;
    
    
    public VelocityRenderer(Template template) throws Exception {
    //#VelocityRenderer.java:55: method: void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer.org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)
    //#input(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): " due to ["._tainted
    //#input(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): "Error creating renderer for "._tainted
    //#input(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): "Unknown exception creatting renderer for "._tainted
    //#input(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): "]"._tainted
    //#input(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): org/apache/roller/weblogger/ui/rendering/velocity/RollerVelocity.velocityEngine
    //#input(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): template
    //#input(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): this
    //#output(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): this.parseException
    //#output(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): this.renderTemplate
    //#output(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): this.velocityDecorator
    //#output(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): this.velocityTemplate
    //#pre[2] (void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): org/apache/roller/weblogger/ui/rendering/velocity/RollerVelocity.velocityEngine != null
    //#pre[3] (void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): (soft) template != null
    //#post(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): init'ed(this.parseException)
    //#post(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): this.renderTemplate == template
    //#post(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): (soft) this.renderTemplate != null
    //#post(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): init'ed(this.velocityDecorator)
    //#post(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): init'ed(this.velocityTemplate)
    //#unanalyzed(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): Effects-of-calling:org.apache.velocity.app.VelocityEngine:getTemplate
    //#test_vector(void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)): org.apache.roller.weblogger.pojos.ThemeTemplate:getDecorator(...)@69: Addr_Set{null}, Inverse{null}
        
        // the Template we are supposed to render
        this.renderTemplate = template;
        
        try {
            // make sure that we can locate the template
            // if we can't then this will throw an exception
            velocityTemplate = RollerVelocity.getTemplate(template.getId(), "UTF-8");
    //#VelocityRenderer.java:63: Warning: method not available
    //#    -- call on String org.apache.roller.weblogger.pojos.Template:getId()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)
    //#    unanalyzed callee: String org.apache.roller.weblogger.pojos.Template:getId()
            
            // if this is a ThemeTemplate than look for a decorator too
            if(template instanceof ThemeTemplate) {
                ThemeTemplate templ = (ThemeTemplate) template;
                
                Template decorator = templ.getDecorator();
    //#VelocityRenderer.java:69: Warning: method not available
    //#    -- call on ThemeTemplate org.apache.roller.weblogger.pojos.ThemeTemplate:getDecorator()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)
    //#    unanalyzed callee: ThemeTemplate org.apache.roller.weblogger.pojos.ThemeTemplate:getDecorator()
                if(decorator != null) {
                    velocityDecorator = RollerVelocity.getTemplate(decorator.getId(), "UTF-8");
    //#VelocityRenderer.java:71: Warning: method not available
    //#    -- call on String org.apache.roller.weblogger.pojos.Template:getId()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)
    //#    unanalyzed callee: String org.apache.roller.weblogger.pojos.Template:getId()
                }
            }

        } catch(ResourceNotFoundException ex) {
            // velocity couldn't find the resource so lets log a warning
            log.warn("Error creating renderer for "+template.getId()+
                    " due to ["+ex.getMessage()+"]");
            
            // then just rethrow so that the caller knows this instantiation failed
            throw ex;
            
        } catch(ParseErrorException ex) {
            // in the case of a parsing error we want to render an
            // error page instead so the user knows what was wrong
            parseException = ex;
            
            // need to lookup error page template
            velocityTemplate = RollerVelocity.getTemplate("templates/error-page.vm");
            
        } catch(Exception ex) {
            // some kind of generic/unknown exception, dump it to the logs
            log.error("Unknown exception creatting renderer for "+template.getId(), ex);
            
            // throw if back to the caller
            throw ex;
        }
    }
    //#VelocityRenderer.java:98: end of method: void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer.org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer(Template)
    
    
    public void render(Map model, Writer out) throws RenderingException {
        
        try {
            if(parseException != null) {
    //#VelocityRenderer.java:104: method: void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer.render(Map, Writer)
    //#input(void render(Map, Writer)): " secs"._tainted
    //#input(void render(Map, Writer)): "Applying decorator "._tainted
    //#input(void render(Map, Writer)): "Rendered ["._tainted
    //#input(void render(Map, Writer)): "] in "._tainted
    //#input(void render(Map, Writer)): log
    //#input(void render(Map, Writer)): model
    //#input(void render(Map, Writer)): out
    //#input(void render(Map, Writer)): this
    //#input(void render(Map, Writer)): this.parseException
    //#input(void render(Map, Writer)): this.renderTemplate
    //#input(void render(Map, Writer)): this.velocityDecorator
    //#input(void render(Map, Writer)): this.velocityTemplate
    //#pre[6] (void render(Map, Writer)): init'ed(this.parseException)
    //#pre[8] (void render(Map, Writer)): this.renderTemplate != null
    //#pre[10] (void render(Map, Writer)): this.velocityTemplate != null
    //#pre[2] (void render(Map, Writer)): (soft) log != null
    //#pre[9] (void render(Map, Writer)): (soft) init'ed(this.velocityDecorator)
    //#test_vector(void render(Map, Writer)): this.parseException: Addr_Set{null}, Inverse{null}
    //#test_vector(void render(Map, Writer)): this.velocityDecorator: Addr_Set{null}, Inverse{null}
                
                Context ctx = new VelocityContext(model);
    //#VelocityRenderer.java:106: Warning: method not available
    //#    -- call on void org.apache.velocity.VelocityContext(Map)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void render(Map, Writer)
    //#    unanalyzed callee: void org.apache.velocity.VelocityContext(Map)
                ctx.put("exception", parseException);
    //#VelocityRenderer.java:107: Warning: method not available
    //#    -- call on Object org.apache.velocity.context.Context:put(String, Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void render(Map, Writer)
    //#    unanalyzed callee: Object org.apache.velocity.context.Context:put(String, Object)
                ctx.put("exceptionSource", renderTemplate.getId());
    //#VelocityRenderer.java:108: Warning: method not available
    //#    -- call on String org.apache.roller.weblogger.pojos.Template:getId()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void render(Map, Writer)
    //#    unanalyzed callee: String org.apache.roller.weblogger.pojos.Template:getId()
    //#VelocityRenderer.java:108: Warning: method not available
    //#    -- call on Object org.apache.velocity.context.Context:put(String, Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void render(Map, Writer)
    //#    unanalyzed callee: Object org.apache.velocity.context.Context:put(String, Object)
                ctx.put("utils", new UtilitiesModel());
    //#VelocityRenderer.java:109: Warning: method not available
    //#    -- call on void org.apache.roller.weblogger.ui.rendering.model.UtilitiesModel()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void render(Map, Writer)
    //#    unanalyzed callee: void org.apache.roller.weblogger.ui.rendering.model.UtilitiesModel()
    //#VelocityRenderer.java:109: Warning: method not available
    //#    -- call on Object org.apache.velocity.context.Context:put(String, Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void render(Map, Writer)
    //#    unanalyzed callee: Object org.apache.velocity.context.Context:put(String, Object)
                
                // render output to Writer
                velocityTemplate.merge(ctx, out);
    //#VelocityRenderer.java:112: Warning: method not available
    //#    -- call on void org.apache.velocity.Template:merge(Context, Writer)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void render(Map, Writer)
    //#    unanalyzed callee: void org.apache.velocity.Template:merge(Context, Writer)
                
                // and we're done
                return;
            }
            
            long startTime = System.currentTimeMillis();
            
            // convert model to Velocity Context
            Context ctx = new VelocityContext(model);
    //#VelocityRenderer.java:121: Warning: method not available
    //#    -- call on void org.apache.velocity.VelocityContext(Map)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void render(Map, Writer)
    //#    unanalyzed callee: void org.apache.velocity.VelocityContext(Map)
            
            if(velocityDecorator != null) {
                
                /**
                 * We only allow decorating once, so the process isn't
                 * fully recursive.  This is just to keep it simple.
                 */
                
                // render base template to a temporary StringWriter
                StringWriter sw = new StringWriter();
                velocityTemplate.merge(ctx, sw);
    //#VelocityRenderer.java:132: Warning: method not available
    //#    -- call on void org.apache.velocity.Template:merge(Context, Writer)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void render(Map, Writer)
    //#    unanalyzed callee: void org.apache.velocity.Template:merge(Context, Writer)
                
                // put rendered template into context
                ctx.put("decorator_body", sw.toString());
    //#VelocityRenderer.java:135: Warning: method not available
    //#    -- call on Object org.apache.velocity.context.Context:put(String, Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void render(Map, Writer)
    //#    unanalyzed callee: Object org.apache.velocity.context.Context:put(String, Object)
                
                log.debug("Applying decorator "+velocityDecorator.getName());
    //#VelocityRenderer.java:137: Warning: method not available
    //#    -- call on String org.apache.velocity.Template:getName()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void render(Map, Writer)
    //#    unanalyzed callee: String org.apache.velocity.Template:getName()
    //#VelocityRenderer.java:137: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void render(Map, Writer)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
                
                // now render decorator to our output writer
                velocityDecorator.merge(ctx, out);
    //#VelocityRenderer.java:140: Warning: method not available
    //#    -- call on void org.apache.velocity.Template:merge(Context, Writer)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void render(Map, Writer)
    //#    unanalyzed callee: void org.apache.velocity.Template:merge(Context, Writer)
                
            } else {
                
                // no decorator, so just merge template to our output writer
                velocityTemplate.merge(ctx, out);
    //#VelocityRenderer.java:145: Warning: method not available
    //#    -- call on void org.apache.velocity.Template:merge(Context, Writer)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void render(Map, Writer)
    //#    unanalyzed callee: void org.apache.velocity.Template:merge(Context, Writer)
            }
            
            long endTime = System.currentTimeMillis();
            long renderTime = (endTime - startTime)/1000;
            
            log.debug("Rendered ["+renderTemplate.getId()+"] in "+renderTime+" secs");
    //#VelocityRenderer.java:151: Warning: method not available
    //#    -- call on String org.apache.roller.weblogger.pojos.Template:getId()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void render(Map, Writer)
    //#    unanalyzed callee: String org.apache.roller.weblogger.pojos.Template:getId()
    //#VelocityRenderer.java:151: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
    //#    method: void render(Map, Writer)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
            
        } catch (Exception ex) {
            // wrap and rethrow so caller can deal with it
            throw new RenderingException("Error during rendering", ex);
        }
    }
    //#VelocityRenderer.java:157: end of method: void org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer.render(Map, Writer)

}
    //#VelocityRenderer.java:: end of class: org.apache.roller.weblogger.ui.rendering.velocity.VelocityRenderer
