Difference between revisions of "EMF/Generating Dynamic Ecore from XML Schema"

From Eclipsepedia

< EMF
Jump to: navigation, search
(Generating Ecore using from XML Schema)
 
Line 47: Line 47:
 
[http://www.devx.com/Java/Article/29093/0/page/3 Discover the Eclipse Modeling Framework (EMF) and Its Dynamic Capabilities]<br>
 
[http://www.devx.com/Java/Article/29093/0/page/3 Discover the Eclipse Modeling Framework (EMF) and Its Dynamic Capabilities]<br>
 
[http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg11109.html Newsgroup Msg11109]<br>
 
[http://dev.eclipse.org/newslists/news.eclipse.tools.emf/msg11109.html Newsgroup Msg11109]<br>
 +
 +
--[[User:Annamalai.chockalingam.gmail.com|Annamalai.chockalingam.gmail.com]] 18:06, 10 February 2007 (EST)

Revision as of 19:06, 10 February 2007

Generating Ecore using from XML Schema
In this Wiki document, i have tried to document the steps involved in Generating Ecore using XML Schema and loading a appropriate XML File into the Model.

  1. Convert a given XSD into Ecore
  2. Load an XML into the generated Ecore
  3. Display the loaded Model using a TreeViewer

For this code to execute, you need to include the following plugins as dependencies

  • org.eclipse.emf.ecore
  • org.eclipse.xsd
  • org.eclipse.emf.ecore.xmi
  • org.eclipse.emf.edit.ui

Convert a XSD into Ecore

The following code snippet would enable you to load an existing XSD and convert it into an Ecore.

    XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
    ResourceSet resourceSet = new ResourceSetImpl();
    Collection eCorePackages = xsdEcoreBuilder.generate(URI.createFileURI("D:/model/example.xsd"));

Note: Running this code could get you a nullPointerException or schemaFileNotFoundException. This is due to a ever popular Crimsom DOM bug. Please refer to the following link to find a workaround to this problem. Manifestation of Crimsom DOM Bug

Load an XML into the Ecore

    HashMap options = new HashMap();
    options.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
    Resource resource = resourceSet.createResource(URI.createFileURI("D:/model/example.xml"));
    resource.load(options);

Display using a TreeViewer

    List factories = new ArrayList();
    factories.add(new ResourceItemProviderAdapterFactory());
    factories.add(new ReflectiveItemProviderAdapterFactory());
    
    ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory();

    TreeViewer tViewer = new TreeViewer(parent);
    tViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory);
    tViewer.setLabelProvider(new AdapterFactoryContentProvider(adapterFactory);
    tViewer.setInput(resourceSet);

Further Reading
Discover the Eclipse Modeling Framework (EMF) and Its Dynamic Capabilities
Newsgroup Msg11109

--Annamalai.chockalingam.gmail.com 18:06, 10 February 2007 (EST)