<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: xlink-analyzer.xsl 459 2007-04-05 01:45:36Z dret $ -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink">
	<xsl:output media-type="xml" indent="yes"/>
	<xsl:param name="linkbase" select="''"/>
	<xsl:template match="/">
		<html>
			<head>
				<title>XLink Analyzer</title>
				<style type="text/css">*:target { outline : red solid medium ; }</style>
			</head>
			<body>
				<xsl:apply-templates select="/" mode="link-analyzer"/>
			</body>
		</html>
	</xsl:template>
	<xsl:template match="/" mode="link-analyzer">
		<xsl:param name="input-doc" select="'stdin'"/>
		<xsl:param name="linkbase-origin" select="''"/>
		<table border="1" cellspacing="1" cellpadding="4">
			<thead>
				<tr>
					<th colspan="8">
						<xsl:value-of select="count(descendant::*[@xlink:type = 'simple' or @xlink:type = 'extended'])"/>
						<xsl:text> Links found in </xsl:text>
						<xsl:value-of select="$input-doc"/>
						<xsl:text>:</xsl:text>
					</th>
				</tr>
				<tr>
					<th>Link Path</th>
					<th>Link Type</th>
					<th>#</th>
					<th>Arcs to</th>
					<th>Arcs from</th>
					<th>Resource Type</th>
					<th>Fragment</th>
					<th>Resources</th>
				</tr>
			</thead>
			<tbody>
				<xsl:for-each select="descendant::*[@xlink:type = 'simple' or @xlink:type = 'extended']">
					<tr>
						<td rowspan="{count(*[@xlink:type = 'locator' or @xlink:type = 'resource'])}">
							<xsl:call-template name="make-step">
								<xsl:with-param name="context" select="/"/>
								<xsl:with-param name="node" select="."/>
							</xsl:call-template>
						</td>
						<th rowspan="{count(*[@xlink:type = 'locator' or @xlink:type = 'resource'])}">
							<xsl:choose>
								<xsl:when test="@xlink:type = 'simple'">
									<span title="simple">S</span>
								</xsl:when>
								<xsl:when test="@xlink:type = 'extended'">
									<span title="extended">E</span>
								</xsl:when>
							</xsl:choose>
							<xsl:if test="@xlink:type = 'extended'">
								<xsl:text>/</xsl:text>
								<xsl:choose>
									<xsl:when test="ancestor::html:body | ancestor::body">
										<span title="inline">I</span>
									</xsl:when>
									<xsl:otherwise>
										<span title="out-of-line">O</span>
									</xsl:otherwise>
								</xsl:choose>
								<xsl:text>/</xsl:text>
								<xsl:choose>
									<xsl:when test="$linkbase-origin = 'l'">
										<span title="linkbase">l</span>
									</xsl:when>
									<xsl:when test="$linkbase-origin = 'l3'">
										<span title="3rd party linkbase">l3</span>
									</xsl:when>
									<xsl:otherwise>
										<span title="embedded">e</span>
									</xsl:otherwise>
								</xsl:choose>
							</xsl:if>
						</th>
						<xsl:for-each select="*[@xlink:type = 'locator' or @xlink:type = 'resource'][1]">
							<xsl:call-template name="resource">
								<xsl:with-param name="offset" select="0"/>
							</xsl:call-template>
						</xsl:for-each>
					</tr>
					<xsl:for-each select="*[@xlink:type = 'locator' or @xlink:type = 'resource'][position() > 1]">
						<tr>
							<xsl:call-template name="resource">
								<xsl:with-param name="offset" select="1"/>
							</xsl:call-template>
						</tr>
					</xsl:for-each>
				</xsl:for-each>
			</tbody>
		</table>
		<xsl:for-each select="//*[@xlink:type = 'arc'][@xlink:arcrole = 'http://www.w3.org/1999/xlink/properties/linkbase']">
			<xsl:variable name="linkbase" select="../*[@xlink:type = 'locator'][@xlink:label = current()/@xlink:to]/@xlink:href"/>
			<xsl:apply-templates select="document($linkbase)" mode="link-analyzer">
				<xsl:with-param name="input-doc" select="$linkbase"/>
				<xsl:with-param name="linkbase-origin" select="'l'"/>
			</xsl:apply-templates>
		</xsl:for-each>
		<xsl:if test="$linkbase != '' and $input-doc != $linkbase">
			<xsl:apply-templates select="document($linkbase)" mode="link-analyzer">
				<xsl:with-param name="input-doc" select="$linkbase"/>
				<xsl:with-param name="linkbase-origin" select="'l3'"/>
			</xsl:apply-templates>
		</xsl:if>
	</xsl:template>
	<xsl:template name="make-step">
		<xsl:param name="context"/>
		<xsl:param name="node"/>
		<xsl:text>/</xsl:text>
		<xsl:variable name="step" select="$context/*[count(. | $node/ancestor-or-self::*) = count($node/ancestor-or-self::*)]"/>
		<xsl:value-of select="name($step)"/>
		<xsl:if test="count($step/../*[name() = name($step)]) > 1">
			<xsl:text>[</xsl:text>
			<xsl:value-of select="1 + count($step/preceding-sibling::*[name() = name($step)])"/>
			<xsl:text>]</xsl:text>
		</xsl:if>
		<xsl:if test="generate-id($step) != generate-id($node)">
			<xsl:call-template name="make-step">
				<xsl:with-param name="context" select="$step"/>
				<xsl:with-param name="node" select="."/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	<xsl:template name="resource">
		<xsl:param name="offset" select="0"/>
		<td id="{generate-id()}">
			<span title="{concat('&lt;', name(), ' label=&quot;', @xlink:label, '&quot; role=&quot;', @xlink:role, '&quot; title=&quot;', @xlink:title, '&quot;/>')}">
				<xsl:value-of select="position() + $offset"/>
			</span>
		</td>
		<td>
			<xsl:variable name="resource" select="."/>
			<xsl:choose>
				<xsl:when test="../*[@xlink:type = 'arc']">
					<xsl:for-each select="../*[@xlink:type = 'arc']">
						<xsl:variable name="arc" select="."/>
						<xsl:variable name="resources" select="../*[@xlink:type = 'resource' or @xlink:type = 'locator'][$resource/@xlink:label = $arc/@xlink:from or (not($arc/@xlink:from))][@xlink:label = $arc/@xlink:to or (not($arc/@xlink:to))]"/>
						<xsl:if test="$resources">
							<span style="border : 1px dotted" title="{concat('&lt;', name($arc), ' show=&quot;', $arc/@xlink:show, '&quot; actuate=&quot;', $arc/@xlink:actuate, '&quot; arcrole=&quot;', $arc/@xlink:arcrole, '&quot; title=&quot;', $arc/@xlink:title, '&quot;/>')}">
								<xsl:text>&#160;</xsl:text>
								<xsl:for-each select="$resources">
									<a href="#{generate-id()}">
										<xsl:value-of select="1 + count(preceding-sibling::*[@xlink:type = 'resource' or @xlink:type = 'locator'])"/>
									</a>
									<xsl:text>&#160;</xsl:text>
								</xsl:for-each>
							</span>
							<xsl:text> </xsl:text>
						</xsl:if>
						<xsl:text> </xsl:text>
					</xsl:for-each>
				</xsl:when>
				<xsl:otherwise>
					<xsl:for-each select="../*[@xlink:type = 'resource' or @xlink:type = 'locator'][generate-id() != generate-id(current())]">
						<a href="#{generate-id()}" title="default arc">
							<xsl:value-of select="1 + count(preceding-sibling::*[@xlink:type = 'resource' or @xlink:type = 'locator'])"/>
						</a>
						<xsl:text> </xsl:text>
					</xsl:for-each>
				</xsl:otherwise>
			</xsl:choose>
		</td>
		<td>
			<xsl:variable name="resource" select="."/>
			<xsl:choose>
				<xsl:when test="../*[@xlink:type = 'arc']">
					<xsl:for-each select="../*[@xlink:type = 'arc']">
						<xsl:variable name="arc" select="."/>
						<xsl:variable name="resources" select="../*[@xlink:type = 'resource' or @xlink:type = 'locator'][$resource/@xlink:label = $arc/@xlink:to or (not($arc/@xlink:to))][@xlink:label = $arc/@xlink:from or (not($arc/@xlink:from))]"/>
						<xsl:if test="$resources">
							<span style="border : 1px dotted" title="{concat('&lt;', name($arc), ' show=&quot;', $arc/@xlink:show, '&quot; actuate=&quot;', $arc/@xlink:actuate, '&quot; arcrole=&quot;', $arc/@xlink:arcrole, '&quot; title=&quot;', $arc/@xlink:title, '&quot;/>')}">
								<xsl:text>&#160;</xsl:text>
								<xsl:for-each select="$resources">
									<a href="#{generate-id()}">
										<xsl:value-of select="1 + count(preceding-sibling::*[@xlink:type = 'resource' or @xlink:type = 'locator'])"/>
									</a>
									<xsl:text>&#160;</xsl:text>
								</xsl:for-each>
							</span>
							<xsl:text> </xsl:text>
						</xsl:if>
						<xsl:text> </xsl:text>
					</xsl:for-each>
				</xsl:when>
				<xsl:otherwise>
					<xsl:for-each select="../*[@xlink:type = 'resource' or @xlink:type = 'locator'][generate-id() != generate-id(current())]">
						<a href="#{generate-id()}" title="default arc">
							<xsl:value-of select="1 + count(preceding-sibling::*[@xlink:type = 'resource' or @xlink:type = 'locator'])"/>
						</a>
						<xsl:text> </xsl:text>
					</xsl:for-each>
				</xsl:otherwise>
			</xsl:choose>
		</td>
		<td>
			<xsl:if test="@xlink:type = 'locator'">
				<xsl:variable name="uri" select="substring-before(concat(@xlink:href, '#'), '#')"/>
				<xsl:choose>
					<xsl:when test="substring($uri, 1 + string-length($uri) - string-length('html')) = 'html' or substring($uri, 1 + string-length($uri) - string-length('htm')) = 'htm'">text/html</xsl:when>
					<xsl:when test="substring($uri, 1 + string-length($uri) - string-length('gif')) = 'gif'">img/gif</xsl:when>
					<xsl:when test="substring($uri, 1 + string-length($uri) - string-length('jpeg')) = 'jpeg' or substring($uri, 1 + string-length($uri) - string-length('jpg')) = 'jpg'">img/jpg</xsl:when>
					<xsl:when test="substring($uri, 1 + string-length($uri) - string-length('png')) = 'png'">img/png</xsl:when>
					<xsl:when test="substring($uri, 1 + string-length($uri) - string-length('mp3')) = 'mp3'">audio/mp3</xsl:when>
					<xsl:when test="substring($uri, 1 + string-length($uri) - string-length('mpg')) = 'mpg' or substring($uri, 1 + string-length($uri) - string-length('mpeg')) = 'mpeg'">video/mpeg</xsl:when>
					<xsl:otherwise>unknown</xsl:otherwise>
				</xsl:choose>
			</xsl:if>
		</td>
		<td>
			<xsl:if test="@xlink:type = 'locator'">
				<xsl:if test="contains(@xlink:href, '#')">
					<xsl:text> # </xsl:text>
					<xsl:value-of select="substring-after(@xlink:href, '#')"/>
				</xsl:if>
			</xsl:if>
		</td>
		<td>
			<xsl:choose>
				<xsl:when test="@xlink:type = 'locator'">
					<xsl:choose>
						<xsl:when test="@xlink:label">
							<xsl:value-of select="@xlink:label"/>
							<xsl:text>: </xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>Locator: </xsl:text>						
						</xsl:otherwise>
					</xsl:choose>
					<a href="{@xlink:href}">
						<xsl:if test="@xlink:title">
							<xsl:attribute name="title">
								<xsl:value-of select="@xlink:title"/>
							</xsl:attribute>
						</xsl:if>
						<xsl:choose>
							<xsl:when test="string-length(@xlink:href) > 80">
								<xsl:value-of select="substring(@xlink:href, 1, 80)"/>
								<xsl:text>... (truncated)</xsl:text>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="@xlink:href"/>
							</xsl:otherwise>
						</xsl:choose>
					</a>
				</xsl:when>
				<xsl:when test="@xlink:type = 'resource'">
					<xsl:choose>
						<xsl:when test="@xlink:label">
							<xsl:value-of select="@xlink:label"/>
							<xsl:text>: </xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>Resource: </xsl:text>						
						</xsl:otherwise>
					</xsl:choose>
					<span>
						<xsl:if test="@xlink:title">
							<xsl:attribute name="title">
								<xsl:value-of select="@xlink:title"/>
							</xsl:attribute>
						</xsl:if>
						<xsl:copy-of select="node()"/>
					</span>
				</xsl:when>
			</xsl:choose>
		</td>
	</xsl:template>
</xsl:stylesheet>
