Difference between revisions of "EMF/Generating Dynamic Ecore from XML Schema"
(15 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
− | <b> | + | <b>EMF and XML - An Introduction</b><br> |
− | In this Wiki document, | + | In this Wiki document, I have tried to document the steps involved in generating [[Ecore]] using [[XML Schema]], loading an [[XML]] file into an [[Ecore]] model, and so on.<br> |
<ol> | <ol> | ||
<li>Convert a given XSD into Ecore</li> | <li>Convert a given XSD into Ecore</li> | ||
− | <li>Load an XML | + | <li>Load an XML according to the generated Ecore</li> |
− | <li>Display the loaded | + | <li>Display the loaded model using a TreeViewer</li> |
</ol> | </ol> | ||
For this code to execute, you need to include the following plugins as dependencies | For this code to execute, you need to include the following plugins as dependencies | ||
Line 22: | Line 22: | ||
Collection eCorePackages = xsdEcoreBuilder.generate(URI.createFileURI("D:/model/example.xsd")); | Collection eCorePackages = xsdEcoreBuilder.generate(URI.createFileURI("D:/model/example.xsd")); | ||
</pre> | </pre> | ||
− | Note: Running this code could get you a | + | Note: Running this code could get you a NullPointerException or schema FileNotFoundException with running on a 1.4 Sun JVM. This is due to the ever popular Crimsom DOM bug. Please refer to the following link to find a workaround to this problem.[http://www.eclipse.org/modeling/emf/downloads/install.php Manifestation of Crimsom DOM Bug] |
− | [http://www.eclipse.org/modeling/emf/downloads/install.php Manifestation of Crimsom DOM Bug]<br><br> | + | <br><br> |
+ | For these loaded Ecores to be available, we need to register them to the package registry. | ||
+ | <pre> | ||
+ | for (Iterator iter = eCorePackages.iterator(); iter.hasNext();) { | ||
+ | EPackage element = (EPackage) iter.next(); | ||
+ | resourceSet.getPackageRegistry().put(element.getNsURI(), element); | ||
+ | } | ||
+ | </pre> | ||
+ | <br> | ||
<b>Load an XML into the Ecore</b><br> | <b>Load an XML into the Ecore</b><br> | ||
<pre> | <pre> | ||
Line 31: | Line 39: | ||
resource.load(options); | resource.load(options); | ||
</pre> | </pre> | ||
− | Note: The XML | + | Note: The XML file should contain an xsi:schemaLocation. EMF provides this a built-in feature wherein if the XML knows its schema, EMF would build the model from the schema and load the XML without having to perform Step 1.Convert XSD into Ecore. |
+ | <br><br> | ||
+ | If no schemaLocation is specified, at least the namespace should be specified so that it may search for the package in the package registry registered with the same namespace and load the XML according to that Ecore model. | ||
+ | <br><br> | ||
+ | If neither a schemaLocation nor namespace are not provided, XML would be loaded with a default schema provided by EMF which would consider all the nodes as xsd:anyType. | ||
+ | <br><br> | ||
+ | Since some of the XML Schema specific contructs are not represented directly in Ecore, they are represented as Ecore Annotations. These annotations are termed as ExtendedMetaData and to query them we need to use the ExtendedMetaData APIs. Therefore to load an XML which contains the schemaLocation we need to provide the loading option XMLResource.OPTION_EXTENDED_META_DATA | ||
+ | as true as shown in the code above. | ||
+ | <br> | ||
+ | <br> | ||
<b>Display using a TreeViewer</b><br> | <b>Display using a TreeViewer</b><br> | ||
<pre> | <pre> | ||
Line 38: | Line 55: | ||
factories.add(new ReflectiveItemProviderAdapterFactory()); | factories.add(new ReflectiveItemProviderAdapterFactory()); | ||
− | ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(); | + | ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(factories); |
TreeViewer tViewer = new TreeViewer(parent); | TreeViewer tViewer = new TreeViewer(parent); | ||
− | tViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory); | + | tViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); |
− | tViewer.setLabelProvider(new | + | tViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); |
tViewer.setInput(resourceSet); | tViewer.setInput(resourceSet); | ||
</pre> | </pre> | ||
Line 48: | Line 65: | ||
[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> | ||
− | + | [http://www.theserverside.com/tt/articles/article.tss?l=BindingXMLJava Binding XML to Java]<br> | |
--[[User:Annamalai.chockalingam.gmail.com|Annamalai.chockalingam.gmail.com]] 18:06, 10 February 2007 (EST) | --[[User:Annamalai.chockalingam.gmail.com|Annamalai.chockalingam.gmail.com]] 18:06, 10 February 2007 (EST) | ||
+ | |||
+ | |||
+ | [[Category:EMF]] [[Category:MDT]] [[Category:Ecore]] [[Category:XML Schema]] |
Latest revision as of 05:24, 13 December 2010
EMF and XML - An Introduction
In this Wiki document, I have tried to document the steps involved in generating Ecore using XML Schema, loading an XML file into an Ecore model, and so on.
- Convert a given XSD into Ecore
- Load an XML according to the generated Ecore
- 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 schema FileNotFoundException with running on a 1.4 Sun JVM. This is due to the ever popular Crimsom DOM bug. Please refer to the following link to find a workaround to this problem.Manifestation of Crimsom DOM Bug
For these loaded Ecores to be available, we need to register them to the package registry.
for (Iterator iter = eCorePackages.iterator(); iter.hasNext();) { EPackage element = (EPackage) iter.next(); resourceSet.getPackageRegistry().put(element.getNsURI(), element); }
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);
Note: The XML file should contain an xsi:schemaLocation. EMF provides this a built-in feature wherein if the XML knows its schema, EMF would build the model from the schema and load the XML without having to perform Step 1.Convert XSD into Ecore.
If no schemaLocation is specified, at least the namespace should be specified so that it may search for the package in the package registry registered with the same namespace and load the XML according to that Ecore model.
If neither a schemaLocation nor namespace are not provided, XML would be loaded with a default schema provided by EMF which would consider all the nodes as xsd:anyType.
Since some of the XML Schema specific contructs are not represented directly in Ecore, they are represented as Ecore Annotations. These annotations are termed as ExtendedMetaData and to query them we need to use the ExtendedMetaData APIs. Therefore to load an XML which contains the schemaLocation we need to provide the loading option XMLResource.OPTION_EXTENDED_META_DATA
as true as shown in the code above.
Display using a TreeViewer
List factories = new ArrayList(); factories.add(new ResourceItemProviderAdapterFactory()); factories.add(new ReflectiveItemProviderAdapterFactory()); ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(factories); TreeViewer tViewer = new TreeViewer(parent); tViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); tViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory)); tViewer.setInput(resourceSet);
Further Reading
Discover the Eclipse Modeling Framework (EMF) and Its Dynamic Capabilities
Newsgroup Msg11109
Binding XML to Java
--Annamalai.chockalingam.gmail.com 18:06, 10 February 2007 (EST)