-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtck-audit.xsl
241 lines (200 loc) · 10.7 KB
/
tck-audit.xsl
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Jakarta Validation: constrain once, validate everywhere.
~
~ License: Apache License, Version 2.0
~ See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
-->
<!--
~ Creates the TCK audit file based on the DocBook XML mark-up of the specification.
~
~ The generation of the audit file is controlled by marking individual sentences from the
~ specification as relevant for the TCK using the "role" attribute, which can be used in all
~ DocBook elements (paras, phrases etc.). The following values for the "role" attribute are
~ supported:
~
~ * tck-testable: Adds a testable assertion with the marked statement to the audit file
~ * tck-not-testable: Adds a not testable assertion with the marked statement to the audit file
~ * tck-ignore: Ignores the marked statement when given with another marked statement (e.g. to
~ exclude explanatory phrases)
~ * tck-needs-update: Adds the note "Needs update" to the concerned statement (can be given
~ together with tck(-not)-testable)
~
~ Implementation note:
~
~ The generation happens in several passes, each processing a result tree fragment (RTF) created
~ by the previous pass:
~
~ * Merge all chapters referenced in the index file
~ * Determine the index numbers of all chapters and sections
~ * Expand xref elements into the index number of the referenced chapter or section
~ * Create the audit file
~
~ @author Gunnar Morling
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt"
xmlns:exslt="http://exslt.org/common" xmlns:xi="http://www.w3.org/2001/XInclude" version="1.0">
<xsl:output method="xml" indent="yes" xalan:indent-amount="4" />
<xsl:param name="currentDate"/>
<xsl:param name="repositoryRevision"/>
<!-- A regular apostroph; provided as a variable to avoid escaping issues -->
<xsl:variable name="apos">'</xsl:variable>
<!-- ### Passes by creating and processing result tree fragments ### -->
<xsl:variable name="merged">
<xsl:apply-templates mode="merge" select="/"/>
</xsl:variable>
<xsl:variable name="withSectionNums">
<xsl:apply-templates mode="addSectionNums" select="exslt:node-set($merged)"/>
</xsl:variable>
<xsl:variable name="prepared">
<xsl:apply-templates mode="prepare" select="exslt:node-set($withSectionNums)"/>
</xsl:variable>
<xsl:template match="/">
<xsl:apply-templates mode="createAuditFile" select="exslt:node-set($prepared)"/>
</xsl:template>
<!-- ### Merge templates ### -->
<xsl:template match="xi:include" mode="merge">
<xsl:variable name="fileName">en/<xsl:value-of select="@href" /></xsl:variable>
<xsl:apply-templates select="document($fileName)" mode="merge"/>
</xsl:template>
<xsl:template match="@*|node()" mode="merge">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="merge"/>
</xsl:copy>
</xsl:template>
<!-- ### addSectionNums templates ### -->
<xsl:template match="/article/section" mode="addSectionNums">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="sectionNum"><xsl:number from="article" level="single" /></xsl:attribute>
<xsl:apply-templates mode="addSectionNums"/>
</xsl:copy>
</xsl:template>
<xsl:template match="section" mode="addSectionNums">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="sectionNum"><xsl:number count="section" from="article" level="multiple" /></xsl:attribute>
<xsl:apply-templates mode="addSectionNums"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()" mode="addSectionNums">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="addSectionNums"/>
</xsl:copy>
</xsl:template>
<!-- ### Prepare templates ### -->
<xsl:template match="xref" mode="prepare">
<xsl:variable name="id" select="@linkend"/>
<xsl:variable name="linkedNode" select="ancestor::*//*[@xml:id=$id]" />
<xsl:if test="not($linkedNode)">
<xsl:message terminate="yes">
No node found for link id: <xsl:value-of select="$id" />
</xsl:message>
</xsl:if>
<xsl:value-of select="$linkedNode/@sectionNum" />
</xsl:template>
<xsl:template match="*[@role = 'tck-ignore']" mode="prepare"/>
<xsl:template match="@*|node()" mode="prepare">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="prepare"/>
</xsl:copy>
</xsl:template>
<!-- ### Create audit file templates ### -->
<xsl:template match="article" mode="createAuditFile">
<xsl:text> </xsl:text>
<xsl:comment>
Jakarta Validation TCK
License: Apache License, Version 2.0
See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
</xsl:comment>
<xsl:text> </xsl:text>
<xsl:comment>
<xsl:text> Generated by tck-audit.xsl at </xsl:text><xsl:value-of select="$currentDate"/><xsl:text> </xsl:text>
<xsl:text>(revision </xsl:text><xsl:value-of select="$repositoryRevision"/><xsl:text>) </xsl:text>
</xsl:comment>
<xsl:text> </xsl:text>
<specification xmlns="http://jboss.com/products/weld/tck/audit" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.com/products/weld/tck/audit" name="Jakarta Validation 3.0"
version="3.0.0" id="beanvalidation" generateSectionIds="true">
<xsl:apply-templates mode="createAuditFile"/>
</specification>
</xsl:template>
<xsl:template match="section[not(ancestor::appendix)]" mode="createAuditFile">
<xsl:call-template name="check-section-id">
<xsl:with-param name="sectionId"><xsl:value-of select="@xml:id" /></xsl:with-param>
<xsl:with-param name="sectionNum"><xsl:value-of select="@sectionNum" /></xsl:with-param>
</xsl:call-template>
<section xmlns="http://jboss.com/products/weld/tck/audit">
<xsl:attribute name="id"><xsl:value-of select="@xml:id" /></xsl:attribute>
<xsl:attribute name="title"><xsl:value-of select="normalize-space(translate(title, '’', $apos))" /></xsl:attribute>
<xsl:attribute name="level"><xsl:value-of select="count(ancestor::section) + 1" /></xsl:attribute>
<xsl:variable name="sectionIdConstant"><xsl:call-template name="section-id-to-constant"><xsl:with-param name="sectionId" select="@xml:id" /></xsl:call-template></xsl:variable>
<xsl:comment><xsl:text> </xsl:text><xsl:value-of select="@sectionNum" /><xsl:text> - </xsl:text><xsl:value-of select="$sectionIdConstant" /><xsl:text> </xsl:text></xsl:comment>
<!-- get all assertions directly under chapter, without a section -->
<xsl:apply-templates select="*[not(local-name() = 'section')]" mode="createAuditFile"/>
</section>
<!-- get all sections, flattened to one level -->
<xsl:apply-templates select="section" mode="createAuditFile"/>
</xsl:template>
<xsl:template match="section[ancestor::preface]" mode="createAuditFile">
</xsl:template>
<xsl:template match="section[ancestor::appendix]" mode="createAuditFile">
</xsl:template>
<!-- Add an assertion for any element with role="tck..." -->
<xsl:template match="*[starts-with(@role, 'tck')]" mode="createAuditFile">
<xsl:variable name="normalized"><xsl:value-of select="normalize-space(translate(., '’', $apos))" /></xsl:variable>
<xsl:variable name="firstWord"><xsl:value-of select="substring-before(concat($normalized, ' '), ' ')"/></xsl:variable>
<xsl:variable name="assertionText">
<!-- Capitalize the first word if it doesn't contain upper-case letter already (e.g. a camel-cased method name) -->
<xsl:choose>
<xsl:when test="string-length(translate($firstWord, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ-', '')) < string-length($firstWord)">
<xsl:value-of select="$normalized"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(translate(substring($normalized, 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), substring($normalized, 2))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<assertion xmlns="http://jboss.com/products/weld/tck/audit">
<xsl:attribute name="id">
<xsl:number count="*[starts-with(@role, 'tck')]" from="section" level="any" format="a" />
</xsl:attribute>
<xsl:if test="contains(@role, 'tck-not-testable')">
<xsl:attribute name="testable">false</xsl:attribute>
</xsl:if>
<text xmlns="http://jboss.com/products/weld/tck/audit">
<xsl:choose>
<!-- Remove trailing ":" -->
<xsl:when test="substring($assertionText, string-length($assertionText)) = ':'">
<xsl:value-of select="substring($assertionText, 0, string-length($assertionText))" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$assertionText" />
</xsl:otherwise>
</xsl:choose>
</text>
<xsl:if test="contains(@role, 'tck-needs-update')">
<note xmlns="http://jboss.com/products/weld/tck/audit">Needs update</note>
</xsl:if>
</assertion>
</xsl:template>
<xsl:template match="@*|node()" mode="createAuditFile">
<xsl:apply-templates select="@*|node()" mode="createAuditFile"/>
</xsl:template>
<!-- Check that the given section id is manually defined -->
<xsl:template name="check-section-id">
<xsl:param name="sectionId" />
<xsl:param name="sectionNum" />
<xsl:if test="starts-with($sectionId, '_')">
<xsl:message terminate="yes">
Error: section <xsl:value-of select="$sectionNum" /><xsl:text> - </xsl:text><xsl:value-of select="$sectionId" /> seems to be automatically generated: it starts with an underscore.
</xsl:message>
</xsl:if>
</xsl:template>
<xsl:template name="section-id-to-constant">
<xsl:param name="sectionId" />
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz-'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'" />
<xsl:value-of select="translate($sectionId, $smallcase, $uppercase)" />
</xsl:template>
</xsl:stylesheet>