Skip to main content
summaryrefslogtreecommitdiffstats
blob: c9e83aa7a36a122539515e690769de5c55d30ba0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?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 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.</p>

    <h3>Loading EMF Documents</h3>

    <p>To enable a JET Transformation to load an EMF document, the transformation&#39;s plugin.xml 
    must be modified. 
    Do the following:</p>

    <ol>

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

      <li>Click on the <strong>Extensions</strong> tab.</li>

      <li>Expand the <strong>org.eclipse.jet.transform</strong> element in 
      the <strong>All Extensions</strong> tree. This will reveal the 
      <strong>(transform)</strong> child element.</li>

      <li>Click on the <strong>(transform)</strong> element.</li>
      <li>In the <strong>modelLoader</strong>
      field, enter <code>org.eclipse.jet.emf</code> .</li>

    </ol>

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

    <p>The JET XPath processor handles EMF documents by mapping the document&#39;s  
    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 <i>;root</i> object. 
      The XPath expression <code>/</code> refers to the Resource 
      containing the input model.</li>

      <li>The Resource contents, as returned by the  
      <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 
      <i>contents</i>. 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. 
      If <code>$class</code> refers to an EObject with an EReference <code>ownedAttributes</code>, then
      <code>$class/ownedAttributes</code> will return all the elements in that collection.
      </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.
      If <code>$class</code> refers to an EObject with an EAttribute <code>name</code>, then
      <code>$class/@name</code> will return the value of this attribute.
      </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.
      For example, if the <code>$class</code> refers to an EObject that contains instances of an EClass <code>Property</code>
      in any of its reference features, the <code>$class/Property</code> will return those objects.
      </li>

    </ul>

   <h3>Example: XPath expressions against .ecore files</h3>

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

    <table border="1">

      <thead align="left">

        <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>the 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>the 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, needed for Java expressions --%&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&#39;s &lt;%= ec.getName() %&gt;
&lt;/c:iterate&gt; 
&lt;/c:log&gt;</pre>

    <h3>Details of EMF Document loading</h3>

    <p>JET uses the EMF method ResourceSetImpl.createResource()  
    to create a Resource implementation capable of loading themode.
    This requires the model&#39;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>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 
    <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 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.</p>

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

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

    <p>The following example loads the file mymodel.ecore from a workspace
    project myproject.</p>

    <p><code>&lt;c:load url=&quot;myproject/mymodel.ecore&quot; 
    urlContext=&quot;workspace&quot; 
    loader=&quot;org.eclipse.jet.emf&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