Expression.java :  » GWT » itemscript » org » itemscript » template » expression » Java Open Source

Java Open Source » GWT » itemscript 
itemscript » org » itemscript » template » expression » Expression.java

package org.itemscript.template.expression;

import java.util.List;

import org.itemscript.core.HasSystem;
import org.itemscript.core.JsonSystem;
import org.itemscript.core.values.JsonValue;
import org.itemscript.template.TemplateExec;
import org.itemscript.template.text.TextElement;

public class Expression implements HasSystem, TextElement {
    private final List<Function> contents;
    private final JsonSystem system;

    public Expression(JsonSystem system, List<Function> contents) {
        this.system = system;
        this.contents = contents;
    }

    //@Override
    public Expression asExpression() {
        return this;
    }

    //@Override
    public JsonValue interpret(TemplateExec templateExec, JsonValue context) {
        // "output" to be fed to the first function in an expression is always a JsonNull.
        JsonValue output = system().createNull();
        for (int i = 0; i < contents.size(); ++i) {
            // Set the input to the output of the previous function.
            JsonValue input = output;
            // Execute this function.
            output = contents.get(i)
                    .execute(templateExec, context, input);
        }
        return output;
    }

    //@Override
    public boolean isExpression() {
        return true;
    }

    public JsonSystem system() {
        return system;
    }

    //@Override
    public String toString() {
        return "[Expression contents=" + contents + "]";
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.