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.template.utility;
054:
055: import freemarker.template.*;
056: import freemarker.core.Environment;
057: import java.io.*;
058: import java.util.Map;
059:
060: /**
061: * A transform that captures the output of a block of FTL code and stores that in a variable.
062: *
063: * <p>As this transform is initially present in the shared variable set, you can always
064: * access it from the templates:</p>
065: *
066: * <pre>
067: * <@capture_output var="captured">
068: * ...
069: * </@capture_output>
070: * </pre>
071: *
072: * <p>And later in the template you can use the captured output:</p>
073: *
074: * ${captured}
075: *
076: * <p>This transform requires one of three parameters: <code>var</code>, <code>local</code>, or <code>global</code>.
077: * Each of them specifies the name of the variable that stores the captured output, but the first creates a
078: * variable in a name-space (as <#assign>), the second creates a macro-local variable (as <#local>),
079: * and the last creates a global variable (as <#global>).
080: * </p>
081: * <p>In the case of an assignment within a namespace, there is an optional parameter
082: * <code>namespace</code> that indicates in which namespace to do the assignment.
083: * if this is omitted, the current namespace is used, and this will be, by far, the most
084: * common usage pattern.</p>
085: *
086: * @deprecated Use block-assignments instead, as <code><assign x>...</assign></code>.
087: *
088: * @version $Id: CaptureOutput.java,v 1.31 2004/01/06 17:06:43 szegedia Exp $
089: */
090: public class CaptureOutput implements TemplateTransformModel {
091:
092: public Writer getWriter(final Writer out, final Map args)
093: throws TemplateModelException {
094: String errmsg = "Must specify the name of the variable in "
095: + "which to capture the output with the 'var' or 'local' or 'global' parameter.";
096: if (args == null)
097: throw new TemplateModelException(errmsg);
098:
099: boolean local = false, global = false;
100: final TemplateModel nsModel = (TemplateModel) args
101: .get("namespace");
102: Object varNameModel = args.get("var");
103: if (varNameModel == null) {
104: varNameModel = args.get("local");
105: if (varNameModel == null) {
106: varNameModel = args.get("global");
107: global = true;
108: } else {
109: local = true;
110: }
111: if (varNameModel == null) {
112: throw new TemplateModelException(errmsg);
113: }
114: }
115: if (args.size() == 2) {
116: if (nsModel == null) {
117: throw new TemplateModelException(
118: "Second parameter can only be namespace");
119: }
120: if (local) {
121: throw new TemplateModelException(
122: "Cannot specify namespace for a local assignment");
123: }
124: if (global) {
125: throw new TemplateModelException(
126: "Cannot specify namespace for a global assignment");
127: }
128: if (!(nsModel instanceof Environment.Namespace)) {
129: throw new TemplateModelException(
130: "namespace parameter does not specify a namespace. It is a "
131: + nsModel.getClass().getName());
132: }
133: } else if (args.size() != 1)
134: throw new TemplateModelException(
135: "Bad parameters. Use only one of 'var' or 'local' or 'global' parameters.");
136:
137: if (!(varNameModel instanceof TemplateScalarModel)) {
138: throw new TemplateModelException(
139: "'var' or 'local' or 'global' parameter doesn't evaluate to a string");
140: }
141: final String varName = ((TemplateScalarModel) varNameModel)
142: .getAsString();
143: if (varName == null) {
144: throw new TemplateModelException(
145: "'var' or 'local' or 'global' parameter evaluates to null string");
146: }
147:
148: final StringBuffer buf = new StringBuffer();
149: final Environment env = Environment.getCurrentEnvironment();
150: final boolean localVar = local;
151: final boolean globalVar = global;
152:
153: return new Writer() {
154:
155: public void write(char cbuf[], int off, int len) {
156: buf.append(cbuf, off, len);
157: }
158:
159: public void flush() throws IOException {
160: out.flush();
161: }
162:
163: public void close() throws IOException {
164: SimpleScalar result = new SimpleScalar(buf.toString());
165: try {
166: if (localVar) {
167: env.setLocalVariable(varName, result);
168: } else if (globalVar) {
169: env.setGlobalVariable(varName, result);
170: } else {
171: if (nsModel == null) {
172: env.setVariable(varName, result);
173: } else {
174: ((Environment.Namespace) nsModel).put(
175: varName, result);
176: }
177: }
178: } catch (java.lang.IllegalStateException ise) { // if somebody uses 'local' outside a macro
179: throw new IOException("Could not set variable "
180: + varName + ": " + ise.getMessage());
181: }
182: }
183: };
184: }
185: }
|