Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » Question Generating from XSD(EMF can't read XML that conforms to schema)
Question Generating from XSD [message #1764218] Fri, 26 May 2017 17:42 Go to next message
Geoffry Roberts is currently offline Geoffry RobertsFriend
Messages: 52
Registered: July 2016
Member
There must be a workaround for this problem. I would like to what it is.

Mucho thanks to who ever knows this.

I generated an EMF graph from the a schema that contains the following fragment. The fragment shows and element and it's type:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://standards.ieee.org/IEEE1516-2010" xmlns:ns="http://standards.ieee.org/IEEE1516-2010" targetNamespace="http://standards.ieee.org/IEEE1516-2010" elementFormDefault="qualified" attributeFormDefault="unqualified" version="2010">
<xs:element name="objectModel" type="objectModelType"/>
<xs:complexType name="objectModelType">
<xs:sequence>
<xs:element name="modelIdentification" type="modelIdentificationType" minOccurs="0">
<xs:annotation>
<xs:documentation>documents certain key identifying information within the object model description
</xs:documentation>
</xs:annotation>
</xs:element>
...more xsd...


The generated EMF cannot read the following, valid XML. It fails at the first element shown:

<objectModel xsi:schemaLocation="http://standards.ieee.org/IEEE1516-2010 http://standards.ieee.org/downloads/1516/1516.2-2010/IEEE1516-DIF-2010.xsd" xmlns="http://standards.ieee.org/IEEE1516-2010" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
...more xml...


and yields the following error:

org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.eclipse.emf.ecore.xmi.ClassNotFoundException: Class 'objectModel' is not found or is abstract. (file:/Users/gcr1/hopeWorkspace/interaction-injection/output/TripleFederation.xml, 2, 252)
Re: Question Generating from XSD [message #1764242 is a reply to message #1764218] Sat, 27 May 2017 06:58 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Did you use the generated resource factory to create the resource that you used to load this XML instance?

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Question Generating from XSD [message #1764408 is a reply to message #1764242] Mon, 29 May 2017 13:48 Go to previous messageGo to next message
Geoffry Roberts is currently offline Geoffry RobertsFriend
Messages: 52
Registered: July 2016
Member
No I dd not. I can't do that. I must use documents generated by others, but that conform to the schema I am using to generate.
Re: Question Generating from XSD [message #1764424 is a reply to message #1764408] Mon, 29 May 2017 15:12 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
That answer doesn't make sense. You must use the generated resource factory to create the resource that you use to load such an XML instance.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Question Generating from XSD [message #1764531 is a reply to message #1764424] Wed, 31 May 2017 01:23 Go to previous messageGo to next message
Geoffry Roberts is currently offline Geoffry RobertsFriend
Messages: 52
Registered: July 2016
Member
What follows is how I am attempting to load the resource. It works when I load an XML resource that was created and saved using EMF. Do I need something else? I don't see where my generated factory (_2010Factory) comes into the picture.

ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getPackageRegistry().put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml", new XMLResourceFactoryImpl());
java.net.URI uri = new File(aValidFileName.xml).toURI();
Resource resource = resourceSet.getResource(URI.createURI(uri.toString()), true);
EObject eObject = (EObject) resource.getContents().get(0);

Re: Question Generating from XSD [message #1764540 is a reply to message #1764531] Wed, 31 May 2017 06:44 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
If you invoke Generate Test Code in the GenModel it will generate a test project that includes a sample for saving and loading instances. It will be named XyzExample.java, I suppose for you that would be _2010Example.java (given you mention _2010Factory). It looks like this for the LibraryExample.java
  public static void main(String[] args)
  {
    // Create a resource set to hold the resources.
    //
    ResourceSet resourceSet = new ResourceSetImpl();
    
    // Register the appropriate resource factory to handle all file extensions.
    //
    resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
      (Resource.Factory.Registry.DEFAULT_EXTENSION, 
       new LibraryResourceFactoryImpl());

    // Register the package to ensure it is available during loading.
    //
    resourceSet.getPackageRegistry().put
      (LibraryPackage.eNS_URI, 
       LibraryPackage.eINSTANCE);
So I imagine you must have a _2010ResourceFactoryImpl (in the util package of the model project) that you should use and should register your _2010Package.eINSTANCE...


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Question Generating from XSD [message #1764550 is a reply to message #1764540] Wed, 31 May 2017 07:55 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

You must also use your _2010ResourceFactoryImpl in

resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
(SOME_EXTENSION,
new _2010ResourceFactoryImpl ());

where SOME_EXTENSION might be Resource.Factory.Registry.DEFAULT_EXTENSION, or "*", or "xml", or "_2010".

I recommend using a distinctive extension, otherwise you will need to delve into the complexities of content types to allow multiple variants of *.xml content.

Regards

Ed Willink
Re: Question Generating from XSD [message #1764587 is a reply to message #1764550] Wed, 31 May 2017 13:54 Go to previous messageGo to next message
Geoffry Roberts is currently offline Geoffry RobertsFriend
Messages: 52
Registered: July 2016
Member
Ed and Ed,

Thanks for the help so far but I am afraid I am still not out of the woods on this. I added the following to my aforementioned setup as suggested. I am also using the suggested extension. It now looks like this:

ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getPackageRegistry().put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml", new XMLResourceFactoryImpl());

resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("_2010", new _2010FactoryImpl());
resourceSet.getPackageRegistry().put(_2010Package.eNS_URI, _2010Package.eINSTANCE);

java.net.URI uri = new File(aValidFileName.xml).toURI();
Resource resource = resourceSet.getResource(URI.createURI(uri.toString()), true);


I am now getting an error.

Error:
java.lang.ClassCastException: org.ieee.standards.ieee1516._2010.impl._2010FactoryImpl cannot be cast to org.eclipse.emf.ecore.resource.Resource$Factory

Re: Question Generating from XSD [message #1764588 is a reply to message #1764587] Wed, 31 May 2017 14:20 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Try new _2010ResourceFactoryImpl ()
Re: Question Generating from XSD [message #1764592 is a reply to message #1764588] Wed, 31 May 2017 14:30 Go to previous message
Geoffry Roberts is currently offline Geoffry RobertsFriend
Messages: 52
Registered: July 2016
Member
Thank you Ed for your sharp eyes. That little bit got past me.

It is a cosmic truth: "You can't read your own bad writing."
Previous Topic:EMFStore: Is EPackage registration at runtime supported?
Next Topic:How to add a map with generics?
Goto Forum:
  


Current Time: Tue Apr 23 11:02:32 GMT 2024

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

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

Back to the top