<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:str="http://www.w3.org/2001/10/str-util.xsl"
    version="1.0">
  <xsl:import href="http://www.w3.org/2001/10/str-util.xsl"/>
  <xsl:import href="Util.xsl"/>

  <xsl:variable name="tmp1" select="'&amp;&gt;&lt;()=&#34;&#35;:'"/>
  <xsl:variable name="tmp2" select='"&apos;"'/>

  <xsl:variable name="forbiddenChars" select="concat($tmp1,$tmp2)"/>

  <xsl:variable name="invalidFirstChar" select="'&#35;[/'"/>

  <xsl:template name="removeForbiddenChars">
    <xsl:param name="str"/>
    <xsl:value-of select="translate($str,$forbiddenChars,' ')"/>
  </xsl:template>

  <xsl:template name="isPreviousSectionValid">
    <xsl:param name="section"/>
    <xsl:if test="(not(contains($section,'references')))and(not(contains($section,'change history')))and(not(contains($section,'abbreviations')))and(not(contains($section,'acronyms')))">
      <xsl:text>yes</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template name="isTermValid">
    <xsl:param name="str"/>
    <xsl:if test="string-length($str) &gt; 2">
      <xsl:variable name="isFirstCharValid">
        <xsl:call-template name="isLetter">
          <xsl:with-param name="char" select="substring(normalize-space($str),1,1)"/>
        </xsl:call-template>
      </xsl:variable>
<!--      <xsl:if test="not(contains($invalidFirstChar,substring(normalize-space($str),1,1)))and(not(contains($str,'Editor')))and(not(contains($str,'Latest version')))"> -->
      <xsl:if test="normalize-space($isFirstCharValid)and(not(contains($str,'Editor')))and(not(contains($str,'Latest version')))">
        <xsl:text>yes</xsl:text>
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <xsl:template name="getRDFSchemaURI">
    <xsl:param name="date"/>
    <xsl:param name="uri"/>
    <xsl:variable name="short_name">
      <xsl:call-template name="getShortName">
        <xsl:with-param name="str" select="$uri"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="concat('http://www.w3.org/2003/03/glossary-project/data/glossaries/',$short_name,'#')"/>
  </xsl:template>

  <xsl:template name="getShortName">
    <xsl:param name="str"/>
    <xsl:variable name="trailing_slash">
      <xsl:call-template name="str:ends-with">
        <xsl:with-param name="string" select="$str"/>
        <xsl:with-param name="string2" select="'/'"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="boolean(normalize-space($trailing_slash))">
        <xsl:variable name="tmp" select="substring($str,1,number(string-length($str)-1))"/>
        <xsl:variable name="tmp1">
          <xsl:call-template name="str:keep-before-last">
            <xsl:with-param name="string" select="$tmp"/>
            <xsl:with-param name="delimiter" select="'/'"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="substring-after($tmp,concat($tmp1,'/'))"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="tmp2">
          <xsl:call-template name="str:keep-before-last">
            <xsl:with-param name="string" select="$str"/>
            <xsl:with-param name="delimiter" select="'/'"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="substring-after($str,concat($tmp2,'/'))"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="computeTermID">
    <xsl:param name="str"/>
    <xsl:choose>
      <xsl:when test="contains($str,' ')">
        <xsl:variable name="substring_before">
          <xsl:value-of select="substring-before($str,' ')"/>
        </xsl:variable>
        <xsl:variable name="substring_after">
          <xsl:value-of select="substring-after($str,' ')"/>
        </xsl:variable>
        <xsl:variable name="substring_after_cap">
          <xsl:call-template name="capitalize">
            <xsl:with-param name="str1" select="$substring_after"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:call-template name="computeTermID">
          <xsl:with-param name="str" select="concat($substring_before,$substring_after_cap)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="str_decap">
          <xsl:call-template name="decapitalize">
            <xsl:with-param name="str1" select="$str"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="$str_decap"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="formatTerm">
    <xsl:param name="str"/>
    <xsl:choose>
      <xsl:when test="contains($str,' ')">
        <xsl:variable name="before_last_space">
          <xsl:call-template name="str:keep-before-last">
            <xsl:with-param name="string" select="$str"/>
            <xsl:with-param name="delimiter" select="' '"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="before_first_underscore">
          <xsl:choose>
            <xsl:when test="contains($str,'_')">
              <xsl:value-of select="substring-before($str,'_')"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$str"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name="tmp" select="normalize-space(substring-after($before_first_underscore,$before_last_space))"/>
        <xsl:variable name="suffix" select="substring-after($str, $tmp)"/>
        <xsl:variable name="endsWith">
          <xsl:call-template name="str:ends-with">
            <xsl:with-param name="string" select="$str"/>
            <xsl:with-param name="string2" select="$tmp"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="end">
          <xsl:if test="(normalize-space($suffix))and(not(normalize-space(endsWith)))and(not(starts-with($str,$tmp)))">
            <xsl:value-of select="$suffix"/>
          </xsl:if>
        </xsl:variable>
        <xsl:variable name="allLettersUpper">
          <xsl:call-template name="areAllLettersUpper">
            <xsl:with-param name="str" select="$tmp"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:choose>
          <xsl:when test="not(normalize-space($allLettersUpper))">
            <xsl:variable name="decap">
              <xsl:call-template name="decapitalize">
                <xsl:with-param name="str1" select="$tmp"/>
              </xsl:call-template>
            </xsl:variable>
            <xsl:call-template name="formatTerm">
              <xsl:with-param name="str" select="concat($before_last_space,'_',$decap,normalize-space($end))"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="formatTerm">
              <xsl:with-param name="str" select="concat($before_last_space,'_',$tmp,normalize-space($end))"/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="not(contains($str,'_'))">
            <xsl:variable name="allLettersUpper">
              <xsl:call-template name="areAllLettersUpper">
                <xsl:with-param name="str" select="$str"/>
              </xsl:call-template>
            </xsl:variable>
            <xsl:choose>
              <xsl:when test="normalize-space($allLettersUpper)">
                <xsl:value-of select="$str"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:variable name="tmp">
                  <xsl:call-template name="decapitalize">
                    <xsl:with-param name="str1" select="$str"/>
                  </xsl:call-template>
                </xsl:variable>
                <xsl:value-of select="$tmp"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <xsl:otherwise>
            <xsl:variable name="substring_before_underscore" select="substring-before($str,'_')"/>
            <xsl:variable name="allLettersUpperBefore">
              <xsl:call-template name="areAllLettersUpper">
                <xsl:with-param name="str" select="$substring_before_underscore"/>
              </xsl:call-template>
            </xsl:variable>
            <xsl:choose>
              <xsl:when test="not(normalize-space($allLettersUpperBefore))">
              <xsl:variable name="substring_after_underscore" select="substring-after($str,'_')"/>
                <xsl:variable name="str_decap">
                  <xsl:call-template name="decapitalize">
                    <xsl:with-param name="str1" select="$substring_before_underscore"/>
                  </xsl:call-template>
                </xsl:variable>
                <xsl:value-of select="translate(concat($str_decap,'_',$substring_after_underscore),'_',' ')"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="translate($str,'_',' ')"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>