KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > core > Interpret


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.core;
54
55 import java.io.*;
56 import java.util.*;
57 import freemarker.template.*;
58
59
60 /**
61  * A method that takes a parameter and evaluates it as a scalar,
62  * then treats that scalar as template source code and returns a
63  * transform model that evaluates the template in place.
64  * The template inherits the configuration and environment of the executing
65  * template. By default, its name will be equal to
66  * <tt>executingTemplate.getName() + "$anonymous_interpreted"</tt>. You can
67  * specify another parameter to the method call in which case the
68  * template name suffix is the specified id instead of "anonymous_interpreted".
69  * @version $Id: Interpret.java,v 1.2 2005/06/16 18:13:56 ddekany Exp $
70  * @author Attila Szegedi
71  */

72 class Interpret extends BuiltIn
73 {
74     /**
75      * Constructs a template on-the-fly and returns it embedded in a
76      * {@link TemplateTransformModel}.
77      *
78      * <p>The built-in has two arguments:
79      * the arguments passed to the method. It can receive at
80      * least one and at most two arguments, both must evaluate to a scalar.
81      * The first scalar is interpreted as a template source code and a template
82      * is built from it. The second (optional) is used to give the generated
83      * template a name.
84      *
85      * @return a {@link TemplateTransformModel} that when executed inside
86      * a <tt>&lt;transform></tt> block will process the generated template
87      * just as if it had been <tt>&lt;transform></tt>-ed at that point.
88      */

89     TemplateModel _getAsTemplateModel(Environment env)
90             throws TemplateException
91     {
92         TemplateModel model = target.getAsTemplateModel(env);
93         Expression sourceExpr = null;
94         String JavaDoc id = "anonymous_interpreted";
95         if(model instanceof TemplateSequenceModel)
96         {
97             sourceExpr = ((Expression)new DynamicKeyName(target, new NumberLiteral(new Integer JavaDoc(0))).copyLocationFrom(target));
98             if(((TemplateSequenceModel)model).size() > 1)
99             {
100                 id = ((Expression)new DynamicKeyName(target, new NumberLiteral(new Integer JavaDoc(1))).copyLocationFrom(target)).getStringValue(env);
101             }
102         }
103         else if (model instanceof TemplateScalarModel)
104         {
105             sourceExpr = target;
106         }
107         else
108         {
109             throw invalidTypeException(model, target, env, "sequence or string");
110         }
111         String JavaDoc templateSource = sourceExpr.getStringValue(env);
112         Template parentTemplate = env.getTemplate();
113         try
114         {
115             Template template = new Template(parentTemplate.getName() + "$" + id, new StringReader(templateSource), parentTemplate.getConfiguration());
116             template.setLocale(env.getLocale());
117             return new TemplateProcessorModel(template);
118         }
119         catch(IOException e)
120         {
121             throw new TemplateException("", e, env);
122         }
123     }
124
125     private static class TemplateProcessorModel
126     implements
127         TemplateTransformModel
128     {
129         private final Template template;
130         
131         TemplateProcessorModel(Template template)
132         {
133             this.template = template;
134         }
135         
136         public Writer getWriter(final Writer out, Map args) throws TemplateModelException, IOException
137         {
138             try
139             {
140                 Environment env = Environment.getCurrentEnvironment();
141                 env.include(template);
142             }
143             catch(TemplateModelException e)
144             {
145                 throw e;
146             }
147             catch(IOException e)
148             {
149                 throw e;
150             }
151             catch(RuntimeException JavaDoc e)
152             {
153                 throw e;
154             }
155             catch(Exception JavaDoc e)
156             {
157                 throw new TemplateModelException(e);
158             }
159     
160             return new Writer(out)
161             {
162                 public void close()
163                 {
164                 }
165                 
166                 public void flush() throws IOException
167                 {
168                     out.flush();
169                 }
170                 
171                 public void write(char[] cbuf, int off, int len) throws IOException
172                 {
173                     out.write(cbuf, off, len);
174                 }
175             };
176         }
177     }
178 }
179
Popular Tags