<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns:c="http://nwalsh.com/rdf/contacts#"
		xmlns:foaf="http://xmlns.com/foaf/0.1/"
		xmlns:addr="http://wymiwyg.org/ontologies/foaf/postaddress#"
		xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
		xmlns:hip="http://nwalsh.com/rdf/hiptop#"
		xmlns:onta="http://nwalsh.com/annotations/ontology#"
		xmlns:owl="http://www.w3.org/2002/07/owl#"
		xmlns:p="http://nwalsh.com/rdf/pim#"
		xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
		xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
		xmlns:rng="http://relaxng.org/ns/structure/1.0"
		xmlns:xs="http://www.w3.org/2001/XMLSchema#"
                xmlns:sha1="http://nwalsh.com/xslt/ext/com.nwalsh.saxon.SHA1"
		xmlns:bio="http://vocab.org/bio/0.1/"
		xmlns:v="http://nwalsh.com/rdf/vCard#"
		exclude-result-prefixes="sha1"
                version="2.0">

<xsl:output method="xml" encoding="utf-8" indent="yes"/>

<xsl:param name="ontBase" select="'http://nwalsh.com/rdf/contacts#'"/>
<xsl:param name="dtBase" select="'http://www.w3.org/2001/XMLSchema#'"/>

<xsl:strip-space elements="*"/>

<xsl:template match="hip:hiptop">
  <rdf:RDF>
    <xsl:apply-templates select="hip:contact"/>
  </rdf:RDF>
</xsl:template>

<xsl:template match="hip:contact[hip:category='Data']" priority="100">
  <c:Place>
    <xsl:attribute name="rdf:about">
      <xsl:text>http://norman.walsh.name/knows/where</xsl:text>
      <xsl:choose>
	<xsl:when test="hip:uris/hip:uri[@label='ID']">
	  <xsl:value-of select="hip:uris/hip:uri[@label='ID']"/>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:text>#</xsl:text>
	  <xsl:value-of select="@id"/>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>

    <rdf:type rdf:resource="http://nwalsh.com/rdf/contacts#Contact"/>

    <xsl:apply-templates/>
  </c:Place>
</xsl:template>

<xsl:template match="hip:contact[hip:firstname or hip:surname]">
  <foaf:Person>
    <xsl:attribute name="rdf:about">
      <xsl:text>http://norman.walsh.name/knows/who</xsl:text>
      <xsl:choose>
	<xsl:when test="hip:uris/hip:uri[@label='ID']">
	  <xsl:value-of select="hip:uris/hip:uri[@label='ID']"/>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:text>#</xsl:text>
	  <xsl:value-of select="@id"/>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>

    <rdf:type rdf:resource="http://nwalsh.com/rdf/contacts#Contact"/>

    <xsl:apply-templates/>
  </foaf:Person>
</xsl:template>

<xsl:template match="hip:contact">
  <foaf:Organization>
    <xsl:attribute name="rdf:about">
      <xsl:text>http://norman.walsh.name/knows/who</xsl:text>
      <xsl:choose>
	<xsl:when test="hip:uris/hip:uri[@label='ID']">
	  <xsl:value-of select="hip:uris/hip:uri[@label='ID']"/>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:text>#</xsl:text>
	  <xsl:value-of select="@id"/>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>

    <rdf:type rdf:resource="http://nwalsh.com/rdf/contacts#Contact"/>

    <xsl:apply-templates/>
  </foaf:Organization>
</xsl:template>

<xsl:template match="hip:category">
  <c:category>
    <xsl:value-of select="."/>
  </c:category>
</xsl:template>

<xsl:template match="hip:firstname">
  <foaf:firstName>
    <xsl:value-of select="."/>
  </foaf:firstName>
</xsl:template>

<xsl:template match="hip:middlename">
  <c:middleName>
    <xsl:value-of select="."/>
  </c:middleName>
</xsl:template>

<xsl:template match="hip:surname">
  <foaf:surname>
    <xsl:value-of select="."/>
  </foaf:surname>

  <xsl:if test="../hip:firstname">
    <foaf:name>
      <xsl:value-of select="../hip:firstname"/>
      <xsl:text> </xsl:text>
      <xsl:if test="../hip:middlename">
	<xsl:value-of select="hip:middlename"/>
	<xsl:text> </xsl:text>
      </xsl:if>
      <xsl:value-of select="."/>
    </foaf:name>
  </xsl:if>
</xsl:template>

<xsl:template match="hip:nickname">
  <foaf:nick>
    <xsl:value-of select="."/>
  </foaf:nick>
</xsl:template>

<xsl:template match="hip:company">
  <c:associatedName>
    <xsl:choose>
      <xsl:when test="../p:company">
	<xsl:value-of select="../p:company"/>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </c:associatedName>
</xsl:template>

<xsl:template match="hip:title">
  <c:associatedTitle>
    <xsl:choose>
      <xsl:when test="../p:title">
	<xsl:value-of select="../p:title"/>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </c:associatedTitle>
</xsl:template>

<xsl:template match="p:title|p:company"/>

<xsl:template match="hip:last_modified">
  <c:lastModified rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">
    <xsl:value-of select="."/>
  </c:lastModified>
</xsl:template>

<xsl:template match="hip:birthday">
  <bio:event>
    <bio:Birth>
      <bio:date rdf:datatype="http://www.w3.org/2001/XMLSchema#date">
	<xsl:value-of select="."/>
      </bio:date>
    </bio:Birth>
  </bio:event>
  <foaf:birthday>
    <xsl:value-of select="substring(.,6)"/>
  </foaf:birthday>
</xsl:template>

<xsl:template match="hip:uris|hip:emails|hip:ims|hip:phones|hip:addresses">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="hip:uri[@label='ID']"/>

<xsl:template match="hip:uri[@label='Home'
		             or @label='Homepage'
		             or @label='Unlabeled'
			     or @label='Custom 1']">
  <foaf:homepage rdf:resource="{.}"/>
</xsl:template>

<xsl:template match="hip:uri[@label='Blog']">
  <foaf:weblog rdf:resource="{.}"/>
</xsl:template>

<xsl:template match="hip:uri[@label='Work']">
  <foaf:workplaceHomepage rdf:resource="{.}"/>
</xsl:template>

<xsl:template match="hip:uri[@label='Data']|p:data">
  <xsl:variable name="value"
		select="if (@rdf:resource) then @rdf:resource else ."/>

  <xsl:choose>
    <xsl:when test="starts-with($value, 'getty:')">
      <c:gettyTGN>
	<xsl:attribute name="rdf:resource">
	  <xsl:text>http://www.getty.edu/vow/TGNFullDisplay?</xsl:text>
	  <xsl:value-of select="substring-after($value, 'getty:')"/>
	</xsl:attribute>
      </c:gettyTGN>
    </xsl:when>
    <xsl:when test="starts-with($value, 'weather:')">
      <c:weather>
	<xsl:attribute name="rdf:resource">
	  <xsl:text>http:</xsl:text>
	  <xsl:value-of select="substring-after($value, 'weather:')"/>
	</xsl:attribute>
      </c:weather>
    </xsl:when>
    <xsl:when test="starts-with($value, 'cia:')">
      <c:ciaFactbook>
	<xsl:attribute name="rdf:resource">
	  <xsl:text>http://www.cia.gov/cia/publications/factbook/geos/</xsl:text>
	  <xsl:value-of select="substring-after($value, 'cia:')"/>
	</xsl:attribute>
      </c:ciaFactbook>
    </xsl:when>
    <xsl:when test="starts-with($value, 'foaf:')">
      <foaf:page>
	<xsl:attribute name="rdf:resource">
	  <xsl:value-of select="substring-after($value, 'foaf:')"/>
	</xsl:attribute>
      </foaf:page>
    </xsl:when>
    <xsl:otherwise>
      <xsl:message>Data URI: <xsl:value-of select="$value"/></xsl:message>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="hip:uri">
  <xsl:message>URI: <xsl:value-of select="@label"/></xsl:message>
</xsl:template>

<xsl:template match="hip:email[@label='Home']">
  <c:personalMbox rdf:resource="mailto:{.}"/>
  <foaf:mbox_sha1sum>
    <xsl:value-of select="sha1:hash(concat('mailto:', .))"/>
  </foaf:mbox_sha1sum>
</xsl:template>

<xsl:template match="hip:email[@label='Work']">
  <c:workMbox rdf:resource="mailto:{.}"/>
  <foaf:mbox_sha1sum>
    <xsl:value-of select="sha1:hash(concat('mailto:', .))"/>
  </foaf:mbox_sha1sum>
</xsl:template>

<xsl:template match="hip:email[@label='Unlabeled']">
  <c:unlabeledMbox rdf:resource="mailto:{.}"/>
  <foaf:mbox_sha1sum>
    <xsl:value-of select="sha1:hash(concat('mailto:', .))"/>
  </foaf:mbox_sha1sum>
</xsl:template>

<xsl:template match="hip:email[@label='Mobile']">
  <c:pagerMbox rdf:resource="mailto:{.}"/>
  <foaf:mbox_sha1sum>
    <xsl:value-of select="sha1:hash(concat('mailto:', .))"/>
  </foaf:mbox_sha1sum>
</xsl:template>

<xsl:template match="hip:email[@label='Obsolete']">
  <c:obsoleteMbox rdf:resource="mailto:{.}"/>
  <foaf:mbox_sha1sum>
    <xsl:value-of select="sha1:hash(concat('mailto:', .))"/>
  </foaf:mbox_sha1sum>
</xsl:template>

<xsl:template match="hip:email">
  <xsl:message>Email: <xsl:value-of select="@label"/></xsl:message>
</xsl:template>

<xsl:template match="p:email">
  <xsl:variable name="value"
		select="if (@rdf:resource) then @rdf:resource else ."/>

  <xsl:variable name="addr">
    <xsl:choose>
      <xsl:when test="starts-with($value,'mailto:')">
	<xsl:value-of select="$value"/>
      </xsl:when>
      <xsl:otherwise>
	<xsl:text>mailto:</xsl:text>
	<xsl:value-of select="$value"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <c:unlabeledMbox rdf:resource="{$addr}"/>
  <foaf:mbox_sha1sum>
    <xsl:value-of select="sha1:hash($addr)"/>
  </foaf:mbox_sha1sum>
</xsl:template>

<xsl:template match="hip:im[@label='AIM']">
  <foaf:aimChatID>
    <xsl:value-of select="."/>
  </foaf:aimChatID>
</xsl:template>

<xsl:template match="hip:im[@label='Yahoo']">
  <foaf:yahooChatID>
    <xsl:value-of select="."/>
  </foaf:yahooChatID>
</xsl:template>

<xsl:template match="hip:im[@label='ICQ']">
  <foaf:icqChatID>
    <xsl:value-of select="."/>
  </foaf:icqChatID>
</xsl:template>

<xsl:template match="hip:im[@label='MSN']">
  <foaf:msnChatID>
    <xsl:value-of select="."/>
  </foaf:msnChatID>
</xsl:template>

<xsl:template match="hip:im[@label='Jabber']">
  <foaf:jabberChatID>
    <xsl:value-of select="."/>
  </foaf:jabberChatID>
</xsl:template>

<xsl:template match="hip:im">
  <xsl:message>IM: <xsl:value-of select="@label"/></xsl:message>
</xsl:template>

<xsl:template match="hip:phone[@label = 'Custom 4']"/>

<xsl:template match="hip:phone[@label = 'Data']">
  <c:dataPhone rdf:resource="tel:{.}"/>
</xsl:template>

<xsl:template match="hip:phone[@label = 'Fax']">
  <c:fax rdf:resource="tel:{.}"/>
</xsl:template>

<xsl:template match="hip:phone[@label = 'Home']">
  <c:homePhone rdf:resource="tel:{.}"/>
</xsl:template>

<xsl:template match="hip:phone[@label = 'Mobile']">
  <c:mobilePhone rdf:resource="tel:{.}"/>
</xsl:template>

<xsl:template match="hip:phone[@label = 'Work']">
  <c:workPhone rdf:resource="tel:{.}"/>
</xsl:template>

<xsl:template match="hip:phone[@label = 'Unlabeled']">
  <c:unlabeledPhone rdf:resource="tel:{.}"/>
</xsl:template>

<xsl:template match="hip:phone">
  <xsl:message>Phone: <xsl:value-of select="@label"/></xsl:message>
</xsl:template>

<xsl:template match="hip:address[@label = 'Work']">
  <v:workAdr rdf:parseType="Resource">
    <rdf:type
	rdf:resource="http://nwalsh.com/rdf/vCard#Address"/>
    <xsl:apply-templates/>
    <xsl:if test="not(hip:country)">
      <v:country-name>US</v:country-name>
    </xsl:if>
  </v:workAdr>
</xsl:template>

<xsl:template match="hip:address[@label = 'Home']">
  <v:homeAdr rdf:parseType="Resource">
    <rdf:type
	rdf:resource="http://nwalsh.com/rdf/vCard#Address"/>
    <xsl:apply-templates/>
    <xsl:if test="not(hip:country)">
      <v:country-name>US</v:country-name>
    </xsl:if>
  </v:homeAdr>
</xsl:template>

<xsl:template match="hip:address[@label = 'Unlabeled']">
  <v:unlabeledAdr rdf:parseType="Resource">
    <rdf:type
	rdf:resource="http://nwalsh.com/rdf/vCard#Address"/>
    <xsl:apply-templates/>
    <xsl:if test="not(hip:country)">
      <v:country-name>US</v:country-name>
    </xsl:if>
  </v:unlabeledAdr>
</xsl:template>

<xsl:template match="hip:address">
  <xsl:message>Address: <xsl:value-of select="@label"/></xsl:message>
</xsl:template>

<xsl:template match="hip:street" name="street">
  <xsl:param name="street" select="."/>

  <!-- try to decompose the street -->
  <xsl:choose>
    <xsl:when test="matches($street,'^P.O. Box .*','i')
		    and contains($street,'&#10;')">
      <xsl:call-template name="street">
	<xsl:with-param name="street"
			select="substring-before($street,'&#10;')"/>
      </xsl:call-template>
      <xsl:call-template name="street">
	<xsl:with-param name="street"
			select="substring-after($street,'&#10;')"/>
      </xsl:call-template>
    </xsl:when>

    <xsl:when test="matches($street,'P.O. Box .*','i')">
      <v:post-office-box>
	<xsl:value-of select="replace($street, 'P.O. Box (.*)','$1')"/>
      </v:post-office-box>
    </xsl:when>

    <xsl:otherwise>
      <v:street-address>
	<xsl:value-of select="$street"/>
      </v:street-address>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="hip:city">
  <v:locality>
    <xsl:value-of select="."/>
  </v:locality>
</xsl:template>

<xsl:template match="hip:postcode">
  <v:postal-code>
    <xsl:value-of select="."/>
  </v:postal-code>
</xsl:template>

<xsl:template match="hip:state">
  <v:region>
    <xsl:value-of select="."/>
  </v:region>
</xsl:template>

<xsl:template match="hip:country">
  <v:country-name>
    <xsl:value-of select="."/>
  </v:country-name>
</xsl:template>

<xsl:template match="hip:notes">
  <c:notes>
    <xsl:value-of select="."/>
  </c:notes>
</xsl:template>

<xsl:template match="p:date">
  <bio:event>
    <bio:Event>
      <bio:date rdf:datatype="http://www.w3.org/2001/XMLSchema#date">
	<xsl:value-of select="."/>
      </bio:date>
    </bio:Event>
  </bio:event>
</xsl:template>

<xsl:template match="p:born">
  <bio:event>
    <bio:Birth>
      <bio:date rdf:datatype="http://www.w3.org/2001/XMLSchema#date">
	<xsl:value-of select="."/>
      </bio:date>
    </bio:Birth>
  </bio:event>
</xsl:template>

<xsl:template match="p:died">
  <bio:event>
    <bio:Death>
      <bio:date rdf:datatype="http://www.w3.org/2001/XMLSchema#date">
	<xsl:value-of select="."/>
      </bio:date>
    </bio:Death>
  </bio:event>
</xsl:template>

<xsl:template match="p:anniversary">
  <bio:event>
    <bio:Marriage>
      <bio:date rdf:datatype="http://www.w3.org/2001/XMLSchema#date">
	<xsl:value-of select="."/>
      </bio:date>
    </bio:Marriage>
  </bio:event>
</xsl:template>

<xsl:template match="geo:lat|geo:long">
  <xsl:element name="{name(.)}"
	       namespace="{namespace-uri(.)}">
    <xsl:attribute name="rdf:datatype"
		   select="'http://www.w3.org/2001/XMLSchema#decimal'"/>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

<xsl:template match="*">
  <xsl:element name="{name(.)}"
	       namespace="{namespace-uri(.)}">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

<xsl:template match="comment()|processing-instruction()|text()">
  <xsl:copy/>
</xsl:template>

</xsl:stylesheet>
