(no commit message)
[utils] / crawler / kiss / src / 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:text> </xsl:text>
70           </xsl:with-param>
71           <xsl:with-param name="from">
72             <xsl:text>  </xsl:text>
73           </xsl:with-param>
74           <xsl:with-param name="to">
75             <xsl:text> </xsl:text>
76           </xsl:with-param>
77         </xsl:call-template>
78       </xsl:with-param>
79       <xsl:with-param name="width">
80         <xsl:value-of select="$width"/>
81       </xsl:with-param>
82       <xsl:with-param name="index">
83         <xsl:value-of select="0"/>
84       </xsl:with-param>
85     </xsl:call-template>
86   </xsl:template>
87
88   <xsl:template name="word-wrap-impl">
89     <xsl:param name="src"/>
90     <xsl:param name="index"/>
91     <xsl:param name="width"/>
92
93     <xsl:variable name="word">
94       <xsl:value-of select="substring-before($src, ' ')"/>
95     </xsl:variable>
96     <xsl:variable name="wordlength">
97       <xsl:value-of select="string-length($word)"/>
98     </xsl:variable>
99     <xsl:variable name="remainder">
100       <xsl:value-of select="substring($src, $wordlength+2)"/>
101     </xsl:variable>
102
103     <xsl:choose>
104       <xsl:when test="$index + $wordlength + 1 &gt; $width">
105         <xsl:value-of select="$newline"/>
106         <xsl:value-of select="$word"/>
107         <xsl:text> </xsl:text>
108         <xsl:if test="string-length($remainder) > 0">
109           <xsl:call-template name="word-wrap-impl">
110             <xsl:with-param name="src">
111               <xsl:value-of select="$remainder"/>
112             </xsl:with-param>
113             <xsl:with-param name="index">
114               <xsl:value-of select="$wordlength + 1"/>
115             </xsl:with-param>
116             <xsl:with-param name="width">
117               <xsl:value-of select="$width"/>
118             </xsl:with-param>
119           </xsl:call-template>
120         </xsl:if>
121       </xsl:when>
122       <xsl:otherwise>
123         <xsl:value-of select="$word"/>
124         <xsl:text> </xsl:text>
125
126         <xsl:if test="string-length($remainder) > 0">
127           <xsl:call-template name="word-wrap-impl">
128             <xsl:with-param name="src">
129               <xsl:value-of select="$remainder"/>
130             </xsl:with-param>
131             <xsl:with-param name="index">
132               <xsl:value-of select="$index + $wordlength+1"/>
133             </xsl:with-param>
134             <xsl:with-param name="width">
135               <xsl:value-of select="$width"/>
136             </xsl:with-param>
137           </xsl:call-template>
138         </xsl:if>
139       </xsl:otherwise>
140     </xsl:choose>
141   </xsl:template>
142
143 </xsl:stylesheet>