本文整理汇总了Java中com.google.template.soy.msgs.restricted.SoyMsgRawTextPart类的典型用法代码示例。如果您正苦于以下问题:Java SoyMsgRawTextPart类的具体用法?Java SoyMsgRawTextPart怎么用?Java SoyMsgRawTextPart使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SoyMsgRawTextPart类属于com.google.template.soy.msgs.restricted包,在下文中一共展示了SoyMsgRawTextPart类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildMsgPartsForChildren
import com.google.template.soy.msgs.restricted.SoyMsgRawTextPart; //导入依赖的package包/类
/**
* Builds the list of SoyMsgParts for all the children of a given parent node.
*
* @param parent Can be MsgNode, MsgPluralCaseNode, MsgPluralDefaultNode, MsgSelectCaseNode, or
* MsgSelectDefaultNode.
* @param msgNode The MsgNode containing 'parent'.
*/
private static ImmutableList<SoyMsgPart> buildMsgPartsForChildren(
BlockNode parent, MsgNode msgNode) {
ImmutableList.Builder<SoyMsgPart> msgParts = ImmutableList.builder();
for (StandaloneNode child : parent.getChildren()) {
if (child instanceof RawTextNode) {
String rawText = ((RawTextNode) child).getRawText();
msgParts.add(SoyMsgRawTextPart.of(rawText));
} else if (child instanceof MsgPlaceholderNode) {
String placeholderName = msgNode.getPlaceholderName((MsgPlaceholderNode) child);
msgParts.add(new SoyMsgPlaceholderPart(placeholderName));
} else if (child instanceof MsgPluralNode) {
msgParts.add(buildMsgPartForPlural((MsgPluralNode) child, msgNode));
} else if (child instanceof MsgSelectNode) {
msgParts.add(buildMsgPartForSelect((MsgSelectNode) child, msgNode));
}
}
return msgParts.build();
}
示例2: convertMsgPartsToEmbeddedIcuSyntax
import com.google.template.soy.msgs.restricted.SoyMsgRawTextPart; //导入依赖的package包/类
/**
* Given a list of msg parts: (a) if it contains any plural/select parts, then builds a new list
* of msg parts where plural/select parts in the original msg parts are all embedded as raw text
* in ICU format, (b) if it doesn't contain any plural/select parts, then simply returns the
* original msg parts instead of creating a new list of identical msg parts.
*
* @param origMsgParts The msg parts to convert.
* @param allowIcuEscapingInRawText If true, then ICU syntax chars needing escaping in will be
* escaped. If false, then a SoySyntaxException will be thrown if an ICU syntax char needing
* escaping is encountered in raw text.
* @return A new list of msg parts with embedded ICU syntax if the original msg parts contain
* plural/select parts, otherwise the original msg parts.
*/
public static ImmutableList<SoyMsgPart> convertMsgPartsToEmbeddedIcuSyntax(
ImmutableList<SoyMsgPart> origMsgParts, boolean allowIcuEscapingInRawText) {
// If origMsgParts doesn't have plural/select parts, simply return it.
if (!MsgPartUtils.hasPlrselPart(origMsgParts)) {
return origMsgParts;
}
// Build the new msg parts.
ImmutableList.Builder<SoyMsgPart> newMsgPartsBuilder = ImmutableList.builder();
StringBuilder currRawTextSb = new StringBuilder();
convertMsgPartsHelper(
newMsgPartsBuilder, currRawTextSb, origMsgParts, false, allowIcuEscapingInRawText);
if (currRawTextSb.length() > 0) {
newMsgPartsBuilder.add(SoyMsgRawTextPart.of(currRawTextSb.toString()));
}
return newMsgPartsBuilder.build();
}
示例3: handleBasicTranslation
import com.google.template.soy.msgs.restricted.SoyMsgRawTextPart; //导入依赖的package包/类
/** Handles a translation consisting of a single raw text node. */
private Statement handleBasicTranslation(
List<SoyPrintDirective> escapingDirectives, Expression soyMsgParts) {
// optimize for simple constant translations (very common)
// this becomes: renderContext.getSoyMessge(<id>).getParts().get(0).getRawText()
SoyExpression text =
SoyExpression.forString(
soyMsgParts
.invoke(MethodRef.LIST_GET, constant(0))
.checkedCast(SoyMsgRawTextPart.class)
.invoke(MethodRef.SOY_MSG_RAW_TEXT_PART_GET_RAW_TEXT));
// Note: there is no point in trying to stream here, since we are starting with a constant
// string.
for (SoyPrintDirective directive : escapingDirectives) {
text = parameterLookup.getRenderContext().applyPrintDirective(directive, text);
}
return appendableExpression.appendString(text.coerceToString()).toStatement();
}
示例4: renderSelect
import com.google.template.soy.msgs.restricted.SoyMsgRawTextPart; //导入依赖的package包/类
/**
* Render a {@code {select}} part of a message. Most of the complexity is handled by {@link
* SoyMsgSelectPart#lookupCase} all this needs to do is apply the placeholders to all the
* children.
*/
private static void renderSelect(
@Nullable ULocale locale,
SoyMsgSelectPart firstPart,
Map<String, Object> placeholders,
Appendable out)
throws IOException {
String selectCase = getSelectCase(placeholders, firstPart.getSelectVarName());
for (SoyMsgPart casePart : firstPart.lookupCase(selectCase)) {
if (casePart instanceof SoyMsgSelectPart) {
renderSelect(locale, (SoyMsgSelectPart) casePart, placeholders, out);
} else if (casePart instanceof SoyMsgPluralPart) {
renderPlural(locale, (SoyMsgPluralPart) casePart, placeholders, out);
} else if (casePart instanceof SoyMsgPlaceholderPart) {
writePlaceholder((SoyMsgPlaceholderPart) casePart, placeholders, out);
} else if (casePart instanceof SoyMsgRawTextPart) {
writeRawText((SoyMsgRawTextPart) casePart, out);
} else {
// select cannot directly contain remainder nodes.
throw new AssertionError("unexpected part: " + casePart);
}
}
}
示例5: renderPlural
import com.google.template.soy.msgs.restricted.SoyMsgRawTextPart; //导入依赖的package包/类
/**
* Render a {@code {plural}} part of a message. Most of the complexity is handled by {@link
* SoyMsgPluralPart#lookupCase} all this needs to do is apply the placeholders to all the
* children.
*/
private static void renderPlural(
@Nullable ULocale locale,
SoyMsgPluralPart plural,
Map<String, Object> placeholders,
Appendable out)
throws IOException {
int pluralValue = getPlural(placeholders, plural.getPluralVarName());
for (SoyMsgPart casePart : plural.lookupCase(pluralValue, locale)) {
if (casePart instanceof SoyMsgPlaceholderPart) {
writePlaceholder((SoyMsgPlaceholderPart) casePart, placeholders, out);
} else if (casePart instanceof SoyMsgRawTextPart) {
writeRawText((SoyMsgRawTextPart) casePart, out);
} else if (casePart instanceof SoyMsgPluralRemainderPart) {
out.append(String.valueOf(pluralValue - plural.getOffset()));
} else {
// Plural parts will not have nested plural/select parts. So, this is an error.
throw new AssertionError("unexpected part: " + casePart);
}
}
}
示例6: processMsgPartsHelper
import com.google.template.soy.msgs.restricted.SoyMsgRawTextPart; //导入依赖的package包/类
/**
* Private helper to build valid Python string for a list of {@link SoyMsgPart}s.
*
* <p>It only processes {@link SoyMsgRawTextPart} and {@link SoyMsgPlaceholderPart} and ignores
* others, because we didn't generate a direct string for plural and select nodes.
*
* <p>For {@link SoyMsgRawTextPart}, it appends the raw text and applies necessary escaping; For
* {@link SoyMsgPlaceholderPart}, it turns the placeholder's variable name into Python replace
* format.
*
* @param parts The SoyMsgPart parts to convert.
* @param escaper A Function which provides escaping for raw text.
* @return A String representing all the {@code parts} in Python.
*/
private static String processMsgPartsHelper(
ImmutableList<SoyMsgPart> parts, Function<String, String> escaper) {
StringBuilder rawMsgTextSb = new StringBuilder();
for (SoyMsgPart part : parts) {
if (part instanceof SoyMsgRawTextPart) {
rawMsgTextSb.append(escaper.apply(((SoyMsgRawTextPart) part).getRawText()));
}
if (part instanceof SoyMsgPlaceholderPart) {
String phName = ((SoyMsgPlaceholderPart) part).getPlaceholderName();
rawMsgTextSb.append("{" + phName + "}");
}
}
return rawMsgTextSb.toString();
}
示例7: endElement
import com.google.template.soy.msgs.restricted.SoyMsgRawTextPart; //导入依赖的package包/类
@Override
public void endElement(String uri, String localName, String qName) {
if (qName.equals("target")) {
// End 'target': Save the preceding raw text (if any). Then create a SoyMsg object from the
// collected message data and add it to msgs list.
if (currRawTextPart != null) {
currMsgParts.add(SoyMsgRawTextPart.of(currRawTextPart));
currRawTextPart = null;
}
isInMsg = false;
if (!currMsgParts.isEmpty()) {
msgs.add(
SoyMsg.builder()
.setId(currMsgId)
.setLocaleString(targetLocaleString)
.setParts(currMsgParts)
.build());
}
}
}
示例8: buildMsgContentStrForMsgIdComputation
import com.google.template.soy.msgs.restricted.SoyMsgRawTextPart; //导入依赖的package包/类
/**
* Private helper to build the canonical message content string that should be used for msg id
* computation.
*
* <p>Note: For people who know what "presentation" means in this context, the result string
* should be exactly the presentation string.
*
* @param msgParts The parts of the message.
* @param doUseBracedPhs Whether to use braced placeholders.
* @return The canonical message content string that should be used for msg id computation.
*/
@VisibleForTesting
static String buildMsgContentStrForMsgIdComputation(
ImmutableList<SoyMsgPart> msgParts, boolean doUseBracedPhs) {
// Note: For source messages, disallow ICU syntax chars that need escaping in raw text.
msgParts = IcuSyntaxUtils.convertMsgPartsToEmbeddedIcuSyntax(msgParts, false);
StringBuilder msgStrSb = new StringBuilder();
for (SoyMsgPart msgPart : msgParts) {
if (msgPart instanceof SoyMsgRawTextPart) {
msgStrSb.append(((SoyMsgRawTextPart) msgPart).getRawText());
} else if (msgPart instanceof SoyMsgPlaceholderPart) {
if (doUseBracedPhs) {
msgStrSb.append('{');
}
msgStrSb.append(((SoyMsgPlaceholderPart) msgPart).getPlaceholderName());
if (doUseBracedPhs) {
msgStrSb.append('}');
}
} else {
throw new AssertionError();
}
}
return msgStrSb.toString();
}
示例9: buildReplacementNodesFromTranslation
import com.google.template.soy.msgs.restricted.SoyMsgRawTextPart; //导入依赖的package包/类
/**
* Private helper for visitMsgFallbackGroupNode() to build the list of replacement nodes for a
* message from its translation.
*/
private void buildReplacementNodesFromTranslation(MsgNode msg, SoyMsg translation) {
currReplacementNodes = Lists.newArrayList();
for (SoyMsgPart msgPart : translation.getParts()) {
if (msgPart instanceof SoyMsgRawTextPart) {
// Append a new RawTextNode to the currReplacementNodes list.
String rawText = ((SoyMsgRawTextPart) msgPart).getRawText();
currReplacementNodes.add(
new RawTextNode(nodeIdGen.genId(), rawText, msg.getSourceLocation()));
} else if (msgPart instanceof SoyMsgPlaceholderPart) {
// Get the representative placeholder node and iterate through its contents.
String placeholderName = ((SoyMsgPlaceholderPart) msgPart).getPlaceholderName();
MsgPlaceholderNode placeholderNode = msg.getRepPlaceholderNode(placeholderName);
for (StandaloneNode contentNode : placeholderNode.getChildren()) {
// If the content node is a MsgHtmlTagNode, it needs to be replaced by a number of
// consecutive siblings. This is done by visiting the MsgHtmlTagNode. Otherwise, we
// simply add the content node to the currReplacementNodes list being built.
if (contentNode instanceof MsgHtmlTagNode) {
visit(contentNode);
} else {
currReplacementNodes.add(contentNode);
}
}
} else {
throw new AssertionError();
}
}
}
示例10: putPlaceholdersIntoMap
import com.google.template.soy.msgs.restricted.SoyMsgRawTextPart; //导入依赖的package包/类
/**
* Adds a {@link Statement} to {@link Map#put} every msg placeholder, plural variable and select
* case value into {@code mapExpression}
*/
private void putPlaceholdersIntoMap(
Expression mapExpression,
MsgNode originalMsg,
Iterable<? extends SoyMsgPart> parts,
Map<String, Statement> placeholderNameToPutStatement) {
for (SoyMsgPart child : parts) {
if (child instanceof SoyMsgRawTextPart || child instanceof SoyMsgPluralRemainderPart) {
// raw text doesn't have placeholders and remainders use the same placeholder as plural they
// are a member of.
continue;
}
if (child instanceof SoyMsgPluralPart) {
putPluralPartIntoMap(
mapExpression, originalMsg, placeholderNameToPutStatement, (SoyMsgPluralPart) child);
} else if (child instanceof SoyMsgSelectPart) {
putSelectPartIntoMap(
mapExpression, originalMsg, placeholderNameToPutStatement, (SoyMsgSelectPart) child);
} else if (child instanceof SoyMsgPlaceholderPart) {
putPlaceholderIntoMap(
mapExpression,
originalMsg,
placeholderNameToPutStatement,
(SoyMsgPlaceholderPart) child);
} else {
throw new AssertionError("unexpected child: " + child);
}
}
}
示例11: renderSoyMsgPartsWithPlaceholders
import com.google.template.soy.msgs.restricted.SoyMsgRawTextPart; //导入依赖的package包/类
/** Render a 'complex' message containing with placeholders. */
public static void renderSoyMsgPartsWithPlaceholders(
ImmutableList<SoyMsgPart> msgParts,
@Nullable ULocale locale,
Map<String, Object> placeholders,
Appendable out)
throws IOException {
// TODO(lukes): the initial plural/select nesting structure of the SoyMsg is determined at
// compile time (though the number of cases varies per locale).
// We could allow the generated code to call renderSelect/renderPlural directly as a
// microoptimization. This could potentially allow us to eliminate the hashmap entry+boxing for
// plural variables when rendering a direct plural tag. ditto for selects, though that is
// more complicated due to the fact that selects can be nested.
SoyMsgPart firstPart = msgParts.get(0);
if (firstPart instanceof SoyMsgPluralPart) {
renderPlural(locale, (SoyMsgPluralPart) firstPart, placeholders, out);
} else if (firstPart instanceof SoyMsgSelectPart) {
renderSelect(locale, (SoyMsgSelectPart) firstPart, placeholders, out);
} else {
// avoid allocating the iterator
for (int i = 0; i < msgParts.size(); i++) {
SoyMsgPart msgPart = msgParts.get(i);
if (msgPart instanceof SoyMsgRawTextPart) {
writeRawText((SoyMsgRawTextPart) msgPart, out);
} else if (msgPart instanceof SoyMsgPlaceholderPart) {
writePlaceholder((SoyMsgPlaceholderPart) msgPart, placeholders, out);
} else {
throw new AssertionError("unexpected part: " + msgPart);
}
}
}
}
示例12: renderMsgFromTranslation
import com.google.template.soy.msgs.restricted.SoyMsgRawTextPart; //导入依赖的package包/类
/** Private helper for visitMsgFallbackGroupNode() to render a message from its translation. */
private void renderMsgFromTranslation(
MsgNode msg, ImmutableList<SoyMsgPart> msgParts, @Nullable ULocale locale) {
SoyMsgPart firstPart = msgParts.get(0);
if (firstPart instanceof SoyMsgPluralPart) {
new PlrselMsgPartsVisitor(msg, locale).visitPart((SoyMsgPluralPart) firstPart);
} else if (firstPart instanceof SoyMsgSelectPart) {
new PlrselMsgPartsVisitor(msg, locale).visitPart((SoyMsgSelectPart) firstPart);
} else {
for (SoyMsgPart msgPart : msgParts) {
if (msgPart instanceof SoyMsgRawTextPart) {
RenderVisitor.append(
master.getCurrOutputBufForUseByAssistants(),
((SoyMsgRawTextPart) msgPart).getRawText());
} else if (msgPart instanceof SoyMsgPlaceholderPart) {
String placeholderName = ((SoyMsgPlaceholderPart) msgPart).getPlaceholderName();
visit(msg.getRepPlaceholderNode(placeholderName));
} else {
throw new AssertionError();
}
}
}
}
示例13: buildGoogMsgContentStr
import com.google.template.soy.msgs.restricted.SoyMsgRawTextPart; //导入依赖的package包/类
/**
* Builds the message content string for a goog.getMsg() call.
*
* @param msgParts The parts of the message.
* @param doUseBracedPhs Whether to use braced placeholders.
* @return The message content string for a goog.getMsg() call.
*/
private static String buildGoogMsgContentStr(
ImmutableList<SoyMsgPart> msgParts, boolean doUseBracedPhs) {
// Note: For source messages, disallow ICU syntax chars that need escaping in raw text.
msgParts = IcuSyntaxUtils.convertMsgPartsToEmbeddedIcuSyntax(msgParts, false);
StringBuilder msgStrSb = new StringBuilder();
for (SoyMsgPart msgPart : msgParts) {
if (msgPart instanceof SoyMsgRawTextPart) {
msgStrSb.append(((SoyMsgRawTextPart) msgPart).getRawText());
} else if (msgPart instanceof SoyMsgPlaceholderPart) {
String placeholderName = ((SoyMsgPlaceholderPart) msgPart).getPlaceholderName();
if (doUseBracedPhs) {
// Add placeholder to message text.
msgStrSb.append("{").append(placeholderName).append("}");
} else {
// For goog.getMsg(), we must change the placeholder name to lower camel-case format.
String googMsgPlaceholderName = genGoogMsgPlaceholderName(placeholderName);
// Add placeholder to message text. Note the '$' for goog.getMsg() syntax.
msgStrSb.append("{$").append(googMsgPlaceholderName).append("}");
}
} else {
throw new AssertionError();
}
}
return msgStrSb.toString();
}
示例14: testKnownMsgIds
import com.google.template.soy.msgs.restricted.SoyMsgRawTextPart; //导入依赖的package包/类
@Test
public void testKnownMsgIds() {
// Important: Do not change these hard-coded values. Changes to the algorithm will break
// backwards compatibility.
assertThat(SoyMsgIdComputer.computeMsgId(HELLO_WORLD_MSG_PARTS, null, null))
.isEqualTo(3022994926184248873L);
assertThat(SoyMsgIdComputer.computeMsgId(HELLO_WORLD_MSG_PARTS, null, "text/html"))
.isEqualTo(3022994926184248873L);
assertThat(SoyMsgIdComputer.computeMsgId(HELLO_NAME_MSG_PARTS, null, null))
.isEqualTo(6936162475751860807L);
assertThat(SoyMsgIdComputer.computeMsgId(HELLO_NAME_MSG_PARTS, null, "text/html"))
.isEqualTo(6936162475751860807L);
assertThat(SoyMsgIdComputer.computeMsgId(PLURAL_MSG_PARTS, null, null))
.isEqualTo(947930983556630648L);
assertThat(SoyMsgIdComputer.computeMsgIdUsingBracedPhs(PLURAL_MSG_PARTS, null, null))
.isEqualTo(1429579464553183506L);
ImmutableList<SoyMsgPart> archiveMsgParts =
ImmutableList.<SoyMsgPart>of(SoyMsgRawTextPart.of("Archive"));
assertThat(SoyMsgIdComputer.computeMsgId(archiveMsgParts, "noun", null))
.isEqualTo(7224011416745566687L);
assertThat(SoyMsgIdComputer.computeMsgId(archiveMsgParts, "verb", null))
.isEqualTo(4826315192146469447L);
ImmutableList<SoyMsgPart> unicodeMsgParts =
ImmutableList.of(
new SoyMsgPlaceholderPart("\u2222\uEEEE"), SoyMsgRawTextPart.of("\u9EC4\u607A"));
assertThat(SoyMsgIdComputer.computeMsgId(unicodeMsgParts, null, null))
.isEqualTo(7971596007260280311L);
assertThat(SoyMsgIdComputer.computeMsgId(unicodeMsgParts, null, "application/javascript"))
.isEqualTo(5109146044343713753L);
}
示例15: testRenderMsgStmtWithFallback
import com.google.template.soy.msgs.restricted.SoyMsgRawTextPart; //导入依赖的package包/类
@Test
public void testRenderMsgStmtWithFallback() throws Exception {
String templateBody =
""
+ " {msg desc=\"\"}\n"
+ " blah\n"
+ " {fallbackmsg desc=\"\"}\n"
+ " bleh\n"
+ " {/msg}\n";
// Without msg bundle.
assertRender(templateBody, "blah");
// With msg bundle.
SoyFileNode file =
SoyFileSetParserBuilder.forFileContents(
"{namespace test}\n{template .foo}\n" + templateBody + "{/template}")
.parse()
.fileSet()
.getChild(0);
MsgFallbackGroupNode msgGroup =
SoyTreeUtils.getAllNodesOfType(file.getChild(0), MsgFallbackGroupNode.class).get(0);
MsgNode fallbackMsg = msgGroup.getChild(1);
SoyMsg translatedFallbackMsg =
SoyMsg.builder()
.setId(MsgUtils.computeMsgIdForDualFormat(fallbackMsg))
.setLocaleString("x-zz")
.setParts(ImmutableList.<SoyMsgPart>of(SoyMsgRawTextPart.of("zbleh")))
.build();
SoyMsgBundle msgBundle =
new SoyMsgBundleImpl("x-zz", Lists.newArrayList(translatedFallbackMsg));
assertThat(renderWithDataAndMsgBundle(templateBody, TEST_DATA, msgBundle)).isEqualTo("zbleh");
}