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.IOException;
056: import java.util.*;
057: import freemarker.template.*;
058:
059: /**
060: * An instruction that processes the nested block within a macro instruction.
061: * @author <a href="mailto:jon@revusky.com">Jonathan Revusky</a>
062: */
063: final class BodyInstruction extends TemplateElement {
064:
065: private List bodyParameters;
066:
067: BodyInstruction(List bodyParameters) {
068: this .bodyParameters = bodyParameters;
069: }
070:
071: List getBodyParameters() {
072: return bodyParameters;
073: }
074:
075: /**
076: * There is actually a subtle but essential point in the code below.
077: * A macro operates in the context in which it is defined. However,
078: * a nested block within a macro instruction is defined in the
079: * context in which the macro was invoked. So, we actually need to
080: * temporarily switch the namespace and macro context back to
081: * what it was before macro invocation to implement this properly.
082: * I (JR) realized this thanks to some incisive comments from Daniel Dekany.
083: */
084: void accept(Environment env) throws IOException, TemplateException {
085: Context bodyContext = new Context(env);
086: env.visit(bodyContext);
087: }
088:
089: public String getCanonicalForm() {
090: StringBuffer buf = new StringBuffer("<#nested");
091: if (bodyParameters != null) {
092: for (int i = 0; i < bodyParameters.size(); i++) {
093: buf.append(' ');
094: buf.append(bodyParameters.get(i));
095: }
096: }
097: buf.append('>');
098: return buf.toString();
099: }
100:
101: public String getDescription() {
102: return "nested macro content";
103: }
104:
105: /*
106: boolean heedsOpeningWhitespace() {
107: return true;
108: }
109:
110: boolean heedsTrailingWhitespace() {
111: return true;
112: }
113: */
114: class Context implements LocalContext {
115: Macro.Context invokingMacroContext;
116: Environment.Namespace bodyVars;
117:
118: Context(Environment env) throws TemplateException {
119: invokingMacroContext = env.getCurrentMacroContext();
120: List bodyParameterNames = invokingMacroContext.bodyParameterNames;
121: if (bodyParameters != null) {
122: for (int i = 0; i < bodyParameters.size(); i++) {
123: Expression exp = (Expression) bodyParameters.get(i);
124: TemplateModel tm = exp.getAsTemplateModel(env);
125: if (bodyParameterNames != null
126: && i < bodyParameterNames.size()) {
127: String bodyParameterName = (String) bodyParameterNames
128: .get(i);
129: if (bodyVars == null) {
130: bodyVars = env.new Namespace();
131: }
132: bodyVars.put(bodyParameterName, tm);
133: }
134: }
135: }
136: }
137:
138: public TemplateModel getLocalVariable(String name)
139: throws TemplateModelException {
140: return bodyVars == null ? null : bodyVars.get(name);
141: }
142:
143: public Set getLocalVariableNames() {
144: List bodyParameterNames = invokingMacroContext.bodyParameterNames;
145: if (bodyParameterNames == null)
146: return Collections.EMPTY_SET;
147: return new HashSet(bodyParameterNames);
148: }
149: }
150: }
|