Coverage Report - org.webmacro.engine.StreamTemplate
 
Classes in this File Line Coverage Branch Coverage Complexity
StreamTemplate
0%
0/57
0%
0/12
2.375
 
 1  
 /*
 2  
  * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
 3  
  *
 4  
  * Redistribution and use in source and binary forms, with or without
 5  
  * modification, are permitted under the terms of either of the following
 6  
  * Open Source licenses:
 7  
  *
 8  
  * The GNU General Public License, version 2, or any later version, as
 9  
  * published by the Free Software Foundation
 10  
  * (http://www.fsf.org/copyleft/gpl.html);
 11  
  *
 12  
  *  or
 13  
  *
 14  
  * The Semiotek Public License (http://webmacro.org/LICENSE.)
 15  
  *
 16  
  * This software is provided "as is", with NO WARRANTY, not even the
 17  
  * implied warranties of fitness to purpose, or merchantability. You
 18  
  * assume all risks and liabilities associated with its use.
 19  
  *
 20  
  * See www.webmacro.org for more information on the WebMacro project.
 21  
  */
 22  
 
 23  
 
 24  
 package org.webmacro.engine;
 25  
 
 26  
 import java.io.IOException;
 27  
 import java.io.InputStream;
 28  
 import java.io.InputStreamReader;
 29  
 import java.io.Reader;
 30  
 import java.util.Date;
 31  
 
 32  
 import org.webmacro.Broker;
 33  
 import org.webmacro.Context;
 34  
 import org.webmacro.Template;
 35  
 import org.webmacro.WM;
 36  
 import org.webmacro.WMConstants;
 37  
 import org.webmacro.WebMacro;
 38  
 import org.webmacro.util.SelectList;
 39  
 
 40  
 /**
 41  
  * StreamTempaltes are constructed with a stream from which they
 42  
  * read their data. They can only read the stream once, and after
 43  
  * that will throw an exception. Mostly they are useful for testing
 44  
  * WebMacro directives on the command line, since a main() is
 45  
  * provided which reads the template on standard input.
 46  
  */
 47  
 
 48  
 public class StreamTemplate extends WMTemplate
 49  
 {
 50  
 
 51  
     /**
 52  
      * Our stream.
 53  
      */
 54  
     private Reader _in;
 55  0
     private String _name = null;
 56  
 
 57  
     /**
 58  
      * Instantiate a template based on the specified stream
 59  
      */
 60  
     public StreamTemplate (Broker broker, Reader inStream)
 61  
     {
 62  0
         super(broker);
 63  0
         _in = inStream;
 64  0
     }
 65  
 
 66  
     /**
 67  
      * Instantiate a template based on the specified stream
 68  
      * Will use webmacro's default encoding.
 69  
      * @param broker broker for this template
 70  
      * @param in input stream to read template from
 71  
      * @throws IOException if default encoding is unsupported
 72  
      */
 73  
     public StreamTemplate (Broker broker, InputStream in)
 74  
             throws IOException
 75  
     {
 76  0
         this(broker, in, null);
 77  0
     }
 78  
 
 79  
     /**
 80  
      * Instantiate a template based on the specified stream
 81  
      * If encoding is null, webmacro's default encoding will
 82  
      * be used.
 83  
      * @param broker broker for this template
 84  
      * @param in input stream to read template from
 85  
      * @param encoding encoding of input stream
 86  
      * @throws IOException if encoding is unsupported
 87  
      */
 88  
     public StreamTemplate (Broker broker, InputStream in, String encoding)
 89  
             throws IOException
 90  
     {
 91  0
         super(broker);
 92  0
         if (encoding == null)
 93  0
             encoding = getDefaultEncoding();
 94  0
         _in = new InputStreamReader(in, encoding);
 95  0
     }
 96  
 
 97  
     /**
 98  
      * Get the stream the template should be read from. Parse will
 99  
      * call this method in order to locate a stream.
 100  
      */
 101  
     protected Reader getReader () throws IOException
 102  
     {
 103  0
         if (_in != null)
 104  
         {
 105  0
             Reader ret = _in;
 106  0
             _in = null;
 107  0
             return ret;
 108  
         }
 109  
         else
 110  
         {
 111  0
             throw new IOException("Already read stream.");
 112  
         }
 113  
     }
 114  
 
 115  
     /**
 116  
      * Return a name for this template. For example, if the template reads
 117  
      * from a file you might want to mention which it is--will be used to
 118  
      * produce error messages describing which template had a problem.
 119  
      */
 120  
     public String toString ()
 121  
     {
 122  0
         String s = null;
 123  0
         if (_in != null)
 124  0
             s = _in.toString();
 125  0
         return (_name != null)
 126  
                 ? "StreamTemplate:" + _name
 127  
                 : (s != null) ? "StreamTemplate(" + _in + ")" : "(stream)";
 128  
     }
 129  
 
 130  
     public String getName ()
 131  
     {
 132  0
         return (_name == null) ? toString() : _name;
 133  
     }
 134  
 
 135  
     public void setName (String name)
 136  
     {
 137  0
         _name = name;
 138  0
     }
 139  
 
 140  
     /**
 141  
      * Simple test.
 142  
      */
 143  
     public static void main (String arg[])
 144  
     {
 145  
 
 146  
         // Build a context
 147  0
         WebMacro wm = null;
 148  0
         Context context = null;
 149  0
         String encoding = null;
 150  
 
 151  
         try
 152  
         {
 153  0
             wm = new WM();
 154  0
             context = wm.getContext();
 155  0
             Object names[] = {"prop"};
 156  0
             context.setProperty(names, "Example property");
 157  0
             encoding = wm.getConfig(WMConstants.TEMPLATE_INPUT_ENCODING);
 158  
         }
 159  0
         catch (Exception e)
 160  
         {
 161  0
             e.printStackTrace();
 162  0
             return;
 163  0
         }
 164  
 
 165  
         try
 166  
         {
 167  
             /*
 168  
             HashMap hm = new HashMap();
 169  
             hm.put("one", "the first");
 170  
             hm.put("two", "the second");
 171  
             hm.put("three", "the third");
 172  
             context.setBean(hm);
 173  
             */
 174  0
             context.put("helloworld", "Hello World");
 175  0
             context.put("hello", "Hello");
 176  0
             context.put("file", "include.txt");
 177  0
             context.put("today", new Date());
 178  0
             TestObject[] fruits = {new TestObject("apple", false),
 179  
                                    new TestObject("lemon", true),
 180  
                                    new TestObject("pear", false),
 181  
                                    new TestObject("orange", true),
 182  
                                    new TestObject("watermelon", false),
 183  
                                    new TestObject("peach", false),
 184  
                                    new TestObject("lime", true)};
 185  
 
 186  0
             SelectList sl = new SelectList(fruits, 3);
 187  0
             context.put("sl-fruits", sl);
 188  
 
 189  0
             context.put("fruits", fruits);
 190  0
             context.put("flipper", new TestObject("flip", false));
 191  
 
 192  0
             System.out.println("- - - - - - - - - - - - - - - - - - - -");
 193  0
             System.out.println("Context contains: helloWorld, " + 
 194  
                     "hello, file, TestObject[] fruits, " + 
 195  
                     "SelectList sl(fruits, 3), TestObject flipper");
 196  0
             System.out.println("- - - - - - - - - - - - - - - - - - - -");
 197  
 
 198  0
             Template t1 = new StreamTemplate(wm.getBroker(),
 199  
                     new InputStreamReader(System.in));
 200  0
             t1.parse();
 201  
 
 202  0
             System.out.println("*** RESULT ***");
 203  0
             t1.write(System.out, encoding, context);
 204  0
             System.out.println("*** DONE ***");
 205  0
             context.clear();
 206  
 
 207  
         }
 208  0
         catch (Exception e)
 209  
         {
 210  0
             e.printStackTrace();
 211  0
         }
 212  
 
 213  0
     }
 214  
 
 215  
 }