Coverage Report - org.webmacro.util.WMStringEval
 
Classes in this File Line Coverage Branch Coverage Complexity
WMStringEval
0%
0/15
N/A
1.4
 
 1  
 /*
 2  
  * Created on Apr 15, 2005
 3  
  *
 4  
  */
 5  
 package org.webmacro.util;
 6  
 
 7  
 import org.webmacro.Context;
 8  
 import org.webmacro.Template;
 9  
 import org.webmacro.WM;
 10  
 import org.webmacro.WebMacro;
 11  
 import org.webmacro.engine.StringTemplate;
 12  
 
 13  
 /**
 14  
  * A simple encapsulation of a WebMacro instance
 15  
  * for evaluating string-based templates.
 16  
  * @author Lane Sharman
 17  
  */
 18  
 public class WMStringEval
 19  
 {
 20  
 
 21  
   private Context context;
 22  
   private WebMacro wm;
 23  
 
 24  
   /**
 25  
    * Constructs an WM String evaluator with a context.
 26  
    */
 27  
   public WMStringEval()
 28  0
   {
 29  
     // Build a web macro environment for currentTemplate execution.
 30  
     try
 31  
     {
 32  0
         wm = new WM();
 33  0
         context = wm.getContext();
 34  
     }
 35  0
     catch (Exception e)
 36  
     {
 37  0
         e.printStackTrace(System.err);
 38  0
         throw new IllegalStateException(e.toString());
 39  0
     }
 40  0
   }
 41  
   
 42  
   /**
 43  
    * Gets the current context.
 44  
    * @return The context.
 45  
    */
 46  
   public Context getCurrentContext()
 47  
   {
 48  0
     return context;
 49  
   }
 50  
   
 51  
   /**
 52  
    * Establish a new context for future evaluations.
 53  
    * @return The new context.
 54  
    */
 55  
   public Context getNewContext()
 56  
   {
 57  0
     context = wm.getContext();
 58  0
     return context;
 59  
   }
 60  
   
 61  
   /**
 62  
    * Adds a reference to the context.
 63  
    * @param name The name of the reference.
 64  
    * @param value The referent.
 65  
    */
 66  
   public void addContextReference(String name, Object value)
 67  
   {
 68  0
     context.put(name, value);
 69  0
   }
 70  
   
 71  
   /**
 72  
    * Evaluate the template using the current context.
 73  
    * @param template The string template to evaluate.
 74  
    * @return The evaluation of the template against the context.
 75  
    * @throws Exception
 76  
    */
 77  
   public String eval(String template) throws Exception
 78  
   {
 79  0
     Template t = new StringTemplate(wm.getBroker(), template, "anonymous");
 80  0
     return t.evaluateAsString(context);
 81  
   }
 82  
 
 83  
 }