VirtualBox

source: vbox/trunk/src/libs/dita-ot-1.8.5/xsl/common/dita-utilities.xsl@ 98727

Last change on this file since 98727 was 98584, checked in by vboxsync, 2 years ago

Docs: bugref:10302. Setting svn properties of DITA-OT library.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.2 KB
Line 
1<?xml version="1.0" encoding="utf-8"?>
2<!-- This file is part of the DITA Open Toolkit project hosted on
3 Sourceforge.net. See the accompanying license.txt file for
4 applicable licenses.-->
5<!-- (c) Copyright IBM Corp. 2004, 2005 All Rights Reserved. -->
6
7<!-- Common utilities that can be used by DITA transforms -->
8<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
9 <xsl:param name="DEFAULTLANG">en-us</xsl:param>
10 <!-- Function to convert a string to lower case -->
11
12 <xsl:variable name="pixels-per-inch" select="number(96)"/>
13
14 <xsl:template name="convert-to-lower">
15 <xsl:param name="inputval"/>
16 <xsl:value-of
17 select="translate($inputval, '._-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+=!@#$%^&amp;*()[]{};:\/&lt;&gt;,~?', '._-abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz+=!@#$%^&amp;*()[]{};:\/&lt;&gt;,~?')"
18 />
19 </xsl:template>
20 <!-- Function to determine the current language, and return it in lower case -->
21 <xsl:template name="getLowerCaseLang">
22 <xsl:variable name="ancestorlangUpper">
23 <!-- the current xml:lang value (en-us if none found) -->
24 <xsl:choose>
25 <xsl:when test="ancestor-or-self::*/@xml:lang">
26 <xsl:value-of select="ancestor-or-self::*[@xml:lang][1]/@xml:lang"/>
27 </xsl:when>
28 <xsl:otherwise>
29 <xsl:value-of select="$DEFAULTLANG"/>
30 </xsl:otherwise>
31 </xsl:choose>
32 </xsl:variable>
33 <xsl:call-template name="convert-to-lower">
34 <!-- ensure lowercase for comparisons -->
35 <xsl:with-param name="inputval" select="$ancestorlangUpper"/>
36 </xsl:call-template>
37 </xsl:template>
38
39 <xsl:template match="*" mode="get-first-topic-lang">
40 <xsl:variable name="first-topic-lang">
41 <xsl:choose>
42 <xsl:when test="/*[@xml:lang]"><xsl:value-of select="/*/@xml:lang"/></xsl:when>
43 <xsl:when test="/dita/*[@xml:lang]"><xsl:value-of select="/dita/*[@xml:lang][1]/@xml:lang"/></xsl:when>
44 <xsl:otherwise><xsl:value-of select="$DEFAULTLANG"/></xsl:otherwise>
45 </xsl:choose>
46 </xsl:variable>
47 <xsl:call-template name="convert-to-lower">
48 <xsl:with-param name="inputval" select="$first-topic-lang"/>
49 </xsl:call-template>
50 </xsl:template>
51
52 <xsl:template match="*" mode="get-render-direction">
53 <xsl:param name="lang">
54 <xsl:apply-templates select="/*" mode="get-first-topic-lang"/>
55 </xsl:param>
56 <xsl:choose>
57 <xsl:when test="$lang='ar-eg' or $lang='ar'">rtl</xsl:when>
58 <xsl:when test="$lang='he-il' or $lang='he'">rtl</xsl:when>
59 <xsl:when test="$lang='ur-pk' or $lang='ur'">rtl</xsl:when>
60 <xsl:otherwise>ltr</xsl:otherwise>
61 </xsl:choose>
62 </xsl:template>
63
64 <!-- Function to get translated text for a common string.
65 * Each language is stored in a unique file. The association between a language and
66 its translations is stored in $stringFileList.
67 * Default file associations are in strings.xml.
68 * Once the file for a language is found, look for the translation in that file.
69 * If the correct file or translation are not found, use the default language.
70
71 If adding translations for a specialization, create a new version of strings.xml,
72 to indicate which languages are supported, and the name of each language file.
73 When calling this template, pass in the new association file as $stringFileList.
74
75 To reset the default language, import this template, and then set the DEFAULTLANG
76 parameter in the importing topic. Or, just pass it in on the command line.
77 -->
78 <xsl:template name="getString">
79 <xsl:param name="stringName"/>
80 <xsl:param name="stringFileList" select="document('allstrings.xml')/allstrings/stringfile"/>
81 <xsl:param name="stringFile">#none#</xsl:param>
82 <xsl:param name="ancestorlang">
83 <!-- Get the current language -->
84 <xsl:call-template name="getLowerCaseLang"/>
85 </xsl:param>
86 <xsl:choose>
87 <xsl:when test="$stringFile != '#none#'">
88 <!-- Use the old getString template interface -->
89 <!-- Get the translated string -->
90 <xsl:variable name="str"
91 select="$stringFile/strings/str[@name=$stringName][lang($ancestorlang)]"/>
92 <xsl:choose>
93 <!-- If the string was found, use it. Cannot test $str, because value could be empty. -->
94 <xsl:when test="$stringFile/strings/str[@name=$stringName][lang($ancestorlang)]">
95 <xsl:value-of select="$str"/>
96 </xsl:when>
97 <!-- If the current language is not the default language, try the default -->
98 <xsl:when test="$ancestorlang!=$DEFAULTLANG">
99 <!-- Determine which file holds the defaults; then get the default translation. -->
100 <xsl:variable name="str-default"
101 select="$stringFile/strings/str[@name=$stringName][lang($DEFAULTLANG)]"/>
102 <xsl:choose>
103 <!-- If a default was found, use it, but warn that fallback was needed.-->
104 <xsl:when test="string-length($str-default)>0">
105 <xsl:value-of select="$str-default"/>
106 <xsl:call-template name="output-message">
107 <xsl:with-param name="msgnum">001</xsl:with-param>
108 <xsl:with-param name="msgsev">W</xsl:with-param>
109 <xsl:with-param name="msgparams">%1=<xsl:value-of select="$stringName"/>;%2=<xsl:value-of select="$ancestorlang"/>;%3=<xsl:value-of select="$DEFAULTLANG"/></xsl:with-param>
110 </xsl:call-template>
111 </xsl:when>
112 <!-- Translation was not even found in the default language. -->
113 <xsl:otherwise>
114 <xsl:value-of select="$stringName"/>
115 <xsl:call-template name="output-message">
116 <xsl:with-param name="msgnum">052</xsl:with-param>
117 <xsl:with-param name="msgsev">W</xsl:with-param>
118 <xsl:with-param name="msgparams">%1=<xsl:value-of select="$stringName"/></xsl:with-param>
119 </xsl:call-template>
120 </xsl:otherwise>
121 </xsl:choose>
122 </xsl:when>
123 <!-- The current language is the default; no translation found at all. -->
124 <xsl:otherwise>
125 <xsl:value-of select="$stringName"/>
126 <xsl:call-template name="output-message">
127 <xsl:with-param name="msgnum">052</xsl:with-param>
128 <xsl:with-param name="msgsev">W</xsl:with-param>
129 <xsl:with-param name="msgparams">%1=<xsl:value-of select="$stringName"/></xsl:with-param>
130 </xsl:call-template>
131 </xsl:otherwise>
132 </xsl:choose>
133 </xsl:when>
134 <xsl:otherwise>
135 <!-- Use the new getString template interface -->
136 <!-- Determine which file holds translations for the current language -->
137 <xsl:variable name="stringfile"
138 select="document($stringFileList)/*/lang[@xml:lang=$ancestorlang]/@filename"/>
139 <!-- Get the translated string -->
140 <xsl:variable name="str" select="document($stringfile)/strings/str[@name=$stringName]"/>
141 <xsl:choose>
142 <!-- If the string was found, use it. -->
143 <xsl:when test="count($str) &gt; 0">
144 <xsl:value-of select="$str[last()]"/>
145 </xsl:when>
146 <!-- If the current language is not the default language, try the default -->
147 <xsl:when test="$ancestorlang!=$DEFAULTLANG">
148 <!-- Determine which file holds the defaults; then get the default translation. -->
149 <xsl:variable name="backupstringfile"
150 select="document($stringFileList)/*/lang[@xml:lang=$DEFAULTLANG]/@filename"/>
151 <xsl:variable name="str-default"
152 select="document($backupstringfile)/strings/str[@name=$stringName]"/>
153 <xsl:choose>
154 <!-- If a default was found, use it, but warn that fallback was needed.-->
155 <xsl:when test="count($str-default) &gt; 0">
156 <xsl:value-of select="$str-default[last()]"/>
157 <xsl:call-template name="output-message">
158 <xsl:with-param name="msgnum">001</xsl:with-param>
159 <xsl:with-param name="msgsev">W</xsl:with-param>
160 <xsl:with-param name="msgparams">%1=<xsl:value-of select="$stringName"/>;%2=<xsl:value-of select="$ancestorlang"/>;%3=<xsl:value-of select="$DEFAULTLANG"/></xsl:with-param>
161 </xsl:call-template>
162 </xsl:when>
163 <!-- Translation was not even found in the default language. -->
164 <xsl:otherwise>
165 <xsl:value-of select="$stringName"/>
166 <xsl:call-template name="output-message">
167 <xsl:with-param name="msgnum">052</xsl:with-param>
168 <xsl:with-param name="msgsev">W</xsl:with-param>
169 <xsl:with-param name="msgparams">%1=<xsl:value-of select="$stringName"/></xsl:with-param>
170 </xsl:call-template>
171 </xsl:otherwise>
172 </xsl:choose>
173 </xsl:when>
174 <!-- The current language is the default; no translation found at all. -->
175 <xsl:otherwise>
176 <xsl:value-of select="$stringName"/>
177 <xsl:call-template name="output-message">
178 <xsl:with-param name="msgnum">052</xsl:with-param>
179 <xsl:with-param name="msgsev">W</xsl:with-param>
180 <xsl:with-param name="msgparams">%1=<xsl:value-of select="$stringName"/></xsl:with-param>
181 </xsl:call-template>
182 </xsl:otherwise>
183 </xsl:choose>
184 </xsl:otherwise>
185 </xsl:choose>
186 </xsl:template>
187
188 <xsl:template name="length-to-pixels">
189 <xsl:param name="dimen"/>
190 <!-- We handle units of cm, mm, in, pt, pc, px. We also accept em,
191 but just treat 1em=1pc. An omitted unit is taken as px. -->
192
193 <xsl:variable name="dimenx" select="concat('00',$dimen)"/>
194 <xsl:variable name="units" select="substring($dimenx,string-length($dimenx)-1)"/>
195 <xsl:variable name="numeric-value" select="number(substring($dimenx,1,string-length($dimenx)-2))"/>
196 <xsl:choose>
197 <xsl:when test="string(number($units))!='NaN' and string(number($numeric-value))!='NaN'">
198 <!-- Since $units is a number, the input was unitless, so we default
199 the unit to pixels and just return the input value -->
200 <xsl:value-of select="round(number(concat($numeric-value,$units)))"/>
201 </xsl:when>
202 <xsl:when test="string(number($numeric-value))='NaN'">
203 <!-- If the input isn't valid, just return 100% -->
204 <xsl:value-of select="'100%'"/>
205 </xsl:when>
206 <xsl:when test="$units='cm'">
207 <xsl:value-of select="number(round($numeric-value * $pixels-per-inch div 2.54))"/>
208 </xsl:when>
209 <xsl:when test="$units='mm'">
210 <xsl:value-of select="number(round($numeric-value * $pixels-per-inch div 25.4))"/>
211 </xsl:when>
212 <xsl:when test="$units='in'">
213 <xsl:value-of select="number(round($numeric-value * $pixels-per-inch))"/>
214 </xsl:when>
215 <xsl:when test="$units='pt'">
216 <xsl:value-of select="number(round($numeric-value * $pixels-per-inch div 72))"/>
217 </xsl:when>
218 <xsl:when test="$units='pc'">
219 <xsl:value-of select="number(round($numeric-value * $pixels-per-inch div 6))"/>
220 </xsl:when>
221 <xsl:when test="$units='px'">
222 <xsl:value-of select="number(round($numeric-value))"/>
223 </xsl:when>
224 <xsl:when test="$units='em'">
225 <xsl:value-of select="number(round($numeric-value * $pixels-per-inch div 6))"/>
226 </xsl:when>
227 <xsl:otherwise>
228 <!-- If the input isn't valid, just return 100% -->
229 <xsl:value-of select="'100%'"/>
230 </xsl:otherwise>
231 </xsl:choose>
232 </xsl:template>
233
234 <!-- replace all the blank in file name or directory with %20 -->
235 <xsl:template name="replace-blank">
236 <xsl:param name="file-origin"></xsl:param>
237 <xsl:choose>
238 <xsl:when test="contains($file-origin,' ')">
239 <xsl:call-template name="replace-blank">
240 <xsl:with-param name="file-origin">
241 <xsl:value-of select="substring-before($file-origin,' ')"/>%20<xsl:value-of select="substring-after($file-origin,' ')"/>
242 </xsl:with-param>
243 </xsl:call-template>
244 </xsl:when>
245 <xsl:otherwise>
246 <xsl:value-of select="$file-origin"/>
247 </xsl:otherwise>
248 </xsl:choose>
249 </xsl:template>
250
251<!-- Return the portion of an HREF value up to the file's extension. This assumes
252 that the file has an extension, and that the topic and/or element ID does not
253 contain a period. Written to allow references such as com.example.dita.files/file.dita#topic -->
254<!-- Deprecated: use replace-extension instead -->
255<xsl:template match="*" mode="parseHrefUptoExtension">
256 <xsl:param name="href" select="@href"/>
257 <xsl:variable name="uptoDot"><xsl:value-of select="substring-before($href,'.')"/></xsl:variable>
258 <xsl:variable name="afterDot"><xsl:value-of select="substring-after($href,'.')"/></xsl:variable>
259 <xsl:value-of select="$uptoDot"/>
260 <xsl:choose>
261 <!-- No more periods, so this is at the extension -->
262 <xsl:when test="not(contains($afterDot,'.'))"/>
263 <!-- Multiple slashes; at least one must be a directory, so it's before the extension -->
264 <xsl:when test="contains(substring-after($afterDot,'/'),'/')">
265 <xsl:text>.</xsl:text>
266 <xsl:value-of select="substring-before($afterDot,'/')"/>
267 <xsl:text>/</xsl:text>
268 <xsl:apply-templates select="." mode="parseHrefUptoExtension"><xsl:with-param name="href" select="substring-after($afterDot,'/')"/></xsl:apply-templates>
269 </xsl:when>
270 <!-- Multiple periods, no slashes, no topic or element ID, so the file name contains more periods -->
271 <xsl:when test="not(contains($afterDot,'#'))">
272 <xsl:text>.</xsl:text>
273 <xsl:apply-templates select="." mode="parseHrefUptoExtension"><xsl:with-param name="href" select="$afterDot"/></xsl:apply-templates>
274 </xsl:when>
275 <!-- Multiple periods, no slashes, with #. Move to next period. Needs additional work to support
276 IDs containing periods. -->
277 <xsl:otherwise>
278 <xsl:text>.</xsl:text>
279 <xsl:apply-templates select="." mode="parseHrefUptoExtension"><xsl:with-param name="href" select="$afterDot"/></xsl:apply-templates>
280 </xsl:otherwise>
281 </xsl:choose>
282</xsl:template>
283
284 <!-- Template returns "true" if $text parameter string ends with the $with parameter string, and otherwise returns "false". -->
285 <xsl:template name="ends-with">
286 <xsl:param name="text"/>
287 <xsl:param name="with"/>
288 <xsl:value-of select="substring($text, string-length($text) - string-length($with) + 1) = $with"/>
289 </xsl:template>
290
291 <!-- Get filename base -->
292 <xsl:template name="getFileName">
293 <xsl:param name="filename"/>
294 <xsl:param name="extension"/>
295 <xsl:choose>
296 <xsl:when test="contains($filename, $extension)">
297 <xsl:call-template name="substring-before-last">
298 <xsl:with-param name="text" select="$filename"/>
299 <xsl:with-param name="delim" select="$extension"/>
300 </xsl:call-template>
301 </xsl:when>
302 <xsl:otherwise>
303 <xsl:value-of select="$filename"/>
304 </xsl:otherwise>
305 </xsl:choose>
306 </xsl:template>
307
308 <!-- Replace file extension in a URI -->
309 <xsl:template name="replace-extension">
310 <xsl:param name="filename"/>
311 <xsl:param name="extension"/>
312 <xsl:param name="ignore-fragment" select="false()"/>
313 <xsl:variable name="f">
314 <xsl:call-template name="substring-before-last">
315 <xsl:with-param name="text">
316 <xsl:choose>
317 <xsl:when test="contains($filename, '#')">
318 <xsl:value-of select="substring-before($filename, '#')"/>
319 </xsl:when>
320 <xsl:otherwise>
321 <xsl:value-of select="$filename"/>
322 </xsl:otherwise>
323 </xsl:choose>
324 </xsl:with-param>
325 <xsl:with-param name="delim" select="'.'"/>
326 </xsl:call-template>
327 </xsl:variable>
328 <xsl:if test="string($f)">
329 <xsl:value-of select="concat($f, $extension)"/>
330 </xsl:if>
331 <xsl:if test="not($ignore-fragment) and contains($filename, '#')">
332 <xsl:value-of select="concat('#', substring-after($filename, '#'))"/>
333 </xsl:if>
334 </xsl:template>
335
336 <xsl:template name="substring-before-last">
337 <xsl:param name="text"/>
338 <xsl:param name="delim"/>
339
340 <xsl:if test="string($text) and string($delim)">
341 <xsl:value-of select="substring-before($text, $delim)" />
342 <xsl:variable name="tail" select="substring-after($text, $delim)" />
343 <xsl:if test="contains($tail, $delim)">
344 <xsl:value-of select="$delim" />
345 <xsl:call-template name="substring-before-last">
346 <xsl:with-param name="text" select="$tail" />
347 <xsl:with-param name="delim" select="$delim" />
348 </xsl:call-template>
349 </xsl:if>
350 </xsl:if>
351 </xsl:template>
352
353 <xsl:template match="processing-instruction('workdir-uri')" mode="get-work-dir">
354 <xsl:value-of select="."/>
355 </xsl:template>
356
357 <xsl:template match="processing-instruction('path2project-uri')" mode="get-path2project">
358 <xsl:choose>
359 <!-- Backwards compatibility with path2project that is empty when current directory is the root directory -->
360 <xsl:when test=". = './'"/>
361 <xsl:otherwise>
362 <xsl:value-of select="."/>
363 </xsl:otherwise>
364 </xsl:choose>
365 </xsl:template>
366
367 <xsl:template match="processing-instruction('path2project')" mode="get-path2project">
368 <xsl:call-template name="get-path2project">
369 <xsl:with-param name="s" select="."/>
370 </xsl:call-template>
371 </xsl:template>
372
373 <xsl:template name="get-path2project">
374 <!-- Deal with being handed a Windows backslashed path by accident. -->
375 <!-- This code only changes \ to / and doesn't handle the many other situations
376 where a URI differs from a file path. Hopefully they don't occur in path2proj anyway. -->
377 <xsl:param name="s"/>
378 <xsl:choose>
379 <xsl:when test="contains($s, '\')">
380 <xsl:value-of select="substring-before($s, '\')"/>
381 <xsl:text>/</xsl:text>
382 <xsl:call-template name="get-path2project">
383 <xsl:with-param name="s" select="substring-after($s, '\')"/>
384 </xsl:call-template>
385 </xsl:when>
386 <xsl:otherwise>
387 <xsl:value-of select="$s"/>
388 </xsl:otherwise>
389 </xsl:choose>
390 </xsl:template>
391
392</xsl:stylesheet>
393
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette