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:
057: import freemarker.cache.TemplateCache;
058: import freemarker.template.*;
059: import freemarker.template.utility.StringUtil;
060:
061: /**
062: * An instruction that gets another template
063: * and processes it within the current template.
064: */
065: final class Include extends TemplateElement {
066:
067: private Expression includedTemplateName, encodingExp, parseExp;
068: private String encoding;
069: private boolean parse;
070: private final String templatePath;
071:
072: /**
073: * @param template the template that this <tt>Include</tt> is a part of.
074: * @param includedTemplateName the name of the template to be included.
075: * @param encodingExp the encoding to be used or null, if it is a default.
076: * @param parseExp whether the template should be parsed (or is raw text)
077: */
078: Include(Template template, Expression includedTemplateName,
079: Expression encodingExp, Expression parseExp) {
080: String templatePath1 = template.getName();
081: int lastSlash = templatePath1.lastIndexOf('/');
082: templatePath = lastSlash == -1 ? "" : templatePath1.substring(
083: 0, lastSlash + 1);
084: this .includedTemplateName = includedTemplateName;
085: if (encodingExp instanceof StringLiteral) {
086: encoding = encodingExp.toString();
087: encoding = encoding.substring(1, encoding.length() - 1);
088: } else {
089: this .encodingExp = encodingExp;
090: }
091: if (parseExp == null || parseExp == TemplateBooleanModel.TRUE) {
092: parse = true;
093: } else if (parseExp == TemplateBooleanModel.FALSE) {
094: parse = false;
095: } else if (parseExp instanceof StringLiteral) {
096: parse = StringUtil.getYesNo(parseExp.toString());
097: } else {
098: this .parseExp = parseExp;
099: }
100: }
101:
102: void accept(Environment env) throws TemplateException, IOException {
103: String templateNameString = includedTemplateName
104: .getStringValue(env);
105: if (templateNameString == null) {
106: String msg = "Error " + getStartLocation()
107: + "The expression " + includedTemplateName
108: + " is undefined.";
109: throw new InvalidReferenceException(msg, env);
110: }
111: String enc = encoding;
112: if (encoding == null && encodingExp != null) {
113: enc = encodingExp.getStringValue(env);
114: }
115:
116: boolean parse = this .parse;
117: if (parseExp != null) {
118: TemplateModel tm = parseExp.getAsTemplateModel(env);
119: if (tm == null) {
120: if (env.isClassicCompatible()) {
121: parse = false;
122: } else {
123: assertNonNull(tm, parseExp, env);
124: }
125: }
126: if (tm instanceof TemplateScalarModel) {
127: parse = getYesNo(EvaluationUtil.getString(
128: (TemplateScalarModel) tm, parseExp, env));
129: } else {
130: parse = parseExp.isTrue(env);
131: }
132: }
133:
134: Template includedTemplate;
135: try {
136: templateNameString = TemplateCache.getFullTemplatePath(env,
137: templatePath, templateNameString);
138: includedTemplate = env.getTemplateForInclusion(
139: templateNameString, enc, parse);
140: } catch (ParseException pe) {
141: String msg = "Error parsing included template "
142: + templateNameString + "\n" + pe.getMessage();
143: throw new TemplateException(msg, pe, env);
144: } catch (IOException ioe) {
145: String msg = "Error reading included file "
146: + templateNameString;
147: throw new TemplateException(msg, ioe, env);
148: }
149: env.include(includedTemplate);
150: }
151:
152: public String getCanonicalForm() {
153: StringBuffer buf = new StringBuffer("<#include ");
154: buf.append(includedTemplateName);
155: if (encoding != null) {
156: buf.append(" encoding=\"");
157: buf.append(encodingExp.getCanonicalForm());
158: buf.append("\"");
159: }
160: if (!parse) {
161: buf.append(" parse=false");
162: }
163: buf.append("/>");
164: return buf.toString();
165: }
166:
167: public String getDescription() {
168: return "include " + includedTemplateName;
169: }
170:
171: private boolean getYesNo(String s) throws ParseException {
172: try {
173: return StringUtil.getYesNo(s);
174: } catch (IllegalArgumentException iae) {
175: throw new ParseException(
176: "Error "
177: + getStartLocation()
178: + "\nValue of include parse parameter "
179: + "must be boolean or one of these strings: "
180: + "\"n\", \"no\", \"f\", \"false\", \"y\", \"yes\", \"t\", \"true\""
181: + "\nFound: " + parseExp, parseExp);
182: }
183: }
184:
185: /*
186: boolean heedsOpeningWhitespace() {
187: return true;
188: }
189:
190: boolean heedsTrailingWhitespace() {
191: return true;
192: }
193: */
194: }
|