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



























































































































































































































                                                                                                                              
<?xml version='1.0'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>

  <head>

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

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

  </head>

  <body>

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

    <ul>

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

    </ul>

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

    <ul>

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

      <li>Each 
      <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>

      <li>Each 
      <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>

    </ul>

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

    <table>

      <thead>

        <tr>

          <th>XPath expression</th>

          <th>Java equivalent</th>

        </tr>

      </thead>

      <tbody>

        <tr>

          <td><code>/contents</code></td>

          <td><code>Resource.getContents()</code></td>

        </tr>

        <tr>

          <td><code>/EPackage</code></td>

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

        </tr>

        <tr>

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

          <td><code>ePackage.getEClassifiers()</code></td>

        </tr>

        <tr>

          <td><code>$ePackage/EClass</code></td>

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

        </tr>

        <tr>

          <td><code>$ePackage/@name</code></td>

          <td><code>ePackage.getName()</code></td>

        </tr>

      </tbody>

    </table>

    <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;
&lt;c:log&gt; 
EPackage: &lt;c:get select=&quot;$ePackage/@name&quot;/&gt;
&lt;c:iterate select=&quot;$ePackage/eClassifiers&quot; var=&quot;eClassifier&quot;&gt; 
&lt;%
EClassifier ec = (EClassifier)context.getVariable(&quot;eClassifier&quot;);
%&gt;
  EClassifier: &lt;c:get select=&quot;$eClassifier/@name&quot;/&gt;. Really it&apos;s &lt;%= ec.getName() %&gt;
&lt;/c:iterate&gt; 
&lt;/c:log&gt;</pre>
    <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 
    instances.</p>

    <p>The Model loader id for EMF document loading is 
    <code>org.eclipse.jet.emf</code>.</p>

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

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

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

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

    <p><code>&lt;c:load url=&quot;mymodel.ecore&quot; 
    urlContext=&quot;transform&quot; 
    loader=&quot;org.eclipse.jet.emf&quot; 
    var=&quot;myEcore&quot;/&gt;</code></p>

    <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; 
    urlContext=&quot;workspace&quot; 
    var=&quot;myEcore&quot;/&gt;</code></p>

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

  </body>

</html>

Back to the top