KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > core > Include


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.core;
54
55 import java.io.IOException JavaDoc;
56
57 import freemarker.cache.TemplateCache;
58 import freemarker.template.*;
59 import freemarker.template.utility.StringUtil;
60
61
62 /**
63  * An instruction that gets another template
64  * and processes it within the current template.
65  */

66 final class Include extends TemplateElement {
67
68     private Expression includedTemplateName, encodingExp, parseExp;
69     private String JavaDoc encoding;
70     private boolean parse;
71     private final String JavaDoc templatePath;
72
73     /**
74      * @param variable the template that this <tt>Include</tt> is a part of.
75      * @param templateName the name of the template to be included.
76      * @param encoding the encoding to be used or null, if it is a default.
77      * @param parse whether the template should be parsed (or is raw text)
78      */

79     Include(Template template,
80             Expression includedTemplateName,
81             Expression encodingExp,
82             Expression parseExp)
83     {
84         String JavaDoc templatePath1 = template.getName();
85         int lastSlash = templatePath1.lastIndexOf('/');
86         templatePath = lastSlash == -1 ? "" : templatePath1.substring(0, lastSlash + 1);
87         this.includedTemplateName = includedTemplateName;
88         if (encodingExp instanceof StringLiteral) {
89             encoding = encodingExp.toString();
90             encoding = encoding.substring(1, encoding.length() -1);
91         }
92         else {
93             this.encodingExp = encodingExp;
94         }
95         if (parseExp == null || parseExp == TemplateBooleanModel.TRUE) {
96             parse = true;
97         }
98         else if (parseExp == TemplateBooleanModel.FALSE) {
99             parse = false;
100         }
101         else if (parseExp instanceof StringLiteral) {
102             parse = StringUtil.getYesNo(parseExp.toString());
103         }
104         else {
105             this.parseExp = parseExp;
106         }
107     }
108
109     void accept(Environment env) throws TemplateException, IOException JavaDoc {
110         String JavaDoc templateNameString = includedTemplateName.getStringValue(env);
111         if( templateNameString == null ) {
112             String JavaDoc msg = "Error " + getStartLocation()
113                         + "The expression " + includedTemplateName + " is undefined.";
114             throw new InvalidReferenceException(msg, env);
115         }
116         String JavaDoc enc = encoding;
117         if (encoding == null && encodingExp != null) {
118             enc = encodingExp.getStringValue(env);
119         }
120         
121         boolean parse = this.parse;
122         if (parseExp != null) {
123             TemplateModel tm = parseExp.getAsTemplateModel(env);
124             if(tm == null) {
125                 if(env.isClassicCompatible()) {
126                     parse = false;
127                 }
128                 else {
129                     assertNonNull(tm, parseExp, env);
130                 }
131             }
132             if (tm instanceof TemplateScalarModel) {
133                 parse = getYesNo(EvaluationUtil.getString((TemplateScalarModel)tm, parseExp, env));
134             }
135             else {
136                 parse = parseExp.isTrue(env);
137             }
138         }
139         
140         Template includedTemplate;
141         try {
142             templateNameString = TemplateCache.getFullTemplatePath(env, templatePath, templateNameString);
143             includedTemplate = env.getTemplateForInclusion(templateNameString, enc, parse);
144         }
145         catch (ParseException pe) {
146             String JavaDoc msg = "Error parsing included template "
147                         + templateNameString;
148             throw new TemplateException(msg, pe, env);
149         }
150         catch (IOException JavaDoc ioe) {
151             String JavaDoc msg = "Error reading included file "
152                         + templateNameString;
153             throw new TemplateException(msg, ioe, env);
154         }
155         env.include(includedTemplate);
156     }
157
158     public String JavaDoc getCanonicalForm() {
159         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("<#include ");
160         buf.append(includedTemplateName);
161         if (encoding != null) {
162             buf.append(" encoding=\"");
163             buf.append(encodingExp.getCanonicalForm());
164             buf.append("\"");
165         }
166         if (!parse) {
167             buf.append(" parse=false");
168         }
169         buf.append("/>");
170         return buf.toString();
171     }
172
173     public String JavaDoc getDescription() {
174         return "include " + includedTemplateName;
175     }
176
177     private boolean getYesNo(String JavaDoc s) throws ParseException {
178         try {
179            return StringUtil.getYesNo(s);
180         }
181         catch (IllegalArgumentException JavaDoc iae) {
182             throw new ParseException("Error " + getStartLocation()
183                  + "\nValue of include parse parameter "
184                  + "must be boolean or one of these strings: "
185                  + "\"n\", \"no\", \"f\", \"false\", \"y\", \"yes\", \"t\", \"true\""
186                  + "\nFound: " + parseExp, parseExp);
187         }
188     }
189
190 /*
191     boolean heedsOpeningWhitespace() {
192         return true;
193     }
194
195     boolean heedsTrailingWhitespace() {
196         return true;
197     }
198 */

199 }
200
Popular Tags