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());
}
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());
}
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;
}
}
}
}
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;
}
}
}
}
Aggregations