从EObject树(模型)获取XtextResource
为了简单起见,假设我有多个 Xtext DSL,它们都描述某种查询。其中之一是我的“基础”DSL。我现在想将我的 DSL 之一转换为我的“基本”DSL 并检索模型的字符串表示形式。
我通过创建了一个 EFactory
MyDslFactory factory = new MyDslFactoryImpl();
然后我手动遍历源 DSL 的 XtextResource (我从 EmbeddedEditor 获得)并使用目标 DSL 工厂来构建我的模型(EObject 树)。 现在我想获得该模型的文本表示,但这就是我陷入困境的地方。我知道如何获取 XtextResource 的字符串表示形式,所以我想这就是可行的方法,但我不知道如何从 EObject 树创建 XtextResource。
在 org.eclipse.xtext.junit.AbstractXtextTests 中有一个方法
doGetResource(InputStream in, URI uri)
,但现在我找不到从模型中获取 InputStream 的方法。
有没有办法从对象树创建 XtextResource 或者有更简单的方法来做我想做的事情?
For the sake of simplicity say I have multiple Xtext DSLs that all describe some sort of query. One of them is my "base" DSL. I now would like to translate one of my DSLs to my "base" DSL and retrieve the string representation of the model.
I created a EFactory via
MyDslFactory factory = new MyDslFactoryImpl();
Then I manually traverse the source-DSL's XtextResource (I got that from the EmbeddedEditor) and use the target-DSL factory to build my model (EObject tree).
Now I would like to get the text representation of that model but this is where I get stuck. I know how to get the string representation of a XtextResource so I guess that's the way to go, but I don't know how to create the XtextResource from the EObject-tree.
In org.eclipse.xtext.junit.AbstractXtextTests there is a method
doGetResource(InputStream in, URI uri)
but now I don't find a way to get an InputStream from my model.
Is there a way to create the XtextResource from the object tree or is there a simpler way to do what I would like to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只需要文本序列化,则应该使用注入获取 org.eclipse.xtext.serializer.ISerializer 的实例。
确保您已在 Formatter 中定义了一些格式化规则,否则输出的可读性有点“有限”;-)
If you just want the text serialization, you should obtain an instance of org.eclipse.xtext.serializer.ISerializer using injection.
Make sure you have defined some formatting rules in Formatter, otherwise the readability of the output is a bit "limited" ;-)