use of com.google.template.soy.soytree.MsgSelectDefaultNode 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.MsgSelectDefaultNode 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.MsgSelectDefaultNode in project closure-templates by google.
the class TemplateParserTest method testParseMsgStmtWithNestedSelects.
@Test
public void testParseMsgStmtWithNestedSelects() throws Exception {
String templateBody = "{@param gender1 : ?}{@param gender2: ?}{@param person1 : ?}{@param person2: ?}\n" + "{msg desc=\"A sample nested message\"}\n" + " {select $gender1}\n" + " {case 'female'}\n" + " {select $gender2}\n" + " {case 'female'}{$person1} added {$person2} and her friends to her circle.\n" + " {default}{$person1} added {$person2} and his friends to her circle.\n" + " {/select}\n" + " {default}\n" + " {select $gender2}\n" + " {case 'female'}{$person1} put {$person2} and her friends to his circle.\n" + " {default}{$person1} put {$person2} and his friends to his circle.\n" + " {/select}\n" + " {/select}\n" + "{/msg}\n";
List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
assertEquals(1, nodes.size());
MsgNode mn = ((MsgFallbackGroupNode) nodes.get(0)).getChild(0);
assertEquals(1, mn.numChildren());
assertEquals("A sample nested message", mn.getDesc());
// Outer select
MsgSelectNode sn = (MsgSelectNode) mn.getChild(0);
assertEquals("$gender1", sn.getExpr().toSourceString());
// female and default
assertEquals(2, sn.numChildren());
// Outer select: Case 'female'
MsgSelectCaseNode cnf = (MsgSelectCaseNode) sn.getChild(0);
assertEquals("female", cnf.getCaseValue());
// Another select
assertEquals(1, cnf.numChildren());
// Outer select: Case 'female': Inner select
MsgSelectNode sn2 = (MsgSelectNode) cnf.getChild(0);
assertEquals("$gender2", sn2.getExpr().toSourceString());
// female and default
assertEquals(2, sn2.numChildren());
// Outer select: Case 'female': Inner select: Case 'female'
MsgSelectCaseNode cnf2 = (MsgSelectCaseNode) sn2.getChild(0);
assertEquals("female", cnf2.getCaseValue());
assertEquals(4, cnf2.numChildren());
// Outer select: Case 'female': Inner select: Case 'female': Placeholder $person1
MsgPlaceholderNode phn1 = (MsgPlaceholderNode) cnf2.getChild(0);
assertEquals("{$person1}", phn1.toSourceString());
// Outer select: Case 'female': Inner select: Case 'female': RawText
RawTextNode rtn1 = (RawTextNode) cnf2.getChild(1);
assertEquals(" added ", rtn1.getRawText());
// Outer select: Case 'female': Inner select: Case 'female': Placeholder $person2
MsgPlaceholderNode phn2 = (MsgPlaceholderNode) cnf2.getChild(2);
assertEquals("{$person2}", phn2.toSourceString());
// Outer select: Case 'female': Inner select: Case 'female': RawText
RawTextNode rtn2 = (RawTextNode) cnf2.getChild(3);
assertEquals(" and her friends to her circle.", rtn2.getRawText());
// Outer select: Case 'female': Inner select: Default
MsgSelectDefaultNode dn2 = (MsgSelectDefaultNode) sn2.getChild(1);
assertEquals(4, dn2.numChildren());
// Outer select: Case 'female': Inner select: Default: Placeholder $person1
MsgPlaceholderNode phn21 = (MsgPlaceholderNode) dn2.getChild(0);
assertEquals("{$person1}", phn21.toSourceString());
// Outer select: Case 'female': Inner select: Default: RawText
RawTextNode rtn21 = (RawTextNode) dn2.getChild(1);
assertEquals(" added ", rtn21.getRawText());
// Outer select: Case 'female': Inner select: Default: Placeholder $person2
MsgPlaceholderNode phn22 = (MsgPlaceholderNode) dn2.getChild(2);
assertEquals("{$person2}", phn22.toSourceString());
// Outer select: Case 'female': Inner select: Default: RawText
RawTextNode rtn22 = (RawTextNode) dn2.getChild(3);
assertEquals(" and his friends to her circle.", rtn22.getRawText());
// Outer select: Default
MsgSelectDefaultNode dn = (MsgSelectDefaultNode) sn.getChild(1);
// Another select
assertEquals(1, dn.numChildren());
// Outer select: Default: Inner select
MsgSelectNode sn3 = (MsgSelectNode) dn.getChild(0);
assertEquals("$gender2", sn3.getExpr().toSourceString());
// female and default
assertEquals(2, sn3.numChildren());
// Outer select: Default: Inner select: Case 'female'
MsgSelectCaseNode cnf3 = (MsgSelectCaseNode) sn3.getChild(0);
assertEquals("female", cnf3.getCaseValue());
assertEquals(4, cnf3.numChildren());
// Outer select: Default: Inner select: Case 'female': Placeholder $person1
MsgPlaceholderNode phn3 = (MsgPlaceholderNode) cnf3.getChild(0);
assertEquals("{$person1}", phn3.toSourceString());
// Outer select: Default: Inner select: Case 'female': RawText
RawTextNode rtn3 = (RawTextNode) cnf3.getChild(1);
assertEquals(" put ", rtn3.getRawText());
// Outer select: Default: Inner select: Case 'female': Placeholder $person2
MsgPlaceholderNode phn4 = (MsgPlaceholderNode) cnf3.getChild(2);
assertEquals("{$person2}", phn4.toSourceString());
// Outer select: Default: Inner select: Case 'female': RawText
RawTextNode rtn4 = (RawTextNode) cnf3.getChild(3);
assertEquals(" and her friends to his circle.", rtn4.getRawText());
// Outer select: Default: Inner select: Default
MsgSelectDefaultNode dn3 = (MsgSelectDefaultNode) sn3.getChild(1);
assertEquals(4, dn3.numChildren());
// Outer select: Default: Inner select: Default: Placeholder $person1
MsgPlaceholderNode phn5 = (MsgPlaceholderNode) dn3.getChild(0);
assertEquals("{$person1}", phn5.toSourceString());
// Outer select: Default: Inner select: Default: RawText
RawTextNode rtn5 = (RawTextNode) dn3.getChild(1);
assertEquals(" put ", rtn5.getRawText());
// Outer select: Default: Inner select: Default: Placeholder $person2
MsgPlaceholderNode phn6 = (MsgPlaceholderNode) dn3.getChild(2);
assertEquals("{$person2}", phn6.toSourceString());
// Outer select: Default: Inner select: Default: RawText
RawTextNode rtn6 = (RawTextNode) dn3.getChild(3);
assertEquals(" and his friends to his circle.", rtn6.getRawText());
}
use of com.google.template.soy.soytree.MsgSelectDefaultNode in project closure-templates by google.
the class RewriteGenderMsgsVisitor method splitMsgForGender.
/**
* Helper to split a msg for gender, by adding a 'select' node and cloning the msg's contents into
* all 3 cases of the 'select' node ('female'/'male'/default).
*
* @param msg The message to split.
* @param genderExpr The expression for the gender value.
* @param baseSelectVarName The base select var name to use, or null if it should be generated
* from the gender expression.
*/
private void splitMsgForGender(MsgNode msg, ExprRootNode genderExpr, @Nullable String baseSelectVarName) {
List<StandaloneNode> origChildren = ImmutableList.copyOf(msg.getChildren());
msg.clearChildren();
MsgSelectCaseNode femaleCase = new MsgSelectCaseNode(nodeIdGen.genId(), msg.getSourceLocation(), "female");
femaleCase.addChildren(SoyTreeUtils.cloneListWithNewIds(origChildren, nodeIdGen));
MsgSelectCaseNode maleCase = new MsgSelectCaseNode(nodeIdGen.genId(), msg.getSourceLocation(), "male");
maleCase.addChildren(SoyTreeUtils.cloneListWithNewIds(origChildren, nodeIdGen));
MsgSelectDefaultNode defaultCase = new MsgSelectDefaultNode(nodeIdGen.genId(), msg.getSourceLocation());
defaultCase.addChildren(SoyTreeUtils.cloneListWithNewIds(origChildren, nodeIdGen));
MsgSelectNode selectNode = new MsgSelectNode(nodeIdGen.genId(), msg.getSourceLocation(), genderExpr, baseSelectVarName);
selectNode.addChild(femaleCase);
selectNode.addChild(maleCase);
selectNode.addChild(defaultCase);
msg.addChild(selectNode);
}
use of com.google.template.soy.soytree.MsgSelectDefaultNode in project closure-templates by google.
the class TemplateParserTest method testParseMsgStmtWithSelect.
@Test
public void testParseMsgStmtWithSelect() throws Exception {
String templateBody = "{@param gender : ?}{@param person : ?}\n" + "{msg desc=\"A sample gender message\"}\n" + " {select $gender}\n" + " {case 'female'}{$person} added you to her circle.\n" + " {default}{$person} added you to his circle.\n" + " {/select}\n" + "{/msg}\n";
List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
assertEquals(1, nodes.size());
MsgNode mn = ((MsgFallbackGroupNode) nodes.get(0)).getChild(0);
assertEquals(1, mn.numChildren());
assertEquals("A sample gender message", mn.getDesc());
MsgSelectNode sn = (MsgSelectNode) mn.getChild(0);
assertEquals("$gender", sn.getExpr().toSourceString());
// female and default
assertEquals(2, sn.numChildren());
// Case 'female'
MsgSelectCaseNode cnf = (MsgSelectCaseNode) sn.getChild(0);
assertEquals("female", cnf.getCaseValue());
assertEquals(2, cnf.numChildren());
MsgPlaceholderNode phnf = (MsgPlaceholderNode) cnf.getChild(0);
assertEquals("{$person}", phnf.toSourceString());
RawTextNode rtnf = (RawTextNode) cnf.getChild(1);
assertEquals(" added you to her circle.", rtnf.getRawText());
// Default
MsgSelectDefaultNode dn = (MsgSelectDefaultNode) sn.getChild(1);
assertEquals(2, dn.numChildren());
MsgPlaceholderNode phnd = (MsgPlaceholderNode) dn.getChild(0);
assertEquals("{$person}", phnd.toSourceString());
RawTextNode rtnd = (RawTextNode) dn.getChild(1);
assertEquals(" added you to his circle.", rtnd.getRawText());
}
Aggregations