Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » XSDEcoreBuilder Problem converting Xsd to ecore in Eclipse plugin
XSDEcoreBuilder Problem converting Xsd to ecore in Eclipse plugin [message #1271191] Fri, 14 March 2014 13:19 Go to next message
Nickie woo is currently offline Nickie wooFriend
Messages: 21
Registered: February 2014
Junior Member
Hi

I´m having a very strange problem converting an XML schema .xsd to ecore Epackage using XSDEcoreBuilder.

This is how i convert the schema to ecore according to the tutorial provided on EMF website

XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
		String schema = getSchemaAbsolutePath(xmlFileUri.toFileString(),
				extractSchemaLocation(xmlFileUri));
		Collection<?> eCorePackages = xsdEcoreBuilder.generate(URI
				.createFileURI(schema));

My schema have multiple xsd:include schemaLocation that refer to many other .xsi files and this code works fine when running as standalone Java application but in my Eclipse plug in the returned eCorePackages is empty.

When i trace this problem in xsdEcoreBuilder.generate(URI)method the resource
generated for the schema differ depending on where the code is running from.

Quote:
In Java application this resource is : org.eclipse.xsd.util.XSDResourceImpl
In Eclipse plug in this resource is : org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl instead.


Anyone know how to fix this issue ? and force it to use org.eclipse.xsd.util.XSDResourceImpl for xsd files instead ?

org.eclipse.xsd.util.XSDResourceImpl is btw private and i cant instantiate and register it to the global registry.

public Collection<EObject> generate(URI uri)
  {
    ResourceSet resourceSet = createResourceSet();
    Resource [b]resource[/b]= resourceSet.getResource(uri, true);
    List<XSDSchema> xsdSchemas = new ArrayList<XSDSchema>();
    for (Object object : resource.getContents())
    {
      if (object instanceof XSDSchema)
      {
        xsdSchemas.add((XSDSchema)object);
      }
    }

    for (XSDSchema xsdSchema : xsdSchemas)
    {
      generate(xsdSchema);
    }

    List<EObject> result = new ArrayList<EObject>(targetNamespaceToEPackageMap.values());
    result.remove(XMLNamespacePackage.eINSTANCE);
    if (mapper != null)
    {
      result.add(mapper.getRoot());
    }
    return result;
  }

[Updated on: Fri, 14 March 2014 13:35]

Report message to a moderator

Re: XSDEcoreBuilder Problem converting Xsd to ecore in Eclipse plugin [message #1271426 is a reply to message #1271191] Sat, 15 March 2014 04:24 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Nickie,

Comments below.

On 14/03/2014 2:19 PM, Nickie woo wrote:
> Hi
>
> I´m having a very strange problem converting an XML schema .xsd to
> ecore Epackage using XSDEcoreBuilder.
>
> This is how i convert the schema to ecore according to the tutorial
> provided on EMF website
>
> XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
> String schema = getSchemaAbsolutePath(xmlFileUri.toFileString(),
> extractSchemaLocation(xmlFileUri));
> Collection<?> eCorePackages = xsdEcoreBuilder.generate(URI
> .createFileURI(schema));
> My schema have multiple xsd:include schemaLocation that refer to many
> other .xsi files
You mean xsd? That's the normal exception for an XML Schema file...
> and this code works fine when running as standalone Java application
> but in my Eclipse plug in the returned eCorePackages is empty.
>
> When i trace this problem in xsdEcoreBuilder.generate(URI)method the
> resource
> generated for the schema differ depending on where the code is running
> from.
>
> In Java application this resource is :
> org.eclipse.xsd.util.XSDResourceImpl
> In Eclipse plug in this resource is :
> org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl instead.
Which resource?
>
> Anyone know how to fix this issue ? so it use
> org.eclipse.xsd.util.XSDResourceImpl instead ?
>
> org.eclipse.xsd.util.XSDResourceImpl is btw privateand i cant
> instanciate it to register it to the global registry.
No, it's not private and in any case, you can use XSDResourceFactoryImpl
to register it for any extensions which contain XML Schemas...
>
> public Collection<EObject> generate(URI uri)
> {
> ResourceSet resourceSet = createResourceSet();
> Resource resource= resourceSet.getResource(uri, true);
> List<XSDSchema> xsdSchemas = new ArrayList<XSDSchema>();
> for (Object object : resource.getContents())
> {
> if (object instanceof XSDSchema)
> {
> xsdSchemas.add((XSDSchema)object);
> }
> }
>
> for (XSDSchema xsdSchema : xsdSchemas)
> {
> generate(xsdSchema);
> }
>
> List<EObject> result = new
> ArrayList<EObject>(targetNamespaceToEPackageMap.values());
> result.remove(XMLNamespacePackage.eINSTANCE);
> if (mapper != null)
> {
> result.add(mapper.getRoot());
> }
> return result;
> }


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: XSDEcoreBuilder Problem converting Xsd to ecore in Eclipse plugin [message #1271499 is a reply to message #1271426] Sat, 15 March 2014 10:04 Go to previous messageGo to next message
Nickie woo is currently offline Nickie wooFriend
Messages: 21
Registered: February 2014
Junior Member
Hi
.xsi is our own Xml schema extension which is basically just normal Xml schema files

I was referring to the resource generate from this row

    Resource resource = resourceSet.getResource(uri, true);


In the XSDEcoreBuilder.generate(URI uri) method.

I tried to register XSDResourceFactoryImpl and still without success
This is what i did.
  Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
	Map<String, Object> map = reg.getExtensionToFactoryMap();
	map.put("xsd", new XSDResourceFactoryImpl());
	map.put("xsi", new XSDResourceFactoryImpl());
		
	XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
	String schema = getSchemaAbsolutePath(xmlFileUri.toFileString(),
			extractSchemaLocation(xmlFileUri));
	Collection<?> eCorePackages = xsdEcoreBuilder.generate(URI
			.createFileURI(schema));


The eCorePackages is still empty when running in a basically empty Eclipse plugin
This registration doesnt seem to make any effect the resource generated for the xml schema is still of

org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl$1@63ae62c1 uri='file:/C:/test.xsd'


Sad
Re: XSDEcoreBuilder Problem converting Xsd to ecore in Eclipse plugin [message #1271550 is a reply to message #1271499] Sat, 15 March 2014 13:11 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Nickie,

Comments below.

On 15/03/2014 3:04 AM, Nickie woo wrote:
> Hi
> xsi is our own Xml schema extension which is basically just normal Xml
> schema files
>
> I was referring to the resource generate from this row
>
> Resource resource = resourceSet.getResource(uri, true);
And where is this resource set coming from?
>
>
> In the XSDEcoreBuilder.generate(URI uri) method.
>
> I tried to register XSDResourceFactoryImpl and still without success
Trying doing it in
org.eclipse.xsd.ecore.XSDEcoreBuilder.createResourceSet() and have a
look at what that method already does.
> This is what i did.
> Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
> Map<String, Object> map = reg.getExtensionToFactoryMap();
> map.put("xsd", new XSDResourceFactoryImpl());
> map.put("xsi", new XSDResourceFactoryImpl());
>
> XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
> String schema = getSchemaAbsolutePath(xmlFileUri.toFileString(),
> extractSchemaLocation(xmlFileUri));
> Collection<?> eCorePackages = xsdEcoreBuilder.generate(URI
> .createFileURI(schema));
>
> The eCorePackages is still empty when running in a basically empty
> Eclipse plugin
> This registration doesnt seem to make any effect the resource
> generated for the xml schema is still of
> org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl$1@63ae62c1
> uri='file:/C:/test.xsd'
If it still doesn't work, set a breakpoint in
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.createResource(URI,
String) and see what registrations it's finding that are causing your
problem.
>
> :(


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: XSDEcoreBuilder Problem converting Xsd to ecore in Eclipse plugin [message #1272207 is a reply to message #1271550] Mon, 17 March 2014 10:47 Go to previous messageGo to next message
Nickie woo is currently offline Nickie wooFriend
Messages: 21
Registered: February 2014
Junior Member
Hi

org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.createResource(URI,
String) never get called for the schema URI running as Eclipse plugin

This is the order of function calls starting from
Quote:

XsdToEcoreBuilder.generate(schemaUri, true)
 public Resource getResource(URI uri, boolean loadOnDemand)
  {
    if (resourceLocator != null)
    {
      return resourceLocator.getResource(uri, loadOnDemand);
    }

    Map<URI, Resource> map = getURIResourceMap();
    if (map != null)
    {
      Resource resource = map.get(uri);
      if (resource != null)
      {
        if (loadOnDemand && !resource.isLoaded())
        {
          demandLoadHelper(resource);
        }
        return resource;
      }
    }

    URIConverter theURIConverter = getURIConverter();
    URI normalizedURI = theURIConverter.normalize(uri);
    for (Resource resource : getResources())
    {
      if (theURIConverter.normalize(resource.getURI()).equals(normalizedURI))
      {
        if (loadOnDemand && !resource.isLoaded())
        {
          demandLoadHelper(resource);
        }

        if (map != null)
        {
          map.put(uri, resource);
        }
        return resource;
      }
    }

    Resource delegatedResource = delegatedGetResource(uri, loadOnDemand); >>>
........
  }

ResourceSetImpl.delegateGetResource(uri, loadondemand)
  
protected Resource delegatedGetResource(URI uri, boolean loadOnDemand)
  {
    EPackage ePackage = getPackageRegistry().getEPackage(uri.toString()); ||| ePackage = Test.impl.TestPackageImpl@d2b2008 (name: Test) (nsURI: file:/C:/Test.xsd, nsPrefix: Test)
    return ePackage == null ? null : ePackage.eResource(); >>>
  }


BasicEObjectImpl
  public Resource eResource()
  {
    return eInternalResource(); >>>
  }
  
>>> public Resource.Internal eInternalResource()
  {
    Resource.Internal result = eDirectResource(); >>>
...
  }

MinimalEObjectImpl
>>> public Resource.Internal eDirectResource()
  {
    return (Resource.Internal)getField(RESOURCE); >>>
  }


>>> private final Object getField(int field) <<<<<<<<<< field = 128
  {
    if (hasField(field))
    {
      int fieldIndex = fieldIndex(field);
      return fieldIndex == -1 ? eStorage : ((Object[])eStorage)[fieldIndex]; Here it return the incorrect Epackage eStorage = org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl$1@2cda5432 uri='file:/C:/Test.xsd'
  }



But when i ran the same code as a Java application the XsdToEcoreBuilder.generate(schemaUri, loadondemand = true) the code continues after Resource delegatedResource = delegatedGetResource(uri, loadOnDemand);
and call Resource resource = demandCreateResource(uri) which returns the right Epackage
public Resource getResource(URI uri, boolean loadOnDemand)
  {
    ....
    Resource delegatedResource = delegatedGetResource(uri, loadOnDemand); >>>> Return delegatedResource = null
    if (delegatedResource != null)
    {
      if (map != null)
      {
        map.put(uri, delegatedResource);
      }
      return delegatedResource;
    }

    if (loadOnDemand)
    {
      Resource resource = demandCreateResource(uri);
      if (resource == null)
      {
        throw new RuntimeException("Cannot create a resource for '" + uri + "'; a registered resource factory is needed");
      }

      demandLoadHelper(resource);

      if (map != null)
      {
        map.put(uri, resource);
      }
      return resource;
    }

    return null;
  }


So when run the same code in an Eclipse plugin Resource delegatedResource = delegatedGetResource(uri, loadOnDemand) found a resource for my schema but when run as a Java application it does not and org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.createResource(URI,
String) got called which returns the right Epackage Crying or Very Sad

[Updated on: Mon, 17 March 2014 11:08]

Report message to a moderator

Re: XSDEcoreBuilder Problem converting Xsd to ecore in Eclipse plugin [message #1272242 is a reply to message #1272207] Mon, 17 March 2014 12:53 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Nickie,

Comments below.

On 17/03/2014 3:47 AM, Nickie woo wrote:
> Hi
>
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.createResource(URI, String)
> never got called for the schema URI
>
> This is the order of function calls starting from Quote:
>> XsdToEcoreBuilder.generate(schemaUri, true)
>>
>> public Resource getResource(URI uri, boolean loadOnDemand)
>> {
>> if (resourceLocator != null)
>> {
>> return resourceLocator.getResource(uri, loadOnDemand);
>> }
>>
>> Map<URI, Resource> map = getURIResourceMap();
>> if (map != null)
>> {
>> Resource resource = map.get(uri);
>> if (resource != null)
>> {
>> if (loadOnDemand && !resource.isLoaded())
>> {
>> demandLoadHelper(resource);
>> }
>> return resource;
>> }
>> }
>>
>> URIConverter theURIConverter = getURIConverter();
>> URI normalizedURI = theURIConverter.normalize(uri);
>> for (Resource resource : getResources())
>> {
>> if
>> (theURIConverter.normalize(resource.getURI()).equals(normalizedURI))
>> {
>> if (loadOnDemand && !resource.isLoaded())
>> {
>> demandLoadHelper(resource);
>> }
>>
>> if (map != null)
>> {
>> map.put(uri, resource);
>> }
>> return resource;
>> }
>> }
>>
>> Resource delegatedResource = delegatedGetResource(uri,
>> loadOnDemand); >>>
>> ........
>> }
>>
>> ResourceSetImpl.delegateGetResource(uri, loadondemand)
>> protected Resource delegatedGetResource(URI uri, boolean loadOnDemand)
>> {
>> EPackage ePackage =
>> getPackageRegistry().getEPackage(uri.toString()); ||| ePackage =
>> Test.impl.TestPackageImpl@d2b2008 (name: Test) (nsURI:
>> file:/C:/Test.xsd, nsPrefix: Test)
>> return ePackage == null ? null : ePackage.eResource(); >>>
So you have a generated package with an nsURI that's exactly the URI of
the schema for which you're trying to generate an Ecore model. Delete
the project with that model, don't deploy it with the Eclipse
application you're trying to run, or just use that already-generated
package...
>> }
>>
>>
>> BasicEObjectImpl
>>
>> public Resource eResource()
>> {
>> return eInternalResource(); >>>
>> }
>> >>> public Resource.Internal eInternalResource()
>> {
>> Resource.Internal result = eDirectResource(); >>>
>> ...
>> }
>>
>> MinimalEObjectImpl
>>
>> >>> public Resource.Internal eDirectResource()
>> {
>> return (Resource.Internal)getField(RESOURCE); >>>
>> }
>>
>>
>>
>> >>> private final Object getField(int field) <<<<<<<<<< field = 128
>> {
>> if (hasField(field))
>> {
>> int fieldIndex = fieldIndex(field);
>> return fieldIndex == -1 ? eStorage :
>> ((Object[])eStorage)[fieldIndex]; Here it return eStorage =
>> org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl$1@2cda5432
>> uri='file:/C:/Test.xsd'
>> }
>
>
> I´m still not sure what causes this since i´m completely lost when the
> code entered BasicEObjectImpl :cry:
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: XSDEcoreBuilder Problem converting Xsd to ecore in Eclipse plugin [message #1272255 is a reply to message #1272242] Mon, 17 March 2014 13:35 Go to previous message
Nickie woo is currently offline Nickie wooFriend
Messages: 21
Registered: February 2014
Junior Member
Ed Merks wrote on Mon, 17 March 2014 08:53
Nickie,

Comments below.
So you have a generated package with an nsURI that's exactly the URI of
the schema for which you're trying to generate an Ecore model. Delete
the project with that model, don't deploy it with the Eclipse
application you're trying to run, or just use that already-generated
package...


Thank you everything work now Very Happy
Previous Topic:Dynamic templates = true causes error when generating model code
Next Topic:Problem in resolving proxies
Goto Forum:
  


Current Time: Fri Apr 26 06:46:59 GMT 2024

Powered by FUDForum. Page generated in 0.05512 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top