Analyzing XML schemas using org.eclipse.xsd and Maven2

﹥>﹥吖頭↗ 提交于 2019-12-24 05:35:09

问题


I'm trying to implement the sample code to this article from 2002 (I know..), but cannot get the schema to load.

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xsd.util.XSDResourceImpl;

    ResourceSet resourceSet = new ResourceSetImpl();
    // I replaced the deprecated createDeviceURI with createURI as recommended in JavaDoc
    XSDResourceImpl xsdSchemaResource = 
        (XSDResourceImpl)resourceSet.getResource(URI.createURI("my.xsd"), true);

I'm using the following Maven2 dependencies:

<dependency>
    <groupId>org.eclipse.xsd</groupId>
    <artifactId>xsd</artifactId>
    <version>2.1.1</version>
</dependency>
<dependency>
    <groupId>org.eclipse.emf</groupId>
    <artifactId>ecore</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>org.eclipse.emf</groupId>
    <artifactId>common</artifactId>
    <version>2.1.0</version>
</dependency>

The code compiles just fine, but produces a RuntimeException at execution time:

java.lang.RuntimeException: 
    Cannot create a resource for 'my.xsd'; a registered resource factory is needed
    at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:346)

I found some resource factory implementations in org.eclipse.emf.ecore.xmi, but AFAIK there's only a xmi snapshot in the public Maven repo, which has a dependency on org.eclipse.core.runtime.. which is not what I want.

Can anyone help?


回答1:


Try adding this code before creating your ResourceSetImpl:

import org.eclipse.xsd.util.XSDResourceFactoryImpl;

Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
java.util.Map m = reg.getExtensionToFactoryMap();
m.put("xsd", new XSDResourceFactoryImpl());

That should create the registry and factory that you need to accomplish what you are trying to do.



来源:https://stackoverflow.com/questions/533573/analyzing-xml-schemas-using-org-eclipse-xsd-and-maven2

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