4

我得到以下异常:

 java.lang.RuntimeException: Cannot create a resource for 'file:/home/my_conf.xml'; a registered resource factory is needed

“爆炸”代码是这样的,停在:resource = resourceSet.....

    ResourceSet resourceSet = new ResourceSetImpl();
    Resource resource = null;

    File f = new File(filename); 
    URI uri = URI.createFileURI(f.getAbsolutePath());

    if (!f.exists()) {
        throw new Exception(filename + " does not exist");

    } else {
        resource = resourceSet.getResource(uri, true);
        mapPrepConfiguration = (MapPrepConfiguration) resource.getContents().get(0);
    }

有没有人有线索?

4

1 回答 1

7

如果您在独立模式下运行,则必须手动将工厂注册到资源集工厂注册表。
在创建资源集实例后添加以下行:

resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml", new XMLResourceFactoryImpl());

请参阅http://wiki.eclipse.org/EMF-FAQ#How_do_I_use_EMF_in_standalone_applications_.28such_as_an_ordinary_main.29.3F

对于 Package not found 问题,根据您的情况有两种可能性:

  • 如果您使用的是静态元模型(从您的 ecore 模型生成一个 java 实现),您只需访问相应的 Package 实例即可将其加载并注册到全局 EMF 包注册表中。

YourPackage packageInstance = YourPackage.eInstance;

  • 如果您使用的是动态元模型(未生成 java 代码),则必须手动注册它。
resourceSet.getPackageRegistry().put(yourPackage.getNsURI(), yourPackage);

使用前面的代码,您必须事先以编程方式从您的 ecore 模型中检索 EPackage。

于 2011-07-04T14:21:59.790 回答