在eclipse中从ecore文件中读取eobjects

时间:2016-07-01 06:54:47

标签: eclipse eclipse-emf eclipse-emf-ecore

我有ecore文件,其中包含类eobjects.Now我想读取ecore文件并从该ecore文件中获取所有类eobjects。

1 个答案:

答案 0 :(得分:3)

您是否想要使用自定义后缀重新加载特定的xmi文件?

以下是在特定位置(路径)加载ecore文件并返回根EObject的方法示例

public static EObject loadYourModel(String path) {
    /*Initialzie Models*/
    YourPackage.eINSTANCE.eClass();

    /*register your xmi resources*/
    final Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
    final Map<String, Object> m = reg.getExtensionToFactoryMap();
    /*put all your different ecore file suffixes in the map; suffix = YourPackage.eNAME*/
    m.put(YourPackage.eNAME, new XMIResourceFactoryImpl());
    /*you can put all different package names here*/

    /*Create a new Resource set to store the EObjects from the file*/
    ResourceSet resSet = new ResourceSetImpl();

    /*get the resource of your ecore file*/
    Resource resource = resSet.getResource(URI.createURI(path), true);
    /*Get the first element = root of your model hierachy*/
    EObject root = resource.getContents().get(0);
    return root;
}