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

Contents of /trunk/projects/theory/snapdm/res/deprecated20080331/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/octet-stream
File size: 9034 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:uml="http://schema.omg.org/spec/UML/2.0"
10 xmlns:xmi="http://schema.omg.org/spec/XMI/2.1">
11
12
13 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
14 <xsl:strip-space elements="*" />
15
16 <!-- xml index on xml:id -->
17 <xsl:key name="classid" match="*" use="@xmi:id"/>
18
19
20 <!-- main -->
21 <xsl:template match="/">
22 <xsl:choose>
23 <xsl:when test="namespace-uri(/*) != 'http://schema.omg.org/spec/XMI/2.1'">
24 <ERROR>Wrong namespace: this script can convert only XMI v2.1</ERROR>
25 </xsl:when>
26 <xsl:otherwise>
27 <xsl:apply-templates select="xmi:XMI" mode="fullCopy"/>
28 </xsl:otherwise>
29 </xsl:choose>
30 </xsl:template>
31
32
33
34 <!-- special trick : copy node with all namespaces for xmi:XMI -->
35 <xsl:template match="@*|node()" mode="fullCopy">
36 <xsl:choose>
37 <xsl:when test="name() = 'uml:Model'">
38 <xsl:apply-templates select="."/>
39 </xsl:when>
40 <xsl:otherwise>
41 <xsl:copy>
42 <xsl:apply-templates select="@*|node()" mode="fullCopy"/>
43 </xsl:copy>
44 </xsl:otherwise>
45 </xsl:choose>
46 </xsl:template>
47
48
49
50 <!-- filters uml:Model : process only uml:Package nodes -->
51 <xsl:template match="uml:Model">
52 <xsl:comment> uml model template</xsl:comment>
53 <xsl:apply-templates select="*[@xmi:type='uml:Package']"/>
54 </xsl:template>
55
56
57
58 <!-- filters uml:Package : process uml:DataType, uml:Enumeration, uml:Class nodes -->
59 <xsl:template match="*[@xmi:type='uml:Package']">
60
61 <!-- check if a name is defined -->
62 <xsl:if test="count(@name) > 0">
63 <!-- explicitly process only datatypes, enumeration, class -->
64 <!-- <xsl:element name="{name()}">
65 <xsl:apply-templates select="@xmi:type|@name" mode="copyAttr"/>
66 -->
67 <!-- later : compute full package name -->
68 <xsl:variable name="path" select="@name"/>
69
70 <xsl:comment>&cr;Package : <xsl:value-of select="$path"/>&cr;</xsl:comment>
71
72 <xsl:apply-templates select=".//*[@xmi:type='uml:PrimitiveType']">
73 <xsl:sort select="@name"/>
74 </xsl:apply-templates>
75
76 <xsl:if test="count(.//*[@xmi:type='uml:DataType']) > 0">
77 <xsl:comment>Data types</xsl:comment>
78 <xsl:apply-templates select=".//*[@xmi:type='uml:DataType']" mode="specialCopy">
79 <xsl:with-param name="package" select="$path"/>
80 </xsl:apply-templates>
81 </xsl:if>
82
83 <xsl:if test="count(.//*[@xmi:type='uml:Enumeration']) > 0">
84 <xsl:comment>Enumerations</xsl:comment>
85 <xsl:apply-templates select=".//*[@xmi:type='uml:Enumeration']" mode="specialCopy">
86 <xsl:with-param name="package" select="$path"/>
87 </xsl:apply-templates>
88 </xsl:if>
89
90 <xsl:if test="count(.//*[@xmi:type='uml:Class']) > 0">
91 <xsl:comment>Classes</xsl:comment>
92 <xsl:apply-templates select=".//*[@xmi:type='uml:Class']" mode="specialCopy">
93 <xsl:with-param name="package" select="$path"/>
94 </xsl:apply-templates>
95 </xsl:if>
96
97 <!-- </xsl:element> -->
98 </xsl:if>
99 </xsl:template>
100
101
102
103 <!-- recursive special copy : removes xmi:id, visibility attributes and processes Generalization & children nodes -->
104 <!-- creates a class node for uml:DataType, uml:Enumeration, uml:Class nodes -->
105 <xsl:template match="node()" mode="specialCopy">
106 <xsl:param name="package"/>
107
108 <xsl:choose>
109 <!-- types -->
110 <xsl:when test="@xmi:type = 'uml:Class' or @xmi:type = 'uml:Enumeration' or @xmi:type = 'uml:DataType'">
111 <bean>
112 <xsl:apply-templates select="@*" mode="filterAttr" />
113 <xsl:attribute name="package"><xsl:value-of select="$package"/></xsl:attribute>
114 <!-- comment is needed for MappingTool java class to parse text fragments : <bean>...</bean> -->
115 <xsl:comment>Class <xsl:value-of select="$package"/>.<xsl:value-of select="@name"/></xsl:comment>
116 <xsl:apply-templates select="child::*" mode="specialCopy">
117 <xsl:with-param name="package" select="$package"/>
118 </xsl:apply-templates>
119 </bean>
120 </xsl:when>
121
122 <!-- specific rules -->
123 <xsl:when test="@xmi:type = 'uml:Generalization'">
124 <xsl:apply-templates select="."/>
125 </xsl:when>
126 <xsl:when test="@xmi:type = 'uml:Comment'">
127 <description><xsl:value-of select="@body"/></description>
128 </xsl:when>
129
130 <!-- specific to uml:Class -->
131 <xsl:when test="@xmi:type = 'uml:Property'">
132 <xsl:apply-templates select=".">
133 <xsl:with-param name="package" select="$package"/>
134 </xsl:apply-templates>
135 </xsl:when>
136
137 <xsl:otherwise>
138 <!-- process all children nodes : uml:Generalization ... -->
139 <xsl:copy>
140 <xsl:apply-templates select="@*" mode="filterAttr"/>
141 <xsl:apply-templates select="node()" mode="specialCopy">
142 <xsl:with-param name="package" select="$package"/>
143 </xsl:apply-templates>
144 </xsl:copy>
145 </xsl:otherwise>
146 </xsl:choose>
147 </xsl:template>
148
149
150
151 <!-- creates an property node with base='type' attribute -->
152 <xsl:template match="*[@xmi:type='uml:Property']">
153 <xsl:param name="package"/>
154
155 <field name="{@name}">
156 <xsl:apply-templates select="@*" mode="filterAttr" />
157 <xsl:if test="@aggregation = 'composite'">
158 <xsl:attribute name="type">list</xsl:attribute>
159 </xsl:if>
160 <xsl:call-template name="get-class-from-id">
161 <xsl:with-param name="id">
162 <xsl:value-of select="@type"/>
163 </xsl:with-param>
164 </xsl:call-template>
165
166 <xsl:apply-templates select="child::*" mode="specialCopy">
167 <xsl:with-param name="package" select="$package"/>
168 </xsl:apply-templates>
169
170 </field>
171 </xsl:template>
172
173
174 <!-- filter attributes -->
175 <xsl:template match="@*" mode="filterAttr">
176 <xsl:choose>
177 <!-- skip attribute xmi:id -->
178 <xsl:when test="name() = 'xmi:id'"/>
179 <!-- skip attribute visibility -->
180 <xsl:when test="name() = 'visibility'"/>
181 <!-- skip attribute type (property) -->
182 <xsl:when test="name() = 'type'"/>
183 <!-- skip attribute association (property) -->
184 <xsl:when test="name() = 'association'"/>
185 <xsl:otherwise>
186 <!-- copy other attributes ... -->
187 <xsl:copy>
188 <xsl:apply-templates select="@*" mode="filterAttr"/>
189 </xsl:copy>
190 </xsl:otherwise>
191 </xsl:choose>
192 </xsl:template>
193
194
195
196 <!-- creates an extends node with base='type' attribute -->
197 <xsl:template match="*[@xmi:type='uml:Generalization']">
198 <extends>
199 <xsl:call-template name="get-class-from-id">
200 <xsl:with-param name="id">
201 <xsl:value-of select="@general"/>
202 </xsl:with-param>
203 </xsl:call-template>
204 </extends>
205 </xsl:template>
206
207
208
209 <!-- puts in comments primitive types -->
210 <xsl:template match="*[@xmi:type='uml:PrimitiveType']">
211 <xsl:choose>
212 <xsl:when test="@name">
213 <primitiveType name="{@name}" />
214 </xsl:when>
215 <xsl:otherwise>
216 <ERROR>No name for uml:PrimitiveType in [type : '<xsl:value-of select="../@xmi:type"/>' - name : '<xsl:value-of select="../@name"/>']</ERROR>
217 </xsl:otherwise>
218 </xsl:choose>
219 </xsl:template>
220
221
222
223 <!-- resolve class type for both data types (primitive or specific) and classes -->
224 <xsl:template name="class-from-id">
225 <xsl:param name="id"/>
226 <xsl:param name="prefix"/>
227 <xsl:variable name="c" select="key('classid',$id)"/>
228 <xsl:choose>
229 <xsl:when test="$c/@xmi:type = 'uml:PrimitiveType'">
230 <xsl:value-of select="$c/@name"/>
231 </xsl:when>
232 <xsl:otherwise>
233 <xsl:value-of select="$prefix"/>
234 <xsl:value-of select="$c/@name"/>
235 </xsl:otherwise>
236 </xsl:choose>
237 </xsl:template>
238
239
240
241 <!-- resolve class type for both data types (primitive or specific) and classes -->
242 <xsl:template name="get-class-from-id">
243 <xsl:param name="id"/>
244 <xsl:variable name="c" select="key('classid',$id)"/>
245 <xsl:choose>
246 <xsl:when test="$c/@xmi:type = 'uml:PrimitiveType'">
247 <xsl:attribute name="type">
248 <xsl:value-of select="$c/@name"/>
249 </xsl:attribute>
250 </xsl:when>
251 <xsl:otherwise>
252 <xsl:attribute name="package"><xsl:text>model.</xsl:text>
253 <xsl:value-of select="$c/../@name"/>
254 </xsl:attribute>
255 <xsl:attribute name="class">
256 <xsl:value-of select="$c/@name"/>
257 </xsl:attribute>
258 </xsl:otherwise>
259 </xsl:choose>
260 </xsl:template>
261
262
263
264 <xsl:template match="@*" mode="copyAttr">
265 <xsl:copy>
266 <xsl:apply-templates select="@*" mode="copyAttr"/>
267 </xsl:copy>
268 </xsl:template>
269
270 </xsl:stylesheet>

Properties

Name Value
svn:mime-type application/octet-stream

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