Search in sources :

Example 6 with ExprRootNode

use of com.google.template.soy.exprtree.ExprRootNode in project closure-templates by google.

the class RenderVisitor method visitCallDelegateNode.

@Override
protected void visitCallDelegateNode(CallDelegateNode node) {
    ExprRootNode variantExpr = node.getDelCalleeVariantExpr();
    String variant;
    if (variantExpr == null) {
        variant = "";
    } else {
        try {
            SoyValue variantData = eval(variantExpr, node);
            if (variantData instanceof IntegerData) {
                // An integer constant is being used as variant. Use the value string representation as
                // variant.
                variant = String.valueOf(variantData.longValue());
            } else {
                // Variant is either a StringData or a SanitizedContent. Use the value as a string. If
                // the value is not a string, and exception will be thrown.
                variant = variantData.stringValue();
            }
        } catch (SoyDataException e) {
            throw RenderException.createWithSource(String.format("Variant expression \"%s\" doesn't evaluate to a valid type " + "(Only string and integer are supported).", variantExpr.toSourceString()), e, node);
        }
    }
    DelTemplateKey delegateKey = DelTemplateKey.create(node.getDelCalleeName(), variant);
    TemplateDelegateNode callee;
    try {
        callee = templateRegistry.selectDelTemplate(delegateKey, activeDelPackageSelector);
    } catch (IllegalArgumentException e) {
        throw RenderException.createWithSource(e.getMessage(), e, node);
    }
    if (callee != null) {
        visitCallNodeHelper(node, callee);
    } else if (node.allowEmptyDefault()) {
        // no active delegate implementation, so the call output is empty string
        return;
    } else {
        throw RenderException.createWithSource("Found no active impl for delegate call to \"" + node.getDelCalleeName() + (variant.isEmpty() ? "" : ":" + variant) + "\" (and delcall does not set allowemptydefault=\"true\").", node);
    }
}
Also used : TemplateDelegateNode(com.google.template.soy.soytree.TemplateDelegateNode) IntegerData(com.google.template.soy.data.restricted.IntegerData) DelTemplateKey(com.google.template.soy.soytree.TemplateDelegateNode.DelTemplateKey) SoyValue(com.google.template.soy.data.SoyValue) SoyDataException(com.google.template.soy.data.SoyDataException) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 7 with ExprRootNode

use of com.google.template.soy.exprtree.ExprRootNode in project closure-templates by google.

the class RenderVisitor method visitVeLogNode.

@Override
protected void visitVeLogNode(VeLogNode node) {
    ExprRootNode logonlyExpression = node.getLogonlyExpression();
    if (logonlyExpression != null) {
        if (eval(logonlyExpression, node).booleanValue()) {
            throw RenderException.createWithSource("Cannot set logonly=\"true\" unless there is a logger configured, but tofu doesn't " + "support loggers", node);
        }
    }
    visitChildren(node);
}
Also used : ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 8 with ExprRootNode

use of com.google.template.soy.exprtree.ExprRootNode in project closure-templates by google.

the class RenderVisitorAssistantForMsgs method visitMsgSelectNode.

@Override
protected void visitMsgSelectNode(MsgSelectNode node) {
    ExprRootNode selectExpr = node.getExpr();
    String selectValue;
    try {
        selectValue = master.evalForUseByAssistants(selectExpr, node).stringValue();
    } catch (SoyDataException e) {
        throw RenderException.createWithSource(String.format("Select expression \"%s\" doesn't evaluate to string.", selectExpr.toSourceString()), e, node);
    }
    // Check each case.
    for (CaseOrDefaultNode child : node.getChildren()) {
        if (child instanceof MsgSelectDefaultNode) {
            // This means it didn't match any other case.
            visitChildren(child);
        } else {
            if (((MsgSelectCaseNode) child).getCaseValue().equals(selectValue)) {
                visitChildren(child);
                return;
            }
        }
    }
}
Also used : CaseOrDefaultNode(com.google.template.soy.soytree.CaseOrDefaultNode) MsgSelectDefaultNode(com.google.template.soy.soytree.MsgSelectDefaultNode) SoyDataException(com.google.template.soy.data.SoyDataException) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 9 with ExprRootNode

use of com.google.template.soy.exprtree.ExprRootNode in project closure-templates by google.

the class RenderVisitorAssistantForMsgs method visitMsgPluralNode.

@Override
protected void visitMsgPluralNode(MsgPluralNode node) {
    ExprRootNode pluralExpr = node.getExpr();
    double pluralValue;
    try {
        pluralValue = master.evalForUseByAssistants(pluralExpr, node).numberValue();
    } catch (SoyDataException e) {
        throw RenderException.createWithSource(String.format("Plural expression \"%s\" doesn't evaluate to number.", pluralExpr.toSourceString()), e, node);
    }
    // Check each case.
    for (CaseOrDefaultNode child : node.getChildren()) {
        if (child instanceof MsgPluralDefaultNode) {
            // This means it didn't match any other case.
            visitChildren(child);
            break;
        } else {
            if (((MsgPluralCaseNode) child).getCaseNumber() == pluralValue) {
                visitChildren(child);
                break;
            }
        }
    }
}
Also used : CaseOrDefaultNode(com.google.template.soy.soytree.CaseOrDefaultNode) MsgPluralDefaultNode(com.google.template.soy.soytree.MsgPluralDefaultNode) SoyDataException(com.google.template.soy.data.SoyDataException) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 10 with ExprRootNode

use of com.google.template.soy.exprtree.ExprRootNode in project closure-templates by google.

the class TemplateDelegateNodeBuilder method setCmdTextInfo.

/**
 * Alternative to {@code setCmdText()} that sets command text info directly as opposed to having
 * it parsed from the command text string. The cmdText field will be set to a canonical string
 * generated from the given info.
 *
 * @param delTemplateName The delegate template name.
 * @param delTemplateVariant The delegate template variant.
 * @param delPriority The delegate priority.
 * @param autoescapeMode The mode of autoescaping for this template.
 * @param contentKind Strict mode context. Nonnull iff autoescapeMode is strict.
 * @param requiredCssNamespaces CSS namespaces required to render the template.
 * @return This builder.
 */
public TemplateDelegateNodeBuilder setCmdTextInfo(String delTemplateName, String delTemplateVariant, Priority delPriority, AutoescapeMode autoescapeMode, SanitizedContentKind contentKind, ImmutableList<String> requiredCssNamespaces) {
    Preconditions.checkState(this.sourceLocation != null);
    Preconditions.checkState(this.cmdText == null);
    Preconditions.checkArgument(BaseUtils.isDottedIdentifier(delTemplateName));
    Preconditions.checkArgument(delTemplateVariant.length() == 0 || BaseUtils.isIdentifier(delTemplateVariant));
    Preconditions.checkArgument((contentKind != null) == (autoescapeMode == AutoescapeMode.STRICT));
    this.delTemplateName = delTemplateName;
    this.delTemplateVariantExpr = new ExprRootNode(new StringNode(delTemplateVariant, QuoteStyle.SINGLE, this.sourceLocation));
    this.delPriority = delPriority;
    setAutoescapeInfo(autoescapeMode, contentKind, sourceLocation);
    setRequiredCssNamespaces(requiredCssNamespaces);
    String cmdText = delTemplateName + ((delTemplateVariant.length() == 0) ? "" : " variant=\"" + delTemplateVariant + "\"") + " autoescape=\"" + autoescapeMode.getAttributeValue() + "\"";
    if (contentKind != null) {
        cmdText += " kind=\"" + contentKind.asAttributeValue() + '"';
    }
    if (!requiredCssNamespaces.isEmpty()) {
        cmdText += " requirecss=\"" + Joiner.on(", ").join(requiredCssNamespaces) + "\"";
    }
    this.cmdText = cmdText;
    genInternalTemplateNameHelper();
    return this;
}
Also used : StringNode(com.google.template.soy.exprtree.StringNode) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Aggregations

ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)16 SoyDataException (com.google.template.soy.data.SoyDataException)3 SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)3 PrintDirectiveNode (com.google.template.soy.soytree.PrintDirectiveNode)3 ArrayList (java.util.ArrayList)3 SoyValue (com.google.template.soy.data.SoyValue)2 StringNode (com.google.template.soy.exprtree.StringNode)2 Expression (com.google.template.soy.jbcsrc.restricted.Expression)2 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)2 Statement (com.google.template.soy.jbcsrc.restricted.Statement)2 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)2 SoyJsSrcPrintDirective (com.google.template.soy.jssrc.restricted.SoyJsSrcPrintDirective)2 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)2 CaseOrDefaultNode (com.google.template.soy.soytree.CaseOrDefaultNode)2 Label (org.objectweb.asm.Label)2 Identifier (com.google.template.soy.base.internal.Identifier)1 IntegerData (com.google.template.soy.data.restricted.IntegerData)1 UndefinedData (com.google.template.soy.data.restricted.UndefinedData)1 Checkpoint (com.google.template.soy.error.ErrorReporter.Checkpoint)1 ExprNode (com.google.template.soy.exprtree.ExprNode)1