Android Open Source - Android-Data-Binding Expression Evaluator






From Project

Back to project page Android-Data-Binding.

License

The source code is released under:

MIT License

If you think the Android project Android-Data-Binding listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.furkanbayraktar.databinding.common.parser;
//from w ww.  ja  v a2s.  co  m
import com.furkanbayraktar.databinding.common.external.Expression;
import com.furkanbayraktar.databinding.common.resolver.PathResolver;
import com.furkanbayraktar.databinding.exception.ResolverException;
import com.furkanbayraktar.databinding.logging.Logger;
import com.furkanbayraktar.databinding.model.BasePOJO;

import java.math.BigDecimal;

/**
 * Created by Furkan Bayraktar.
 * Created at 8/18/14.
 * Copyright Valensas A.S.
 */
public class ExpressionEvaluator{

    private static ExpressionEvaluator instance;

    private ExpressionEvaluator(){
        Logger.info("ExpressionEvaluator object created.");
    }

    public static ExpressionEvaluator getInstance(){
        if(instance == null){
            instance = new ExpressionEvaluator();
        }
        return instance;
    }

    public BigDecimal evaluate(String tag, BasePOJO object){

        try {
            Logger.info("Expression evaluation started...");

            int start;
            int end;

            do {
                start = tag.indexOf("(");
                if (start != -1) {
                    end = tag.indexOf(")", start);
                } else {
                    end = -1;
                }

                if (end != -1) {
                    String current = tag.substring(start + 1, end);

                    if (current.contains("!=")) {
                        tag = tag.replace(current, findToBeReplaced(current, current.indexOf("!"), current.indexOf("=") + 1, object));

                        start = tag.indexOf("(", end);
                        if (start != -1) {
                            end = tag.indexOf(")", start);
                        } else {
                            end = -1;
                        }
                    } else if (current.contains("==")) {
                        tag = tag.replace(current, findToBeReplaced(current, current.indexOf("="), current.indexOf("=") + 2, object));

                        start = tag.indexOf("(", end);
                        if (start != -1) {
                            end = tag.indexOf(")", start);
                        } else {
                            end = -1;
                        }
                    } else if (current.contains("<=")) {
                        tag = tag.replace(current, findToBeReplaced(current, current.indexOf("<"), current.indexOf("=") + 1, object));

                        start = tag.indexOf("(", end);
                        if (start != -1) {
                            end = tag.indexOf(")", start);
                        } else {
                            end = -1;
                        }
                    } else if (current.contains(">=")) {
                        tag = tag.replace(current, findToBeReplaced(current, current.indexOf(">"), current.indexOf("=") + 1, object));

                        start = tag.indexOf("(", end);
                        if (start != -1) {
                            end = tag.indexOf(")", start);
                        } else {
                            end = -1;
                        }
                    } else if (current.contains("<")) {
                        tag = tag.replace(current, findToBeReplaced(current, current.indexOf("<"), current.indexOf("<") + 1, object));

                        start = tag.indexOf("(", end);
                        if (start != -1) {
                            end = tag.indexOf(")", start);
                        } else {
                            end = -1;
                        }
                    } else if (current.contains(">")) {
                        tag = tag.replace(current, findToBeReplaced(current, current.indexOf(">"), current.indexOf(">") + 1, object));

                        start = tag.indexOf("(", end);
                        if (start != -1) {
                            end = tag.indexOf(")", start);
                        } else {
                            end = -1;
                        }
                    } else if (current.contains("&&")) {
                        tag = tag.replace(current, findToBeReplaced(current, current.indexOf("&"), current.indexOf("&") + 2, object));

                        start = tag.indexOf("(", end);
                        if (start != -1) {
                            end = tag.indexOf(")", start);
                        } else {
                            end = -1;
                        }
                    } else if (current.contains("||")) {
                        tag = tag.replace(current, findToBeReplaced(current, current.indexOf("|"), current.indexOf("|") + 2, object));

                        start = tag.indexOf("(", end);
                        if (start != -1) {
                            end = tag.indexOf(")", start);
                        } else {
                            end = -1;
                        }
                    } else {
                        Object value = PathResolver.getInstance().resolve(current, object);

                        if (value != null) {
                            String toReplace = current.replace(current, String.valueOf(value));
                            tag = tag.replace(current, toReplace);
                        }

                        start = tag.indexOf("(", end);
                        if (start != -1) {
                            end = tag.indexOf(")", start);
                        } else {
                            end = -1;
                        }
                    }
                }
            } while (end != -1);

            BigDecimal result = null;

            tag = tag.replace("true", "TRUE");
            tag = tag.replace("false", "FALSE");

            try {
                Expression expression = new Expression(tag);
                result = expression.eval();
            } catch (Exception e) {
                ResolverException ex = new ResolverException("Cannot evaluate expression.", e);
                Logger.error("Cannot evaluate expression.", ex);
            }

            Logger.info("Expression evaluation finished.");
            return result;
        } catch (Exception ex){
            Logger.error("Unexpected error.", ex);
        }
        return null;
    }

        private String findToBeReplaced(String current, int firstIndex, int secondIndex, BasePOJO object){

        try {
            String key1;
            Object value1;

            key1 = current.substring(0, firstIndex).trim();

            Logger.info("Searching value for key: " + key1);

            value1 = PathResolver.getInstance().resolve(key1, object);

            String key2;
            Object value2;

            key2 = current.substring(secondIndex).trim();

            Logger.info("Searching value for key: " + key2);

            value2 = PathResolver.getInstance().resolve(key2, object);

            if (value1 != null) {
                Logger.info("Value found for key: " + key1);

                current = current.replace(key1, String.valueOf(value1));
            }else{
                Logger.info("Value not found for key: " + key1);
            }

            if (value2 != null) {
                Logger.info("Value found for key: " + key2);

                current = current.replace(key2, String.valueOf(value2));
            }else{
                Logger.info("Value not found for key: " + key2);
            }
        } catch (Exception ex){
            
        }

        return current;
    }
}




Java Source Code List

com.furkanbayraktar.databinding.ViewBinder.java
com.furkanbayraktar.databinding.annotations.BindedFunction.java
com.furkanbayraktar.databinding.common.adapter.BoundListAdapter.java
com.furkanbayraktar.databinding.common.external.Expression.java
com.furkanbayraktar.databinding.common.parser.ExpressionEvaluator.java
com.furkanbayraktar.databinding.common.parser.Parser.java
com.furkanbayraktar.databinding.common.resolver.AssignmentResolver.java
com.furkanbayraktar.databinding.common.resolver.FunctionResolver.java
com.furkanbayraktar.databinding.common.resolver.PathResolver.java
com.furkanbayraktar.databinding.common.resolver.ViewResolver.java
com.furkanbayraktar.databinding.common.resolver.VisibilityResolver.java
com.furkanbayraktar.databinding.exception.FunctionException.java
com.furkanbayraktar.databinding.exception.POJOParseException.java
com.furkanbayraktar.databinding.exception.ResolverException.java
com.furkanbayraktar.databinding.exception.UnsupportedObjectException.java
com.furkanbayraktar.databinding.exception.UnsupportedSyntaxError.java
com.furkanbayraktar.databinding.exception.UnsupportedViewException.java
com.furkanbayraktar.databinding.logging.Logger.java
com.furkanbayraktar.databinding.model.BasePOJO.java
com.furkanbayraktar.databindingprivate.app.ListActivity.java
com.furkanbayraktar.databindingprivate.app.MainActivity.java
com.furkanbayraktar.databindingprivate.app.model.news.Content.java
com.furkanbayraktar.databindingprivate.app.model.news.Entry.java
com.furkanbayraktar.databindingprivate.app.model.news.Feed.java
com.furkanbayraktar.databindingprivate.app.model.news.MediaGroup.java
com.furkanbayraktar.databindingprivate.app.model.news.ResponseData.java
com.furkanbayraktar.databindingprivate.app.model.news.ResponseObject.java
com.furkanbayraktar.databindingprivate.app.model.sample.SampleObject.java
com.furkanbayraktar.databindingprivate.app.network.GsonRequest.java