use of com.google.template.soy.soytree.SoyFileNode in project closure-templates by google.
the class SoyFileSet method pruneTranslatedMsgs.
/**
* Prunes messages from a given message bundle, keeping only messages used in this Soy file set.
*
* <p>Important: Do not use directly. This is subject to change and your code will break.
*
* <p>Note: This method memoizes intermediate results to improve efficiency in the case that it is
* called multiple times (which is a common case). Thus, this method will not work correctly if
* the underlying Soy files are modified between calls to this method.
*
* @param origTransMsgBundle The message bundle to prune.
* @return The pruned message bundle.
* @throws SoyCompilationException If compilation fails.
*/
public SoyMsgBundle pruneTranslatedMsgs(SoyMsgBundle origTransMsgBundle) {
resetErrorReporter();
if (memoizedExtractedMsgIdsForPruning == null) {
ParseResult result = parse(passManagerBuilder(SyntaxVersion.V1_0).allowUnknownGlobals().disableAllTypeChecking(), // can't resolve strict types
SoyTypeRegistry.DEFAULT_UNKNOWN, new PluginResolver(PluginResolver.Mode.ALLOW_UNDEFINED, printDirectives, soyFunctionMap, errorReporter));
throwIfErrorsPresent();
SoyFileSetNode soyTree = result.fileSet();
TemplateRegistry registry = result.registry();
List<TemplateNode> allPublicTemplates = Lists.newArrayList();
for (SoyFileNode soyFile : soyTree.getChildren()) {
for (TemplateNode template : soyFile.getChildren()) {
if (template.getVisibility() == Visibility.PUBLIC) {
allPublicTemplates.add(template);
}
}
}
Map<TemplateNode, TransitiveDepTemplatesInfo> depsInfoMap = new FindTransitiveDepTemplatesVisitor(registry).execOnMultipleTemplates(allPublicTemplates);
TransitiveDepTemplatesInfo mergedDepsInfo = TransitiveDepTemplatesInfo.merge(depsInfoMap.values());
SoyMsgBundle extractedMsgBundle = new ExtractMsgsVisitor().execOnMultipleNodes(mergedDepsInfo.depTemplateSet);
ImmutableSet.Builder<Long> extractedMsgIdsBuilder = ImmutableSet.builder();
for (SoyMsg extractedMsg : extractedMsgBundle) {
extractedMsgIdsBuilder.add(extractedMsg.getId());
}
throwIfErrorsPresent();
memoizedExtractedMsgIdsForPruning = extractedMsgIdsBuilder.build();
}
// ------ Prune. ------
ImmutableList.Builder<SoyMsg> prunedTransMsgsBuilder = ImmutableList.builder();
for (SoyMsg transMsg : origTransMsgBundle) {
if (memoizedExtractedMsgIdsForPruning.contains(transMsg.getId())) {
prunedTransMsgsBuilder.add(transMsg);
}
}
throwIfErrorsPresent();
return new SoyMsgBundleImpl(origTransMsgBundle.getLocaleString(), prunedTransMsgsBuilder.build());
}
use of com.google.template.soy.soytree.SoyFileNode in project closure-templates by google.
the class SoyFileSetParser method parseWithVersions.
/**
* Parses a set of Soy files, returning a structure containing the parse tree and template
* registry.
*/
private ParseResult parseWithVersions() throws IOException {
IdGenerator nodeIdGen = (cache() != null) ? cache().getNodeIdGenerator() : new IncrementingIdGenerator();
SoyFileSetNode soyTree = new SoyFileSetNode(nodeIdGen.genId(), nodeIdGen);
boolean filesWereSkipped = false;
// generator but fail to lock on it. Eliminate the id system to avoid this whole issue.
synchronized (nodeIdGen) {
// Avoid using the same ID generator in multiple threads.
for (SoyFileSupplier fileSupplier : soyFileSuppliers().values()) {
SoyFileSupplier.Version version = fileSupplier.getVersion();
VersionedFile cachedFile = cache() != null ? cache().get(fileSupplier.getFilePath(), version) : null;
SoyFileNode node;
if (cachedFile == null) {
node = parseSoyFileHelper(fileSupplier, nodeIdGen, typeRegistry());
// a malformed parse tree.
if (node == null) {
filesWereSkipped = true;
continue;
}
// Run passes that are considered part of initial parsing.
passManager().runSingleFilePasses(node, nodeIdGen);
// Run passes that check the tree.
if (cache() != null) {
cache().put(fileSupplier.getFilePath(), VersionedFile.of(node, version));
}
} else {
node = cachedFile.file();
}
soyTree.addChild(node);
}
TemplateRegistry registry;
// Run passes that check the tree iff we successfully parsed every file.
if (!filesWereSkipped) {
registry = passManager().runWholeFilesetPasses(soyTree);
} else {
registry = new TemplateRegistry(soyTree, errorReporter());
}
return ParseResult.create(soyTree, registry);
}
}
use of com.google.template.soy.soytree.SoyFileNode in project closure-templates by google.
the class RenderVisitorTest method testRenderMsgStmtWithFallback.
@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");
}
use of com.google.template.soy.soytree.SoyFileNode in project closure-templates by google.
the class GenerateParseInfoVisitorTest method testJavaClassNameSource.
@Test
public void testJavaClassNameSource() {
SoyFileNode soyFileNode = forFilePathAndNamespace("BooFoo.soy", "aaa.bbb.cccDdd");
assertEquals("BooFoo", SOY_FILE_NAME.generateBaseClassName(soyFileNode));
soyFileNode = forFilePathAndNamespace("blah/bleh/boo_foo.soy", "aaa.bbb.cccDdd");
assertEquals("BooFoo", SOY_FILE_NAME.generateBaseClassName(soyFileNode));
soyFileNode = forFilePathAndNamespace("boo-FOO.soy", "aaa.bbb.cccDdd");
assertEquals("BooFoo", SOY_FILE_NAME.generateBaseClassName(soyFileNode));
soyFileNode = forFilePathAndNamespace("\\BLAH\\BOO_FOO.SOY", "aaa.bbb.cccDdd");
assertEquals("BooFoo", SOY_FILE_NAME.generateBaseClassName(soyFileNode));
soyFileNode = forFilePathAndNamespace("", "cccDdd");
assertEquals("CccDdd", SOY_NAMESPACE_LAST_PART.generateBaseClassName(soyFileNode));
soyFileNode = forFilePathAndNamespace("", "aaa.bbb.cccDdd");
assertEquals("CccDdd", SOY_NAMESPACE_LAST_PART.generateBaseClassName(soyFileNode));
soyFileNode = forFilePathAndNamespace("", "aaa_bbb.ccc_ddd");
assertEquals("CccDdd", SOY_NAMESPACE_LAST_PART.generateBaseClassName(soyFileNode));
soyFileNode = forFilePathAndNamespace("", "CccDdd");
assertEquals("CccDdd", SOY_NAMESPACE_LAST_PART.generateBaseClassName(soyFileNode));
soyFileNode = forFilePathAndNamespace("", "aaa.bbb.ccc_DDD");
assertEquals("CccDdd", SOY_NAMESPACE_LAST_PART.generateBaseClassName(soyFileNode));
soyFileNode = forFilePathAndNamespace("BooFoo.soy", "aaa.bbb.cccDdd");
assertEquals("File", GENERIC.generateBaseClassName(soyFileNode));
soyFileNode = forFilePathAndNamespace("blah/bleh/boo-foo.soy", "ccc_ddd");
assertEquals("File", GENERIC.generateBaseClassName(soyFileNode));
}
use of com.google.template.soy.soytree.SoyFileNode in project closure-templates by google.
the class FindCalleesNotInFileVisitorTest method testFindCalleesNotInFile.
@Test
public void testFindCalleesNotInFile() {
String testFileContent = "" + "{namespace boo.foo}\n" + "\n" + "/** Test template 1. */\n" + "{template .goo autoescape=\"deprecated-noncontextual\"}\n" + " {call .goo data=\"all\" /}\n" + " {call .moo data=\"all\" /}\n" + " {call boo.woo.hoo data=\"all\" /}\n" + // not defined in this file
"{/template}\n" + "\n" + "/** Test template 2. */\n" + "{template .moo autoescape=\"deprecated-noncontextual\"}\n" + " {for $i in range(8)}\n" + " {call boo.foo.goo data=\"all\" /}\n" + " {call .too data=\"all\" /}\n" + // not defined in this file
" {call .goo}" + " {param a}{call .moo /}{/param}" + " {param b}{call .zoo /}{/param}" + // not defined in this file
" {/call}" + " {/for}\n" + "{/template}\n" + "\n" + "/** Test template 3. */\n" + "{deltemplate booHoo autoescape=\"deprecated-noncontextual\"}\n" + " {call .goo data=\"all\" /}\n" + " {call .moo data=\"all\" /}\n" + " {call boo.hoo.roo data=\"all\" /}\n" + // not defined in this file
"{/deltemplate}\n";
ErrorReporter boom = ErrorReporter.exploding();
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(testFileContent).errorReporter(boom).parse().fileSet();
SoyFileNode soyFile = soyTree.getChild(0);
Iterable<String> calleesNotInFile = Iterables.transform(new FindCalleesNotInFileVisitor().exec(soyFile), CallBasicNode::getCalleeName);
assertThat(calleesNotInFile).containsExactly("boo.woo.hoo", "boo.foo.too", "boo.foo.zoo", "boo.hoo.roo");
}
Aggregations