pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / site / src / documentation / skins / common / xslt / html / pathutils.xsl
1 <?xml version="1.0"?>
2 <!--
3   Licensed to the Apache Software Foundation (ASF) under one or more
4   contributor license agreements.  See the NOTICE file distributed with
5   this work for additional information regarding copyright ownership.
6   The ASF licenses this file to You under the Apache License, Version 2.0
7   (the "License"); you may not use this file except in compliance with
8   the License.  You may obtain a copy of the License at
9
10       http://www.apache.org/licenses/LICENSE-2.0
11
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   See the License for the specific language governing permissions and
16   limitations under the License.
17 -->
18 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
19 <!--
20 PathUtils.xsl
21
22 A set of XSLT templates useful for parsing URI paths:
23
24 dirname: return the directory part of a path
25 filename: return the file part of a path
26 ext: return the last extension of the filename in a path
27 filename-noext: return the file part of a path without its last extension
28
29 -->
30 <!-- Returns the directory part of a path.  Equivalent to Unix 'dirname'.
31 Examples:
32 '' -> ''
33 'foo/index.html' -> 'foo/'
34 -->
35   <xsl:template name="dirname">
36     <xsl:param name="path" />
37     <xsl:if test="contains($path, '/')">
38       <xsl:value-of select="concat(substring-before($path, '/'), '/')" />
39       <xsl:call-template name="dirname">
40         <xsl:with-param name="path"
41         select="substring-after($path, '/')" />
42       </xsl:call-template>
43     </xsl:if>
44   </xsl:template>
45 <!-- Normalized (..'s eliminated) version of 'dirname' -->
46   <xsl:template name="dirname-nz">
47     <xsl:param name="path" />
48     <xsl:call-template name="normalize">
49       <xsl:with-param name="path">
50         <xsl:call-template name="dirname">
51           <xsl:with-param name="path" select="$path" />
52         </xsl:call-template>
53       </xsl:with-param>
54     </xsl:call-template>
55   </xsl:template>
56 <!-- Returns the filename part of a path.  Equivalent to Unix 'basename'
57 Examples:
58 'index.html'  ->  'index.html' 
59 'foo/bar/'  ->  '' 
60 'foo/bar/index.html'  ->  'index.html' 
61 -->
62   <xsl:template name="filename">
63     <xsl:param name="path"/>
64     <xsl:choose>
65       <xsl:when test="contains($path, '/')">
66         <xsl:call-template name="filename">
67           <xsl:with-param name="path" select="substring-after($path, '/')"/>
68         </xsl:call-template>
69       </xsl:when>
70       <xsl:otherwise>
71         <xsl:value-of select="$path"/>
72       </xsl:otherwise>
73     </xsl:choose>
74   </xsl:template>
75 <!-- Returns the last extension of a filename in a path.
76 Examples:
77 'index.html'  ->  '.html' 
78 'index.dtdx.html'  ->  '.html' 
79 'foo/bar/'  ->  '' 
80 'foo/bar/index.html'  ->  '.html' 
81 'foo/bar/index'  ->  '' 
82 -->
83   <xsl:template name="ext">
84     <xsl:param name="path"/>
85     <xsl:param name="subflag"/>
86 <!-- Outermost call? -->
87     <xsl:choose>
88       <xsl:when test="contains($path, '.')">
89         <xsl:call-template name="ext">
90           <xsl:with-param name="path" select="substring-after($path, '.')"/>
91           <xsl:with-param name="subflag" select="'sub'"/>
92         </xsl:call-template>
93       </xsl:when>
94 <!-- Handle extension-less filenames by returning '' -->
95       <xsl:when test="not($subflag) and not(contains($path, '.'))">
96 <xsl:text/>
97       </xsl:when>
98       <xsl:otherwise>
99         <xsl:value-of select="concat('.', $path)"/>
100       </xsl:otherwise>
101     </xsl:choose>
102   </xsl:template>
103 <!-- Returns a filename of a path stripped of its last extension.
104 Examples:
105 'foo/bar/index.dtdx.html' -> 'index.dtdx'
106 -->
107   <xsl:template name="filename-noext">
108     <xsl:param name="path"/>
109     <xsl:variable name="filename">
110       <xsl:call-template name="filename">
111         <xsl:with-param name="path" select="$path"/>
112       </xsl:call-template>
113     </xsl:variable>
114     <xsl:variable name="ext">
115       <xsl:call-template name="ext">
116         <xsl:with-param name="path" select="$filename"/>
117       </xsl:call-template>
118     </xsl:variable>
119     <xsl:value-of select="substring($filename, 1, string-length($filename) - string-length($ext))"/>
120   </xsl:template>
121 <!-- Returns a path with the filename stripped of its last extension.
122 Examples:
123 'foo/bar/index.dtdx.html' -> 'foo/bar/index.dtdx'
124 -->
125   <xsl:template name="path-noext">
126     <xsl:param name="path"/>
127     <xsl:variable name="ext">
128       <xsl:call-template name="ext">
129         <xsl:with-param name="path" select="$path"/>
130       </xsl:call-template>
131     </xsl:variable>
132     <xsl:value-of select="substring($path, 1, string-length($path) - string-length($ext))"/>
133   </xsl:template>
134 <!-- Normalized (..'s eliminated) version of 'path-noext' -->
135   <xsl:template name="path-noext-nz">
136     <xsl:param name="path" />
137     <xsl:call-template name="normalize">
138       <xsl:with-param name="path">
139         <xsl:call-template name="path-noext">
140           <xsl:with-param name="path" select="$path" />
141         </xsl:call-template>
142       </xsl:with-param>
143     </xsl:call-template>
144   </xsl:template>
145 <!-- Returns a path with any fragment identifier ('#...') stripped off
146 Examples:
147 'foo/bar/index.dtdx.html#blah' -> 'foo/bar/index.dtdx.html'
148 -->
149   <xsl:template name="path-nofrag">
150     <xsl:param name="path"/>
151     <xsl:if test="not(contains($path, '#'))">
152       <xsl:value-of select="$path"/>
153     </xsl:if>
154     <xsl:if test="contains($path, '#')">
155       <xsl:value-of select="substring-before($path, '#')"/>
156     </xsl:if>
157   </xsl:template>
158 <!-- Normalizes a path, converting '/' to '\' and eliminating ..'s
159 Examples:
160 'foo/bar/../baz/index.html' -> foo/baz/index.html'
161 -->
162   <xsl:template name="normalize">
163     <xsl:param name="path"/>
164 <!-- replace all \  with / -->
165     <xsl:variable name="path-" select="translate($path, '\', '/')"/>
166     <xsl:choose>
167 <!-- process relative refs here -->
168       <xsl:when test="contains($path-, '/../')">
169 <!--  put part before /../ into $pa: "foo/bar" -->
170         <xsl:variable name="pa" select="substring-before($path-, '/../')"/>
171 <!-- put part after first occurrence /../ into $th: "baz/index.html" -->
172         <xsl:variable name="th" select="substring-after($path-, '/../')"/>
173 <!-- cut last real directory name before /../ and put rest in $pa- : "foo/"  -->
174         <xsl:variable name="pa-">
175           <xsl:call-template name="dirname">
176             <xsl:with-param name="path" select="$pa"/>
177           </xsl:call-template>
178         </xsl:variable>
179 <!-- recombine pieces thus eliminating one .. and one dir step before it
180               and recurse into this template to eliminate more /../
181       -->
182         <xsl:variable name="pa-th" select="concat($pa-, $th)"/>
183         <xsl:call-template name="normalize">
184           <xsl:with-param name="path" select="$pa-th"/>
185         </xsl:call-template>
186       </xsl:when>
187       <xsl:otherwise>
188         <xsl:value-of select="$path-"/>
189       </xsl:otherwise>
190     </xsl:choose>
191   </xsl:template>
192 <!--
193 Uncomment this to test.
194 Usage: saxon pathutils.xsl pathutils.xsl path=foo/bar
195
196 <xsl:param name="path" select="'/foo/bar/../baz/index.html'"/>
197 <xsl:template match="/">
198   <xsl:message>
199     path           = <xsl:value-of select="$path"/>
200     normalize      = <xsl:call-template name="normalize">
201       <xsl:with-param name="path" select="$path"/>
202     </xsl:call-template>
203     dirname        = <xsl:call-template name="dirname">
204       <xsl:with-param name="path" select="$path"/>
205     </xsl:call-template>
206     dirname-nz     = <xsl:call-template name="dirname-nz">
207       <xsl:with-param name="path" select="$path"/>
208     </xsl:call-template>
209     filename       = <xsl:call-template name="filename">
210       <xsl:with-param name="path" select="$path"/>
211     </xsl:call-template>
212     ext            = <xsl:call-template name="ext">
213       <xsl:with-param name="path" select="$path"/>
214     </xsl:call-template>
215     filename-noext = <xsl:call-template name="filename-noext">
216       <xsl:with-param name="path" select="$path"/>
217     </xsl:call-template>
218     path-noext     = <xsl:call-template name="path-noext">
219       <xsl:with-param name="path" select="$path"/>
220     </xsl:call-template>
221     path-noext-nz  = <xsl:call-template name="path-noext-nz">
222       <xsl:with-param name="path" select="$path"/>
223     </xsl:call-template>
224     path-nofrag    = <xsl:call-template name="path-nofrag">
225       <xsl:with-param name="path" select="$path"/>
226     </xsl:call-template>
227  
228   </xsl:message>
229 </xsl:template>
230 -->
231 </xsl:stylesheet>