StringTemplate为给定模板定义的属性列表 [英] StringTemplate list of attributes defined for a given template
问题描述
我开始使用StringTemplate 4,我正在尝试从存储在数据库中的简单字符串创建模板。我使用这样的东西:
I am getting started with StringTemplate 4 and I am trying to create a template from a simple string stored in a database. I use something like this:
STGroup group = new STGroupString(null, someTemplateString, '$', '$');
ST st = group.getInstanceOf(someTemplateName);
st.add(someAttribute, someValue);
如果定义全部或少于为模板定义的属性<$ c $,现在一切正常C> someTemplateName 。现在如果我尝试添加一个不存在的属性,我会得到以下异常:
Now everything works fine if I define all or less than the attribute defined for the template someTemplateName
. Now if I try to add an attribute that doesn't exist, I get the following exception:
no such attribute: fake
java.lang.IllegalArgumentException: no such attribute: fake
...
这是有道理的。但是,似乎我无法事先知道为模板 someTemplateName
定义了哪些属性。我期待找到类似的东西:
which makes sense. However, it seems like there's no way for me to know beforehand which attributes are defined for the template someTemplateName
. I was expecting to find something like:
bool isDef = st.isDefined(someAttribute);
但是没有这样的方法。我对么?有什么方法吗?
but there's no such method. Am I correct? Is there any way around this?
推荐答案
你可以用 st.impl.formalArguments
访问 Map< String,FormalArgument>
定义参数的位置。请注意,对于某些模板,此字段将为 null
。
You can use st.impl.formalArguments
to access the Map<String, FormalArgument>
where the arguments are defined. Note that for some templates this field will be null
.
这篇关于StringTemplate为给定模板定义的属性列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!