Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/tools/boostbook/xsl/lookup.xsl @ 12

Last change on this file since 12 was 12, checked in by landauf, 18 years ago

added boost

File size: 12.8 KB
Line 
1<?xml version="1.0" encoding="utf-8"?>
2<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3                version="1.0">
4
5  <xsl:param name="boost.max.id.length">26</xsl:param>
6
7  <!-- Generate an ID for the entity referenced -->
8  <xsl:template name="generate.id">
9    <xsl:param name="node" select="."/>
10    <xsl:variable name="id">
11      <xsl:apply-templates select="$node" mode="generate.id"/>
12    </xsl:variable>
13    <xsl:choose>
14      <xsl:when test="string-length($id) &gt; $boost.max.id.length">
15        <xsl:value-of select="generate-id($node)"/>
16      </xsl:when>
17      <xsl:otherwise>
18        <xsl:value-of select="string($id)"/>
19      </xsl:otherwise>
20    </xsl:choose>
21  </xsl:template>
22
23  <xsl:template match="*" mode="generate.id">
24    <xsl:value-of select="generate-id(.)"/>
25    <xsl:text>-bb</xsl:text>
26  </xsl:template>
27
28  <!-- Strip the qualifiers off a qualified name and return the unqualified
29       name. For instance, "boost::python::function" would become just
30       "function". -->
31  <xsl:template name="strip-qualifiers">
32    <xsl:param name="name"/>
33    <xsl:choose>
34      <xsl:when test="contains($name, '::')">
35        <xsl:call-template name="strip-qualifiers">
36          <xsl:with-param name="name" select="substring-after($name, '::')"/>
37        </xsl:call-template>
38      </xsl:when>
39      <xsl:otherwise>
40        <xsl:value-of select="$name"/>
41      </xsl:otherwise>
42    </xsl:choose>
43  </xsl:template>
44
45  <!-- Build the fully-qualified name of the given node -->
46  <xsl:template name="fully-qualified-name">
47    <xsl:param name="node"/>
48    <xsl:param name="separator" select="'::'"/>
49    <xsl:apply-templates select="$node" mode="fully-qualified-name">
50      <xsl:with-param name="separator" select="$separator"/>
51    </xsl:apply-templates>
52  </xsl:template>
53
54  <!-- Hack to make the node we are building the current node so that the
55       ancestor:: syntax will work -->
56  <xsl:template match="*" mode="fully-qualified-name">
57    <xsl:param name="separator" select="'::'"/>
58    <xsl:call-template name="build-fully-qualified-name">
59      <xsl:with-param name="separator" select="$separator"/>
60    </xsl:call-template>
61  </xsl:template>
62
63  <!-- The real routine that builds a fully-qualified name for the current
64       node. -->
65  <xsl:template name="build-fully-qualified-name">
66    <xsl:param name="separator" select="'::'"/>
67
68    <!-- The depth of qualified name element that we will print now-->
69    <xsl:param name="depth" select="1"/>
70
71    <!-- Determine the set of ancestor namespaces -->
72    <xsl:variable name="ancestors" 
73      select="ancestor::namespace|ancestor::class"/>
74
75    <xsl:choose>
76      <xsl:when test="$depth &gt; count($ancestors)">
77        <xsl:apply-templates select="." mode="print-name"/>
78      </xsl:when>
79      <xsl:otherwise>
80        <xsl:if test="name($ancestors[$depth])='namespace' or
81                      count(ancestor::free-function-group)=0">
82          <xsl:apply-templates select="$ancestors[$depth]" mode="print-name"/>
83          <xsl:value-of select="$separator"/>
84        </xsl:if>
85        <xsl:call-template name="build-fully-qualified-name">
86          <xsl:with-param name="separator" select="$separator"/>
87          <xsl:with-param name="depth" select="$depth + 1"/>
88        </xsl:call-template>
89      </xsl:otherwise>
90    </xsl:choose>
91  </xsl:template>
92
93  <!-- Print the name of the current node -->
94  <xsl:template match="*" mode="print-name">
95    <xsl:value-of select="@name"/>
96  </xsl:template>
97
98  <xsl:template name="name-matches-node">
99    <!-- The name we are looking for -->
100    <xsl:param name="name"/>
101
102    <!-- The name to display -->
103    <xsl:param name="display-name" select="$name"/>
104
105    <!-- The context in which this name occurs -->
106    <xsl:param name="context"/>
107
108    <!-- The node that we are checking against -->
109    <xsl:param name="node"/>
110
111    <!-- The mode we are in. Can be one of:
112           matches: emits the matches as they are found (for debugging)
113           link: link to the node that was found
114         -->
115    <xsl:param name="mode" select="'matches'"/>
116
117    <!-- The index into the list of using directives for the context node -->
118    <xsl:param name="index" select="1"/>
119
120    <!-- The prefix we should append to the name when checking this node -->
121    <xsl:param name="prefix" select="''"/>
122
123    <xsl:choose>
124      <xsl:when test="count($node) &gt; 1">
125        <xsl:variable name="matches">
126          <xsl:call-template name="count-matches">
127            <xsl:with-param name="name" select="$name"/>
128            <xsl:with-param name="context" select="$context"/>
129            <xsl:with-param name="nodes" select="$node[position() = 1]"/>
130          </xsl:call-template>
131        </xsl:variable>
132
133        <xsl:choose>
134          <xsl:when test="$matches = 0">
135            <xsl:call-template name="name-matches-node">
136              <xsl:with-param name="name" select="$name"/>
137              <xsl:with-param name="display-name" select="$display-name"/>
138              <xsl:with-param name="context" select="$context"/>
139              <xsl:with-param name="node" select="$node[position() &gt; 1]"/>
140              <xsl:with-param name="mode" select="$mode"/>
141            </xsl:call-template>
142          </xsl:when>
143          <xsl:otherwise>
144            <xsl:call-template name="name-matches-node">
145              <xsl:with-param name="name" select="$name"/>
146              <xsl:with-param name="display-name" select="$display-name"/>
147              <xsl:with-param name="context" select="$context"/>
148              <xsl:with-param name="node" select="$node[position() = 1]"/>
149              <xsl:with-param name="mode" select="$mode"/>
150            </xsl:call-template>
151          </xsl:otherwise>
152        </xsl:choose>
153      </xsl:when>
154      <xsl:when test="count($node) = 1">
155        <!-- The fully-qualified name of the node we are checking against -->
156        <xsl:variable name="fully-qualified-name">
157          <xsl:call-template name="fully-qualified-name">
158            <xsl:with-param name="node" select="$node"/>
159          </xsl:call-template>
160        </xsl:variable>
161       
162        <!-- The set of using directives for this context node -->
163        <xsl:variable name="directives"
164          select="$context/ancestor::*/using-namespace |
165                  $context/ancestor::namespace |
166                  $context/ancestor::*/using-class |
167                  $context/ancestor::class"/>
168       
169        <!-- The name of the current directive -->
170        <xsl:variable name="this-context">
171          <xsl:apply-templates select="$directives[$index]" mode="print-name"/>
172        </xsl:variable>
173       
174        <!-- Check if we have a match -->
175        <xsl:variable name="have-match" 
176          select="$fully-qualified-name = concat($prefix, $name)"/>
177       
178        <xsl:if test="$have-match">
179          <xsl:choose>
180            <xsl:when test="$mode='matches'">
181              Match in namespace ::<xsl:value-of select="$prefix"/>
182            </xsl:when>
183            <xsl:when test="$mode='link'">
184              <xsl:call-template name="internal-link">
185                <xsl:with-param name="to">
186                  <xsl:call-template name="generate.id">
187                    <xsl:with-param name="node" select="$node"/>
188                  </xsl:call-template>
189                </xsl:with-param>
190                <xsl:with-param name="text" select="$display-name"/>
191              </xsl:call-template>
192            </xsl:when>
193          </xsl:choose>
194        </xsl:if>
195       
196        <xsl:if test="(not($index &gt; count($directives))) and
197                      (not($have-match) or ($mode = 'matches'))">
198          <xsl:variable name="first-branch">
199            <xsl:if test="not ($prefix = '')">
200              <!-- Recurse and append the current context node to the prefix -->
201              <xsl:call-template name="name-matches-node">
202                <xsl:with-param name="name" select="$name"/>
203                <xsl:with-param name="display-name" select="$display-name"/>
204                <xsl:with-param name="context" select="$context"/>
205                <xsl:with-param name="node" select="$node"/>
206                <xsl:with-param name="mode" select="$mode"/>
207                <xsl:with-param name="index" select="$index + 1"/>
208                <xsl:with-param name="prefix"
209                  select="concat($prefix, $this-context, '::')"/>
210              </xsl:call-template>
211            </xsl:if>
212          </xsl:variable>
213         
214          <xsl:choose>
215            <xsl:when test="string($first-branch) != ''">
216              <xsl:copy-of select="$first-branch"/>
217            </xsl:when>
218            <xsl:otherwise>
219              <!-- Recurse with just the current context node -->
220              <xsl:call-template name="name-matches-node">
221                <xsl:with-param name="name" select="$name"/>
222                <xsl:with-param name="display-name" select="$display-name"/>
223                <xsl:with-param name="context" select="$context"/>
224                <xsl:with-param name="node" select="$node"/>
225                <xsl:with-param name="mode" select="$mode"/>
226                <xsl:with-param name="index" select="$index + 1"/>
227                <xsl:with-param name="prefix" 
228                  select="concat($this-context, '::')"/>
229              </xsl:call-template>
230            </xsl:otherwise>
231          </xsl:choose>
232        </xsl:if>       
233      </xsl:when>
234    </xsl:choose>
235  </xsl:template>
236
237  <!-- Count the number of nodes in the set that match the given name and
238       lookup context -->
239  <xsl:template name="count-matches">
240    <xsl:param name="name"/>
241    <xsl:param name="context"/>
242    <xsl:param name="nodes"/>
243
244    <xsl:variable name="match-string">
245      <xsl:for-each select="$nodes">
246        <xsl:variable name="does-match">
247          <xsl:call-template name="name-matches-node">
248            <xsl:with-param name="name" select="$name"/>
249            <xsl:with-param name="context" select="$context"/>
250            <xsl:with-param name="node" select="."/>
251          </xsl:call-template>
252        </xsl:variable>
253        <xsl:if test="not($does-match='')">X</xsl:if>
254      </xsl:for-each>
255    </xsl:variable>
256    <xsl:value-of select="string-length($match-string)"/>
257  </xsl:template>
258
259  <xsl:template name="cxx-link-name">
260    <!-- The actual lookup node -->
261    <xsl:param name="lookup"/> 
262
263    <!-- The type of name to lookup (e.g., class) -->
264    <xsl:param name="type"/> 
265
266    <!-- The name we are looking for -->
267    <xsl:param name="name"/> 
268
269    <!-- The name we will display  -->
270    <xsl:param name="display-name"/>
271
272    <!-- The name we are looking for (unqualified)-->
273    <xsl:param name="unqualified name"/> 
274
275    <!-- The list of nodes that match the lookup node in both name and type -->
276    <xsl:param name="nodes"/>
277
278    <!-- Count the number of nodes that match -->
279    <xsl:variable name="matches">
280      <xsl:call-template name="count-matches">
281        <xsl:with-param name="name" select="$name"/>
282        <xsl:with-param name="context" select="$lookup"/>
283        <xsl:with-param name="nodes" select="$nodes"/>
284      </xsl:call-template>
285    </xsl:variable>
286
287    <xsl:choose>
288      <xsl:when test="$matches = 0">
289        <xsl:message>
290          <xsl:text>Cannot find </xsl:text>
291          <xsl:value-of select="$type"/>
292          <xsl:text> named '</xsl:text>
293          <xsl:value-of select="$name"/>
294          <xsl:text>'</xsl:text>
295        </xsl:message>
296        <xsl:value-of select="$display-name"/>
297      </xsl:when>
298      <xsl:when test="$matches = 1">
299        <xsl:for-each select="$nodes">
300          <xsl:call-template name="name-matches-node">
301            <xsl:with-param name="name" select="$name"/>
302            <xsl:with-param name="display-name" select="$display-name"/>
303            <xsl:with-param name="context" select="$lookup"/>
304            <xsl:with-param name="node" select="."/>
305            <xsl:with-param name="mode" select="'link'"/>
306          </xsl:call-template>
307        </xsl:for-each>
308      </xsl:when>
309      <xsl:otherwise>
310        <xsl:message>
311          <xsl:text>Reference to </xsl:text>
312          <xsl:value-of select="$type"/>
313          <xsl:text> '</xsl:text>
314          <xsl:value-of select="$name"/>
315          <xsl:text>' is ambiguous. Found:</xsl:text>
316          <xsl:for-each select="$nodes">
317            <xsl:call-template name="name-matches-node">
318              <xsl:with-param name="name" select="$name"/>
319              <xsl:with-param name="context" select="$lookup"/>
320              <xsl:with-param name="node" select="."/>
321              <xsl:with-param name="mode" select="'matches'"/>
322            </xsl:call-template>
323          </xsl:for-each>
324        </xsl:message>
325        <xsl:call-template name="name-matches-node">
326          <xsl:with-param name="name" select="$name"/>
327          <xsl:with-param name="display-name" select="$display-name"/>
328          <xsl:with-param name="context" select="$lookup"/>
329          <xsl:with-param name="node" select="$nodes"/>
330          <xsl:with-param name="mode" select="'link'"/>
331        </xsl:call-template>
332      </xsl:otherwise>
333    </xsl:choose>
334  </xsl:template>
335</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.