Search in sources :

Example 6 with StringData

use of com.google.template.soy.data.restricted.StringData in project closure-templates by google.

the class StrSubFunction method computeForJava.

@Override
public SoyValue computeForJava(List<SoyValue> args) {
    SoyValue arg0 = args.get(0);
    SoyValue arg1 = args.get(1);
    SoyValue arg2 = args.size() == 3 ? args.get(2) : null;
    Preconditions.checkArgument(arg0 instanceof StringData || arg0 instanceof SanitizedContent, "First argument to strSub() function is not StringData or SanitizedContent: %s", arg0);
    Preconditions.checkArgument(arg1 instanceof IntegerData, "Second argument to strSub() function is not IntegerData: %s", arg1);
    if (arg2 != null) {
        Preconditions.checkArgument(arg2 instanceof IntegerData, "Third argument to strSub() function is not IntegerData: %s", arg2);
    }
    String strArg0 = arg0.coerceToString();
    int intArg1 = arg1.integerValue();
    if (arg2 != null) {
        return StringData.forValue(strArg0.substring(intArg1, arg2.integerValue()));
    } else {
        return StringData.forValue(strArg0.substring(intArg1));
    }
}
Also used : SanitizedContent(com.google.template.soy.data.SanitizedContent) IntegerData(com.google.template.soy.data.restricted.IntegerData) StringData(com.google.template.soy.data.restricted.StringData) SoyValue(com.google.template.soy.data.SoyValue)

Example 7 with StringData

use of com.google.template.soy.data.restricted.StringData in project closure-templates by google.

the class StrContainsFunction method computeForJava.

@Override
public SoyValue computeForJava(List<SoyValue> args) {
    SoyValue arg0 = args.get(0);
    SoyValue arg1 = args.get(1);
    Preconditions.checkArgument(arg0 instanceof StringData || arg0 instanceof SanitizedContent, "First argument to strContains() function is not StringData or SanitizedContent: %s", arg0);
    String strArg0 = arg0.coerceToString();
    String strArg1 = arg1.coerceToString();
    return BooleanData.forValue(strArg0.contains(strArg1));
}
Also used : SanitizedContent(com.google.template.soy.data.SanitizedContent) StringData(com.google.template.soy.data.restricted.StringData) SoyValue(com.google.template.soy.data.SoyValue)

Aggregations

StringData (com.google.template.soy.data.restricted.StringData)7 SoyValue (com.google.template.soy.data.SoyValue)5 Test (org.junit.Test)4 SanitizedContent (com.google.template.soy.data.SanitizedContent)2 SoyValueProvider (com.google.template.soy.data.SoyValueProvider)2 FloatData (com.google.template.soy.data.restricted.FloatData)2 HashMap (java.util.HashMap)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 SoyEasyList (com.google.template.soy.data.SoyEasyList)1 SoyList (com.google.template.soy.data.SoyList)1 IntegerData (com.google.template.soy.data.restricted.IntegerData)1 AbstractParentExprNode (com.google.template.soy.exprtree.AbstractParentExprNode)1 ExprNode (com.google.template.soy.exprtree.ExprNode)1 LinkedHashMap (java.util.LinkedHashMap)1