Package com.floreysoft.jmte.token

Examples of com.floreysoft.jmte.token.ForEachToken


  // ${if item}${item2.property1}INNER_SUFFIX${end}${end}\n${end}
  protected String transformCompiled(TemplateContext context) {
    StringBuilder buffer = new StringBuilder();

    // ${foreach list item}
    ForEachToken token1 = new ForEachToken("list", "item", "");
    token1.setIterable((Iterable) token1.evaluate(context));
    context.model.enterScope();
    context.push(token1);
    try {
      while (token1.iterator().hasNext()) {
        context.model.put(token1.getVarName(), token1.advance());
        addSpecialVariables(token1, context.model);

        // ${foreach item.list item2}
        ForEachToken token2 = new ForEachToken(Arrays
            .asList(new String[] { "item", "list" }), "item.list",
            "item2", "");
        token2.setIterable((Iterable) token2.evaluate(context));
        context.model.enterScope();
        context.push(token2);
        try {
          while (token2.iterator().hasNext()) {
            context.model
                .put(token2.getVarName(), token2.advance());
            addSpecialVariables(token2, context.model);

            // OUTER_PRFIX
            buffer.append("OUTER_PRFIX");

            // ${if item}
            IfToken token3 = new IfToken("item", false);
            context.push(token3);
            try {
              if ((Boolean) token3.evaluate(context)) {

                // ${item2.property1}
                buffer.append(new StringToken(Arrays
                    .asList(new String[] { "item2",
                        "property1" }),
                    "item.property1").evaluate(context));

                // INNER_SUFFIX
                buffer.append("INNER_SUFFIX");

              }
            } finally {
              context.pop();
            }

            if (!token2.isLast()) {
              buffer.append(token2.getSeparator());
            }
          }
        } finally {
          context.model.exitScope();
          context.pop();
View Full Code Here


  @Override
  @SuppressWarnings("unchecked")
  protected String transformCompiled(TemplateContext context) {
    StringBuilder buffer = new StringBuilder();

    ForEachToken token1 = new ForEachToken(Arrays
        .asList(new String[] { "list" }), "list", "item", "\n");
    token1.setIterable((Iterable) token1.evaluate(context));

    context.model.enterScope();
    context.push(token1);
    try {
      while (token1.iterator().hasNext()) {
        context.model.put(token1.getVarName(), token1.advance());
        addSpecialVariables(token1, context.model);

        buffer.append(new StringToken(Arrays.asList(new String[] {
            "item", "property1" }), "item.property1")
            .evaluate(context));

        if (!token1.isLast()) {
          buffer.append(token1.getSeparator());
        }
      }
    } finally {
      context.model.exitScope();
      context.pop();
View Full Code Here

  public void processor() throws Exception {
    Processor<Boolean> oddExpression = new Processor<Boolean>() {

      @Override
      public Boolean eval(TemplateContext context) {
        ForEachToken foreach = context.peek(ForEachToken.class);
        if (foreach != null) {
          return foreach.getIndex() % 2 == 1;
        }
        return false;
      }
    };
    Map<String, Object> model = new HashMap<String, Object>();
View Full Code Here

    return output.toString();
  }

  @SuppressWarnings({ "unchecked", "rawtypes" })
  private void foreach(boolean inheritedSkip) {
    ForEachToken feToken = (ForEachToken) tokenStream.currentToken();
    Iterable iterable = (Iterable) feToken.evaluate(context);
    // begin a fresh iteration with a reset index
    feToken.setIterator(iterable.iterator());
    feToken.resetIndex();
    tokenStream.consume();

    context.model.enterScope();
    context.push(feToken);
    try {

      // in case we do not want to evaluate the body, we just do a quick
      // scan until the matching end
      if (inheritedSkip || !feToken.iterator().hasNext()) {
        Token contentToken;
        while ((contentToken = tokenStream.currentToken()) != null
            && !(contentToken instanceof EndToken)) {
          content(true);
        }
        if (contentToken == null) {
          engine.getErrorHandler().error("missing-end", feToken);
        } else {
          tokenStream.consume();
          context.notifyProcessListener(contentToken, Action.END);
        }
      } else {

        while (feToken.iterator().hasNext()) {

          context.model.put(feToken.getVarName(), feToken.advance());
          addSpecialVariables(feToken, context.model);

          // for each iteration we need to rewind to the beginning
          // of the for loop
          tokenStream.rewind(feToken);
          Token contentToken;
          while ((contentToken = tokenStream.currentToken()) != null
              && !(contentToken instanceof EndToken)) {
            content(false);
          }
          if (contentToken == null) {
            engine.getErrorHandler().error("missing-end", feToken);
          } else {
            tokenStream.consume();
            context.notifyProcessListener(contentToken, Action.END);
          }
          if (!feToken.isLast()) {
            output.append(feToken.getSeparator());
          }
        }
      }

    } finally {
View Full Code Here

      usedVariables.add(variableName);
    }
  }

  private void foreach() {
    ForEachToken feToken = (ForEachToken) tokenStream.currentToken();
    tokenStream.consume();

    localVarStack.add(0, feToken.getVarName());

    Label loopStart = new Label();
    Label loopEnd = new Label();
    Label tryEndLabel = new Label();

    codeGenerateForeachBlockStart(feToken, loopStart, loopEnd, tryEndLabel);

    addUsedVariableIfNotLocal(feToken.getExpression());
    Token contentToken;
    while ((contentToken = tokenStream.currentToken()) != null
        && !(contentToken instanceof EndToken)) {
      content();
    }
View Full Code Here

TOP

Related Classes of com.floreysoft.jmte.token.ForEachToken

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.