Search in sources :

Example 1 with CaseOrDefaultNode

use of com.google.template.soy.soytree.CaseOrDefaultNode in project closure-templates by google.

the class MsgUtils method buildMsgPartForSelect.

/**
 * Builds the list of SoyMsgParts for the given MsgSelectNode.
 *
 * @param msgSelectNode The select node parsed from the Soy source.
 * @param msgNode The MsgNode containing 'msgSelectNode'.
 * @return A SoyMsgSelectPart.
 */
private static SoyMsgSelectPart buildMsgPartForSelect(MsgSelectNode msgSelectNode, MsgNode msgNode) {
    // This is the list of the cases.
    ImmutableList.Builder<SoyMsgPart.Case<String>> selectCases = ImmutableList.builder();
    for (CaseOrDefaultNode child : msgSelectNode.getChildren()) {
        ImmutableList<SoyMsgPart> caseMsgParts = buildMsgPartsForChildren(child, msgNode);
        String caseValue;
        if (child instanceof MsgSelectCaseNode) {
            caseValue = ((MsgSelectCaseNode) child).getCaseValue();
        } else if (child instanceof MsgSelectDefaultNode) {
            caseValue = null;
        } else {
            throw new AssertionError("Unidentified node under a select node.");
        }
        selectCases.add(SoyMsgPart.Case.create(caseValue, caseMsgParts));
    }
    return new SoyMsgSelectPart(msgNode.getSelectVarName(msgSelectNode), selectCases.build());
}
Also used : CaseOrDefaultNode(com.google.template.soy.soytree.CaseOrDefaultNode) ImmutableList(com.google.common.collect.ImmutableList) MsgSelectDefaultNode(com.google.template.soy.soytree.MsgSelectDefaultNode) SoyMsgSelectPart(com.google.template.soy.msgs.restricted.SoyMsgSelectPart) MsgSelectCaseNode(com.google.template.soy.soytree.MsgSelectCaseNode) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart)

Example 2 with CaseOrDefaultNode

use of com.google.template.soy.soytree.CaseOrDefaultNode in project closure-templates by google.

the class MsgUtils method buildMsgPartForPlural.

/**
 * Builds the list of SoyMsgParts for the given MsgPluralNode.
 *
 * @param msgPluralNode The plural node parsed from the Soy source.
 * @param msgNode The MsgNode containing 'msgPluralNode'.
 * @return A SoyMsgPluralPart.
 */
private static SoyMsgPluralPart buildMsgPartForPlural(MsgPluralNode msgPluralNode, MsgNode msgNode) {
    // This is the list of the cases.
    ImmutableList.Builder<SoyMsgPart.Case<SoyMsgPluralCaseSpec>> pluralCases = ImmutableList.builder();
    for (CaseOrDefaultNode child : msgPluralNode.getChildren()) {
        ImmutableList<SoyMsgPart> caseMsgParts = buildMsgPartsForChildren(child, msgNode);
        SoyMsgPluralCaseSpec caseSpec;
        if (child instanceof MsgPluralCaseNode) {
            caseSpec = new SoyMsgPluralCaseSpec(((MsgPluralCaseNode) child).getCaseNumber());
        } else if (child instanceof MsgPluralDefaultNode) {
            caseSpec = new SoyMsgPluralCaseSpec(Type.OTHER);
        } else {
            throw new AssertionError("Unidentified node under a plural node.");
        }
        pluralCases.add(SoyMsgPart.Case.create(caseSpec, caseMsgParts));
    }
    return new SoyMsgPluralPart(msgNode.getPluralVarName(msgPluralNode), msgPluralNode.getOffset(), pluralCases.build());
}
Also used : CaseOrDefaultNode(com.google.template.soy.soytree.CaseOrDefaultNode) SoyMsgPluralCaseSpec(com.google.template.soy.msgs.restricted.SoyMsgPluralCaseSpec) MsgPluralCaseNode(com.google.template.soy.soytree.MsgPluralCaseNode) ImmutableList(com.google.common.collect.ImmutableList) SoyMsgPluralPart(com.google.template.soy.msgs.restricted.SoyMsgPluralPart) MsgPluralDefaultNode(com.google.template.soy.soytree.MsgPluralDefaultNode) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart)

Example 3 with CaseOrDefaultNode

use of com.google.template.soy.soytree.CaseOrDefaultNode 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 4 with CaseOrDefaultNode

use of com.google.template.soy.soytree.CaseOrDefaultNode 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)

Aggregations

CaseOrDefaultNode (com.google.template.soy.soytree.CaseOrDefaultNode)4 ImmutableList (com.google.common.collect.ImmutableList)2 SoyDataException (com.google.template.soy.data.SoyDataException)2 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)2 SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)2 MsgPluralDefaultNode (com.google.template.soy.soytree.MsgPluralDefaultNode)2 MsgSelectDefaultNode (com.google.template.soy.soytree.MsgSelectDefaultNode)2 SoyMsgPluralCaseSpec (com.google.template.soy.msgs.restricted.SoyMsgPluralCaseSpec)1 SoyMsgPluralPart (com.google.template.soy.msgs.restricted.SoyMsgPluralPart)1 SoyMsgSelectPart (com.google.template.soy.msgs.restricted.SoyMsgSelectPart)1 MsgPluralCaseNode (com.google.template.soy.soytree.MsgPluralCaseNode)1 MsgSelectCaseNode (com.google.template.soy.soytree.MsgSelectCaseNode)1