[英]How to programmatically generate an EMF model (.ecore, .genmodel) from XSD schema?
I have been trying to do this as the last stage in a standalone application to convert from the file format used by a modeling program to an EMF model. 我一直在尝试将其作为独立应用程序中的最后一个步骤,以将建模程序使用的文件格式转换为EMF模型。 I am able to convert the original format to XSD, which I can manually convert to an EMF model using the Eclipse importer, but I do not know how to do this programmatically to automate the process.
我能够将原始格式转换为XSD,可以使用Eclipse导入程序将其手动转换为EMF模型,但是我不知道如何以编程方式执行此操作以使过程自动化。 Java commands would work fine, as would any command-line statement to do the same, since I could just execute the statement from within Java.
Java命令可以正常工作,就像任何命令行语句一样可以正常工作,因为我可以从Java内部执行该语句。 I have spent a while looking for how to do this, trying http://wiki.eclipse.org/Generating_Dynamic_Ecore_from_XML_Schema and a variety of other potential solutions, but nothing seems to work.
我花了一段时间寻找方法,尝试使用http://wiki.eclipse.org/Generating_Dynamic_Ecore_from_XML_Schema和其他各种可能的解决方案,但似乎没有任何效果。 If anyone might be able to provide some sample code as to how to generate the .ecore and(/or?) .genmodel files from an XSD file, I'd very much appreciate it, but even some guidance as to how I can proceed would help very much.
如果有人能够提供一些示例代码,说明如何从XSD文件生成.ecore和(/或?).genmodel文件,我将不胜感激,但即使是有关如何进行的一些指导,将非常有帮助。
Thank you. 谢谢。
Take a look at the class org.eclipse.xsd.ecore.XSDEcoreBuilder
and the way it's used by the Eclipse importer wizard. 看一看
org.eclipse.xsd.ecore.XSDEcoreBuilder
类及其在Eclipse导入器向导中的使用方式。
Seems to be fairly straightforward to use, you simply call one of its generate
methods and you get back either a Collection<Resource>
or a Collection<EObject>
. 似乎使用起来非常简单,只需调用它的
generate
方法之一,然后返回Collection<Resource>
或Collection<EObject>
。
(Edit: answering additional questions in comments) (编辑:在评论中回答其他问题)
The EPackage
class is the Ecore equivalent of xs:schema
, which contains the EClass
es, which are in turn the Ecore equivalents of xs:complexType
s. EPackage
类是xs:schema
的Ecore等效项,其中包含EClass
es,而EClass
则是xs:complexType
的Ecore等效项。
The following code snippet should create and save a foo.ecore file into the same folder as the source XSD. 以下代码片段应创建foo.ecore文件并将其保存到与源XSD相同的文件夹中。 If foo.xsd has additional imported XSDs, they will be coverted into separate .ecore files, hence the return type
Collection<Resource>
. 如果foo.xsd具有其他导入的XSD,它们将被覆盖到单独的.ecore文件中,因此返回类型为
Collection<Resource>
。
URI schemaURI = URI.createFileURI("foo.xsd");
Collection<Resource> ecoreResources = XSDEcoreBuilder.generateResources(schemaURI);
for (Resource ecoreResource : ecoreResources) {
ecoreResource.save(null);
}
The above-mentioned code works here using the following setup: 上面的代码在这里使用以下设置工作:
Version: Luna Service Release 2 (4.4.2) Build id: 20150219-0600 版本:Luna Service Release 2(4.4.2)内部版本:20150219-0600
Plugins: 插件:
However, the initial XSD file is overwritten by the Ecore content. 但是,初始XSD文件会被Ecore内容覆盖。 Also, the
save
operation is called twice (once for the XSDResourceImpl
and once for EcoreResourceFactoryImpl
). 另外,
save
操作被调用两次(对于XSDResourceImpl
一次,对于EcoreResourceFactoryImpl
一次)。 We need it only for EcoreResourceFactoryImpl
. 我们仅对
EcoreResourceFactoryImpl
需要它。 To fix this, here the sample code: 要解决此问题,请参见以下示例代码:
URI schemaURI = URI.createFileURI("library3.xsd");
File outputFile = new File("library3.ecore");
XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
Collection<Resource> ecoreResources = xsdEcoreBuilder.generateResources(schemaURI);
// for every resource found (includes eventually referenced XSDs)
for (Resource ecoreResource : ecoreResources) {
try {
if (ecoreResource.getClass().getName().contains("EcoreResourceFactoryImpl")) {
ecoreResource.save(new FileOutputStream(outputFile), null);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.