Skip to content

Commit

Permalink
Change namespace and prefix in UUID and message handling code
Browse files Browse the repository at this point in the history
For all functions, use `xmlns:x3f="http://csrc.nist.gov/ns/xslt3-functions"`
  • Loading branch information
galtm committed Mar 2, 2023
1 parent 1f96869 commit ff9c513
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 85 deletions.
2 changes: 1 addition & 1 deletion message-handler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This directory contains XSLT named templates for handling error and warning messages:

* `mh:message-handler` template: Emit message to console, or output processing instruction instead.
* `x3f:message-handler` template: Emit message to console, or output processing instruction instead.



8 changes: 4 additions & 4 deletions message-handler/message-handler.xsl
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:mh="http://csrc.nist.gov/ns/message"
xmlns:x3f="http://csrc.nist.gov/ns/xslt3-functions"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">

<xsl:param name="mh:returns_pi" as="xs:boolean" select="false()"/>
<xsl:param name="x3f:returns_pi" as="xs:boolean" select="false()"/>

<xsl:template name="mh:message-handler">
<xsl:template name="x3f:message-handler">
<xsl:param name="text" as="xs:string"/>
<xsl:param name="message-type" as="xs:string?"/><!-- e.g., 'Error', 'Warning' -->
<xsl:param name="error-code" as="xs:string?"/>
<xsl:param name="terminate" as="xs:boolean" select="false()"/>
<xsl:param name="returns_pi" as="xs:boolean" select="$mh:returns_pi"/>
<xsl:param name="returns_pi" as="xs:boolean" select="$x3f:returns_pi"/>
<xsl:variable name="joined-string" as="xs:string"
select="string-join(($message-type, $error-code, $text),': ')"/>
<xsl:choose expand-text="yes">
Expand Down
18 changes: 9 additions & 9 deletions message-handler/tests/message-handler.xspec
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<x:description
xmlns:x="http://www.jenitennison.com/xslt/xspec"
xmlns:mh="http://csrc.nist.gov/ns/message"
xmlns:x3f="http://csrc.nist.gov/ns/xslt3-functions"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
stylesheet="../message-handler.xsl"
xslt-version="3.0">

<x:param name="mh:returns_pi" as="xs:boolean" select="true()"/>
<x:param name="x3f:returns_pi" as="xs:boolean" select="true()"/>

<x:scenario label="Tests for mh:message-handler template">
<x:scenario label="Tests for x3f:message-handler template">
<x:scenario label="Call with text, type, and code">
<x:call template="mh:message-handler">
<x:call template="x3f:message-handler">
<x:param name="text" select="'message text'"/>
<x:param name="message-type" select="'Warning'"/>
<x:param name="error-code" select="'ERR1'"/>
Expand All @@ -20,7 +20,7 @@
</x:expect>
</x:scenario>
<x:scenario label="Call with text and type only">
<x:call template="mh:message-handler">
<x:call template="x3f:message-handler">
<x:param name="text" select="'message text'"/>
<x:param name="message-type" select="'Warning'"/>
</x:call>
Expand All @@ -29,7 +29,7 @@
</x:expect>
</x:scenario>
<x:scenario label="Call with text and code only">
<x:call template="mh:message-handler">
<x:call template="x3f:message-handler">
<x:param name="text" select="'message text'"/>
<x:param name="error-code" select="'ERR1'"/>
</x:call>
Expand All @@ -38,7 +38,7 @@
</x:expect>
</x:scenario>
<x:scenario label="Call with text only">
<x:call template="mh:message-handler">
<x:call template="x3f:message-handler">
<x:param name="text" select="'message text'"/>
</x:call>
<x:expect label="PI indicating text">
Expand All @@ -47,7 +47,7 @@
</x:scenario>
<x:scenario label="Call with text and type only, with terminate=true: ">
<x:scenario label="Check that message really terminates transform" catch="yes">
<x:call template="mh:message-handler">
<x:call template="x3f:message-handler">
<x:param name="text" select="'message text'"/>
<x:param name="message-type" select="'Error'"/>
<x:param name="terminate" select="true()"/>
Expand All @@ -56,7 +56,7 @@
<x:expect label="Error" test="$x:result instance of map(*) and $x:result('err') instance of map(*)"/>
</x:scenario>
<x:scenario label="Check PI">
<x:call template="mh:message-handler">
<x:call template="x3f:message-handler">
<x:param name="text" select="'message text'"/>
<x:param name="message-type" select="'Error'"/>
<x:param name="terminate" select="true()"/>
Expand Down
8 changes: 4 additions & 4 deletions random-util/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

This directory contains XSLT functions and named templates for working with pseudorandom numbers:

* `u:determine-uuid` template: Return UUID string based on choice of generation options.
* `r:make-uuid` function: Return one v4 UUID.
* `r:make-uuid-sequence` function: Return sequence of v4 UUIDs.
* `r:make-random-string-sequence` function: Return sequence of strings based on user-specified pattern.
* `x3f:determine-uuid` template: Return UUID string based on choice of generation options.
* `x3f:make-uuid` function: Return one v4 UUID.
* `x3f:make-uuid-sequence` function: Return sequence of v4 UUIDs.
* `x3f:make-random-string-sequence` function: Return sequence of strings based on user-specified pattern.



44 changes: 22 additions & 22 deletions random-util/random-util.xsl
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math r"
xmlns:r="http://csrc.nist.gov/ns/random" version="3.0">
xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="#all"
xmlns:x3f="http://csrc.nist.gov/ns/xslt3-functions" version="3.0">

<!-- from the spec https://www.w3.org/TR/xpath-functions-31/#func-random-number-generator
declare %public function r:random-sequence($length as xs:integer) as xs:double* {
r:random-sequence($length, fn:random-number-generator())
declare %public function x3f:random-sequence($length as xs:integer) as xs:double* {
x3f:random-sequence($length, fn:random-number-generator())
};
declare %private function r:random-sequence($length as xs:integer,
declare %private function x3f:random-sequence($length as xs:integer,
$G as map(xs:string, item())) {
if ($length eq 0)
then ()
else ($G?number, r:random-sequence($length - 1, $G?next()))
else ($G?number, x3f:random-sequence($length - 1, $G?next()))
};
r:random-sequence(200);
x3f:random-sequence(200);
v4 UUID
hex fields 8 4 4 4 12
Expand All @@ -27,7 +27,7 @@ v4 UUID
-->
<xsl:output indent="yes"/>

<!-- Set $germ to a string for reproducible outputs of r:make-uuid.
<!-- Set $germ to a string for reproducible outputs of x3f:make-uuid.
Pass in a blind value - and don't save it - for irreproducible outputs. -->

<xsl:param name="germ" select="current-dateTime() || document-uri(/)"/>
Expand All @@ -36,47 +36,47 @@ v4 UUID
<xsl:template match="/" name="xsl:initial-template" expand-text="true">
<!--<uuid><xsl:value-of select="uuid:randomUUID()" xmlns:uuid="java:java.util.UUID"/></uuid>-->
<randomness>
<now>{ r:make-uuid(current-dateTime()) }</now>
<germ>{ r:make-uuid($germ) }</germ>
<a>{ r:make-uuid('a') }</a>
<a>{ r:make-uuid('a') }</a>
<b>{ r:make-uuid('b') }</b>
<now>{ x3f:make-uuid(current-dateTime()) }</now>
<germ>{ x3f:make-uuid($germ) }</germ>
<a>{ x3f:make-uuid('a') }</a>
<a>{ x3f:make-uuid('a') }</a>
<b>{ x3f:make-uuid('b') }</b>
<ten>
<xsl:for-each select="r:make-uuid-sequence($germ, 10)">
<xsl:for-each select="x3f:make-uuid-sequence($germ, 10)">
<uuid>{ . }</uuid>
</xsl:for-each>
</ten>
</randomness>
</xsl:template>

<!-- r:make-uuid produces one v4 UUID. Output is repeatable for a given seed.
<!-- x3f:make-uuid produces one v4 UUID. Output is repeatable for a given seed.
If the random-number-generator() function is not available,
this function returns an empty sequence. -->
<xsl:function name="r:make-uuid" as="xs:string?">
<xsl:function name="x3f:make-uuid" as="xs:string?">
<xsl:param name="seed" as="item()"/>
<xsl:sequence select="r:make-uuid-sequence($seed, 1)"/>
<xsl:sequence select="x3f:make-uuid-sequence($seed, 1)"/>
</xsl:function>

<!-- r:make-uuid-sequence produces a sequence of $seq-length v4 UUIDs.
<!-- x3f:make-uuid-sequence produces a sequence of $seq-length v4 UUIDs.
Output is repeatable for a given seed. If the random-number-generator()
function is not available, this function returns an empty sequence. -->
<xsl:function name="r:make-uuid-sequence" as="xs:string*">
<xsl:function name="x3f:make-uuid-sequence" as="xs:string*">
<xsl:param name="seed" as="item()"/>
<xsl:param name="seq-length" as="xs:integer"/>
<xsl:variable name="uuid-v4-template" as="xs:string">________-____-4___-=___-____________</xsl:variable>
<!-- a847eaab-cec8-41bd-98e2-02d02900b554 -->
<xsl:sequence select="r:make-random-string-sequence($seed, $seq-length, $uuid-v4-template)"/>
<xsl:sequence select="x3f:make-random-string-sequence($seed, $seq-length, $uuid-v4-template)"/>
</xsl:function>

<!-- r:make-random-string-sequence produces a sequence of $seq-length strings.
<!-- x3f:make-random-string-sequence produces a sequence of $seq-length strings.
The $template parameter specifies the pattern of characters in each
string, where:
* '_' becomes a random hex value 0-9a-f
* '=' becomes one of '8','9','a','b' at random
* Any other character is copied to the output string
Output is repeatable for a given seed. If the random-number-generator()
function is not available, this function returns an empty sequence. -->
<xsl:function name="r:make-random-string-sequence" as="xs:string*">
<xsl:function name="x3f:make-random-string-sequence" as="xs:string*">
<xsl:param name="seed" as="item()"/>
<xsl:param name="seq-length" as="xs:integer"/>
<xsl:param name="template" as="xs:string"/>
Expand Down
38 changes: 19 additions & 19 deletions random-util/tests/random-util.xspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<x:description xmlns:x="http://www.jenitennison.com/xslt/xspec"
xmlns:ov="http://csrc.nist.gov/ns/oscal/xspec/variable"
xmlns:r="http://csrc.nist.gov/ns/random"
xmlns:x3f="http://csrc.nist.gov/ns/xslt3-functions"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
stylesheet="../random-util.xsl"
Expand All @@ -18,33 +18,33 @@

<x:scenario label="Tests for top-level template">
<x:call template="xsl:initial-template"/>
<!-- Assertions here are minimal because r:make-uuid
<!-- Assertions here are minimal because x3f:make-uuid
has function-level tests below. -->
<x:expect label="Repeatable result for the seed, 'a'"
test="$x:result/a[1]/string()" select="$x:result/a[2]/string()"/>
<x:expect label="The 'b' uuid is different from the 'a' uuid"
test="not($x:result/a[1]/string() = $x:result/b/string())"/>
</x:scenario>

<x:scenario label="Tests for r:make-uuid function">
<x:call function="r:make-uuid">
<x:scenario label="Tests for x3f:make-uuid function">
<x:call function="x3f:make-uuid">
<x:param name="seed" select="$ov:seed"/>
</x:call>
<x:expect label="Same as calling r:make-uuid-sequence with seq-length=1"
select="r:make-uuid-sequence($ov:seed, 1)"/>
<x:expect label="Same as calling x3f:make-uuid-sequence with seq-length=1"
select="x3f:make-uuid-sequence($ov:seed, 1)"/>
</x:scenario>
<x:scenario label="Tests for r:make-uuid-sequence function">
<x:scenario label="Tests for x3f:make-uuid-sequence function">
<x:scenario label="seq-length=1">
<x:variable name="ov:seq-length" as="xs:integer" select="1"/>
<x:call function="r:make-uuid-sequence">
<x:call function="x3f:make-uuid-sequence">
<x:param name="seed" select="$ov:seed"/>
<x:param name="seq-length" select="$ov:seq-length"/>
</x:call>
<x:like label="SHARED: Check sequence of uuids"/>
</x:scenario>
<x:scenario label="seq-length=10000">
<x:variable name="ov:seq-length" as="xs:integer" select="10000"/>
<x:call function="r:make-uuid-sequence">
<x:call function="x3f:make-uuid-sequence">
<x:param name="seed" select="$ov:seed"/>
<x:param name="seq-length" select="$ov:seq-length"/>
</x:call>
Expand All @@ -56,7 +56,7 @@
</x:scenario>
<x:scenario label="seq-length=500,000" pending="To save time, run only when needed">
<x:variable name="ov:seq-length" as="xs:integer" select="500000"/>
<x:call function="r:make-uuid-sequence">
<x:call function="x3f:make-uuid-sequence">
<x:param name="seed" select="$ov:seed"/>
<x:param name="seq-length" select="$ov:seq-length"/>
</x:call>
Expand All @@ -65,17 +65,17 @@
test="$x:result => distinct-values() => count() = $ov:seq-length"/>
</x:scenario>
<x:scenario label="Edge case: seq-length=0">
<x:call function="r:make-uuid-sequence">
<x:call function="x3f:make-uuid-sequence">
<x:param name="seed" select="$ov:seed"/>
<x:param name="seq-length" as="xs:integer" select="0"/>
</x:call>
<x:expect label="Nothing" select="()"/>
</x:scenario>
</x:scenario>
<x:scenario label="Tests for r:make-random-string-sequence function">
<x:scenario label="Tests for x3f:make-random-string-sequence function">
<x:scenario label="Template starts with _">
<x:variable name="ov:template" select="'_xyz'"/>
<x:call function="r:make-random-string-sequence">
<x:call function="x3f:make-random-string-sequence">
<x:param name="seed" select="$ov:seed"/>
<x:param name="seq-length" select="1"/>
<x:param name="template" select="$ov:template"/>
Expand All @@ -89,7 +89,7 @@
</x:scenario>
<x:scenario label="Template starts with =">
<x:variable name="ov:template" select="'=xyz'"/>
<x:call function="r:make-random-string-sequence">
<x:call function="x3f:make-random-string-sequence">
<x:param name="seed" select="$ov:seed"/>
<x:param name="seq-length" select="1"/>
<x:param name="template" select="$ov:template"/>
Expand All @@ -102,7 +102,7 @@
test="not(matches(substring($x:result,2),'[89ab]'))"/>
</x:scenario>
<x:scenario label="Template is empty string">
<x:call function="r:make-random-string-sequence">
<x:call function="x3f:make-random-string-sequence">
<x:param name="seed" select="$ov:seed"/>
<x:param name="seq-length" select="1"/>
<x:param name="template" select="''"/>
Expand All @@ -112,7 +112,7 @@
<x:scenario label="Long template of repeated '_' characters">
<x:variable name="ov:template" select="string-join(for $j in (1 to 10000) return '_','')"/>
<x:variable name="ov:expected-digits" as="xs:string" select="'^[0-9a-f]+$'"/>
<x:call function="r:make-random-string-sequence">
<x:call function="x3f:make-random-string-sequence">
<x:param name="seed" select="$ov:seed"/>
<x:param name="seq-length" select="1"/>
<x:param name="template" select="$ov:template"/>
Expand All @@ -122,7 +122,7 @@
<x:scenario label="Long template of repeated '=' characters">
<x:variable name="ov:template" select="string-join(for $j in (1 to 10000) return '=','')"/>
<x:variable name="ov:expected-digits" as="xs:string" select="'^[89ab]+$'"/>
<x:call function="r:make-random-string-sequence">
<x:call function="x3f:make-random-string-sequence">
<x:param name="seed" select="$ov:seed"/>
<x:param name="seq-length" select="1"/>
<x:param name="template" select="$ov:template"/>
Expand Down Expand Up @@ -199,9 +199,9 @@
<x:expect label="Each string matches uuid regular expression"
test="every $uuid in $x:result satisfies matches($uuid, $ov:uuid-v4-regex)"/>
<x:expect label="Check repeatability for same seed"
test="deep-equal($x:result, r:make-uuid-sequence($ov:seed, $ov:seq-length))"/>
test="deep-equal($x:result, x3f:make-uuid-sequence($ov:seed, $ov:seq-length))"/>
<x:expect label="Not typically equal to function output for a different seed"
test="not(deep-equal($x:result, r:make-uuid-sequence($ov:seed || '1', $ov:seq-length)))"/>
test="not(deep-equal($x:result, x3f:make-uuid-sequence($ov:seed || '1', $ov:seq-length)))"/>
</x:scenario>

</x:description>
Loading

0 comments on commit ff9c513

Please sign in to comment.