1<xsl:stylesheet
2    version="1.0"
3    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4    xmlns:UML = 'org.omg.xmi.namespace.UML'
5    xmlns:exslt="http://exslt.org/common"
6    xml:space="default">
7<!--***********************************************************
8 *
9 * Licensed to the Apache Software Foundation (ASF) under one
10 * or more contributor license agreements.  See the NOTICE file
11 * distributed with this work for additional information
12 * regarding copyright ownership.  The ASF licenses this file
13 * to you under the Apache License, Version 2.0 (the
14 * "License"); you may not use this file except in compliance
15 * with the License.  You may obtain a copy of the License at
16 *
17 *   http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing,
20 * software distributed under the License is distributed on an
21 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22 * KIND, either express or implied.  See the License for the
23 * specific language governing permissions and limitations
24 * under the License.
25 *
26 ***********************************************************-->
27  <xsl:output method="xml"/>
28
29  <xsl:template match="/">
30    <xsl:apply-templates select="//UML:Model"/>
31  </xsl:template>
32
33  <xsl:template match="UML:Model">
34    <model>
35      <xsl:attribute name="name">
36	<xsl:value-of select="@name"/>
37      </xsl:attribute>
38      <xsl:call-template name="nostereotype"/>
39      <xsl:call-template name="ww8resources"/>
40      <xsl:call-template name="sprms"/>
41    </model>
42  </xsl:template>
43
44  <xsl:template name="nostereotype">
45    <xsl:for-each select="UML:Namespace.ownedElement/UML:Class[not(UML:ModelElement.stereotype)]">
46      <xsl:variable name="myid" select="@xmi.id"/>
47      <class>
48	<xsl:attribute name="name">
49	  <xsl:value-of select="$myid"/>
50	</xsl:attribute>
51	<xsl:attribute name="resource">
52	<xsl:for-each select="//UML:Generalization[UML:Generalization.child/UML:Class/@xmi.idref=$myid]">
53	  <xsl:value-of select="UML:Generalization.parent/UML:Class/@xmi.idref"/>
54	</xsl:for-each>
55      </xsl:attribute>
56      <xsl:apply-templates select=".//UML:Attribute|.//UML:Operation" mode="ww8resource"/>
57      </class>
58    </xsl:for-each>
59  </xsl:template>
60
61  <xsl:template name="ww8resources">
62    <xsl:for-each select="UML:Namespace.ownedElement/UML:Class[UML:ModelElement.stereotype/UML:Stereotype/@xmi.idref='ww8resource' and not(UML:ModelElement.stereotype/UML:Stereotype/@xmi.idref='ww8sprm')]">
63      <xsl:sort select="@xmi.id"/>
64      <xsl:call-template name="ww8resource"/>
65    </xsl:for-each>
66  </xsl:template>
67
68  <xsl:template name="ww8resource">
69    <xsl:variable name="myid" select="@xmi.id"/>
70    <xsl:variable name="classid">
71      <xsl:call-template name="gettaggedvalue">
72	<xsl:with-param name="name">classid</xsl:with-param>
73      </xsl:call-template>
74    </xsl:variable>
75    <ww8resource>
76      <xsl:attribute name="name">
77	<xsl:value-of select="$myid"/>
78      </xsl:attribute>
79      <xsl:attribute name="resource">
80	<xsl:for-each select="//UML:Generalization[UML:Generalization.child/UML:Class/@xmi.idref=$myid]">
81	  <xsl:value-of select="UML:Generalization.parent/UML:Class/@xmi.idref"/>
82	</xsl:for-each>
83      </xsl:attribute>
84      <xsl:if test="string-length($classid) &gt; 0">
85	<xsl:attribute name="classid">
86	  <xsl:value-of select="$classid"/>
87	</xsl:attribute>
88      </xsl:if>
89      <xsl:for-each select="UML:ModelElement.stereotype/UML:Stereotype">
90	<xsl:if test="@xmi.idref != 'ww8resource'">
91	  <stereotype>
92	    <xsl:attribute name="name">
93	      <xsl:value-of select="@xmi.idref"/>
94	    </xsl:attribute>
95	  </stereotype>
96	</xsl:if>
97      </xsl:for-each>
98      <xsl:apply-templates select=".//UML:Attribute|.//UML:Operation" mode="ww8resource"/>
99    </ww8resource>
100  </xsl:template>
101
102  <xsl:template name="gettaggedvalue">
103    <xsl:param name="name"/>
104    <xsl:for-each select="UML:ModelElement.taggedValue/UML:TaggedValue[UML:TaggedValue.type/UML:TagDefinition/@xmi.idref=$name]">
105      <xsl:value-of select="UML:TaggedValue.dataValue"/>
106    </xsl:for-each>
107  </xsl:template>
108
109  <xsl:template name="sizefortype">
110    <xsl:param name="type"/>
111    <xsl:choose>
112      <xsl:when test="$type='S8'">1</xsl:when>
113      <xsl:when test="$type='U8'">1</xsl:when>
114      <xsl:when test="$type='S16'">2</xsl:when>
115      <xsl:when test="$type='U16'">2</xsl:when>
116      <xsl:when test="$type='S32'">4</xsl:when>
117      <xsl:when test="$type='U32'">4</xsl:when>
118      <xsl:otherwise>0</xsl:otherwise>
119    </xsl:choose>
120  </xsl:template>
121
122  <xsl:template name="bits">
123    <xsl:param name="bits"/>
124    <bits>
125      <xsl:attribute name="name">
126	<xsl:value-of select="@name"/>
127      </xsl:attribute>
128      <xsl:attribute name="bits">
129	<xsl:value-of select="$bits"/>
130      </xsl:attribute>
131      <xsl:attribute name="token">
132	<xsl:call-template name="gettaggedvalue">
133	  <xsl:with-param name="name">attrid</xsl:with-param>
134	</xsl:call-template>
135      </xsl:attribute>
136    </bits>
137  </xsl:template>
138
139  <xsl:template match="UML:Attribute" mode="ww8resource">
140    <xsl:variable name="mask">
141      <xsl:call-template name="gettaggedvalue">
142	<xsl:with-param name="name">mask</xsl:with-param>
143      </xsl:call-template>
144    </xsl:variable>
145    <xsl:variable name="offset">
146      <xsl:call-template name="gettaggedvalue">
147	<xsl:with-param name="name">offset</xsl:with-param>
148      </xsl:call-template>
149    </xsl:variable>
150    <xsl:variable name="type">
151      <xsl:value-of select="UML:StructuralFeature.type/UML:DataType/@xmi.idref"/>
152    </xsl:variable>
153    <xsl:variable name="size">
154      <xsl:call-template name="sizefortype">
155	<xsl:with-param name="type" select="$type"/>
156      </xsl:call-template>
157    </xsl:variable>
158    <xsl:variable name="arraycount">
159      <xsl:call-template name="gettaggedvalue">
160	<xsl:with-param name="name">arraycount</xsl:with-param>
161      </xsl:call-template>
162    </xsl:variable>
163    <xsl:choose>
164      <xsl:when test="string-length($mask) = 0">
165	<attribute>
166	  <xsl:attribute name="name">
167	    <xsl:value-of select="@name"/>
168	  </xsl:attribute>
169	  <xsl:attribute name="type">
170	    <xsl:value-of select="$type"/>
171	  </xsl:attribute>
172	  <xsl:attribute name="size">
173	    <xsl:value-of select="$size"/>
174	  </xsl:attribute>
175	  <xsl:for-each select="UML:ModelElement.stereotype/UML:Stereotype[@xmi.idref='array']">
176	    <xsl:attribute name="array-count">
177	      <xsl:value-of select="$arraycount"/>
178	    </xsl:attribute>
179	  </xsl:for-each>
180	  <xsl:attribute name="token">
181	    <xsl:call-template name="gettaggedvalue">
182	      <xsl:with-param name="name">attrid</xsl:with-param>
183	    </xsl:call-template>
184	  </xsl:attribute>
185	</attribute>
186      </xsl:when>
187      <xsl:otherwise>
188	<xsl:variable name="attrswithoffset">
189	  <xsl:for-each select="ancestor::UML:Class//UML:Attribute[UML:ModelElement.taggedValue/UML:TaggedValue[UML:TaggedValue.type/UML:TagDefinition/@xmi.idref='offset' and UML:TaggedValue.dataValue=$offset]]">
190	    <xsl:sort select="UML:ModelElement.taggedValue/UML:TaggedValue[UML:TaggedValue.type/UML:TagDefinition/@xmi.idref='shift']/UML:TaggedValue.dataValue" data-type="number" order="descending"/>
191	    <xsl:copy-of select="."/>
192	  </xsl:for-each>
193	</xsl:variable>
194	<xsl:text>&#xa;</xsl:text>
195	<xsl:if test="@name = exslt:node-set($attrswithoffset)/UML:Attribute[1]/@name">
196	  <bitfield>
197	    <xsl:attribute name="size">
198	      <xsl:value-of select="$size"/>
199	    </xsl:attribute>
200	    <xsl:attribute name="type">
201	      <xsl:value-of select="$type"/>
202	    </xsl:attribute>
203	    <xsl:variable name="allbits" select="8 * $size"/>
204	    <xsl:for-each select="exslt:node-set($attrswithoffset)/UML:Attribute">
205	      <xsl:variable name="lastshift">
206		<xsl:choose>
207		  <xsl:when test="count(preceding-sibling::UML:Attribute) = 0">
208		    <xsl:value-of select="$allbits"/>
209		  </xsl:when>
210		  <xsl:otherwise>
211		    <xsl:value-of select="preceding-sibling::UML:Attribute[1]/UML:ModelElement.taggedValue/UML:TaggedValue[UML:TaggedValue.type/UML:TagDefinition/@xmi.idref='shift']/UML:TaggedValue.dataValue"/>
212		  </xsl:otherwise>
213		</xsl:choose>
214	      </xsl:variable>
215	      <xsl:variable name="bits" select="$lastshift - UML:ModelElement.taggedValue/UML:TaggedValue[UML:TaggedValue.type/UML:TagDefinition/@xmi.idref='shift']/UML:TaggedValue.dataValue"/>
216	      <xsl:call-template name="bits">
217		<xsl:with-param name="bits" select="$bits"/>
218	      </xsl:call-template>
219	    </xsl:for-each>
220	  </bitfield>
221	</xsl:if>
222      </xsl:otherwise>
223    </xsl:choose>
224  </xsl:template>
225
226  <xsl:template match="UML:Operation" mode="ww8resource">
227    <operation>
228      <xsl:attribute name="name">
229	<xsl:value-of select="@name"/>
230      </xsl:attribute>
231      <xsl:attribute name="type">
232	<xsl:for-each select="UML:BehavioralFeature.parameter/UML:Parameter[@kind='return']">
233	  <xsl:value-of select="UML:Parameter.type/UML:Class/@xmi.idref"/>
234	</xsl:for-each>
235      </xsl:attribute>
236      <xsl:attribute name="token">
237	<xsl:call-template name="gettaggedvalue">
238	  <xsl:with-param name="name">opid</xsl:with-param>
239	</xsl:call-template>
240      </xsl:attribute>
241      <xsl:for-each select="UML:ModelElement.stereotype">
242	<stereotype>
243	  <xsl:value-of select="UML:Stereotype/@xmi.idref"/>
244	</stereotype>
245      </xsl:for-each>
246    </operation>
247  </xsl:template>
248
249  <xsl:template match="*" mode="ww8resources">
250    <xsl:apply-templates select="*" mode="ww8resources"/>
251  </xsl:template>
252
253  <xsl:template name="sprms">
254    <xsl:for-each select="UML:Namespace.ownedElement/UML:Class[UML:ModelElement.stereotype/UML:Stereotype/@xmi.idref='ww8sprm']">
255      <xsl:sort select="@xmi.id"/>
256      <sprm>
257	<xsl:attribute name="name">
258	  <xsl:value-of select="@xmi.id"/>
259	</xsl:attribute>
260	<xsl:attribute name="code">
261	  <xsl:call-template name="gettaggedvalue">
262	    <xsl:with-param name="name">sprmcode</xsl:with-param>
263	  </xsl:call-template>
264	</xsl:attribute>
265	<xsl:attribute name="kind">
266	  <xsl:call-template name="gettaggedvalue">
267	    <xsl:with-param name="name">kind</xsl:with-param>
268	  </xsl:call-template>
269	</xsl:attribute>
270	<xsl:attribute name="token">
271	  <xsl:call-template name="gettaggedvalue">
272	    <xsl:with-param name="name">sprmid</xsl:with-param>
273	  </xsl:call-template>
274	</xsl:attribute>
275	<xsl:if test="UML:ModelElement.stereotype/UML:Stereotype/@xmi.idref='ww8resource'">
276	  <xsl:apply-templates select=".//UML:Attribute|.//UML:Operation" mode="ww8resource"/>
277	</xsl:if>
278      </sprm>
279    </xsl:for-each>
280  </xsl:template>
281
282</xsl:stylesheet>
283