advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
advertisement
Average Rating: 3.1/5 | Rate this item | 9 users have rated this item.
Discover the Eclipse Modeling Framework (EMF) and Its Dynamic Capabilities (cont'd)
Generating Dynamic Ecore from XML Schema
As mentioned previously, if your model is an XML Schema but you choose not to generate Java classes, you can dynamically create an Ecore model using the XSDEcoreBuilder. This example uses ipo.xsd:

XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
ResourceSet resourceSet = new ResourceSetImpl();
Collection eCorePackages = 
xsdEcoreBuilder.generate(URI.createFileURI("c:/temp/ipo.xsd"));
advertisement
The generate method returns Ecore packages that are created for each URI in this schema. If the schema imports other namespaces, more than one Ecore package is returned. Each package is registered locally in the resource set used to convert schema. Therefore, if you use the same resource set to load your instance XML document, you don't need to register the packages yourself.

Because XML Schema includes more concepts than Ecore, for example wildcards, EMF uses Ecore EAnnotations to record the mapping to XML Schema. During (de) serialization of the data EMF needs to process these annotations. To ensure these annotations are respected during (de) serialization, you must use the XMLResource.ExtendedMetaData option:


HashMap options = new HashMap();
options.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
// refer http://www.w3.org/TR/2004/PER-xmlschema-0-20040318/#ipo.xml
Resource resource = resourceSet.createResource(URI.createFileURI("c:/temp/ipo.xml"));
resource.load(options);
EMF 2.1 also adds a new capability that allows you to convert schemas to Ecore on the fly while loading an XML document that contains an xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute. It also allows you to load an XML document that has no schema associated with it. To use this functionality you need to register org.eclipse.emf.ecore.xmi.impl.GenericXMLResourceFactoryImpl:

resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml", 
new GenericXMLResourceFactoryImpl());
This article gave you a short introduction to EMF, explaining the core EMF concepts, and provided useful examples on how to exploit the dynamic capabilities of EMF.

Note: The opinions expressed in this paper are those of the authors, not of the IBM Corporation. IBM, alphaWorks, developerWorks, and WebSphere are trademarks of International Business Machines Corporation in the United States, other countries, or both. Rational is a registered trademark of International Business Machines Corporation and Rational Software Corporation, in the United States, other countries or both. Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both. Other company, product and service names may be trademarks or service marks of others.
Previous Page: Using Dynamic EMF Capabilities  
Elena Litani is a software developer working for IBM. She is one of the main contributors to the Eclipse Modeling Framework (EMF) project at Eclipse.org working on implementation of EMF and Service Data Objects (SDO). Previously, Elena was one of the main contributors to the Apache Xerces2 project, working on Xerces2 XML Schema and DOM Level 3 implementations, as well as analyzing and improving performance of the parser.
Ed Merks is the project lead of the Eclipse Modeling Framework open source project at Eclipse.org. He has many years of in-depth experience in the design and implementation of languages, frameworks, and application development environments, including several patents on the subject. Ed is a member of the JAXB expert group, representing IBM. He holds a Ph.D in computer science and is a co-author of the authoritative 'Eclipse Modeling Framework, A Developer's Guide' (Addison-Wesley 2003).
Dave Steinberg has been a committer on EMF since its launch at Eclipse.org in 2002, contributing to the design and implementation of the framework and tools. He is a developer in IBM's Software Group and a co-author of the authoritative EMF book 'Eclipse Modeling Framework, A Developer's Guide' (Addison-Wesley 2003).
Page 1: IntroductionPage 3: Generating Dynamic Ecore from XML Schema
Page 2: Using Dynamic EMF Capabilities 
Please rate this item (5=best)
 1  2  3  4  5
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs