Xtend / Xpand查找和替换(规则)

时间:2013-02-06 17:48:37

标签: java loops xtext xtend xpand

是否可以设置一个规则,允许Xpand方法根据输入输出指定的文本字符串。例如:

«FOR a:e.attributes»
 Type = «a.eClass.name»
 Value = «a.name.toFirstUpper»

«ENDFOR»

以上代码可能会输出:

Type = StringAttribute
Value = String1

Type = IntegerAttribute
Value = 123

我该如何输出:

Type = String
Value = String1

Type = int
Value = 123

我知道这可以用if语句完成,但我希望能够或多或少地自动化。每次我需要在同一文件中输出这些细节时,必须指定这些规则是一种浪费。有人可以告诉我我可以用什么样的代码来实现这个目标吗?谢谢。

1 个答案:

答案 0 :(得分:0)

我建议您创建一个可重复使用的Xtend帮助程序

toSimpleName(String inp):
    switch (inp) {
        case "StringAttribute"  : "String"
        case "IntegerAttribute" : "int"
        // ...more cases here...
        default                 : inp
    }
;

然后从你的Xpand模板中调用它:

«FOR a:e.attributes»
 Type = «a.eClass.name.toSimpleName()»
 Value = «a.name.toFirstUpper»

«ENDFOR»