Package org.auraframework.impl.expression

Source Code of org.auraframework.impl.expression.ExpressionFactory

/*
* Copyright (C) 2013 salesforce.com, inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*         http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.auraframework.impl.expression;

import static org.auraframework.impl.expression.functions.BooleanFunctions.AND;
import static org.auraframework.impl.expression.functions.BooleanFunctions.NOT;
import static org.auraframework.impl.expression.functions.BooleanFunctions.OR;
import static org.auraframework.impl.expression.functions.BooleanFunctions.TERNARY;
import static org.auraframework.impl.expression.functions.MathFunctions.ABSOLUTE;
import static org.auraframework.impl.expression.functions.MathFunctions.DIVIDE;
import static org.auraframework.impl.expression.functions.MathFunctions.MODULUS;
import static org.auraframework.impl.expression.functions.MathFunctions.MULTIPLY;
import static org.auraframework.impl.expression.functions.MathFunctions.NEGATE;
import static org.auraframework.impl.expression.functions.MathFunctions.SUBTRACT;
import static org.auraframework.impl.expression.functions.MultiFunctions.ADD;
import static org.auraframework.impl.expression.functions.MultiFunctions.EQUALS;
import static org.auraframework.impl.expression.functions.MultiFunctions.NOTEQUALS;
import static org.auraframework.impl.expression.functions.MultiFunctions.GREATER_THAN;
import static org.auraframework.impl.expression.functions.MultiFunctions.GREATER_THAN_OR_EQUAL;
import static org.auraframework.impl.expression.functions.MultiFunctions.LESS_THAN;
import static org.auraframework.impl.expression.functions.MultiFunctions.LESS_THAN_OR_EQUAL;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.auraframework.expression.Expression;
import org.auraframework.expression.PropertyReference;
import org.auraframework.impl.expression.functions.Function;
import org.auraframework.system.Location;
import org.auraframework.throwable.AuraRuntimeException;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;

/**
* factory used by the parser to create the expression objects
*/
public class ExpressionFactory {

    private static final Map<String, Function> functionsByName;

    static {
        List<Function> l = new LinkedList<Function>();
        l.add(ADD);
        l.add(SUBTRACT);
        l.add(MULTIPLY);
        l.add(DIVIDE);
        l.add(MODULUS);
        l.add(GREATER_THAN);
        l.add(GREATER_THAN_OR_EQUAL);
        l.add(LESS_THAN);
        l.add(LESS_THAN_OR_EQUAL);
        l.add(AND);
        l.add(OR);
        l.add(NOT);
        l.add(NEGATE);
        l.add(ABSOLUTE);
        l.add(EQUALS);
        l.add(NOTEQUALS);
        l.add(TERNARY);
        Builder<String, Function> b = ImmutableMap.builder();
        for (Function f : l) {
            for (String k : f.getKeys()) {
                b.put(k, f);
            }
        }
        functionsByName = b.build();
    }

    private static Function lookup(String name) {
        return functionsByName.get(name);
    }

    private final Location l;

    // TODO: advance locations based on token positions

    public ExpressionFactory(Location l) {
        this.l = l;
    }

    public Expression createBool(String s) {
        return new LiteralImpl(Boolean.valueOf(s), l);
    }

    public Expression createNumber(String s) {
        return new LiteralImpl(Double.parseDouble(s), l);
    }

    public Expression createNull() {
        return new LiteralImpl(null, l);
    }

    public Expression createString(String s) {
        return new LiteralImpl(s.substring(1, s.length()-1), l);
    }

    public PropertyReference createPropertyReference(List<String> path) {
        return new PropertyReferenceImpl(path == null ? ImmutableList.<String> of() : ImmutableList.copyOf(path), l);
    }

    /**
     * for unary ops
     */
    public Expression createFunction(Function ft, Expression e1) {
        return new FunctionCallImpl(ft, ImmutableList.of(e1), l);
    }

    /**
     * for binary ops
     */
    public Expression createFunction(Function ft, Expression e1, Expression e2) {
        return new FunctionCallImpl(ft, ImmutableList.of(e1, e2), l);
    }

    /**
     * for ternary op
     */
    public Expression createTernaryFunction(Expression e1, Expression e2, Expression e3) {
        return new FunctionCallImpl(TERNARY, ImmutableList.of(e1, e2, e3), l);
    }

    /**
     * for calling a function by name()
     */
    public Expression createFunction(String name, List<Expression> args) {
        Function f = lookup(name);
        if (f == null) {
            // TODO: typed exception
            throw new AuraRuntimeException("No function found for key: " + name, l);
        }
        return new FunctionCallImpl(f, args == null ? ImmutableList.<Expression> of() : ImmutableList.copyOf(args), l);
    }

}
TOP

Related Classes of org.auraframework.impl.expression.ExpressionFactory

TOP
Copyright © 2018 www.massapi.com. 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.