Exercise 5. Dynamic EMF

What This Exercise Is About

Using the Purchase Order schema from Exercise 1 and 2, you will now use the new XMLProcessor API to dynamically load conformant XML documents.

Primer PO model


What You Should Be Able To Do

At the end of the lab, you should be able to:

Required Materials

General Advice / Warnings

Exercise Instructions

This exercise is carried out entirely using the Eclipse Software Development Kit (SDK) version 3.2 with the Eclipse Modeling Framework (EMF) 2.2 installed into it. The exercise instructions refer to this product as either Eclipse or as "the workbench."

In your workspace, there should be a EMF_Workshop/Exercise5_Dynamic_EMF folder containing Exercise5.java and XMLSchemaDynamicProcessing.java. Exercise5.java is the class you will modify to perform the work noted above. XMLSchemaDynamicProcessing.java is an alternate solution, using older EMF API, to further demonstrate the speed and simplicity of the new XMLProcessor API. This API is only available in EMF 2.2 (and beyond) - in the past, a more cumbersome solution was required to accomplish the same task.


Directions

Step A: Setup

  1. You will need to complete Exercise 2 before proceeding with this exercise. The solution can also be copied from the Solution2 folder. Use the Navigator view for copying, so as to include the .project and .classpath files.
  2. Switch to the Java perspective, if not already there.
    1. Select Window -> Open Perspective -> Java.
  3. Copy PurchaseOrder.xsd from the Exercise 1 source folder into Exercises/data.
  4. Copy java code from the Exercise 5 source folder into Exercises/src/exercises.

Step B: Load Schema And Instance Document

  1. Open Exercise5.java, in Exercises/src/exercises.
  2. Work your way through the exercise by filling in the missing code for each of the TODO comments. As before, if you run into trouble or are pressed for time, you can copy code from the JPages folder under the Exercise 5 source folder.
  3. Note also that there is more than one way to accomplish this task. The solution presented in the JPages folder is the recommended approach; however, the solution presented in the Solution5 folder provides a second way.
  4. Run your code (SHIFT-ALT-X, J).
  5. You should see the following output:

      name: Scarlet O'Hara
      street: 321 Backwoods Lane
      city: Louisville
      state: AL
      zip: 67655
      <?xml version="1.0" encoding="UTF-8"?>
      <po:order xmlns:po="http://www.example.com/po" orderDate="2006-03-20">
        <shipTo>
          <name>Scarlet O'Hara</name>
          <street>321 Backwoods Lane</street>
          <city>Louisville</city>
          <state>AL</state>
          <zip>67655</zip>
        </shipTo>
        <billTo>
          <name>Rhett Butler</name>
          <street>123 Iditarod Lane</street>
          <city>Nome</city>
          <state>AK</state>
          <zip>34582</zip>
        </billTo>
        <po:comment>For Scarlet's Birthday</po:comment>
        <items partNum="SWH123456">
          <productName>Wireless Headphones</productName>
          <quantity>2</quantity>
          <price>75.0</price>
          <po:comment>Backordered</po:comment>
          <shipDate>2006-03-23</shipDate>
        </items>
        <items partNum="STV999876">
          <productName>Plasma Television</productName>
          <quantity>1</quantity>
          <price>499.0</price>
          <po:comment>Fragile</po:comment>
          <shipDate>2006-03-23</shipDate>
        </items>
      </po:order>

  6. Note that with this new dynamic implementation, we no longer need to include com.example.po in our .classpath file. Additionally, since no Change Recorder is used in this exercise, org.eclipe.emf.ecore.change can also be omitted. If you switch to the Navigator view, you can simplify your .classpath to the following.
  7.   <?xml version="1.0" encoding="UTF-8"?>
      <classpath>
        <classpathentry kind="src" path="src"/>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
        <classpathentry kind="src" path="/org.eclipse.emf.common"/>
        <classpathentry kind="src" path="/org.eclipse.emf.ecore"/>
        <classpathentry kind="src" path="/org.eclipse.emf.ecore.xmi"/>
        <classpathentry kind="src" path="/org.eclipse.xsd"/>
        <classpathentry kind="output" path="bin"/>
      </classpath>

    Bear in mind, however, that you will get errors in Exercise2.java, Exercise3.java and Exercise4.java, since they will now be missing dependencies. If Eclipse offers a warning stating "Errors exist in required project(s): Exercises. Continue launch?", you can safely hit Yes to continue. In order to re-run Exercises 2 - 4, you will have to undo this step.


Summary

In Exercise 2, we used the static API (generated model code) to load data from a model instance document. With the XMLProcessor API, you no longer need to have generated all that model code - you can now accomplish the same tasks dynamically using nothing more than 4 EMF plugins and a schema document.