Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5f790d7ad4663277d96f9278c7b636cc52a8eff5 (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
<?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.emf</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