Package com.google.template.soy.data

Examples of com.google.template.soy.data.SoyData


    return ImmutableSet.of(1);
  }


  @Override public SoyData compute(List<SoyData> args) {
    SoyData arg = args.get(0);

    if (arg instanceof IntegerData) {
      return arg;
    } else {
      return toSoyData((int) Math.ceil(arg.floatValue()));
    }
  }
View Full Code Here


    // ------ Build the call data. ------
    SoyMapData dataToPass;
    if (node.isPassingAllData()) {
      dataToPass = data;
    } else if (node.isPassingData()) {
      SoyData dataRefValue = eval(node.getDataExpr());
      if (!(dataRefValue instanceof SoyMapData)) {
        throw new RenderException(
            "In 'call' command " + node.toSourceString() +
            ", the data reference does not resolve to a SoyMapData.");
      }
      dataToPass = (SoyMapData) dataRefValue;
    } else {
      dataToPass = null;
    }

    SoyMapData callData;
    if (!node.isPassingData()) {
      // Case 1: Not passing data. Start with a fresh SoyMapData object.
      callData = new SoyMapData();
    } else if (node.numChildren() == 0) {
      // Case 2: No params. Just pass in the current data.
      callData = dataToPass;
    } else {
      // Case 3: Passing data and adding params. Need to augment the current data.
      callData = new AugmentedSoyMapData(dataToPass);
    }

    for (CallParamNode child : node.getChildren()) {

      if (child instanceof CallParamValueNode) {
        callData.putSingle(
            child.getKey(), eval(((CallParamValueNode) child).getValueExprUnion().getExpr()));

      } else if (child instanceof CallParamContentNode) {
        CallParamContentNode childCpcn = (CallParamContentNode) child;
        SoyData renderedBlock = renderBlock(childCpcn);

        // If the param node has a content kind attribute, it will have been autoescaped in the
        // corresponding context by the strict contextual autoescaper. Hence, the result of
        // evaluating the param block is wrapped in SanitizedContent of the specified kind.
        if (childCpcn.getContentKind() != null) {
          renderedBlock = UnsafeSanitizedContentOrdainer.ordainAsSafe(
              renderedBlock.stringValue(), childCpcn.getContentKind());
        }

        callData.putSingle(child.getKey(), renderedBlock);

      } else {
        throw new AssertionError();
      }
    }

    // ------ Render the callee template with the callData built above. ------

    if (node.getEscapingDirectiveNames().isEmpty()) {
      // No escaping at the call site -- render directly into the output buffer.
      RenderVisitor rv = createHelperInstance(currOutputBuf, callData);
      rv.exec(callee);
    } else {
      // Escaping the call site's result, such as at a strict template boundary.
      // TODO: Some optimization is needed here before Strict Soy can be widely used:
      // - Only create this temporary buffer when contexts mismatch. We could run a pre-pass that
      // eliminates escaping directives when all callers are known.
      // - Instead of creating a temporary buffer and copying, wrap with an escaping StringBuilder.
      StringBuilder calleeBuilder = new StringBuilder();
      RenderVisitor rv = createHelperInstance(calleeBuilder, callData);
      rv.exec(callee);
      SoyData resultData = (callee.getContentKind() != null) ?
          UnsafeSanitizedContentOrdainer.ordainAsSafe(
              calleeBuilder.toString(), callee.getContentKind()) :
          StringData.forValue(calleeBuilder.toString());
      for (String directiveName : node.getEscapingDirectiveNames()) {
        resultData = applyDirective(directiveName, resultData, ImmutableList.<SoyData>of(), node);
      }
      append(currOutputBuf, resultData.toString());
    }
  }
View Full Code Here

    return ImmutableSet.of(1, 2);
  }


  @Override public SoyData compute(List<SoyData> args) {
    SoyData value = args.get(0);
    int numDigitsAfterPt = (args.size() == 2) ? args.get(1).integerValue() : 0 /* default */;

    if (numDigitsAfterPt == 0) {
      if (value instanceof IntegerData) {
        return toSoyData(value.integerValue());
      } else {
        return toSoyData((int) Math.round(value.numberValue()));
      }
    } else if (numDigitsAfterPt > 0) {
      double valueDouble = value.numberValue();
      double shift = Math.pow(10, numDigitsAfterPt);
      return toSoyData(Math.round(valueDouble * shift) / shift);
    } else {
      double valueDouble = value.numberValue();
      double shift = Math.pow(10, -numDigitsAfterPt);
      return toSoyData((int) (Math.round(valueDouble / shift) * shift));
    }
  }
View Full Code Here

    return ImmutableSet.of(1);
  }


  @Override public SoyData compute(List<SoyData> args) {
    SoyData arg = args.get(0);

    if (!(arg instanceof SoyListData)) {
      throw new IllegalArgumentException("Argument to length() function is not SoyListData.");
    }
    return toSoyData(((SoyListData) arg).length());
View Full Code Here

    return ImmutableSet.of(2);
  }


  @Override public SoyData compute(List<SoyData> args) {
    SoyData arg0 = args.get(0);
    SoyData arg1 = args.get(1);

    Preconditions.checkArgument(arg0 instanceof SoyMapData,
        "First argument to augmentMap() function is not SoyMapData.");
    Preconditions.checkArgument(arg1 instanceof SoyMapData,
        "Second argument to augmentMap() function is not SoyMapData.");
View Full Code Here

    return ImmutableSet.of(2);
  }


  @Override public SoyData compute(List<SoyData> args) {
    SoyData arg0 = args.get(0);
    SoyData arg1 = args.get(1);

    Preconditions.checkArgument(arg0 instanceof StringData, String.format(
        "First argument to strContains() function is not StringData: %s",
        arg0.stringValue()));

    Preconditions.checkArgument(arg1 instanceof StringData, String.format(
        "Second argument to strContains() function is not StringData: %s",
        arg1.stringValue()));

    String strArg0 = arg0.stringValue();
    String strArg1 = arg1.stringValue();

    return toSoyData(strArg0.contains(strArg1));
  }
View Full Code Here

    return ImmutableSet.of(1);
  }


  @Override public SoyData compute(List<SoyData> args) {
    SoyData arg = args.get(0);

    return toSoyData((int) Math.floor(Math.random() * arg.integerValue()));
  }
View Full Code Here

    return ImmutableSet.of(2);
  }


  @Override public SoyData compute(List<SoyData> args) {
    SoyData arg0 = args.get(0);
    SoyData arg1 = args.get(1);

    if (arg0 instanceof IntegerData && arg1 instanceof IntegerData) {
      return toSoyData(Math.max(arg0.integerValue(), arg1.integerValue()));
    } else {
      return toSoyData(Math.max(arg0.numberValue(), arg1.numberValue()));
    }
  }
View Full Code Here

    return ImmutableSet.of(1);
  }


  @Override public SoyData compute(List<SoyData> args) {
    SoyData arg = args.get(0);

    if (! (arg instanceof SoyMapData)) {
      throw new IllegalArgumentException("Argument to keys() function is not SoyMapData.");
    }
    return new SoyListData(((SoyMapData) arg).getKeys());
View Full Code Here

    int numKeys = keys.size();

    CollectionData collectionData = this;
    for (int i = 0; i <= numKeys - 2; ++i) {

      SoyData nextSoyData = collectionData.getSingle(keys.get(i));
      if (nextSoyData != null && !(nextSoyData instanceof CollectionData)) {
        throw new SoyDataException(
            "Failed to evaluate key string \"" + keyStr + "\" for put().");
      }
      CollectionData nextCollectionData = (CollectionData) nextSoyData;
View Full Code Here

TOP

Related Classes of com.google.template.soy.data.SoyData

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.