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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
<!--
Copyright 2019 the Pacemaker project contributors
The version control history for this file may have further details.
Licensed under the GNU General Public License version 2 or later (GPLv2+).
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:accessrender="http://clusterlabs.org/ns/pacemaker/access/render/2"
xmlns:accessrendercfg="http://clusterlabs.org/ns/pacemaker/access/render/cfg">
<xsl:output method="text" encoding="UTF-8"/>
<!--
see https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bith;
note that we need to retain XML 1.0 (as opposed to 1.1, which in turn
is not supported in libxml) compatibility in this very template, meaning
we cannot output a superset of what's expressible in the template itself
(escaped or not), hence we are forced to work that around for \x1b (ESC,
unavoidable for ANSI colorized output) character with encoding it in some
way (here using "\x1b" literal notation) and requiring a trivial
"xsltproc ... | sed 's/\\x1b/\x1b/'" postprocessing;
the above, however, only applies when used directly (which may be the
reason to pay attention to this comment to begin with), but fortunately
it is conveniently avoidable when XSLT triggered programatically (see
pcmk__acl_evaled_render), since libxslt allows for passing raw (further
unchecked) parameter strings, in which case the actual content of those
parameters is decoded on the fly, meaning that this file is still open
to compilation-free customizations if there's an irresistible need...
-->
<xsl:param name="accessrendercfg:c-writable"><!-- green -->\x1b[32m</xsl:param>
<xsl:param name="accessrendercfg:c-readable"><!-- blue -->\x1b[34m</xsl:param>
<xsl:param name="accessrendercfg:c-denied"><!-- red -->\x1b[31m</xsl:param>
<xsl:param name="accessrendercfg:c-reset"><!-- reset -->\x1b[0m</xsl:param>
<xsl:param name="accessrender:extra-spacing">
<xsl:value-of select="'no'"/>
</xsl:param>
<xsl:param name="accessrender:self-reproducing-prefix">
<xsl:value-of select="''"/>
</xsl:param>
<xsl:variable name="accessrender:ns-writable" select="'http://clusterlabs.org/ns/pacemaker/access/writable'"/>
<xsl:variable name="accessrender:ns-readable" select="'http://clusterlabs.org/ns/pacemaker/access/readable'"/>
<xsl:variable name="accessrender:ns-denied" select="'http://clusterlabs.org/ns/pacemaker/access/denied'"/>
<!--
accessrender:interpolate-annotation named template
-->
<xsl:template name="accessrender:interpolate-annotation">
<xsl:choose>
<xsl:when test="namespace-uri() = $accessrender:ns-writable">
<xsl:value-of select="$accessrendercfg:c-writable"/>
</xsl:when>
<xsl:when test="namespace-uri() = $accessrender:ns-readable">
<xsl:value-of select="$accessrendercfg:c-readable"/>
</xsl:when>
<xsl:when test="namespace-uri() = $accessrender:ns-denied">
<xsl:value-of select="$accessrendercfg:c-denied"/>
</xsl:when>
</xsl:choose>
</xsl:template>
<!--
accessrender:namespaces mode
-->
<xsl:template match="*" mode="accessrender:namespaces">
<!-- assume c-writable is representative of others (c-readable, c-denied) -->
<xsl:if test="concat(
substring-before($accessrendercfg:c-writable, ':'),
':'
) = $accessrendercfg:c-writable">
<xsl:if test="//*[namespace-uri() = $accessrender:ns-writable]
or
//@*[namespace-uri() = $accessrender:ns-writable]">
<xsl:value-of select="concat(' xmlns:',
substring-before($accessrendercfg:c-writable, ':'),
'="', $accessrender:ns-writable, '"')"/>
</xsl:if>
<xsl:if test="//*[namespace-uri() = $accessrender:ns-readable]
or
//@*[namespace-uri() = $accessrender:ns-readable]">
<xsl:value-of select="concat(' xmlns:',
substring-before($accessrendercfg:c-readable, ':'),
'="', $accessrender:ns-readable, '"')"/>
</xsl:if>
<xsl:if test="//*[namespace-uri() = $accessrender:ns-denied]
or
//@*[namespace-uri() = $accessrender:ns-denied]">
<xsl:value-of select="concat(' xmlns:',
substring-before($accessrendercfg:c-denied, ':'),
'="', $accessrender:ns-denied, '"')"/>
</xsl:if>
</xsl:if>
</xsl:template>
<!--
accessrender:proceed mode
-->
<xsl:template match="*" mode="accessrender:proceed">
<xsl:variable name="whitespace-before">
<!-- ensure newline also for the root element -->
<xsl:choose>
<xsl:when test="preceding-sibling::text()[last()] != ''">
<xsl:value-of select="preceding-sibling::text()[last()]"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'
'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="extra-annotation">
<xsl:if test="namespace-uri() != namespace-uri(..)
or
$accessrendercfg:c-reset != ''">
<xsl:call-template name="accessrender:interpolate-annotation"/>
</xsl:if>
</xsl:variable>
<!-- tag opening -->
<xsl:choose>
<!-- special-casing based on $extra-annotation ending with colon -->
<xsl:when test="$accessrender:self-reproducing-prefix != ''
and
concat(
substring-before($extra-annotation, ':'),
':'
) = $extra-annotation">
<xsl:value-of select="concat('<', $extra-annotation, local-name())"/>
</xsl:when>
<xsl:when test="$accessrender:extra-spacing = 'yes'
and
$extra-annotation != ''">
<xsl:value-of select="concat(
preceding-sibling::text()[last()],
$extra-annotation,
$whitespace-before,
'<',
local-name()
)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($extra-annotation, '<', local-name())"/>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates mode="accessrender:proceed" select="@*"/>
<!-- for root and true XML output, figure out the namespaces used -->
<xsl:if test=". = /*
and
$accessrender:self-reproducing-prefix != ''">
<xsl:apply-templates mode="accessrender:namespaces" select="."/>
</xsl:if>
<!-- tag closing -->
<xsl:choose>
<xsl:when test="*|comment()|processing-instruction()">
<xsl:value-of select="'>'"/>
<xsl:apply-templates mode="accessrender:proceed" select="node()"/>
<xsl:choose>
<!-- special-casing based on $extra-annotation ending with colon -->
<xsl:when test="$accessrender:self-reproducing-prefix != ''
and
concat(
substring-before($extra-annotation, ':'),
':'
) = $extra-annotation">
<xsl:value-of select="concat(
'</',
$extra-annotation,
local-name(), '>'
)"/>
</xsl:when>
<xsl:otherwise>
<xsl:if test="$accessrender:extra-spacing = 'no'">
<xsl:value-of select="$extra-annotation"/>
</xsl:if>
<xsl:value-of select="concat(
'</',
local-name(),
'>'
)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'/>'"/>
<xsl:apply-templates mode="accessrender:proceed" select="node()"/>
</xsl:otherwise>
</xsl:choose>
<!-- do not taint any subsequent terminal session -->
<xsl:value-of select="$accessrendercfg:c-reset"/>
</xsl:template>
<xsl:template match="@*" mode="accessrender:proceed">
<!-- XXX especially "text" output untest{ed,able} since no support for
attribute granularity for now -->
<xsl:variable name="extra-annotation">
<xsl:if test="namespace-uri() != namespace-uri(..)">
<xsl:call-template name="accessrender:interpolate-annotation"/>
</xsl:if>
</xsl:variable>
<xsl:choose>
<xsl:when test="namespace-uri() != namespace-uri(..)
and
$accessrender:self-reproducing-prefix != ''">
<xsl:value-of select="' '"/>
<xsl:choose>
<xsl:when test="concat(
substring-before($extra-annotation, ':'),
':'
) = $extra-annotation">
<xsl:value-of select="substring-before($extra-annotation, ':')"/>
</xsl:when>
</xsl:choose>
<xsl:value-of select="concat(':', local-name(), '="', ., '"')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(' ', local-name(), '="', ., '"')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="comment()|processing-instruction()|text()" mode="accessrender:proceed">
<xsl:choose>
<xsl:when test="self::comment()">
<xsl:value-of select="'<!-- '"/>
</xsl:when>
<xsl:when test="self::processing-instruction()">
<xsl:value-of select="'<? '"/>
</xsl:when>
</xsl:choose>
<xsl:value-of select="."/>
<xsl:choose>
<xsl:when test="self::comment()">
<xsl:value-of select="' -->
'"/>
</xsl:when>
<xsl:when test="self::processing-instruction()">
<xsl:value-of select="'?>'
"/>
</xsl:when>
</xsl:choose>
</xsl:template>
<!-- mode-less, easy to override kick-off -->
<xsl:template match="/">
<xsl:apply-templates mode="accessrender:proceed" select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
|