“Registered factory needed” exception when loading resource

六眼飞鱼酱① 提交于 2019-12-22 09:46:54

问题


I get the following exception:

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

The "explosion" code is like this and stops at: 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);
    }

Is there anyone that has a clue?


回答1:


If you are running in standalone mode, you will have to manually register the factories to your resource set factory registry.
Add the following line after the creation of your resource set instance:

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

Please see http://wiki.eclipse.org/EMF-FAQ#How_do_I_use_EMF_in_standalone_applications_.28such_as_an_ordinary_main.29.3F

For the Package not found issue there is two possibilities depending of your case:

  • If you are using a static metamodel (a java implementation is generated from your ecore model), you only have to access the corresponding Package instance for it to be loaded and registered into the global EMF package registry.

YourPackage packageInstance = YourPackage.eInstance;

  • If you are using a dynamic metamodel (no java code generated), you have to register it manually.
resourceSet.getPackageRegistry().put(yourPackage.getNsURI(), yourPackage);

With the previous code, you will have to previously retrieve the EPackage from your ecore model programmatically.



来源:https://stackoverflow.com/questions/6571638/registered-factory-needed-exception-when-loading-resource

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!