-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions.xsl
312 lines (286 loc) · 12.1 KB
/
functions.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:controls="http://scap.nist.gov/schema/sp800-53/feed/2.0" xmlns:def="http://scap.nist.gov/schema/sp800-53/2.0">
<xsl:template name="CamelCase">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text,' ')">
<xsl:call-template name="CamelCaseWord">
<xsl:with-param name="text" select="substring-before($text,' ')"/>
</xsl:call-template>
<xsl:text> </xsl:text>
<xsl:call-template name="CamelCase">
<xsl:with-param name="text" select="substring-after($text,' ')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="CamelCaseWord">
<xsl:with-param name="text" select="$text"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="CamelCaseWord">
<xsl:param name="text"/>
<xsl:value-of select="translate(substring($text,1,1),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')" /><xsl:value-of select="translate(substring($text,2,string-length($text)-1),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
</xsl:template>
<xsl:template match="text()" name="replaceNL">
<xsl:param name="pText" select="."/>
<xsl:choose>
<xsl:when test="contains($pText, '
')">
<xsl:value-of select="substring-before($pText, '
')"/>
<br />
<br />
<xsl:call-template name="replaceNL">
<xsl:with-param name="pText" select="substring-after($pText, '
')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pText"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Find and replace on whole words only -->
<xsl:template name="search-and-replace-whole-words-only">
<xsl:param name="input"/>
<xsl:param name="search-string"/>
<xsl:param name="replace-string"/>
<xsl:variable name="punc"
select="concat('.,;:( )[ ]!?$@&"></',"'")"/>
<xsl:choose>
<!-- See if the input contains the search string -->
<xsl:when test="contains($input,$search-string)">
<!-- If so, then test that the before and after characters are word
delimiters. -->
<xsl:variable name="before"
select="substring-before($input,$search-string)"/>
<xsl:variable name="before-char"
select="substring(concat(' ',$before),string-length($before) +1, 1)"/>
<xsl:variable name="after"
select="substring-after($input,$search-string)"/>
<xsl:variable name="after-char"
select="substring($after,1,1)"/>
<xsl:value-of select="$before"/>
<xsl:choose>
<xsl:when test="(not(normalize-space($before-char)) or
contains($punc,$before-char)) and
(not(normalize-space($after-char)) or
contains($punc,$after-char))">
<xsl:value-of select="$replace-string"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$search-string"/>
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="search-and-replace-whole-words-only">
<xsl:with-param name="input" select="$after"/>
<xsl:with-param name="search-string" select="$search-string"/>
<xsl:with-param name="replace-string" select="$replace-string"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- There are no more occurrences of the search string so
just return the current input string -->
<xsl:value-of select="$input"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="string-replace-all">
<xsl:param name="text"/>
<xsl:param name="replace" select="'\, 
, ", ",'"/>
<xsl:param name="by" select="'|'"/>
<xsl:param name="times" select="1"/>
<xsl:choose>
<!--Checks if $text contains the first replace string in the $replace value-->
<xsl:when test="contains($text, substring-before($replace,','))">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="concat(substring-before($text,substring-before($replace,',')),$by,substring-after($text,substring-before($replace,',')))"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="by" select="$by"/>
<xsl:with-param name="times" select="$times+1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<!--Checks to see if there is any strings left to replace-->
<xsl:when test="substring-after($replace,',') != ''">
<xsl:choose>
<!--Here you could call the template directly and the iteration will do the check to see if the next replace string is contained in the $text value-->
<!--but to save some iteration loops I did the work here-->
<xsl:when test="contains($text, substring-before(substring-after($replace,','),','))">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="concat(substring-before($text,substring-before(substring-after($replace,','),',')),$by,substring-after($text,substring-before(substring-after($replace,','),',')))"/>
<xsl:with-param name="replace" select="substring-after($replace,',')"/>
<xsl:with-param name="by" select="$by"/>
<xsl:with-param name="times" select="$times+1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="$text"/>
<xsl:with-param name="replace" select="substring-after($replace,',')"/>
<xsl:with-param name="by" select="$by"/>
<xsl:with-param name="times" select="$times+1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="translate(translate(translate(translate(translate(translate($text,'“',''),'”',''),'
',''),'"',''),'	',''),'"','') "/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--contant-->
<xsl:variable name="d">0123456789</xsl:variable>
<!-- ignore document text -->
<xsl:template match="text()[preceding-sibling::node() or following-sibling::node()]"/>
<!-- string -->
<xsl:template match="text()">
<xsl:call-template name="escape-string">
<xsl:with-param name="s" select="."/>
</xsl:call-template>
</xsl:template>
<!-- Main template for escaping strings; used by above template and for object-properties
Responsibilities: placed quotes around string, and chain up to next filter, escape-bs-string -->
<xsl:template name="escape-string">
<xsl:param name="s"/>
<xsl:text>"</xsl:text>
<xsl:call-template name="escape-bs-string">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
<xsl:text>"</xsl:text>
</xsl:template>
<!-- Escape the backslash (\) before everything else. -->
<xsl:template name="escape-bs-string">
<xsl:param name="s"/>
<xsl:choose>
<xsl:when test="contains($s,'\')">
<xsl:call-template name="escape-quot-string">
<xsl:with-param name="s" select="concat(substring-before($s,'\'),'\\')"/>
</xsl:call-template>
<xsl:call-template name="escape-bs-string">
<xsl:with-param name="s" select="substring-after($s,'\')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="escape-quot-string">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Escape the double quote ("). -->
<xsl:template name="escape-quot-string">
<xsl:param name="s"/>
<xsl:choose>
<xsl:when test="contains($s,'"')">
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="concat(substring-before($s,'"'),'\"')"/>
</xsl:call-template>
<xsl:call-template name="escape-quot-string">
<xsl:with-param name="s" select="substring-after($s,'"')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Replace tab, line feed and/or carriage return by its matching escape code. Can't escape backslash
or double quote here, because they don't replace characters (� becomes \t), but they prefix
characters (\ becomes \\). Besides, backslash should be seperate anyway, because it should be
processed first. This function can't do that. -->
<xsl:template name="encode-string">
<xsl:param name="s"/>
<xsl:choose>
<!-- tab -->
<xsl:when test="contains($s,'	')">
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="concat(substring-before($s,'	'),'\t',substring-after($s,'	'))"/>
</xsl:call-template>
</xsl:when>
<!-- line feed -->
<xsl:when test="contains($s,'
')">
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="concat(substring-before($s,'
'),'\n',substring-after($s,'
'))"/>
</xsl:call-template>
</xsl:when>
<!-- carriage return -->
<xsl:when test="contains($s,'
')">
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="concat(substring-before($s,'
'),'\r',substring-after($s,'
'))"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$s"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- number (no support for javascript mantissa) -->
<xsl:template match="text()[not(string(number())='NaN' or (starts-with(.,'0' ) and . != '0'))]">
<xsl:value-of select="."/>
</xsl:template>
<!-- boolean, case-insensitive -->
<xsl:template match="text()[translate(.,'TRUE','true')='true']">true</xsl:template>
<xsl:template match="text()[translate(.,'FALSE','false')='false']">false</xsl:template>
<!-- object -->
<xsl:template match="*" name="base">
<xsl:if test="not(preceding-sibling::*)">{</xsl:if>
<xsl:call-template name="escape-string">
<xsl:with-param name="s" select="name()"/>
</xsl:call-template>
<xsl:text>:</xsl:text>
<!-- check type of node -->
<xsl:choose>
<!-- null nodes -->
<xsl:when test="count(child::node())=0">null</xsl:when>
<!-- other nodes -->
<xsl:otherwise>
<xsl:apply-templates select="child::node()"/>
</xsl:otherwise>
</xsl:choose>
<!-- end of type check -->
<xsl:if test="following-sibling::*">,</xsl:if>
<xsl:if test="not(following-sibling::*)">}</xsl:if>
</xsl:template>
<!-- array -->
<xsl:template match="*[count(../*[name(../*)=name(.)])=count(../*) and count(../*)>1]">
<xsl:if test="not(preceding-sibling::*)">[</xsl:if>
<xsl:choose>
<xsl:when test="not(child::node())">
<xsl:text>null</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="child::node()"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="following-sibling::*">,</xsl:if>
<xsl:if test="not(following-sibling::*)">]</xsl:if>
</xsl:template>
<!-- convert root element to an anonymous container -->
<xsl:template match="/">
<xsl:apply-templates select="node()"/>
</xsl:template>
<xsl:template match="item" mode="stig-menu" >
<xsl:param name="selectedStig"/>
<option>
<xsl:attribute name="value">
<xsl:value-of select="@file-ref" />
</xsl:attribute>
<xsl:if test="@stig-title = $selectedStig">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="text()" />
</option>
</xsl:template>
<xsl:template name="dodImage">
<xsl:copy-of select="document('includes.xml')/includes/ref[@id='stigLogo']"/>
</xsl:template>
<xsl:template name="lib_Css">
<xsl:copy-of select="document('includes.xml')/includes/ref[@id='lib_Css']"/>
</xsl:template>
<xsl:template name="lib_Js">
<xsl:copy-of select="document('includes.xml')/includes/ref[@id='lib_Js']"/>
</xsl:template>
</xsl:stylesheet>