(no commit message)
[utils] / support / resources / test / org / wamblee / xml / utilities.xsl
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!-- Note the declaration of the namespace for XInclude. -->
4 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
5   xmlns:xi="http://www.w3.org/2001/XInclude">
6
7
8   <xsl:variable name="newline">
9     <xsl:text>&#10;</xsl:text>
10   </xsl:variable>
11
12   <xsl:variable name="carriageReturn">
13     <xsl:text>&#13;&#10;</xsl:text>
14   </xsl:variable>
15
16   <!-- =====================================================
17     Replace one string by another
18     - src: string to do substituion in
19     - from: literal string to replace
20     - to:substitution string.
21     ======================================================-->
22   <xsl:template name="string-replace">
23     <xsl:param name="src"/>
24     <xsl:param name="from"/>
25     <xsl:param name="to"/>
26     <xsl:choose>
27       <xsl:when test="contains($src, $from)">
28         <xsl:value-of select="substring-before($src, $from)"/>
29         <xsl:value-of select="$to"/>
30         <xsl:call-template name="string-replace">
31           <xsl:with-param name="src" select="substring-after($src, $from)"/>
32           <xsl:with-param name="from" select="$from"/>
33           <xsl:with-param name="to" select="$to"/>
34         </xsl:call-template>
35       </xsl:when>
36       <xsl:otherwise>
37         <xsl:value-of select="$src"/>
38       </xsl:otherwise>
39     </xsl:choose>
40   </xsl:template>
41
42   <xsl:template name="indent">
43     <xsl:param name="src"/>
44     <xsl:param name="indentString"/>
45     <xsl:value-of select="$indentString"/>
46     <xsl:call-template name="string-replace">
47       <xsl:with-param name="src">
48         <xsl:value-of select="$src"/>
49       </xsl:with-param>
50       <xsl:with-param name="from">
51         <xsl:value-of select="$newline"/>
52       </xsl:with-param>
53       <xsl:with-param name="to">
54         <xsl:value-of select="$newline"/>
55         <xsl:value-of select="$indentString"/>
56       </xsl:with-param>
57     </xsl:call-template>
58   </xsl:template>
59
60   <xsl:template name="word-wrap">
61     <xsl:param name="src"/>
62     <xsl:param name="width"/>
63
64     <xsl:call-template name="word-wrap-impl">
65       <xsl:with-param name="src">
66         <xsl:call-template name="string-replace">
67           <xsl:with-param name="src">
68             <xsl:value-of select="$src"/>
69           </xsl:with-param>
70           <xsl:with-param name="from">
71             <xsl:text>  </xsl:text>
72           </xsl:with-param>
73           <xsl:with-param name="to">
74             <xsl:text> </xsl:text>
75           </xsl:with-param>
76         </xsl:call-template>
77       </xsl:with-param>
78       <xsl:with-param name="width">
79         <xsl:value-of select="$width"/>
80       </xsl:with-param>
81       <xsl:with-param name="index">
82         <xsl:value-of select="0"/>
83       </xsl:with-param>
84     </xsl:call-template>
85   </xsl:template>
86
87   <xsl:template name="word-wrap-impl">
88     <xsl:param name="src"/>
89     <xsl:param name="index"/>
90     <xsl:param name="width"/>
91
92     <xsl:variable name="word">
93       <xsl:value-of select="substring-before($src, ' ')"/>
94     </xsl:variable>
95     <xsl:variable name="wordlength">
96       <xsl:value-of select="string-length($word)"/>
97     </xsl:variable>
98     <xsl:variable name="remainder">
99       <xsl:value-of select="substring($src, $wordlength+2)"/>
100     </xsl:variable>
101
102     <xsl:choose>
103       <xsl:when test="$index + $wordlength + 1 &gt; $width">
104         <xsl:value-of select="$newline"/>
105         <xsl:value-of select="$word"/>
106         <xsl:text> </xsl:text>
107         <xsl:if test="string-length($remainder) > 0">
108           <xsl:call-template name="word-wrap-impl">
109             <xsl:with-param name="src">
110               <xsl:value-of select="$remainder"/>
111             </xsl:with-param>
112             <xsl:with-param name="index">
113               <xsl:value-of select="$wordlength + 1"/>
114             </xsl:with-param>
115             <xsl:with-param name="width">
116               <xsl:value-of select="$width"/>
117             </xsl:with-param>
118           </xsl:call-template>
119         </xsl:if>
120       </xsl:when>
121       <xsl:otherwise>
122         <xsl:value-of select="$word"/>
123         <xsl:text> </xsl:text>
124
125         <xsl:if test="string-length($remainder) > 0">
126           <xsl:call-template name="word-wrap-impl">
127             <xsl:with-param name="src">
128               <xsl:value-of select="$remainder"/>
129             </xsl:with-param>
130             <xsl:with-param name="index">
131               <xsl:value-of select="$index + $wordlength+1"/>
132             </xsl:with-param>
133             <xsl:with-param name="width">
134               <xsl:value-of select="$width"/>
135             </xsl:with-param>
136           </xsl:call-template>
137         </xsl:if>
138       </xsl:otherwise>
139     </xsl:choose>
140   </xsl:template>
141
142 </xsl:stylesheet>