## Using JET Transformations with EMF Documents

By default, JET transformations expect an XML document as input. However, JET transformations can be modified to load EMF-based models, too. This section describes how to load EMF documents with JET and how JET accesses the models.

### Loading EMF Documents

To enable a JET Transformation to load an EMF document, the transformation's plugin.xml must be modified. Do the following:

1. Open the plugin.xml file in the root of the JET transformation project.
2. Click on the Extensions tab.
3. Expand the org.eclipse.jet.transform element in the All Extensions tree. This will reveal the (transform) child element.
4. Click on the (transform) element.
5. In the modelLoader field, enter `org.eclipse.jet.emf` .

### XPath expressions against EMF documents

The JET XPath processor handles EMF documents by mapping the document's meta-model to the XPath infoset model as follows:

- Resource objects map to the infoset ;root object. The XPath expression `/` refers to the Resource containing the input model.
- The Resource contents, as returned by the getContents() method, maps to an element contents. The XPath expression `/contents` returns the results of this method.
- Each EReference feature on an object is mapped to an element with the same name. If `$class` refers to an EObject with an EReference `ownedAttributes`, then `$class/ownedAttributes` will return all the elements in that collection.
- Each EAttribute feature on an object is mapped to an attribute with the same name. If `$class` refers to an EObject with an EAttribute `name`, then `$class/@name` will return the value of this attribute.
- When evaluating XPath child-axis expressions, JET will first attempt to identify an feature with the requested name. If no feature is found, then the contained children of the element are searched for instances of an EClass with the specified name. For example, if the `$class` refers to an EObject that contains instances of an EClass `Property` in any of its reference features, the `$class/Property` will return those objects.

### Example: XPath expressions against .ecore files

If a JET transform loads an ECore model (a .ecore file), then the following XPath expressions would return the following results:

XPath expression | Java equivalent
--- | ---
`/contents` | `Resource.getContents()`
`/EPackage` | the subset of `Resource.getContents()` of type `EPackage`
`$ePackage/eClassifiers` (ePackage is an EPackage instance) | `ePackage.getEClassifiers()`
`$ePackage/EClass` | the subset of `ePackage.eContents()` of type `EClass`
`$ePackage/@name` | `ePackage.getName()`

### Accessing XPath Variables from Java code

JET templates may contain Java code, and this Java code may access XPath variables. This is particularly valuable when the input model is EMF-based. The following examples handles hypothetical ECORE model loaded by a JET transform, and accesses the model with both XPath and Java expressions

    <%-- contents of main.jet that reads handles a .ecore file --%>
    <%-- import the ecore namespace, needed for Java expressions --%>
    <%@jet imports="org.eclipse.emf.ecore.*"%>

    <c:setVariable var="ePackage" select="/contents"/>

    <%-- write to the JET execution console --%>
    <c:log>
    EPackage: <c:get select="$ePackage/@name"/>
    <c:iterate select="$ePackage/eClassifiers" var="eClassifier">
    <%
    EClassifier ec = (EClassifier)context.getVariable("eClassifier");
    %>
      EClassifier: <c:get select="$eClassifier/@name"/>. Really it's <%= ec.getName() %>
    </c:iterate>
    </c:log>

### Details of EMF Document loading

JET uses the EMF method ResourceSetImpl.createResource() to create a Resource implementation capable of loading themode. This requires the model's EPackage is registered with EMF in order to load model instances.

The Model loader id for EMF document loading is `org.eclipse.jet.emf`.

### Loading EMF documents during transformation execution

The launch framework automatically handles the loading of EMF resources passed to a JET Transformation as input. If required, JET templates can load other EMF-based documents during execution. The <c:load> and <c:loadContent> tags may be used for this purpose. The former loads a document from an file, while the second loads the document by parsing the content of the c:loadContent tag.

The following loads the mymodel.ecore file that is part of the transformation plug-in:

`<c:load url="mymodel.ecore" loader="org.eclipse.jet.emf" var="myEcore"/>`

The following example loads the file mymodel.ecore from a workspace project myproject.

`<c:load url="myproject/mymodel.ecore" urlContext="workspace" loader="org.eclipse.jet.emf" var="myEcore"/>`

Finally, while it is possible to load ecore models with <c:loadContent>, the format of Ecore models often makes this impractical to embed a document within a JET template.