Java-解析用户定义的函数

时间:2019-02-14 14:48:43

标签: java function parsing defined

我正在开发一个程序,希望用户定义一个简单的函数,例如     randomInt(0,10) 要么     randomString(10) 而不是静态参数。解析和处理此类功能的最佳方法是什么?

我还没有发现任何此类问题的示例,解析器不必具有超高效率,它不会经常被调用,但主要是我想着重于良好的代码可读性和可伸缩性。

用户输入示例:

"This is user randomString(5) and he is randomInt(18,60) years old!"

预期输出:

"This is user phiob and he is 45 years old!"

"This is user sdfrt and he is 30 years old!"

2 个答案:

答案 0 :(得分:0)

一种选择是使用Spring SPEL。但这会迫使您稍微更改表达式并使用Spring库:

表达式可以如下所示:

'This is user ' + randomString(5) + ' and he is ' + randomInt(18,60) + ' years old!'

或者这个:

This is user #{randomString(5)} and he is #{randomInt(18,60)} years old!

或者您可以通过自定义TemplateParserContext来实现自己的功能。

这是代码:

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class SomeTest {

        @Test
        public void test() {
            ExpressionParser parser = new SpelExpressionParser();

            Expression exp = parser.parseExpression(
                "This is user #{randomString(5)} and he is #{randomInt(18,60)} years old!", 
                new TemplateParserContext() );

            //alternative
            //Expression exp = parser.parseExpression(
            //        "'This is user ' + randomString(5) + ' and he is ' + randomInt(18,60) + ' years old!'");
            //    String message = (String) exp.getValue( new StandardEvaluationContext(this) );


            String message = (String) exp.getValue( new StandardEvaluationContext(this) );
        }

        public String randomString(int i) {
            return "rs-" + i;
        }

        public String randomInt(int i, int j) {
            return "ri-" + i + ":" + "j";
        }

    }

无论您传递给StandardEvaluationContext的任何对象都应具有这些方法。我把它们放在同样运行表达式的同一个类中。

答案 1 :(得分:-1)

您可以使用以下内容: 警告,我尚未测试。只是一些入门

public String parseInput(String input){
    String[] inputArray = input.split(" ");
    String output = "";
    for(String in : inputArray){ //run through each word of the user input
        if(in.contains("randomString(")){ //if the user is calling randomString
            String params = in.replace("randomString(", ""); //strip away function to get to params
            params = in.replace("(", ""); //strip away function to get to params
            String[] paramsArray = params.split(",");  //these are string integers, and could be converted
            //send off these split apart parameters to your randomString method
            String out = randomString(paramsArray); //method parses string integers, outputs string
            output += out + " ";
        }else if(in.contains("randomInt(")){ //if the user is calling randomInt
            String params = in.replace("randomInt(", ""); //strip away function to get to params
            params = in.replace("(", ""); //strip away function to get to params
            String[] paramsArray = params.split(","); //these are string integers, and could be converted
            //send off these split apart parameters to your randomInt method
            String out = randomInt(paramsArray); //method parses string integers, outputs string
            output += out + " ";
        }else{ //if the user is just entering text
            output += in + " "; //concat the output with what the user wrote plus a space
        }

    }
    return output;
}