001: /*
002: * Copyright (c) 2003 The Visigoth Software Society. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowledgement:
019: * "This product includes software developed by the
020: * Visigoth Software Society (http://www.visigoths.org/)."
021: * Alternately, this acknowledgement may appear in the software itself,
022: * if and wherever such third-party acknowledgements normally appear.
023: *
024: * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
025: * project contributors may be used to endorse or promote products derived
026: * from this software without prior written permission. For written
027: * permission, please contact visigoths@visigoths.org.
028: *
029: * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
030: * nor may "FreeMarker" or "Visigoth" appear in their names
031: * without prior written permission of the Visigoth Software Society.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of the Visigoth Software Society. For more
049: * information on the Visigoth Software Society, please see
050: * http://www.visigoths.org/
051: */
052:
053: package freemarker.core;
054:
055: import java.io.*;
056: import java.util.*;
057: import freemarker.template.*;
058:
059: /**
060: * A method that takes a parameter and evaluates it as a scalar,
061: * then treats that scalar as template source code and returns a
062: * transform model that evaluates the template in place.
063: * The template inherits the configuration and environment of the executing
064: * template. By default, its name will be equal to
065: * <tt>executingTemplate.getName() + "$anonymous_interpreted"</tt>. You can
066: * specify another parameter to the method call in which case the
067: * template name suffix is the specified id instead of "anonymous_interpreted".
068: * @version $Id: Interpret.java,v 1.2 2005/06/16 18:13:56 ddekany Exp $
069: * @author Attila Szegedi
070: */
071: class Interpret extends BuiltIn {
072: /**
073: * Constructs a template on-the-fly and returns it embedded in a
074: * {@link TemplateTransformModel}.
075: *
076: * <p>The built-in has two arguments:
077: * the arguments passed to the method. It can receive at
078: * least one and at most two arguments, both must evaluate to a scalar.
079: * The first scalar is interpreted as a template source code and a template
080: * is built from it. The second (optional) is used to give the generated
081: * template a name.
082: *
083: * @return a {@link TemplateTransformModel} that when executed inside
084: * a <tt><transform></tt> block will process the generated template
085: * just as if it had been <tt><transform></tt>-ed at that point.
086: */
087: TemplateModel _getAsTemplateModel(Environment env)
088: throws TemplateException {
089: TemplateModel model = target.getAsTemplateModel(env);
090: Expression sourceExpr = null;
091: String id = "anonymous_interpreted";
092: if (model instanceof TemplateSequenceModel) {
093: sourceExpr = ((Expression) new DynamicKeyName(target,
094: new NumberLiteral(new Integer(0)))
095: .copyLocationFrom(target));
096: if (((TemplateSequenceModel) model).size() > 1) {
097: id = ((Expression) new DynamicKeyName(target,
098: new NumberLiteral(new Integer(1)))
099: .copyLocationFrom(target)).getStringValue(env);
100: }
101: } else if (model instanceof TemplateScalarModel) {
102: sourceExpr = target;
103: } else {
104: throw invalidTypeException(model, target, env,
105: "sequence or string");
106: }
107: String templateSource = sourceExpr.getStringValue(env);
108: Template parentTemplate = env.getTemplate();
109: try {
110: Template template = new Template(parentTemplate.getName()
111: + "$" + id, new StringReader(templateSource),
112: parentTemplate.getConfiguration());
113: template.setLocale(env.getLocale());
114: return new TemplateProcessorModel(template);
115: } catch (IOException e) {
116: throw new TemplateException("", e, env);
117: }
118: }
119:
120: private static class TemplateProcessorModel implements
121: TemplateTransformModel {
122: private final Template template;
123:
124: TemplateProcessorModel(Template template) {
125: this .template = template;
126: }
127:
128: public Writer getWriter(final Writer out, Map args)
129: throws TemplateModelException, IOException {
130: try {
131: Environment env = Environment.getCurrentEnvironment();
132: env.include(template);
133: } catch (TemplateModelException e) {
134: throw e;
135: } catch (IOException e) {
136: throw e;
137: } catch (RuntimeException e) {
138: throw e;
139: } catch (Exception e) {
140: throw new TemplateModelException(e);
141: }
142:
143: return new Writer(out) {
144: public void close() {
145: }
146:
147: public void flush() throws IOException {
148: out.flush();
149: }
150:
151: public void write(char[] cbuf, int off, int len)
152: throws IOException {
153: out.write(cbuf, off, len);
154: }
155: };
156: }
157: }
158: }
|