Using JET Transformations with EMF Documents

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

Loading EMF Documents

To force JET to load an EMF document, the transformation information must be modified in the transformations plugin.xml. Follow the following steps:

XPath expressions against EMF documents

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

If a JET transform loads an ECore model, then the following XPath expressions would return the following results:

XPath expression Java equivalent
/contents Resource.getContents()
/EPackage subset of Resource.getContents() of type EPackage
$ePackage/eClassifiers (ePackage is an EPackage instance) ePackage.getEClassifiers()
$ePackage/EClass 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 --%>
<%@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 EMF EMF's ResourceSetImpl createResource() method to determine EMF-based models. JET 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

EMF documents my by loaded using the <c:load> and <c:loadContent> tags. The former loads a document from an file, while the second loads the document by parsing a text string.

The following loads the mymodel.ecore file from the transformation:

<c:load url="mymodel.ecore" var="myEcore"/>

This is equivalent to:

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

The following example loads the plugin.xml document from a project 'myproject' in the Eclipse workspace.

<c:load url="myproject/mymodel.ecore" urlContext="workspace" 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.