Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在StringTemplate v4中获取属性_Java_Stringtemplate 4 - Fatal编程技术网

Java 如何在StringTemplate v4中获取属性

Java 如何在StringTemplate v4中获取属性,java,stringtemplate-4,Java,Stringtemplate 4,我有以下代码 String templateString = "Some Text $attribute1$ more text $attribute2$ more text"; ST stringTemplate = new ST(templateString ,'$','$');` 如何迭代所有属性,即attribute1、attribute2等? 我想获得模板中的所有属性列表。在groovy中使用的只是这样一个正则表达式,它似乎完成了我现在需要的工作 List<String

我有以下代码

String templateString = "Some Text $attribute1$ more text $attribute2$ more text"; 
ST stringTemplate = new ST(templateString ,'$','$');`
如何迭代所有属性,即attribute1、attribute2等?
我想获得模板中的所有属性列表。

在groovy中使用的只是这样一个正则表达式,它似乎完成了我现在需要的工作

   List<String> extractTemplateVariables( String statement ) {
    Pattern pattern = Pattern.compile( /\$(\w*)\$/ );
    def List<String> runTimeParms = []
    def matcher = pattern.matcher( statement )

    while (matcher.find()) {
        runTimeParms << matcher.group( 1 )

    }
    runTimeParms.removeAll( Collections.singleton( null ) );
    runTimeParms.unique( false )

但我不明白它的结构,也不明白我在这里做什么,而且它也没有看到比第一个更多的东西。也许有人能帮我们找出最好的方法是什么

你确定这是一个有效的
ST
?您可以使用它吗?是的,它是有效的。我可以使用add()替换属性值,但无法获取属性列表。
   final int ID = 25
    char delimiter = '$'
    ST st = new org.stringtemplate.v4.ST( statement, delimiter, delimiter );

    def dataFieldNames = []
    def t = st.getAttributes(  )
    st.impl.ast.getChildren().each {

        if (it != null) {
            CommonTree child = it as CommonTree
            if (child.toString().equals( "EXPR" )) {
                if (child.getChildCount() == 1) {
                    CommonTree expressionChild = child.getChild( 0 )
                    if (expressionChild.getToken().getType() == ID) {
                        dataFieldNames.add( expressionChild.toString() )
                    } else if (expressionChild.toString().equals( "PROP" )) {
                        if (expressionChild.getChildCount() == 2) {
                            dataFieldNames.add(
                                    expressionChild.getChild( 0 ).toString() +
                                            "." +
                                            expressionChild.getChild( 1 ).toString() )
                        }
                    }

                }
            }
        }
    }

    dataFieldNames.unique( false )

}