<?xml version="1.0" encoding="UTF-8"?>

<!--
　foaf:Person を XHTML (の table) へ変換。他。
　XSLT プロセッサ内蔵のブラウザによる利用が想定されるため、
　Xalan-Java の拡張機能を使っている common.xsl 等を呼び出さないよう注意。＞自分
-->

<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:foaf="http://xmlns.com/foaf/0.1/"
	xmlns:fp="http://www.remus.dti.ne.jp/~a-satomi/foaf-props"
	xmlns:xalan="http://xml.apache.org/xalan"
	xmlns="http://www.w3.org/1999/xhtml"
	exclude-result-prefixes="rdf rdfs dc foaf fp xalan">

<!-- ================ 各種設定 ================  -->

<!-- xsl:param name="foafProps">foaf_props.xml</xsl:param -->
<xsl:param name="foafProps" select="concat($baseURI, '/common/xml/foaf_props.xml')"/><!-- MSXSL対策 -->
<xsl:param name="myFoafURI" select="concat($baseURI, '/profiles/foaf/foaf.rdf')"/>
<xsl:param name="iconDirURI" select="concat($baseURI, '/profiles/foaf')"/>

<!-- ================ 自分の foaf プロパティを整形 ================  -->

<xsl:template match="foaf:Person" mode="myself">
	<table summary="{foaf:name} のデータ">
		<tr>
			<th class="prop">プロパティ</th>
			<th class="descript">説明</th>
			<th class="content">内容</th>
		</tr>
		<xsl:for-each select="foaf:*[local-name() != 'knows']">
			<xsl:variable name="propName" select="local-name()"/>
			<tr>
				<td class="prop">
					<xsl:value-of select="$propName"/>
					<xsl:if test="string(@xml:lang)">
						<xsl:text> </xsl:text>
						<span class="memo">(<xsl:value-of select="@xml:lang"/>)</span>
					</xsl:if>
				</td>
				<td class="descript">
					<xsl:choose>
						<xsl:when test="document($foafProps)/fp:foaf-props/fp:prop[@name = $propName]">
							<xsl:value-of select="document($foafProps)/fp:foaf-props/fp:prop[@name = $propName]"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>-</xsl:text>
						</xsl:otherwise>
					</xsl:choose>
				</td>
				<td class="content">
					<xsl:copy-of select="@xml:lang"/>
					<xsl:choose>
						<xsl:when test="@rdf:resource and @dc:title">
							<a href="{@rdf:resource}"><xsl:value-of select="@dc:title"/></a>
						</xsl:when>
						<xsl:when test="@rdf:resource">
							<a href="{@rdf:resource}"><xsl:value-of select="@rdf:resource"/></a>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="self::node()"/>
						</xsl:otherwise>
					</xsl:choose>
				</td>
			</tr>
		</xsl:for-each>
	</table>

	<div class="picture small">
		<img title="{foaf:depiction/@dc:title}" alt="" src="{foaf:depiction/@rdf:resource}"/>
	</div>

	<ul>
		<li>
			<a href="{$myFoafURI}">
				<img alt="" height="14" width="26" src="{$iconDirURI}/foaf_tiny.png"/>
				<xsl:text>FOAF (RDF/XML) を直接表示</xsl:text>
			</a>
		</li>
		<li>
			<a href="http://xml.mfd-consult.dk/foaf/explorer/?foaf={$myFoafURI}">
				<img alt="" height="16" width="16" src="{$iconDirURI}/foaf-explorer.16.png"/>
				<xsl:text>FoaF Explorer で表示</xsl:text>
			</a>
		</li>
		<li>
			<a href="http://eikeon.com/foaf/?mbox_sha1sum={foaf:mbox_sha1sum}">
				<img alt="" height="16" width="16" src="{$iconDirURI}/w3c-rdf.png"/>
				<xsl:text>FOAF: Web View で表示</xsl:text>
			</a>
		</li>
		<li>
			<a href="http://jibbering.com/foaf/foafnaut.svg?email={substring-after(foaf:mbox/@rdf:resource, ':')}&amp;go=launch+foafnaut!">
				<img alt="" height="16" width="16" src="{$iconDirURI}/foafnaut.png"/>
				<xsl:text>FOAFNAUT で表示</xsl:text>
			</a>
		</li>
		<li>
			<a href="http://www.w3.org/RDF/Validator/ARPServlet?TRIPLES_AND_GRAPH=PRINT_TRIPLES&amp;URI={$myFoafURI}">
				<img alt="" height="16" width="38" src="{$iconDirURI}/w3c-rdfm16.png"/>
				<xsl:text>RDF Validator でチェキ</xsl:text>
			</a>
		</li>
	</ul>
</xsl:template>

<!-- ================ 知り合い筋コンテナ ================  -->

<xsl:template match="foaf:knows[1]">
	<table summary="お知り合いデータ">
		<tr>
			<th class="name">お名前</th>
			<th class="foafdata">FOAF DATA</th>
			<th class="interest">関心事</th>
			<th class="descript">紹介文</th>
		</tr>
		<xsl:call-template name="knows"/>
	</table>
</xsl:template>

<xsl:template name="knows">
	<xsl:apply-templates select="foaf:Person" mode="friends"/>
	<xsl:for-each select="following-sibling::foaf:knows[1]">
		<xsl:call-template name="knows"/>
	</xsl:for-each>
</xsl:template>

<xsl:template match="foaf:knows[position() &gt; 1]"/>

<!-- ================ 知り合い筋の foaf プロパティを整形 ================  -->

<xsl:template match="foaf:Person" mode="friends">
	<tr>
		<td class="name">
			<p><xsl:call-template name="getName"/></p>
			<xsl:if test="string(foaf:homepage/@rdf:resource) or string(foaf:weblog/@rdf:resource)">
				<ul><xsl:apply-templates select="foaf:homepage | foaf:weblog" mode="friends"/></ul>
			</xsl:if>
		</td>
		<td class="foafData">
			<xsl:choose>
				<xsl:when test="string(rdfs:seeAlso/@rdf:resource)">
					<ul>
						<li>
							<a title="FOAF (RDF/XML) を直接表示" href="{rdfs:seeAlso/@rdf:resource}">
								<img alt="[RDF]" height="14" width="26" src="{$iconDirURI}/foaf_tiny.png"/>
							</a>
						</li>
						<li>
							<a title="FoaF Explorer で表示" href="http://xml.mfd-consult.dk/foaf/explorer/?foaf={rdfs:seeAlso/@rdf:resource}">
								<img alt="[FoaF Explorer]" height="16" width="16" src="{$iconDirURI}/foaf-explorer.16.png"/>
							</a>
						</li>
						<xsl:choose>
							<xsl:when test="string(foaf:mbox_sha1sum)">
								<li>
									<a title="FOAF: Web View で表示" href="http://eikeon.com/foaf/?mbox_sha1sum={foaf:mbox_sha1sum}">
										<img alt="[FOAF: Web View]" height="16" width="16" src="{$iconDirURI}/w3c-rdf.png"/>
									</a>
								</li>
							</xsl:when>
							<xsl:when test="string(foaf:mbox/@rdf:resource)">
								<li>
									<a title="FOAF: Web View で表示" href="http://eikeon.com/foaf/?mbox={foaf:mbox/@rdf:resource}">
										<img alt="[FOAF: Web View]" height="16" width="16" src="{$iconDirURI}/w3c-rdf.png"/>
									</a>
								</li>
							</xsl:when>
							<xsl:otherwise/>
						</xsl:choose>
					</ul>
				</xsl:when>
				<xsl:otherwise>-</xsl:otherwise>
			</xsl:choose>
		</td>
		<td class="interest">
			<xsl:choose>
				<xsl:when test="foaf:interest">
					<ul>
						<xsl:apply-templates select="foaf:interest" mode="friends"/>
					</ul>
				</xsl:when>
				<xsl:otherwise>-</xsl:otherwise>
			</xsl:choose>
		</td>
		<td class="descript">
			<xsl:choose>
				<xsl:when test="string(dc:description)">
					<xsl:value-of select="dc:description"/>
				</xsl:when>
				<xsl:otherwise>-</xsl:otherwise>
			</xsl:choose>
		</td>
	</tr>
</xsl:template>

<xsl:template match="foaf:interest" mode="friends">
	<li>
		<a href="{@rdf:resource}">
			<xsl:call-template name="cutText">
				<xsl:with-param name="str">
					<xsl:choose>
						<xsl:when test="string(@dc:title)">
							<xsl:value-of select="@dc:title"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="@rdf:resource"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:with-param>
			</xsl:call-template>
		</a>
	</li>
</xsl:template>

<xsl:template match="foaf:homepage | foaf:weblog" mode="friends">
	<xsl:param name="title">
		<xsl:choose>
			<xsl:when test="string(@dc:title)">
				<xsl:value-of select="@dc:title"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="@rdf:resource"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:param>
	<li>
		<a href="{@rdf:resource}" title="{$title}">
			<img src="{$iconDirURI}/{local-name()}.png" width="16" height="16" alt="{$title}" title="{$title}"/>
		</a>
	</li>
</xsl:template>

<xsl:template name="getName">
	<xsl:choose>
		<xsl:when test="string(ancestor-or-self::foaf:Person[1]/foaf:name)">
			<xsl:value-of select="ancestor-or-self::foaf:Person[1]/foaf:name"/>さん</xsl:when>
		<xsl:when test="string(ancestor-or-self::foaf:Person[1]/foaf:nick)">
			<xsl:value-of select="ancestor-or-self::foaf:Person[1]/foaf:nick"/>さん</xsl:when>
		<xsl:otherwise>Name_Not_Found</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="cutText">
	<xsl:param name="str"/>
	<xsl:choose>
		<xsl:when test="string-length($str) &gt; 30">
			<xsl:value-of select="substring($str, 1, 15)"/>
			<xsl:text>...</xsl:text>
			<xsl:value-of select="substring($str, string-length($str) - 14, 15)"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$str"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>
