otto.poellath
otto.poellath

Reputation: 4239

Analyzing XML schemas using org.eclipse.xsd and Maven2

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?

Upvotes: 2

Views: 2795

Answers (1)

KeithL
KeithL

Reputation: 1150

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.

Upvotes: 2

Related Questions