1 52 53 package freemarker.core; 54 55 import java.io.IOException ; 56 57 import freemarker.cache.TemplateCache; 58 import freemarker.template.*; 59 import freemarker.template.utility.StringUtil; 60 61 62 66 final class Include extends TemplateElement { 67 68 private Expression includedTemplateName, encodingExp, parseExp; 69 private String encoding; 70 private boolean parse; 71 private final String templatePath; 72 73 79 Include(Template template, 80 Expression includedTemplateName, 81 Expression encodingExp, 82 Expression parseExp) 83 { 84 String 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 { 110 String templateNameString = includedTemplateName.getStringValue(env); 111 if( templateNameString == null ) { 112 String msg = "Error " + getStartLocation() 113 + "The expression " + includedTemplateName + " is undefined."; 114 throw new InvalidReferenceException(msg, env); 115 } 116 String 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 msg = "Error parsing included template " 147 + templateNameString; 148 throw new TemplateException(msg, pe, env); 149 } 150 catch (IOException ioe) { 151 String msg = "Error reading included file " 152 + templateNameString; 153 throw new TemplateException(msg, ioe, env); 154 } 155 env.include(includedTemplate); 156 } 157 158 public String getCanonicalForm() { 159 StringBuffer buf = new StringBuffer ("<#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 getDescription() { 174 return "include " + includedTemplateName; 175 } 176 177 private boolean getYesNo(String s) throws ParseException { 178 try { 179 return StringUtil.getYesNo(s); 180 } 181 catch (IllegalArgumentException 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 199 } 200 | Popular Tags |