/[volute]/trunk/projects/theory/snapdm/res/xmiPreProcessTool.xsl
ViewVC logotype

Contents of /trunk/projects/theory/snapdm/res/xmiPreProcessTool.xsl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 126 - (show annotations)
Mon Mar 31 19:41:40 2008 UTC (12 years, 7 months ago) by gerard.lemson
File MIME type: application/xml
File size: 11143 byte(s)
Clean up after Laurent and I decided to concentrate on the pure XSLT way of generating code. This does require Saxon to be able to use the xsl:result-document syntax to output multiple files.
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!DOCTYPE stylesheet [
4 <!ENTITY cr "<xsl:text>
5 </xsl:text>">
6 ]>
7
8 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
9 xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
10 xmlns:uml="http://schema.omg.org/spec/UML/2.0">
11
12 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
13 <xsl:strip-space elements="*" />
14
15 <!-- xml index on xml:id -->
16 <xsl:key name="classid" match="*" use="@xmi:id"/>
17
18 <xsl:variable name="dmmd_namespace" select="'http://ivoa.org/theory/datamodel/generationmetadata/v0.1'"/>
19 <xsl:variable name="xmi_namespace" select="'http://schema.omg.org/spec/XMI/2.1'"/>
20 <xsl:variable name="uml_namespace" select="'http://schema.omg.org/spec/UML/2.0'"/>
21
22 <!-- main -->
23 <xsl:template match="/">
24 <xsl:choose>
25 <xsl:when test="namespace-uri(/*) != 'http://schema.omg.org/spec/XMI/2.1'">
26 <ERROR>Wrong namespace: this script can convert only XMI v2.1</ERROR>
27 </xsl:when>
28 <xsl:otherwise>
29 <xsl:apply-templates select="*/uml:Model" />
30 </xsl:otherwise>
31 </xsl:choose>
32 </xsl:template>
33
34 <!-- filters uml:Model : process only uml:Package nodes -->
35 <xsl:template match="uml:Model">
36 <xsl:element name="model">
37 <xsl:element name="name"><xsl:value-of select="@name"/></xsl:element>
38 <xsl:element name="description"><xsl:value-of select="./ownedComment/@body"/></xsl:element>
39 <xsl:apply-templates select="./*[@xmi:type='uml:PrimitiveType']">
40 <xsl:sort select="@name"/>
41 </xsl:apply-templates>
42 <xsl:apply-templates select="./*[@xmi:type='uml:Package']"/>
43 </xsl:element>
44 </xsl:template>
45
46
47
48 <!-- filters uml:Package : process uml:DataType, uml:Enumeration, uml:Class nodes -->
49 <xsl:template match="*[@xmi:type='uml:Package']">
50
51 <!-- check if a name is defined -->
52 <xsl:if test="count(@name) > 0">
53 <!-- explicitly process only datatypes, enumeration, class -->
54
55 <!-- later : compute full package name -->
56 <xsl:element name="package">
57 <xsl:element name="name">
58 <xsl:value-of select="@name"/>
59 </xsl:element>
60 <xsl:apply-templates select="ownedComment"/>
61
62 <xsl:if test="count(./*[@xmi:type='uml:DataType']) > 0">
63 <xsl:comment>Data types</xsl:comment>&cr;
64 <xsl:apply-templates select="./*[@xmi:type='uml:DataType']">
65 </xsl:apply-templates>
66 </xsl:if>
67
68 <xsl:if test="count(./*[@xmi:type='uml:Enumeration']) > 0">
69 <xsl:comment>Enumerations</xsl:comment>&cr;
70 <xsl:apply-templates select="./*[@xmi:type='uml:Enumeration']">
71 </xsl:apply-templates>
72 </xsl:if>
73
74 <xsl:if test="count(./*[@xmi:type='uml:Class']) > 0">
75 <xsl:comment>Classes</xsl:comment>&cr;
76 <xsl:apply-templates select="./*[@xmi:type='uml:Class']">
77 </xsl:apply-templates>
78 </xsl:if>
79
80 <xsl:apply-templates select="./*[@xmi:type='uml:Package']"/>
81
82 </xsl:element>
83 </xsl:if>
84 </xsl:template>
85
86 <xsl:template match="*[@xmi:type='uml:Class']">
87 <xsl:element name="objectType">
88 <xsl:if test="@isAbstract">
89 <xsl:attribute name="abstract">
90 <xsl:text>true</xsl:text>
91 </xsl:attribute>
92 </xsl:if>
93 <xsl:element name="name">
94 <xsl:value-of select="@name"/>
95 </xsl:element>
96 <xsl:apply-templates select="ownedComment"/>
97 <xsl:if test="*[@xmi:type='uml:Generalization']">
98 <xsl:apply-templates select="*[@xmi:type='uml:Generalization']"/>
99 </xsl:if>
100
101 <xsl:apply-templates select=".//*[@xmi:type='uml:Property' and not(@association)]" mode="attributes"/>
102 <xsl:apply-templates select=".//*[@xmi:type='uml:Property' and @association and @aggregation='composite']" mode="collections"/>
103 <xsl:apply-templates select=".//*[@xmi:type='uml:Property' and @association and (not(@aggregation) or @aggregation='shared')]" mode="references"/>
104
105 </xsl:element>
106 </xsl:template>
107
108 <xsl:template match="*[@xmi:type='uml:DataType']">
109 <xsl:element name="dataType">
110 <xsl:if test="@isAbstract">
111 <xsl:attribute name="abstract">
112 <xsl:text>true</xsl:text>
113 </xsl:attribute>
114 </xsl:if>
115 <xsl:element name="name">
116 <xsl:value-of select="@name"/>
117 </xsl:element>
118 <xsl:apply-templates select="ownedComment"/>
119 <xsl:if test="*[@xmi:type='uml:Generalization']">
120 <xsl:apply-templates select="*[@xmi:type='uml:Generalization']"/>
121 </xsl:if>
122
123 <xsl:apply-templates select=".//*[@xmi:type='uml:Property' and not(@association)]" mode="attributes"/>
124
125 </xsl:element>
126 </xsl:template>
127
128 <xsl:template match="*[@xmi:type='uml:Generalization']">
129 <xsl:element name="extends">
130 <xsl:call-template name="get-class-from-id">
131 <xsl:with-param name="id">
132 <xsl:value-of select="@general"/>
133 </xsl:with-param>
134 </xsl:call-template>
135 </xsl:element>
136 </xsl:template>
137
138 <xsl:template match="*[@xmi:type='uml:Enumeration']">
139 <xsl:element name="enumeration">
140 <xsl:element name="name">
141 <xsl:value-of select="@name"/>
142 </xsl:element>
143 <xsl:apply-templates select="ownedComment"/>
144 <xsl:apply-templates select="*[@xmi:type='uml:EnumerationLiteral']"/>
145 </xsl:element>
146 </xsl:template>
147
148 <xsl:template match="*[@xmi:type='uml:EnumerationLiteral']">
149 <xsl:element name="literal">
150 <xsl:element name="value">
151 <xsl:value-of select="@name"/>
152 </xsl:element>
153 <xsl:apply-templates select="ownedComment"/>
154 </xsl:element>
155 </xsl:template>
156
157 <xsl:template match="ownedComment">
158 <xsl:element name="description">
159 <xsl:value-of select="@body"/>
160 </xsl:element>
161 </xsl:template>
162
163 <xsl:template match="*[@xmi:type='uml:Property']" mode="attributes">
164 <xsl:element name="attribute">
165 <xsl:apply-templates select="." mode="properties"/>
166 </xsl:element>
167 </xsl:template>
168
169 <xsl:template match="*[@xmi:type='uml:Property']" mode="references">
170 <xsl:element name="reference">
171 <xsl:apply-templates select="." mode="properties"/>
172 </xsl:element>
173 </xsl:template>
174
175 <xsl:template match="*[@xmi:type='uml:Property']" mode="collections">
176 <xsl:element name="collection">
177 <xsl:apply-templates select="." mode="properties"/>
178 </xsl:element>
179 </xsl:template>
180
181 <xsl:template match="*[@xmi:type='uml:Property']" mode="properties">
182 <xsl:variable name="id" select="key('classid',@type)"/>
183 <xsl:element name="name">
184 <xsl:value-of select="@name"/>
185 </xsl:element>
186 <xsl:apply-templates select="ownedComment"/>
187 <xsl:call-template name="get-class-from-id">
188 <xsl:with-param name="id">
189 <xsl:value-of select="@type"/>
190 </xsl:with-param>
191 </xsl:call-template>
192 <xsl:element name="multiplicity">
193 <xsl:call-template name="multiplicity">
194 <xsl:with-param name="lower" select="lowerValue/@value"/>
195 <xsl:with-param name="upper" select="upperValue/@value"/>
196 </xsl:call-template>
197 </xsl:element>
198 </xsl:template>
199
200
201 <xsl:template name="multiplicity">
202 <xsl:param name="lower"/>
203 <xsl:param name="upper"/>
204 <xsl:variable name="u">
205 <xsl:choose>
206 <xsl:when test="not($upper)">
207 <xsl:value-of select="'1'"/>
208 </xsl:when>
209 <xsl:when test="$upper = -1">
210 <xsl:value-of select="'*'"/>
211 </xsl:when>
212 <xsl:otherwise>
213 <xsl:value-of select="$upper"/>
214 </xsl:otherwise>
215 </xsl:choose>
216 </xsl:variable>
217 <xsl:variable name="l">
218 <xsl:choose>
219 <xsl:when test="not($lower)">
220 <xsl:choose>
221 <xsl:when test="not($upper)">
222 <xsl:value-of select="'0'"/>
223 </xsl:when>
224 <xsl:otherwise>
225 <xsl:value-of select="$u"/>
226 </xsl:otherwise>
227 </xsl:choose>
228 </xsl:when>
229 <xsl:otherwise>
230 <xsl:value-of select="$lower"/>
231 </xsl:otherwise>
232 </xsl:choose>
233 </xsl:variable>
234 <xsl:choose>
235 <xsl:when test="$l = $u">
236 <xsl:value-of select="$l"/>
237 </xsl:when>
238 <xsl:otherwise>
239 <xsl:value-of select="$l"/>..<xsl:value-of select="$u"/>
240 </xsl:otherwise>
241 </xsl:choose>
242 </xsl:template>
243
244 <!-- resolve class type for both data types (primitive or specific) and classes -->
245 <xsl:template name="get-class-from-id">
246 <xsl:param name="id"/>
247 <xsl:variable name="c" select="key('classid',$id)"/>
248 <xsl:element name="datatype">
249 <xsl:choose>
250 <xsl:when test="$c/@xmi:type = 'uml:PrimitiveType'">
251 <xsl:attribute name="type">
252 <xsl:value-of select="$c/@name"/>
253 </xsl:attribute>
254 </xsl:when>
255 <xsl:otherwise>
256 <xsl:attribute name="package">
257 <xsl:call-template name="full-path">
258 <xsl:with-param name="id">
259 <xsl:value-of select="$c/../@xmi:id"/>
260 </xsl:with-param>
261 <xsl:with-param name="delimiter">
262 <xsl:value-of select="'.'"/>
263 </xsl:with-param>
264
265 </xsl:call-template>
266 </xsl:attribute>
267 <xsl:attribute name="class">
268 <xsl:value-of select="$c/@name"/>
269 </xsl:attribute>
270 </xsl:otherwise>
271 </xsl:choose>
272 </xsl:element>
273 </xsl:template>
274
275 <!-- ==================================================================================== -->
276 <!-- print the full path up to the specified package and append the specified suffix -->
277 <!-- ==================================================================================== -->
278 <xsl:template name="full-path">
279 <xsl:param name="id"/>
280 <xsl:param name="delimiter"/>
281 <xsl:param name="suffix"/>
282
283 <xsl:variable name="package" select="key('classid',$id)"/>
284 <xsl:variable name="path">
285 <xsl:choose>
286 <xsl:when test="$suffix">
287 <xsl:value-of select="concat($package/@name,$delimiter,$suffix)"/>
288 </xsl:when>
289 <xsl:otherwise>
290 <xsl:value-of select="$package/@name"/>
291 </xsl:otherwise>
292 </xsl:choose>
293 </xsl:variable>
294
295
296 <!-- determine whether parent is a Package or not, if not, this is the root package which should be ignored. -->
297 <xsl:variable name="parent" select="$package/..[@xmi:type='uml:Package']"/>
298
299 <xsl:choose>
300 <xsl:when test="not($parent)">
301 <xsl:value-of select="$path"/>
302 </xsl:when>
303 <xsl:otherwise>
304 <xsl:call-template name="full-path">
305 <xsl:with-param name="id" select="$parent/@xmi:id"/>
306 <xsl:with-param name="delimiter" select="$delimiter"/>
307 <xsl:with-param name="suffix" select="$path"/>
308 </xsl:call-template>
309 </xsl:otherwise>
310 </xsl:choose>
311 </xsl:template>
312 </xsl:stylesheet>

msdemlei@ari.uni-heidelberg.de
ViewVC Help
Powered by ViewVC 1.1.26