    View Javadoc
    1   /*
    2    * Copyright (c) 2003
    3    * Information Desire GmbH
    4    * All rights reserved.
    5    */
    6   package com.infodesire.infobit.render.velocity;
    7
    8   import com.infodesire.infobit.render.InfobitRenderer;
    9
    10  import org.apache.commons.logging.Log;
    11  import org.apache.commons.logging.LogFactory;
    12
    13  import org.apache.velocity.VelocityContext;
    14  import org.apache.velocity.context.Context;
    15  import org.apache.velocity.app.Velocity;
    16  import org.apache.velocity.app.VelocityEngine;
    17  import org.apache.velocity.runtime.RuntimeConstants;
    18
    19  import com.infodesire.infobit.InfobitManager;
    20  import com.infodesire.infobit.InfobitException;
    21  import com.infodesire.infobit.InfobitSecurityException;
    22
    23  import com.infodesire.infobit.data.Content;
    24
    25  import java.util.Properties;
    26  import java.util.Iterator;
    27  import java.util.Map;
    28
    29  import java.io.Writer;
    30  import java.io.InputStream;
    31
    32  /***
    33   * base class for all velocity rendering. we handle general configuration here.
    34   * implementing classes shall provide case specific rendering
    35   *
    36   * @author    konstantin
    37   * @created   September 16, 2003
    38   * @version   $Revision: 1.10 $
    39   */
    40  public abstract class VelocityRenderer implements InfobitRenderer {
    41      private static Log _log = LogFactory.getLog(VelocityRenderer.class);
    42
    43      InfobitManager  _manager;
    44      VelocityEngine  _engine = null;
    45
    46
    47      /***
    48       * Constructor for the VelocityRenderer object
    49       */
    50      public VelocityRenderer() {
    51          _engine = new VelocityEngine();
    52
    53      }
    54
    55
    56      /***
    57       * Gets the Manager attribute of the VelocityRenderer object
    58       *
    59       * @return   The Manager value
    60       */
    61      public InfobitManager getManager() {
    62          return _manager;
    63      }
    64
    65
    66      /***
    67       * Sets the Manager attribute of the VelocityRenderer object
    68       *
    69       * @param manager  The new Manager value
    70       */
    71      public void setManager(InfobitManager manager) {
    72          _manager = manager;
    73      }
    74
    75
    76      /***
    77       * setup context for rendering of certain infobit
    78       *
    79       * @param context      Description of Parameter
    80       * @param infobitName  Description of Parameter
    81       */
    82      public abstract void setupContext(Context context, String infobitName);
    83
    84
    85      /***
    86       * entry point for infobit rendering
    87       *
    88       * @param writer         sink for rendered infobit data
    89       * @param name           name of infobit in question
    90       * @param parameters     parameter map - wil lbe used to rig up velocity
    91       *      context.
    92       * @exception Exception  could be thrown if something wrong happens.
    93       */
    94      public void render(String name, Writer writer, Map parameters) throws Exception {
    95          VelocityContext vc = new VelocityContext();
    96          vc.put("_name_", name);
    97          if (parameters != null) {
    98              Object key;
    99              for (Iterator iter = parameters.keySet().iterator(); iter.hasNext(); ) {
    100                 key = iter.next();
    101                 if (_log.isDebugEnabled()) {
    102                     _log.debug("passing parameter to velocity context: " + key + ":" + parameters.get(key));
    103                 }
    104                 vc.put(key.toString(), parameters.get(key));
    105             }
    106         }
    107         _engine.mergeTemplate("com/infodesire/infobit/render/velocity/entrypoint.vm", vc, writer);
    108     }
    109
    110
    111     /***
    112      * render infobit content in appropriate way
    113      *
    114      * @param writer   Description of Parameter
    115      * @param content  Description of Parameter
    116      */
    117     public abstract void renderContent(Writer writer, Content content);
    118
    119
    120     /***
    121      * initialize renderer with supplied properties
    122      *
    123      * @param properties  Description of Parameter
    124      */
    125     public void init(Properties properties) {
    126
    127         Properties vp = new Properties();
    128         _engine.setApplicationAttribute(InfobitManager.class.getName(), _manager);
    129         _engine.setApplicationAttribute(VelocityRenderer.class.getName(), this);
    130         try {
    131             _log.debug("loading velocity propeties from: " + getClass().getResource("/com/infodesire/infobit/render/velocity/velocity.properties"));
    132             vp.load(AdvancedClasspathLoader.getResourceAsStream("com/infodesire/infobit/render/velocity/velocity.properties"));
    133             _engine.init(vp);
    134         } catch (Exception ex) {
    135             _log.error("unable to init velocity", ex);
    136         }
    137
    138     }
    139
    140
    141     /***
    142      * Gets the Template attribute of the VelocityRenderer object
    143      *
    144      * @param name  Description of Parameter
    145      * @return      The Template value
    146      */
    147     abstract InputStream getTemplate(String name);
    148 }

This page was automatically generated by Maven