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: /*
054: * 22 October 1999: This class added by Holger Arendt.
055: */
056:
057: package freemarker.core;
058:
059: import java.util.ArrayList;
060: import java.util.List;
061: import java.io.Writer;
062: import freemarker.template.*;
063: import java.io.IOException;
064:
065: /**
066: * A unary operator that calls a TemplateMethodModel. It associates with the
067: * <tt>Identifier</tt> or <tt>Dot</tt> to its left.
068: */
069: final class MethodCall extends Expression {
070:
071: private final Expression target;
072: private final ListLiteral arguments;
073:
074: MethodCall(Expression target, ArrayList arguments) {
075: this (target, new ListLiteral(arguments));
076: }
077:
078: private MethodCall(Expression target, ListLiteral arguments) {
079: this .target = target;
080: this .arguments = arguments;
081: }
082:
083: TemplateModel _getAsTemplateModel(Environment env)
084: throws TemplateException {
085: TemplateModel targetModel = target.getAsTemplateModel(env);
086: if (targetModel instanceof TemplateMethodModel) {
087: TemplateMethodModel targetMethod = (TemplateMethodModel) targetModel;
088: List argumentStrings = targetMethod instanceof TemplateMethodModelEx ? arguments
089: .getModelList(env)
090: : arguments.getValueList(env);
091: Object result = targetMethod.exec(argumentStrings);
092: return env.getObjectWrapper().wrap(result);
093: } else if (targetModel instanceof Macro) {
094: Macro func = (Macro) targetModel;
095: env.setLastReturnValue(null);
096: if (!func.isFunction) {
097: throw new TemplateException(
098: "A macro cannot be called in an expression.",
099: env);
100: }
101: Writer prevOut = env.getOut();
102: try {
103: env.setOut(Environment.NULL_WRITER);
104: env.visit(func, null, arguments.values, null, null);
105: } catch (IOException ioe) {
106: throw new InternalError("This should be impossible.");
107: } finally {
108: env.setOut(prevOut);
109: }
110: return env.getLastReturnValue();
111: } else {
112: throw invalidTypeException(targetModel, target, env,
113: "method");
114: }
115: }
116:
117: public String getCanonicalForm() {
118: StringBuffer buf = new StringBuffer();
119: buf.append(target.getCanonicalForm());
120: buf.append("(");
121: String list = arguments.getCanonicalForm();
122: buf.append(list.substring(1, list.length() - 1));
123: buf.append(")");
124: return buf.toString();
125: }
126:
127: TemplateModel getConstantValue() {
128: return null;
129: }
130:
131: boolean isLiteral() {
132: return false;
133: }
134:
135: Expression _deepClone(String name, Expression subst) {
136: return new MethodCall(target.deepClone(name, subst),
137: (ListLiteral) arguments.deepClone(name, subst));
138: }
139:
140: }
|