“需要注册的工厂”加载资源时的异常 [英] "Registered factory needed" exception when loading resource

查看:194
本文介绍了“需要注册的工厂”加载资源时的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下异常:

  java.lang.RuntimeException:无法为'file:/ home /创建资源my_conf.xml;需要一个注册资源工厂

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

  ResourceSet resourceSet = new ResourceSetImpl(); 
资源资源= null;

文件f =新文件(文件名);
URI uri = URI.createFileURI(f.getAbsolutePath());

if(!f.exists()){
throw new Exception(filename +不存在);

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

有没有人有线索?

解决方案

如果您在独立模式下运行,则必须手动将工厂注册到资源集工厂注册表。

添加以下内容创建资源集实例后的行:

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

请参阅











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




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




YourPackage packageInstance =
YourPackage.eInstance;





  • 如果您使用的是动态元模型(不生成Java代码),则必须手动注册。



  resourceSet.getPackageRegistry()。把(yourPackage.get NsURI(),yourPackage);  

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


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?

解决方案

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.

这篇关于“需要注册的工厂”加载资源时的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆