Search in sources :

Example 1 with IncrementingIdGenerator

use of com.google.template.soy.base.internal.IncrementingIdGenerator in project closure-templates by google.

the class HtmlRewritePassTest method assertThatSourceString.

private static StringSubject assertThatSourceString(TemplateNode node) {
    SoyFileNode parent = node.getParent().copy(new CopyState());
    new DesugarHtmlNodesPass().run(parent, new IncrementingIdGenerator());
    StringBuilder sb = new StringBuilder();
    parent.getChild(0).appendSourceStringForChildren(sb);
    return assertThat(sb.toString());
}
Also used : CopyState(com.google.template.soy.basetree.CopyState) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) SoyFileNode(com.google.template.soy.soytree.SoyFileNode)

Example 2 with IncrementingIdGenerator

use of com.google.template.soy.base.internal.IncrementingIdGenerator in project closure-templates by google.

the class SoyTreeUtilsTest method testCloneWithNewIds.

@Test
public final void testCloneWithNewIds() throws Exception {
    IdGenerator nodeIdGen = new IncrementingIdGenerator();
    SoyFileSetNode soyTree = new SoyFileSetNode(nodeIdGen.genId(), nodeIdGen);
    SoyFileNode soyFile = SoyFileSetParserBuilder.forFileContents(SOY_SOURCE_FOR_TESTING_CLONING).parse().fileSet().getChild(0);
    soyTree.addChild(soyFile);
    SoyFileSetNode clone = SoyTreeUtils.cloneWithNewIds(soyTree, nodeIdGen);
    assertEquals(1, clone.numChildren());
    assertFalse(clone.getId() == soyTree.getId());
    assertEquals(clone.getChild(0).toSourceString(), soyFile.toSourceString());
}
Also used : IdGenerator(com.google.template.soy.base.internal.IdGenerator) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) Test(org.junit.Test)

Example 3 with IncrementingIdGenerator

use of com.google.template.soy.base.internal.IncrementingIdGenerator 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);
    }
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) IdGenerator(com.google.template.soy.base.internal.IdGenerator) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) SoyFileSupplier(com.google.template.soy.base.internal.SoyFileSupplier) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) SoyFileNode(com.google.template.soy.soytree.SoyFileNode) VersionedFile(com.google.template.soy.shared.SoyAstCache.VersionedFile)

Example 4 with IncrementingIdGenerator

use of com.google.template.soy.base.internal.IncrementingIdGenerator in project closure-templates by google.

the class InferenceEngineTest method assertTransitions.

private static void assertTransitions(SanitizedContentKind kind, String src) {
    TemplateNode template = SoyFileSetParserBuilder.forFileContents("{namespace ns}\n{template .foo" + (kind == SanitizedContentKind.HTML ? "" : " kind=\"" + Ascii.toLowerCase(kind.toString()) + '"') + " stricthtml=\"false\"}" + src + "{/template}").addSoyFunction(new AssertFunction()).desugarHtmlNodes(false).parse().fileSet().getChild(0).getChild(0);
    Inferences inferences = new Inferences(new IncrementingIdGenerator(), ImmutableListMultimap.<String, TemplateNode>of());
    InferenceEngine.inferTemplateEndContext(template, Context.getStartContextForContentKind(kind), inferences, ErrorReporter.exploding());
    for (PrintNode print : SoyTreeUtils.getAllNodesOfType(template, PrintNode.class)) {
        if (print.getExpr().getChild(0) instanceof FunctionNode) {
            FunctionNode fn = (FunctionNode) print.getExpr().getChild(0);
            if (fn.getSoyFunction() instanceof AssertFunction) {
                assertWithMessage("expected print node at " + print.getSourceLocation() + " to have context").that(contextString(inferences.getContextForNode(print))).isEqualTo(((StringNode) fn.getChild(0)).getValue());
            }
        }
    }
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) PrintNode(com.google.template.soy.soytree.PrintNode)

Example 5 with IncrementingIdGenerator

use of com.google.template.soy.base.internal.IncrementingIdGenerator in project closure-templates by google.

the class AddHtmlCommentsForDebugPassTest method runPass.

/**
 * Parses the given input as a Soy file content, runs the AddHtmlCommentsForDebug pass and returns
 * a map that contains pairs of template names and rewritten template body.
 */
private static ImmutableMap<String, String> runPass(String input, String fileName) {
    String soyFile = "{namespace ns}" + input;
    IncrementingIdGenerator nodeIdGen = new IncrementingIdGenerator();
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forSuppliers(SoyFileSupplier.Factory.create(soyFile, SoyFileKind.SRC, fileName)).errorReporter(boom).parse().fileSet();
    TemplateRegistry registry = new TemplateRegistry(soyTree, boom);
    // We need to run HtmlRewritePass to produce HTML nodes. Otherwise we will not add any comments.
    new HtmlRewritePass(boom).run(soyTree.getChild(0), nodeIdGen);
    new AddHtmlCommentsForDebugPass().run(soyTree, registry);
    ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
    for (TemplateNode node : soyTree.getChild(0).getChildren()) {
        StringBuilder sb = new StringBuilder();
        node.appendSourceStringForChildren(sb);
        String templateName = (node instanceof TemplateDelegateNode) ? ((TemplateDelegateNode) node).getDelTemplateName() : node.getTemplateName();
        builder.put(templateName, sb.toString());
    }
    return builder.build();
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateDelegateNode(com.google.template.soy.soytree.TemplateDelegateNode) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

IncrementingIdGenerator (com.google.template.soy.base.internal.IncrementingIdGenerator)5 IdGenerator (com.google.template.soy.base.internal.IdGenerator)2 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)2 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)2 TemplateNode (com.google.template.soy.soytree.TemplateNode)2 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 SoyFileSupplier (com.google.template.soy.base.internal.SoyFileSupplier)1 CopyState (com.google.template.soy.basetree.CopyState)1 ErrorReporter (com.google.template.soy.error.ErrorReporter)1 FunctionNode (com.google.template.soy.exprtree.FunctionNode)1 VersionedFile (com.google.template.soy.shared.SoyAstCache.VersionedFile)1 PrintNode (com.google.template.soy.soytree.PrintNode)1 TemplateDelegateNode (com.google.template.soy.soytree.TemplateDelegateNode)1 Test (org.junit.Test)1