package freemarker.core;
import java.io.IOException;
import java.io.Writer;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import freemarker.template.utility.StringUtil;
final class DollarVariable extends Interpolation {
private final Expression expression;
private final Expression escapedExpression;
private final OutputFormat outputFormat;
private final MarkupOutputFormat markupOutputFormat;
private final boolean autoEscape;
DollarVariable(
Expression expression, Expression escapedExpression,
OutputFormat outputFormat, boolean autoEscape) {
this.expression = expression;
this.escapedExpression = escapedExpression;
this.outputFormat = outputFormat;
this.markupOutputFormat
= (MarkupOutputFormat) (outputFormat instanceof MarkupOutputFormat ? outputFormat : null);
this.autoEscape = autoEscape;
}
@Override
TemplateElement[] accept(Environment env) throws TemplateException, IOException {
final Object moOrStr = calculateInterpolatedStringOrMarkup(env);
final Writer out = env.getOut();
if (moOrStr instanceof String) {
final String s = (String) moOrStr;
if (autoEscape) {
markupOutputFormat.output(s, out);
} else {
out.write(s);
}
} else {
final TemplateMarkupOutputModel mo = (TemplateMarkupOutputModel) moOrStr;
final MarkupOutputFormat moOF = mo.getOutputFormat();
if (moOF != outputFormat && !outputFormat.isOutputFormatMixingAllowed()) {
final String srcPlainText;
srcPlainText = moOF.getSourcePlainText(mo);
if (srcPlainText == null) {
throw new _TemplateModelException(escapedExpression,
"The value to print is in ", new _DelayedToString(moOF),
" format, which differs from the current output format, ",
new _DelayedToString(outputFormat), ". Format conversion wasn't possible.");
}
if (outputFormat instanceof MarkupOutputFormat) {
((MarkupOutputFormat) outputFormat).output(srcPlainText, out);
} else {
out.write(srcPlainText);
}
} else {
moOF.output(mo, out);
}
}
return null;
}
@Override
protected Object calculateInterpolatedStringOrMarkup(Environment env) throws TemplateException {
return EvalUtil.coerceModelToStringOrMarkup(escapedExpression.eval(env), escapedExpression, null, env);
}
@Override
protected String dump(boolean canonical, boolean inStringLiteral) {
StringBuilder sb = new StringBuilder();
int syntax = getTemplate().getInterpolationSyntax();
sb.append(syntax != Configuration.SQUARE_BRACKET_INTERPOLATION_SYNTAX ? "${" : "[=");
final String exprCF = expression.getCanonicalForm();
sb.append(inStringLiteral ? StringUtil.FTLStringLiteralEnc(exprCF, '"') : exprCF);
sb.append(syntax != Configuration.SQUARE_BRACKET_INTERPOLATION_SYNTAX ? "}" : "]");
if (!canonical && expression != escapedExpression) {
sb.append(" auto-escaped");
}
return sb.toString();
}
@Override
String getNodeTypeSymbol() {
return "${...}";
}
@Override
boolean heedsOpeningWhitespace() {
return true;
}
@Override
boolean heedsTrailingWhitespace() {
return true;
}
@Override
int getParameterCount() {
return 1;
}
@Override
Object getParameterValue(int idx) {
if (idx != 0) throw new IndexOutOfBoundsException();
return expression;
}
@Override
ParameterRole getParameterRole(int idx) {
if (idx != 0) throw new IndexOutOfBoundsException();
return ParameterRole.CONTENT;
}
@Override
boolean isNestedBlockRepeater() {
return false;
}
}