<xsl:stylesheet 
 	xmlns:xlink="http://www.w3.org/1999/xlink"
 	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   	xmlns:lxslt="http://xml.apache.org/xslt"
   	xmlns:redirect="org.apache.xalan.xslt.extensions.Redirect"
   	extension-element-prefixes="redirect"
   	version="1.0">

<!-- 
author: Nikita Ogievetsky
(c) Copyright: Cogitech Inc, 2000. 
-->

  <xsl:output method="html"  indent="yes" />

<xsl:key
  name = "topicByID" 
  match = "topic" 
  use = "concat('#',@id)" /> 

<xsl:key
  name = "instanceOf" 
  match = "topic" 
  use = "instanceOf/topicRef/@xlink:href" /> 

<xsl:key
  name = "subjectIndicator" 
  match = "topic" 
  use = "subjectIdentity/subjectIndicatorRef/@xlink:href" /> 

<xsl:key
  name = "classAssoc" 
  match = "association[instanceOf/topicRef/@xlink:href='#_class-subclass']" 
  use = "member[roleSpec/topicRef/@xlink:href='#_superclass']/topicRef/@xlink:href" /> 

<xsl:key
  name = "subClassAssoc" 
  match = "association[instanceOf/topicRef/@xlink:href='#_class-subclass']" 
  use = "member[roleSpec/topicRef/@xlink:href='#_subclass']/topicRef/@xlink:href" /> 

<xsl:key 
 name="plang-codes" 
 match="occurrence" 
 use="scope/topicRef/@xlink:href"/>

<xsl:param name="out">c:/temp/html</xsl:param>
<xsl:variable name="out-dir" select="concat($out,'/')"/>
<xsl:variable name="root" select="key('subjectIndicator',concat('#',/topicMap/@id))"/>

<xsl:template match="/">
	<xsl:call-template name="root-page"/>

	<!--generate pages for every subclass of an algorithm-->
	<xsl:call-template name="algorithm-page">
		<xsl:with-param name="this" select="key('topicByID','#sort')"/>
	</xsl:call-template>

	<!--generate pages for every instance of a programming language-->
	<xsl:for-each select="key('instanceOf','#plang')">
		<xsl:call-template name="plang-page"/>
	</xsl:for-each>

</xsl:template>

<xsl:template name="page">
	<xsl:param name="this" select="key('topicByID','#sort')"/>
	<xsl:param name="content"/>
	<redirect:write select="concat($out-dir,$this/@id,'.html')">
	<HTML>
		<HEADER>
			<TITLE>
				<xsl:apply-templates select="$this" mode="label"/>
			</TITLE>
			<STYLE>
			a  {
				COLOR: #0066ff;
				FONT-FAMILY: sans serif;
				FONT-SIZE: 11pt;
				FONT-WEIGHT: bold;
				text-decoration : none;
			}
                      a:hover   { COLOR: #95C295;}
			a:active {
				background-color: #fdb400;
				color: #663300;
			}
			aa {
				color: #996633;
				font-family: Arial, Helvetica, sans-serif;
				font-size : 11pt;
			}

			.A {
				    COLOR: #777777;
				    FONT-FAMILY: sans serif;
				    FONT-SIZE: 11pt;
				    FONT-WEIGHT: bold;
			}
			UL {MARGIN:0 15;}
			</STYLE>
		</HEADER>
		<BODY>
		<table width="1000" height="100%" cellspacing="0" cellpadding="10">
		<tr>
			<td width="250" height="20" bgcolor="#ffddbb" align="center">
				<xsl:apply-templates select="$root" mode="link"/>
			</td>
			<td width="750" height="20" valign="top" bgcolor="#eeeeee">
			<table cellspacing="10"><tr>
				<xsl:for-each select="key('instanceOf','#plang')">
				<td background="grey">
					<xsl:choose>
						<xsl:when test="@id=$this/@id">
							<span class="A"><xsl:apply-templates select="." mode="label"/></span>
						</xsl:when>
						<xsl:otherwise>
							<xsl:apply-templates select="." mode="link"/>
						</xsl:otherwise>
					</xsl:choose>
				</td>	
				</xsl:for-each>
			</tr></table>	
			</td>
		</tr><tr>
			<td valign="top" bgcolor="#eeeeee">
				<xsl:call-template name="sitemap">
					<xsl:with-param name="classRef">#sort</xsl:with-param>
					<xsl:with-param name="current" select="$this/@id"/>
				</xsl:call-template>
			</td>
			<td valign="top" bgcolor="#ffeedd" ><xsl:copy-of select="$content"/></td>
		</tr></table>
<center><font size="1"><xsl:value-of select="$root/occurrence[instanceOf/topicRef/@xlink:href='#copyright']/resourceData" disable-output-escaping="yes"/></font></center>
		</BODY>
	</HTML>
	</redirect:write>
</xsl:template>  

<xsl:template name="algorithm-page">
	<xsl:param name="this"/>
	<xsl:call-template name="page">
		<xsl:with-param name="this" select="$this"/>
		<xsl:with-param name="content">
			<font size="+2"><xsl:apply-templates select="$this" mode="label"/>
				<xsl:if test="$this/baseName[scope/topicRef/@xlink:href='#also-known-as']">
						[<xsl:value-of select="$this/baseName[scope/topicRef/@xlink:href='#also-known-as']/baseNameString"/>]
				</xsl:if>
			</font>
			<br/><br/>
			<xsl:apply-templates select="$this/subjectIdentity"/>
			<xsl:variable name="superclasses" select="key('subClassAssoc',concat('#',$this/@id))
										/member[roleSpec/topicRef/@xlink:href='#_superclass']/topicRef"/>
			<xsl:if test="$superclasses">
				Inherits from 
					<xsl:for-each select="$superclasses">
						<xsl:apply-templates select="key('topicByID',@xlink:href)" mode="link"/>
						<xsl:if test="position() != last()">, </xsl:if>
					</xsl:for-each>
			<br/><br/>
			</xsl:if>
			<xsl:apply-templates select="$this/occurrence[instanceOf/topicRef/@xlink:href='#definition']"/>
			<xsl:variable name="demos" select="$this/occurrence[instanceOf/topicRef/@xlink:href='#demo']"/>
			<xsl:if test="$demos">
				<span>Demonstrations: </span>
				<ul>
					<xsl:for-each select="$demos">
						<li>
							<a href="{resourceRef/@xlink:href}"><xsl:value-of select="resourceRef/@xlink:href"/></a><br/>
						</li>
					</xsl:for-each>
				</ul>
				<br/>
			</xsl:if>
			<xsl:variable name="codes" select="$this/occurrence[instanceOf/topicRef/@xlink:href='#code']"/>
			<xsl:if test="$codes">
				<span>Implementations and sample code: </span>
				<ul>
					<xsl:for-each select="$codes">
						<li>
							<a href="{resourceRef/@xlink:href}"><xsl:value-of select="resourceRef/@xlink:href"/></a>
							[<xsl:apply-templates select="key('topicByID',scope/topicRef/@xlink:href)" mode="link"/>]
						</li>
					</xsl:for-each>
				</ul>
				<br/>
			</xsl:if>
			<xsl:variable name="subclasses" select="key('classAssoc',concat('#',$this/@id))
										/member[roleSpec/topicRef/@xlink:href='#_subclass']/topicRef"/>
			<xsl:if test="$subclasses">
				See also <xsl:value-of select="$this/baseName[not(scope)]/baseNameString"/> variants: 
				<xsl:for-each select="$subclasses">
					<xsl:apply-templates select="key('topicByID',@xlink:href)" mode="link"/>
					<xsl:if test="position() != last()">, </xsl:if>
				</xsl:for-each>
			</xsl:if>
		</xsl:with-param>
	</xsl:call-template>
	<xsl:variable name="aref" select="key('classAssoc',concat('#',$this/@id))"/>
	<xsl:for-each select="$aref/member[roleSpec/topicRef/@xlink:href='#_subclass']/topicRef">
		<xsl:call-template name="algorithm-page">
			<xsl:with-param name="this" select="key('topicByID',@xlink:href)"/>
		</xsl:call-template>
	</xsl:for-each>
</xsl:template>

<xsl:template name="plang-page">
	<xsl:param name="this" select="."/>
	<xsl:call-template name="page">
		<xsl:with-param name="this" select="$this"/>
		<xsl:with-param name="content">
			<font size="+2"><xsl:apply-templates select="$this" mode="label"/>, 
				a <xsl:apply-templates select="key('topicByID',$this/instanceOf/topicRef/@xlink:href)" mode="label"/>.
			</font>
			<br/><br/>
			<xsl:apply-templates select="$this/subjectIdentity"/>
			<xsl:apply-templates select="$this/occurrence[instanceOf/topicRef/@xlink:href='#definition']"/>
			<xsl:variable name="codes" select="key('plang-codes',concat('#',$this/@id))"/>
			<xsl:if test="$codes">
				<span>Sorting algorithms implemented in <xsl:apply-templates select="$this" mode="label"/>:</span>
				<ul>
					<xsl:for-each select="$codes">
						<li>
							<a href="{resourceRef/@xlink:href}"><xsl:value-of select="resourceRef/@xlink:href"/></a>
							[<xsl:apply-templates select=".." mode="link"/>]<br/>
						</li>
					</xsl:for-each>
				</ul>
				<br/><br/>
			</xsl:if>
		</xsl:with-param>
	</xsl:call-template>
</xsl:template>

<xsl:template name="root-page">
	<xsl:param name="this" select="$root"/>
	<xsl:call-template name="page">
		<xsl:with-param name="this" select="$this"/>
		<xsl:with-param name="content">
			<font size="+1">
				<xsl:apply-templates select="$this/occurrence[instanceOf/topicRef/@xlink:href='#definition']"/>
			</font>
		</xsl:with-param>
	</xsl:call-template>
</xsl:template>

<xsl:template match="subjectIdentity">
	Also defined at: 
	<a href="{subjectIndicatorRef/@xlink:href}"><xsl:value-of select="subjectIndicatorRef/@xlink:href"/></a>.
	<br/><br/>
</xsl:template>
<xsl:template match="occurrence[instanceOf/topicRef/@xlink:href='#definition']">
	<xsl:value-of select="resourceData"    disable-output-escaping="yes"/>
	<xsl:if test="scope">
		[<xsl:apply-templates select="key('topicByID',scope/topicRef/@xlink:href)" mode="indicator"/>]
	</xsl:if>	
	<br/><br/>
</xsl:template>

<xsl:template name="sitemap">
	<xsl:param name="classRef"/>
	<xsl:param name="current"/>
	<xsl:variable name="topic" select="key('topicByID',$classRef)"/>
	<xsl:choose>
		<xsl:when test="$topic/@id=$current">
			<span class="A"><xsl:apply-templates select="$topic" mode="label"/></span>
		</xsl:when>
		<xsl:otherwise>
			<xsl:apply-templates select="$topic" mode="link"/>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:variable name="aref" select="key('classAssoc',$classRef)"/>
	<xsl:if test="$aref">
		<ul>
			<xsl:for-each select="$aref/member[roleSpec/topicRef/@xlink:href='#_subclass']/topicRef">
				<li>
				<xsl:call-template name="sitemap">
					<xsl:with-param name="classRef" select="@xlink:href"/>
					<xsl:with-param name="current" select="$current"/>
				</xsl:call-template>
				</li>
			</xsl:for-each>
		</ul>
	</xsl:if>
</xsl:template>  


<xsl:template match="topic" mode="link">
	<a href="{@id}.html"><xsl:value-of select="baseName[not(scope)]/baseNameString"/></a>
</xsl:template>
<xsl:template match="topic" mode="indicator">
	<a href="{subjectIdentity/subjectIndicatorRef/@xlink:href}"><xsl:value-of select="baseName[not(scope)]/baseNameString"/></a>
</xsl:template>
<xsl:template match="topic" mode="label">
	<xsl:value-of select="baseName[not(scope)]/baseNameString"/>
</xsl:template>

</xsl:stylesheet>

