Package com.google.template.soy.soytree

Examples of com.google.template.soy.soytree.TemplateNode


   
    SoyFileSetNode soyTree = SharedTestUtils.parseSoyFiles(source);
    new CheckEscapingSanityVisitor().exec(soyTree);
    new ContextualAutoescaper(SOY_PRINT_DIRECTIVES).rewrite(
        soyTree, false /* assumeNoExternalCalls */);
    TemplateNode mainTemplate = soyTree.getChild(0).getChild(0);
    assertEquals("Sanity check", "ns.main", mainTemplate.getTemplateName());
    final List<CallNode> callNodes = SharedTestUtils.getAllNodesOfType(
        mainTemplate, CallNode.class);
    assertEquals(4, callNodes.size());
    assertEquals("HTML->HTML escaping should be pruned",
        ImmutableList.of(), callNodes.get(0).getEscapingDirectiveNames());
View Full Code Here


   
    SoyFileSetNode soyTree = SharedTestUtils.parseSoyFiles(source);
    new CheckEscapingSanityVisitor().exec(soyTree);
    new ContextualAutoescaper(SOY_PRINT_DIRECTIVES).rewrite(
        soyTree, false /* assumeNoExternalCalls */);
    TemplateNode mainTemplate = soyTree.getChild(0).getChild(0);
    assertEquals("Sanity check", "ns.main", mainTemplate.getTemplateName());
    final List<CallNode> callNodes = SharedTestUtils.getAllNodesOfType(
        mainTemplate, CallNode.class);
    assertEquals(1, callNodes.size());
    assertEquals("Escaping should be there since there might be extern delegates",
        ImmutableList.of("|escapeHtml"), callNodes.get(0).getEscapingDirectiveNames());
View Full Code Here

   
    SoyFileSetNode soyTree = SharedTestUtils.parseSoyFiles(source);
    new CheckEscapingSanityVisitor().exec(soyTree);
    new ContextualAutoescaper(SOY_PRINT_DIRECTIVES).rewrite(
        soyTree, true /* assumeNoExternalCalls */);
    TemplateNode mainTemplate = soyTree.getChild(0).getChild(0);
    assertEquals("Sanity check", "ns.main", mainTemplate.getTemplateName());
    final List<CallNode> callNodes = SharedTestUtils.getAllNodesOfType(
        mainTemplate, CallNode.class);
    assertEquals(2, callNodes.size());
    assertEquals("We're compiling a complete set; we can optimize based on usages.",
        ImmutableList.of(), callNodes.get(0).getEscapingDirectiveNames());
View Full Code Here

   * thread-safe manner, then please use {@link #execForAllTemplates}() in a thread-safe manner.
   */
  @Override public IjParamsInfo exec(SoyNode node) {

    Preconditions.checkArgument(node instanceof TemplateNode);
    TemplateNode nodeAsTemplate = (TemplateNode) node;

    // Build templateRegistry and initialize templateToFinishedInfoMap if necessary.
    if (templateRegistry == null) {
      SoyFileSetNode soyTree = nodeAsTemplate.getParent().getParent();
      templateRegistry = new TemplateRegistry(soyTree);
    }
    if (templateToFinishedInfoMap == null) {
      templateToFinishedInfoMap = Maps.newHashMap();
    }
View Full Code Here

    // If all the data keys being passed are listed using 'param' commands, then check that all
    // required params of the callee are included.
    if (! node.isPassingData()) {

      // Get the callee node (basic or delegate).
      TemplateNode callee;
      if (node instanceof CallBasicNode) {
        callee = templateRegistry.getBasicTemplate(((CallBasicNode) node).getCalleeName());
      } else {
        Set<DelegateTemplateDivision> divisions =
            templateRegistry.getDelTemplateDivisionsForAllVariants(
                ((CallDelegateNode) node).getDelCalleeName());
        if (divisions != null) {
          callee = Iterables.get(
              Iterables.getFirst(divisions, null).delPackageNameToDelTemplateMap.values(), 0);
        } else {
          callee = null;
        }
      }

      // Do the check if the callee node has SoyDoc.
      if (callee != null && callee.getSoyDocParams() != null) {
        // Get param keys passed by caller.
        Set<String> callerParamKeys = Sets.newHashSet();
        for (CallParamNode callerParam : node.getChildren()) {
          callerParamKeys.add(callerParam.getKey());
        }
        // Check param keys required by callee.
        List<String> missingParamKeys = Lists.newArrayListWithCapacity(2);
        for (SoyDocParam calleeParam : callee.getSoyDocParams()) {
          if (calleeParam.isRequired && ! callerParamKeys.contains(calleeParam.key)) {
            missingParamKeys.add(calleeParam.key);
          }
        }
        // Report errors.
        if (missingParamKeys.size() > 0) {
          String errorMsgEnd = (missingParamKeys.size() == 1) ?
              "param '" + missingParamKeys.get(0) + "'" : "params " + missingParamKeys;
          throw SoySyntaxExceptionUtils.createWithNode(
              String.format(
                  "Call to '%s' is missing required %s.",
                  callee.getTemplateNameForUserMsgs(), errorMsgEnd),
              node);
        }
      }
    }
  }
View Full Code Here

    return getUsedIjParamsForTemplate(templateInfo.getName());
  }


  @Override public ImmutableSortedSet<String> getUsedIjParamsForTemplate(String templateName) {
    TemplateNode template = templateRegistryForNoCaching.getBasicTemplate(templateName);
    if (template == null) {
      throw new SoyTofuException("Template '" + templateName + "' not found.");
    }
    IjParamsInfo ijParamsInfo = templateToIjParamsInfoMap.get(template);
    // TODO: Ideally we'd check that there are no external calls, but we find that in practice many
View Full Code Here

  private void renderMainHelper(
      TemplateRegistry templateRegistry, Appendable outputBuf, String templateName,
      @Nullable SoyMapData data, @Nullable SoyMapData ijData, Set<String> activeDelPackageNames,
      @Nullable SoyMsgBundle msgBundle, @Nullable SoyCssRenamingMap cssRenamingMap) {

    TemplateNode template = templateRegistry.getBasicTemplate(templateName);
    if (template == null) {
      throw new SoyTofuException("Attempting to render undefined template '" + templateName + "'.");
    }

    if (data == null) {
View Full Code Here

      int cloneId = tn.getNearestAncestor(SoyFileSetNode.class).getNodeIdGenerator().genId();

      // We need to use the unnamespaced name in the command text since we'll be inserting this
      // template into a file node that already has a namespace declaration.
      TemplateNode clone;
      boolean useAttrStyleForName = tn.getCommandText().contains("name=");
      if (tn instanceof TemplateBasicNode) {
        TemplateBasicNode tbn = (TemplateBasicNode) tn;

        String derivedPartialName = (tn.getPartialTemplateName() != null) ?
            derivedName.substring(soyFileHeaderInfo.namespace.length()) : null;

        clone = new TemplateBasicNode(
            cloneId, soyFileHeaderInfo, derivedName, derivedPartialName,
            useAttrStyleForName, tbn.isOverride(), tn.isPrivate(),
            tn.getAutoescapeMode(), tn.getContentKind(), tn.getSoyDoc(), tn.getSyntaxVersion());

        if (! (derivedName.equals(clone.getTemplateName()) &&
            Objects.equal(derivedPartialName, clone.getPartialTemplateName()))) {
          throw new AssertionError();
        }
      } else if (tn instanceof TemplateDelegateNode) {
        TemplateDelegateNode tdn = (TemplateDelegateNode) tn;
        clone = new TemplateDelegateNode(
            cloneId, soyFileHeaderInfo, derivedName, tdn.getDelTemplateVariant(),
            tdn.getDelPriority(), tn.getAutoescapeMode(), tn.getContentKind(), tn.getSoyDoc());

        if (! (derivedName.equals(((TemplateDelegateNode) clone).getDelTemplateName()))) {
          throw new AssertionError();
        }
      } else {
        throw new AssertionError("Unknown template node type: " + tn.getClass());
      }

      clone.setSourceLocation(tn.getSourceLocation());

      for (StandaloneNode child : tn.getChildren()) {
        clone.addChild(SoytreeUtils.cloneWithNewIds(child));
      }

      b.add(clone);
    }
View Full Code Here

    String templateName = node.getTemplateName();
    // Template name should be full name (not start with '.').
    Preconditions.checkArgument(templateName.charAt(0) != '.');

    if (basicTemplatesMap.containsKey(templateName)) {
      TemplateNode prevTemplate = basicTemplatesMap.get(templateName);
      // If this duplicate definition is not an explicit Soy V1 override, report error.
      if (!node.isOverride()) {
        SoyFileNode prevTemplateFile = prevTemplate.getNearestAncestor(SoyFileNode.class);
        SoyFileNode currTemplateFile = node.getNearestAncestor(SoyFileNode.class);
        if (currTemplateFile == prevTemplateFile) {
          throw SoySyntaxExceptionUtils.createWithNode(
              "Found two definitions for template name '" + templateName + "', both in the file " +
                  currTemplateFile.getFilePath() + ".",
View Full Code Here

  @Override protected void visitSoyNode(SoyNode node) {

    if (node.getSyntaxVersion() == SyntaxVersion.V1) {

      if (node instanceof TemplateNode) {
        TemplateNode templateNodeCast = (TemplateNode) node;

        // Specific error message for missing SoyDoc.
        if (templateNodeCast.getSoyDocParams() == null) {
          throw SoySyntaxExceptionUtils.createWithNode(
              "Missing SoyDoc for template: " + templateNodeCast.getTagString() +
                  " (required in Soy V2)",
              node);
        }

        // Specific error message for incorrect param syntax.
        if (templateNodeCast.getParamSrcsWithIncorrectSyntax().size() > 0) {
          throw SoySyntaxExceptionUtils.createWithNode(
              "Template " +
                  templateNodeCast.getTagString() + " has params with invalid Soy V2 syntax " +
                  templateNodeCast.getParamSrcsWithIncorrectSyntax() + ").",
              node);
        }

        // Checks for fully-qualified template names. (In V2, they need to be namespace-relative).
        if (templateNodeCast instanceof TemplateBasicNode &&
            (templateNodeCast.getPartialTemplateName() == null)) {
          throw SoySyntaxExceptionUtils.createWithNode(
              "Template names must be namespace-relative, i.e. have a leading dot: " +
                  templateNodeCast.getTagString() + " (required in Soy V2)",
              node);
        }
      }

      // General error message.
View Full Code Here

TOP

Related Classes of com.google.template.soy.soytree.TemplateNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.