com.akrema.stringeval.StringEvaluator.java Source code

Java tutorial

Introduction

Here is the source code for com.akrema.stringeval.StringEvaluator.java

Source

/*
 *  ===============================================================
 *  * Copyright  2014   <http://Akrema.darvour-tut.com>
 *  ==============================================================
 *  ***************This file is part of StringEval*******************
 *
 *  *StringEval is free software: you can redistribute it and/or modify
 *  *it under the terms of the GNU General Public License as published by
 *  *the Free Software Foundation, either version 3 of the License, or
 *  *(at your option) any later version.
 *
 *  *StringEval is distributed in the hope that it will be useful,
 *  *but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  *GNU General Public License for more details.
 *
 *  *You should have received a copy of the GNU General Public License
 *  *along with StringEval.  If not, see <http://www.gnu.org/licenses/>.
 *  ================================================================
 *  ==Abdelhak Akermi
 *  ==Tunisia
 *  ==Mednine-RTE Gabes
 *  ==Contact : Akrema@darvour-tut.com/abdelhak.akermi@gmail.com
 *  ==================================================================
 */

package com.akrema.stringeval;

import com.akrema.stringeval.exception.InputStringException;
import com.akrema.stringeval.exception.InvalidTypeArgumentException;
import com.akrema.stringeval.exception.ParseExpressionException;
import com.akrema.stringeval.utils.ExprUtils;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;

/**
 * this class evaluate string expressions (methods)
 *
 * @author A.Akermi
 * @version 1.1
 */
public class StringEvaluator extends AbstractEvaluator {

    /**
     * Default Constructor
     */
    public StringEvaluator() {
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void ini() {
        super.ini();
        listOfArgument = new ArrayList<Argument>();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void WalkThroughtExpression(String expression) throws ParseExpressionException, InputStringException {
        this.fullExpression = StringUtils.remove(expression, ' ');
        System.out.println(this.fullExpression);
        int indexleftPar = 0;
        int indexrightPar = 0;
        int length = this.fullExpression.length();
        int index = 0;
        //loop throught expression
        while (index < length) {
            //define parameters index in expression
            if (this.fullExpression.charAt(index) == StringCONST.LEFT_PARENTHESE)
                indexleftPar = index;
            if (this.fullExpression.charAt(index) == StringCONST.RIGHT_PARENTHESE)
                indexrightPar = index;
            index++;
        }

        //extract method ,list of arguments and store them in listOfArgument
        this.currentMethod = this.getMMethodByName(this.fullExpression.substring(0, indexleftPar));
        this.listOfArgument = this.getArguments(this.fullExpression, indexleftPar, this.fullExpression.length());
    }

    public static void main(String[] args)
            throws InputStringException, ParseExpressionException, InvalidTypeArgumentException {
        StringEvaluator s = new StringEvaluator();
        System.out.println(s.evaluate("charAt('abdelhak',5)"));

    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void WalkThroughtExpression(String expression) throws ParseExpressionException, InputStringException {
        this.fullExpression = StringUtils.remove(expression, ' ');
        System.out.println(this.fullExpression);
        int indexleftPar = 0;
        int indexrightPar = 0;
        int length = this.fullExpression.length();
        int index = 0;
        //loop throught expression
        while (index < length) {
            //define parameters index in expression
            if (this.fullExpression.charAt(index) == StringCONST.LEFT_PARENTHESE)
                indexleftPar = index;
            if (this.fullExpression.charAt(index) == StringCONST.RIGHT_PARENTHESE)
                indexrightPar = index;
            index++;
        }

        //extract method ,list of arguments and store them in listOfArgument
        this.currentMethod = this.getMMethodByName(this.fullExpression.substring(0, indexleftPar));
        this.listOfArgument = this.getArguments(this.fullExpression, indexleftPar, this.fullExpression.length());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ArrayList<Argument> getArguments(String expr, int beginindex, int endindex)
            throws ParseExpressionException, InputStringException {
        ArrayList<Argument> args = new ArrayList<Argument>();
        this.argString = expr.substring(beginindex, endindex);
        String argsource;
        this.argString = StringUtils.remove(this.argString, StringCONST.LEFT_PARENTHESE);
        this.argString = StringUtils.remove(this.argString, StringCONST.RIGHT_PARENTHESE);
        String[] tocken = this.argString.split(StringCONST.DELM);
        this.argString = this.argString.trim();
        if (tocken.length > 3) {
            throw new InputStringException("missing arguments ,the number of argument must be between" + "1 and 3");
        }

        if (!tocken[0].startsWith(String.valueOf(StringCONST.SINGLE_QUOTE))) {
            throw new ParseExpressionException(" invalid String Source ");
        }
        tocken[0] = StringUtils.remove(tocken[0], StringCONST.SINGLE_QUOTE);
        argsource = tocken[0].trim();
        args.add(new Argument(0, StringCONST.TYPE_STRING, argsource));
        int tokenindex = 1;
        while (tokenindex < tocken.length) {
            String arg = tocken[tokenindex];
            arg = arg.trim();
            if (arg.startsWith("\'"))
                arg = StringUtils.remove(arg, StringCONST.SINGLE_QUOTE);
            arg = arg.trim();
            switch (ExprUtils.getTypeArgs(arg)) {
            case 0:
                args.add(new Argument(tokenindex, StringCONST.TYPE_CHAR, arg));
                break;
            case 1:
                args.add(new Argument(tokenindex, StringCONST.TYPE_INTEGER, arg));
                break;
            case 2:
                args.add(new Argument(tokenindex, StringCONST.TYPE_STRING, arg));
                break;
            default:
                break;
            }
            tokenindex++;
        }
        return args;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String evaluate(String expression)
            throws InputStringException, ParseExpressionException, InvalidTypeArgumentException {
        if (expression.length() == 0) {
            throw new InputStringException(" the string expression must not be empty");
        }
        this.WalkThroughtExpression(expression);

        return this.currentMethod.process(this, this.listOfArgument).getResultat().toString();

    }

    /**
     * the name of the current method evaluating
     **/
    private String namedMethod;

    public MMethod getCurrentMethod() {
        return currentMethod;
    }

    /**
     * the methode to be processed
     **/
    private MMethod currentMethod;

    public ArrayList<Argument> getListOfArgument() {
        return listOfArgument;
    }

    /**
     * list of argument passed in the expression
     **/
    private ArrayList<Argument> listOfArgument;
}