2
|
1 <?xml version="1.0" encoding="UTF-8"?>
|
|
2 <xsl:stylesheet version="1.0"
|
|
3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://implab.org/schemas/data-model.v1.xsd"
|
|
4 xmlns:clr="http://implab.org/schemas/data-model/dotnet.v1.xsd"
|
|
5 xmlns:cs="http://implab.org/schemas/code-dom/csharp.v1.xsd" xmlns:sql="http://implab.org/schemas/data-model/sql.v1.xsd"
|
|
6 xmlns:t="http://implab.org/schemas/temp" xmlns:exsl="http://exslt.org/common"
|
|
7 exclude-result-prefixes="clr m xsl exsl sql">
|
|
8
|
|
9 <xsl:import href="preprocess.xsl" />
|
|
10 <xsl:include href="text-tools.xsl"/>
|
|
11
|
|
12 <xsl:output method="xml" indent="yes" />
|
|
13
|
|
14 <xsl:key name="type" match="m:package/m:*" use="@name" />
|
|
15
|
|
16 <xsl:template match="/">
|
|
17 <cs:document>
|
|
18 <xsl:apply-templates select="$module" mode="document" />
|
|
19 </cs:document>
|
|
20 </xsl:template>
|
|
21
|
|
22 <!-- code generation -->
|
|
23
|
|
24 <!-- disable default template -->
|
|
25 <xsl:template match="*|text()" mode="document" />
|
|
26
|
|
27 <xsl:template match="t:module" mode="document">
|
|
28 <xsl:apply-templates mode="document" />
|
|
29 </xsl:template>
|
|
30
|
3
|
31
|
2
|
32
|
|
33 <!-- member resolution traits -->
|
|
34
|
|
35 <!-- Resolves members using given criteria
|
|
36 @param type
|
|
37 the name of the type whose members are inspected
|
|
38 @param memberName
|
|
39 the name of the member to lookup
|
|
40 @param scope
|
|
41 the scope where to look (members, properties, keys, relations)
|
|
42
|
|
43 This template recursively inspects the given type down to it's inheritance
|
|
44 hierarchy and outputs members matching criteria.
|
|
45 -->
|
|
46 <xsl:template name="getMembers">
|
|
47 <xsl:param name="type"/>
|
|
48 <xsl:param name="memberName" select="''"/>
|
|
49 <xsl:param name="scope" select="'members'"/>
|
|
50 <!-- prevents cyclic references -->
|
|
51 <xsl:param name="seen" select="/.."/>
|
|
52 <xsl:for-each select="$module">
|
|
53 <xsl:apply-templates select="key('type', $type)"
|
|
54 mode="lookup-members">
|
|
55 <xsl:with-param name="memberName" select="$memberName"/>
|
|
56 <xsl:with-param name="scope" select="$scope"/>
|
|
57 <xsl:with-param name="seen" select="$seen"/>
|
|
58 </xsl:apply-templates>
|
|
59 </xsl:for-each>
|
|
60 </xsl:template>
|
|
61
|
|
62 <xsl:template match="*" mode="lookup-members"/>
|
|
63
|
|
64 <xsl:template match="m:entity" mode="lookup-members">
|
|
65 <xsl:param name="memberName" select="''"/>
|
|
66 <xsl:param name="scope" select="'members'"/>
|
|
67 <xsl:param name="seen"/>
|
|
68
|
|
69 <xsl:variable name="self" select="."/>
|
|
70
|
|
71 <xsl:if test="not($seen[generate-id() = generate-id($self)])">
|
|
72 <xsl:variable name="scopeMembers">
|
|
73 <xsl:call-template name="selectScopeMembers">
|
|
74 <xsl:with-param name="scope" select="$scope"/>
|
|
75 </xsl:call-template>
|
|
76 </xsl:variable>
|
|
77
|
6
|
78 <xsl:variable name="members" select="exsl:node-set($scopeMembers)/*[not($memberName) or @name=$memberName]"/>
|
2
|
79
|
|
80 <xsl:choose>
|
|
81 <xsl:when test="$members">
|
|
82 <xsl:copy-of select="$members"/>
|
|
83 </xsl:when>
|
|
84 <xsl:when test="m:extends">
|
|
85 <xsl:call-template name="getMembers">
|
|
86 <xsl:with-param name="type" select="m:extends/@type"/>
|
|
87 <xsl:with-param name="memberName" select="$memberName"/>
|
|
88 <xsl:with-param name="scope" select="$scope"/>
|
|
89 <xsl:with-param name="seen" select="$seen | $self"/>
|
|
90 </xsl:call-template>
|
|
91 </xsl:when>
|
|
92 <xsl:otherwise>
|
|
93 <xsl:call-template name="warn">
|
|
94 <xsl:with-param name="msg">
|
6
|
95 <t:trace msg="failed to resolve {$memberName}, scope={$scope}"/>
|
2
|
96 <t:trace msg="inspected classes">
|
|
97 <xsl:for-each select="$seen | $self">
|
|
98 <t:trace msg="{name()} {@name}"/>
|
|
99 </xsl:for-each>
|
|
100 </t:trace>
|
|
101 </xsl:with-param>
|
|
102 </xsl:call-template>
|
|
103 </xsl:otherwise>
|
|
104 </xsl:choose>
|
|
105 </xsl:if>
|
|
106 </xsl:template>
|
|
107
|
|
108 <!-- this template implements scope filtering -->
|
|
109 <xsl:template name="selectScopeMembers">
|
|
110 <xsl:param name="scope" select="'members'"/>
|
|
111 <xsl:choose>
|
|
112 <xsl:when test="$scope='members'">
|
|
113 <xsl:apply-templates mode="filter-members"/>
|
|
114 </xsl:when>
|
|
115 <xsl:when test="$scope='keys'">
|
6
|
116 <xsl:apply-templates mode="filter-keys"/>
|
2
|
117 </xsl:when>
|
|
118 <xsl:when test="$scope='properties'">
|
|
119 <xsl:apply-templates mode="fiflter-properties"/>
|
|
120 </xsl:when>
|
|
121 <xsl:when test="$scope='relations'">
|
|
122 <xsl:apply-templates mode="fiflter-relations"/>
|
|
123 </xsl:when>
|
|
124 </xsl:choose>
|
|
125 </xsl:template>
|
|
126
|
|
127 <!-- filter-members -->
|
|
128 <xsl:template match="*|text()" mode="filter-members"/>
|
|
129 <xsl:template match="*[@name]" mode="filter-members">
|
|
130 <xsl:copy>
|
|
131 <xsl:copy-of select="@*" />
|
|
132 <xsl:attribute name="declaringType"><xsl:value-of select="../@name" /></xsl:attribute>
|
|
133 <xsl:copy-of select="*" />
|
|
134 </xsl:copy>
|
|
135 </xsl:template>
|
|
136
|
|
137 <!-- filter-keys -->
|
|
138 <xsl:template match="*|text()" mode="filter-keys"/>
|
|
139 <xsl:template match="m:primaryKey" mode="filter-keys">
|
|
140 <xsl:apply-templates select="." mode="filter-members"/>
|
|
141 </xsl:template>
|
|
142
|
|
143 <!-- filter-properties -->
|
|
144 <xsl:template match="*|text()" mode="filter-properties"/>
|
|
145 <xsl:template match="m:property" mode="filter-properties">
|
|
146 <xsl:apply-templates select="." mode="filter-members"/>
|
|
147 </xsl:template>
|
|
148
|
|
149 <!-- filter-relations -->
|
|
150 <xsl:template match="*|text()" mode="filter-relations"/>
|
|
151 <xsl:template match="m:hasA | m:hasMany" mode="filter-relations">
|
|
152 <xsl:apply-templates select="." mode="filter-members"/>
|
|
153 </xsl:template>
|
|
154
|
|
155
|
|
156
|
|
157 <!-- Resolves primaryKey information for the given type name.
|
|
158 @returns m:primaryKey node copy with additional attribute
|
|
159 @declaringType which points to the type where this primaryKey
|
|
160 was defined.
|
|
161 -->
|
|
162 <xsl:template name="getPrimaryKey">
|
|
163 <xsl:param name="type" />
|
|
164 <xsl:call-template name="getMembers">
|
|
165 <xsl:with-param name="type" select="$type"/>
|
6
|
166 <xsl:with-param name="scope" select="'keys'"/>
|
2
|
167 </xsl:call-template>
|
|
168 </xsl:template>
|
|
169
|
7
|
170 <!-- expand-member templates are used to process relations
|
8
|
171 and keys and provide usefull information. This mode is designed
|
|
172 to recursively traverse members expanding details like special
|
|
173 properties generated by relations.
|
|
174
|
|
175 expand-member-reference is special mode used to traverse inside
|
|
176 members which are referenced from relations. By default this mode
|
|
177 does nothing.
|
|
178
|
|
179 Use this templates to produce intermediate model for further
|
|
180 processing by specific code-generators.
|
7
|
181
|
|
182 @special - means that the member is a composite part and isn't
|
|
183 accessible directly
|
|
184 -->
|
|
185
|
8
|
186 <xsl:template match="*" mode="expand-member">
|
|
187 <xsl:apply-templates mode="expand-member"/>
|
|
188 </xsl:template>
|
|
189 <xsl:template match="text()" mode="expand-member"/>
|
|
190
|
|
191 <xsl:template match="*|text()" mode="expand-member-reference"/>
|
|
192
|
7
|
193 <!-- short form of primaryKey{@name, @type} -->
|
|
194 <xsl:template match="m:primaryKey" mode="expand-member">
|
8
|
195 <xsl:variable name="specialProperty">
|
7
|
196 <m:property special="true">
|
|
197 <xsl:copy-of select="@*" />
|
8
|
198 <m:hiddenBy name="{@name}" type="{@declaringType}"/>
|
7
|
199 <xsl:copy-of select="*" />
|
|
200 </m:property>
|
8
|
201 </xsl:variable>
|
|
202 <xsl:apply-templates select="exsl:node-set($specialProperty)" mode="expand-member"/>
|
|
203 <xsl:apply-templates mode="expand-member"/>
|
7
|
204 </xsl:template>
|
|
205
|
|
206 <!-- long form of primaryKey{ member{@name}+, property{@name, @type} } -->
|
|
207 <xsl:template match="m:primaryKey[m:member | m:property]" mode="expand-member">
|
8
|
208 <xsl:apply-templates mode="expand-member"/>
|
7
|
209 </xsl:template>
|
|
210
|
|
211 <!-- properties declared inside relations, they are @special -->
|
|
212 <xsl:template match="m:primaryKey/m:property | m:thisKey/m:property" mode="expand-member">
|
8
|
213 <xsl:variable name="specialProperty">
|
|
214 <xsl:copy>
|
|
215 <xsl:attribute name="special">true</xsl:attribute>
|
|
216 <xsl:copy-of select="@*"/>
|
|
217 <m:hiddenBy name="{@name}" type="{@declaringType}"/>
|
|
218 <xsl:copy-of select="*"/>
|
|
219 </xsl:copy>
|
|
220 </xsl:variable>
|
|
221 <xsl:apply-templates select="exsl:node-set($specialProperty)" mode="expand-member"/>
|
7
|
222 </xsl:template>
|
|
223
|
8
|
224 <!-- traverse inside referenced members -->
|
7
|
225 <xsl:template match="m:member" mode="expand-member">
|
|
226 <xsl:param name="declaringType"/>
|
|
227 <xsl:variable name="expanded">
|
|
228 <xsl:call-template name="getMembers">
|
|
229 <xsl:with-param name="memberName" select="@name"/>
|
|
230 <xsl:with-param name="type" select="$declaringType"/>
|
|
231 </xsl:call-template>
|
|
232 </xsl:variable>
|
|
233 <!-- recusive expand -->
|
8
|
234 <xsl:apply-templates select="exsl:node-set($expanded)" mode="expand-member-reference"/>
|
7
|
235 </xsl:template>
|
|
236
|
8
|
237 <xsl:template match="m:hasA/m:thisKey" mode="expand-member">
|
7
|
238 <xsl:variable name="otherKey">
|
|
239 <xsl:call-template name="getPrimaryKey">
|
|
240 <xsl:with-param name="type" select="../@type"/>
|
|
241 </xsl:call-template>
|
|
242 </xsl:variable>
|
|
243 <xsl:variable name="otherKeyExpanded">
|
|
244 <xsl:apply-templates select="exsl:node-set($otherKey)" mode="expand-member"/>
|
|
245 </xsl:variable>
|
|
246 <xsl:variable name="pk" select="exsl:node-set($otherKeyExpanded)[1]"/>
|
|
247
|
|
248 <m:property name="{@name}" type="{$pk/@type}" hidden="true">
|
|
249 <m:related name="{$pk/@name}" type="{../@type}"/>
|
|
250 </m:property>
|
|
251 </xsl:template>
|
|
252
|
|
253 <xsl:template match="m:thisKey[m:member | property]">
|
|
254 <xsl:apply-templates mode="expand-member"/>
|
|
255 </xsl:template>
|
|
256
|
2
|
257 </xsl:stylesheet>
|