Skip to main content
blob: d4a4ba8dba5ec3cca95500382877195f1afd014a (plain) (tree)

<?xml version='1.0'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">


    <title>Using JET Transformations with EMF Documents</title>

    <link charset="ISO-8859-1" href="../book.css" rel="STYLESHEET"



    <h2>Using JET Transformations with EMF Documents</h2>

    <p>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.</p>

    <h3>Loading EMF Documents</h3>

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


      <li>Open the plugin.xml file in the root of the JET 
      transformation project.</li>

      <li>Switch to the &apos;Extensions&apos; tab.</li>

      <li>Find the &apos;org.eclipse.jet.transform&apos; extension in 
      the &apos;All Extensions&apos; tree, and expand it to reveal the 
      &apos;transform&apos; child element.</li>

      <li>Select the &apos;transform&apos; element, and enter 
      <code>org.eclipse.jet.xml</code> in the &apos;modelLoader&apos; 


    <h3>XPath expressions against EMF documents</h3>

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


      <a href="PLUGINS_ROOT/org.eclipse.emf.doc/references/javadoc/org/eclipse/emf/ecore/resource/Resource.html"
      >Resource</a> objects map to the infoset &apos;root&apos; object. 
      The XPath expression <code>/</code> refers to the Resource 
      containing the input model.</li>

      <li>The Resource 
      <a href="PLUGINS_ROOT/org.eclipse.emf.doc/references/javadoc/org/eclipse/emf/ecore/resource/Resource.html#getContents()"
      >getContents()</a> method maps to an element 
      &apos;contents&apos;. The XPath expression <code>/contents</code> 
      returns the results of this method.</li>

      <a href="PLUGINS_ROOT/org.eclipse.emf.doc/references/javadoc/org/eclipse/emf/ecore/EReference.html"
      >EReference</a> feature on an object is mapped to an element with 
      the same name.</li>

      <a href="PLUGINS_ROOT/org.eclipse.emf.doc/references/javadoc/org/eclipse/emf/ecore/EAttribute.html"
      >EAttribute</a> feature on an object is mapped to an attribute 
      with the same name.</li>

      <li>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.</li>


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




          <th>XPath expression</th>

          <th>Java equivalent</th>










          <td>subset of <code>Resource.getContents()</code> of type 



          <td><code>$ePackage/eClassifiers</code> (ePackage is an 
          EPackage instance)</td>





          <td>subset of <code>ePackage.eContents()</code> of type 








    <h3>Accessing XPath Variables from Java code</h3>

    <p>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</p>

    <pre>&lt;%-- contents of main.jet that reads handles a .ecore file --%&gt;
&lt;%-- import the ecore namespace --%&gt;
&lt;%@jet imports=&quot;org.eclipse.emf.ecore.*&quot;%&gt;

&lt;c:setVariable var=&quot;ePackage&quot; select=&quot;/contents&quot;/&gt; 

&lt;%-- write to the JET execution console --%&gt;
EPackage: &lt;c:get select=&quot;$ePackage/@name&quot;/&gt;
&lt;c:iterate select=&quot;$ePackage/eClassifiers&quot; var=&quot;eClassifier&quot;&gt; 
EClassifier ec = (EClassifier)context.getVariable(&quot;eClassifier&quot;);
  EClassifier: &lt;c:get select=&quot;$eClassifier/@name&quot;/&gt;. Really it&apos;s &lt;%= ec.getName() %&gt;
    <h3>Details of EMF Document loading</h3>

    <p>JET uses EMF EMF&apos;s ResourceSetImpl createResource() method 
    to determine EMF-based models. JET requires the model&apos;s 
    EPackage is registered with EMF in order to load model 

    <p>The Model loader id for EMF document loading is 

    <h3>Loading EMF documents during transformation execution</h3>

    <p>EMF documents my by loaded using the 
    <a href="PLUGINS_ROOT/org.eclipse.jet.doc/references/taglibs/controlTags/loadTag.html"
    >&lt;c:load&gt;</a> and 
    <a href="PLUGINS_ROOT/org.eclipse.jet.doc/references/taglibs/controlTags/loadContentTag.html"
    >&lt;c:loadContent&gt;</a> tags. The former loads a document from 
    an file, while the second loads the document by parsing a text 

    <p>The following loads the mymodel.ecore file from the 

    <p><code>&lt;c:load url=&quot;mymodel.ecore&quot; 

    <p>This is equivalent to:</p>

    <p><code>&lt;c:load url=&quot;mymodel.ecore&quot; 

    <p>The following example loads the plugin.xml document from a 
    project &apos;myproject&apos; in the Eclipse workspace.</p>

    <p><code>&lt;c:load url=&quot;myproject/mymodel.ecore&quot; 

    <p>Finally, while it is possible to load ecore models with 
    <a href="PLUGINS_ROOT/org.eclipse.jet.doc/references/taglibs/controlTags/loadContentTag.html">
    &lt;c:loadContent&gt;</a>, the format of Ecore models often makes 
    this impractical to embed a document within a JET template.</p>



Back to the top