<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns="urn:Eclipse-Contexts-Schema"
                              xmlns:econtexts="urn:Eclipse-Contexts-Schema"
                              xmlns:wwtopics="urn:WebWorks-Topics-Schema"
                              xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                              xmlns:wwtoc="urn:WebWorks-Engine-TOC-Schema"
                              xmlns:wwlinks="urn:WebWorks-Engine-Links-Schema"
                              xmlns:wwmode="urn:WebWorks-Engine-Mode"
                              xmlns:wwfiles="urn:WebWorks-Engine-Files-Schema"
                              xmlns:wwdoc="urn:WebWorks-Document-Schema"
                              xmlns:wwbehaviors="urn:WebWorks-Behaviors-Schema"
                              xmlns:wwsplits="urn:WebWorks-Engine-Splits-Schema"
                              xmlns:wwproject="urn:WebWorks-Publish-Project"
                              xmlns:wwpage="urn:WebWorks-Page-Template-Schema"
                              xmlns:wwlocale="urn:WebWorks-Locale-Schema"
                              xmlns:wwprogress="urn:WebWorks-XSLT-Extension-Progress"
                              xmlns:wwlog="urn:WebWorks-XSLT-Extension-Log"
                              xmlns:wwfilesystem="urn:WebWorks-XSLT-Extension-FileSystem"
                              xmlns:wwuri="urn:WebWorks-XSLT-Extension-URI"
                              xmlns:wwstring="urn:WebWorks-XSLT-Extension-StringUtilities"
                              xmlns:wwfilesext="urn:WebWorks-XSLT-Extension-Files"
                              xmlns:wwprojext="urn:WebWorks-XSLT-Extension-Project"
                              xmlns:wwexsldoc="urn:WebWorks-XSLT-Extension-Document"
                              exclude-result-prefixes="wwtopics xsl msxsl wwtoc wwlinks wwmode wwfiles wwdoc wwbehaviors wwsplits wwproject wwpage wwlocale wwprogress wwlog wwfilesystem wwuri wwstring wwfilesext wwprojext wwexsldoc"
>
 <xsl:param name="GlobalInput" />
 <xsl:param name="GlobalPipelineName" />
 <xsl:param name="GlobalProject" />
 <xsl:param name="GlobalFiles" />
 <xsl:param name="ParameterDependsType" />
 <xsl:param name="ParameterSplitsType" />
 <xsl:param name="ParameterType" />
 <xsl:param name="ParameterContextsSplitFileType" />
 <xsl:param name="ParameterUse" />
 <xsl:param name="ParameterDeploy" />


 <xsl:output encoding="UTF-8" indent="yes" />
 <xsl:namespace-alias stylesheet-prefix="econtexts" result-prefix="#default" />
 <xsl:strip-space elements="*" />


 <xsl:key name="wwfiles-files-by-type" match="wwfiles:File" use="@type" />
 <xsl:key name="wwsplits-files-by-type" match="wwsplits:File" use="@type" />
 <xsl:key name="wwsplits-splits-by-id" match="wwsplits:Split" use="@id" />
 <xsl:key name="wwtopics-topics-by-type" match="wwtopics:Topic" use="@type" />
 <xsl:key name="wwtopics-topics-by-child-text" match="wwtopics:Topic" use="./wwtopics:Text/text()" />


 <xsl:variable name="GlobalActionChecksum">
  <xsl:variable name="VarTransformChecksums">
   <xsl:value-of select="concat(wwuri:AsFilePath('wwtransform:self'), ':', wwfilesystem:GetChecksum(wwuri:AsFilePath('wwtransform:self')))" />
  </xsl:variable>
  <xsl:value-of select="wwstring:MD5Checksum($VarTransformChecksums)" />
 </xsl:variable>


 <xsl:template match="/">
  <wwfiles:Files version="1.0">

   <!-- Iterate input documents -->
   <!--                         -->
   <xsl:for-each select="$GlobalFiles[1]">
    <xsl:variable name="VarFilesByType" select="key('wwfiles-files-by-type', $ParameterDependsType)" />

    <xsl:variable name="VarProgressStartFiles" select="wwprogress:Start(count($VarFilesByType))" />

    <xsl:for-each select="$VarFilesByType">
     <xsl:variable name="VarProgressStartFile" select="wwprogress:Start(1)" />

     <xsl:variable name="VarFilesDocument" select="." />

     <!-- Load splits -->
     <!--             -->
     <xsl:variable name="VarFilesSplits" select="key('wwfiles-files-by-type', $ParameterSplitsType)[@groupID = $VarFilesDocument/@groupID][1]" />
     <xsl:variable name="VarSplits" select="wwexsldoc:LoadXMLWithoutResolver($VarFilesSplits/@path)" />

     <xsl:for-each select="$VarSplits[1]">
      <!-- Split -->
      <!--       -->
      <xsl:variable name="VarSplit" select="key('wwsplits-files-by-type', $ParameterContextsSplitFileType)[@groupID = $VarFilesDocument/@groupID][1]" />

      <!-- Transform -->
      <!--           -->
      <xsl:variable name="VarUpToDate" select="wwfilesext:UpToDate($VarSplit/@path, '', $VarFilesDocument/@groupID, $VarFilesDocument/@documentID, $GlobalActionChecksum)" />
      <xsl:if test="not($VarUpToDate)">
       <xsl:variable name="VarResultAsXML">
        <!-- Load document -->
        <!--               -->
        <xsl:variable name="VarContextTopics" select="wwexsldoc:LoadXMLWithoutResolver($VarFilesDocument/@path)" />

        <xsl:call-template name="Contexts-Contexts">
         <xsl:with-param name="ParamSplits" select="$VarSplits" />
         <xsl:with-param name="ParamContextTopics" select="$VarContextTopics" />
         <xsl:with-param name="ParamSplit" select="$VarSplit" />
        </xsl:call-template>
       </xsl:variable>
       <xsl:variable name="VarResult" select="msxsl:node-set($VarResultAsXML)" />
       <xsl:variable name="VarWriteResult" select="wwexsldoc:Document($VarResult, $VarSplit/@path, wwprojext:GetFormatSetting('encoding', 'utf-8'), 'xml', '1.0', 'yes')" />
      </xsl:if>

      <!-- Report Files -->
      <!--              -->
      <wwfiles:File path="{$VarSplit/@path}" type="{$ParameterType}" checksum="{wwfilesystem:GetChecksum($VarSplit/@path)}" projectchecksum="" groupID="{$VarFilesDocument/@groupID}" documentID="{$VarFilesDocument/@documentID}" actionchecksum="{$GlobalActionChecksum}" category="" use="{$ParameterUse}" deploy="{$ParameterDeploy}">
       <wwfiles:Depends path="{$VarFilesSplits/@path}" checksum="{$VarFilesSplits/@checksum}" groupID="{$VarFilesSplits/@groupID}" documentID="{$VarFilesSplits/@documentID}" />
       <wwfiles:Depends path="{$VarFilesDocument/@path}" checksum="{$VarFilesDocument/@checksum}" groupID="{$VarFilesDocument/@groupID}" documentID="{$VarFilesDocument/@documentID}" />
      </wwfiles:File>
     </xsl:for-each>

     <xsl:variable name="VarProgressEndFile" select="wwprogress:End()" />
    </xsl:for-each>

    <xsl:variable name="VarProgressEndFiles" select="wwprogress:End()" />
   </xsl:for-each>

  </wwfiles:Files>
 </xsl:template>


 <xsl:template name="Contexts-Contexts">
  <xsl:param name="ParamSplits" />
  <xsl:param name="ParamContextTopics" />
  <xsl:param name="ParamSplit" />

  <xsl:processing-instruction name="NLS">
   <xsl:text>TYPE="org.eclipse.help.contexts"</xsl:text>
  </xsl:processing-instruction>

  <!-- Define link root -->
  <!--                  -->
  <xsl:variable name="VarLinkRoot">
   <xsl:text>/</xsl:text>
   <xsl:value-of select="wwprojext:GetFormatSetting('eclipse-help-id-prefix')" />
   <xsl:text>.</xsl:text>
   <xsl:value-of select="wwstring:EclipseId(wwprojext:GetGroupName($ParamSplit/@groupID))" />
   <xsl:text>/</xsl:text>
  </xsl:variable>

  <econtexts:contexts>
   <xsl:for-each select="$ParamContextTopics[1]">

    <xsl:variable name="VarTopics" select="key('wwtopics-topics-by-type', 'topic') | key('wwtopics-topics-by-type', 'filename-and-topic')" />

    <xsl:for-each select="$VarTopics">
     <xsl:variable name="VarTopic" select="." />

     <xsl:if test="count($VarTopic | $VarTopics[wwtopics:Text/text() = $VarTopic/wwtopics:Text/text()][1]) = 1">
      <xsl:variable name="VarTopicText" select="$VarTopic/wwtopics:Text/text()" />

      <xsl:variable name="VarDescription">
       <!-- CUSTOM: Look for custom description -->
       <xsl:choose>
         <xsl:when test="string-length(substring-after($VarTopicText, '%')) &gt; 0" >
           <!-- Contains a custom description in the form '*%description' -->
           <xsl:value-of select="substring-after($VarTopicText, '%')" />
         </xsl:when>
         <xsl:otherwise>
           <!-- Use default -->
           <xsl:choose>
            <xsl:when test="$VarTopic/following-sibling::wwtopics:Topic[1]/@type = 'topic-description'">
             <xsl:value-of select="$VarTopic/following-sibling::wwtopics:Topic[1]/wwtopics:Text/text()" />
            </xsl:when>
            <xsl:otherwise>
             <xsl:value-of select="$VarTopic/following-sibling::wwtopics:ParagraphText/text()" />
            </xsl:otherwise>
           </xsl:choose>
         </xsl:otherwise>
       </xsl:choose>
      </xsl:variable>

      <xsl:variable name="VarTopicsByText" select="key('wwtopics-topics-by-child-text', $VarTopicText)" />

      <!-- CUSTOM: Look for a custom ID -->
      <xsl:variable name="VarContextID">
        <xsl:choose>
          <xsl:when test="contains($VarTopicText, ':')">
            <!-- Contains a description 'id:*' -->
            <xsl:value-of select="substring-before($VarTopicText, ':')" />
          </xsl:when>
          <xsl:when test="contains($VarTopicText, '%')">
            <!-- Contains a description without a colon 'id%*' (bad form) -->
            <xsl:value-of select="substring-before($VarTopicText, '%')" />
          </xsl:when>
          <xsl:otherwise>
            <!-- Use default value -->
            <xsl:value-of select="$VarTopicText" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <!-- CUSTOM: Look for custom label -->
      <xsl:variable name="VarContextLabel">
        <xsl:choose>
          <xsl:when test="(contains($VarTopicText, ':')) and (contains($VarTopicText, '%'))" >
            <!-- Contains an ID, label, and description  'id:label%description' -->
            <xsl:value-of select="substring-before(substring-after($VarTopicText, ':'),'%')" />
          </xsl:when>
          <xsl:when test="contains($VarTopicText, ':')" >
            <!-- No description, just ID and label  'id:label' -->
            <xsl:value-of select="substring-after($VarTopicText, ':')" />
          </xsl:when>
          <xsl:otherwise>
            <!-- Use default value, to be determined later -->
            <xsl:value-of select="''" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>


      <!-- CUSTOM: Use custom ID -->
      <!-- Original line
      <econtexts:context id="{$VarTopicText}">
      -->
      <econtexts:context id="{$VarContextID}" >
       <econtexts:description><xsl:value-of select="$VarDescription" /></econtexts:description>

       <xsl:for-each select="$VarTopicsByText">
        <xsl:variable name="VarTopicByText" select="." />

        <!-- Get topic split -->
        <!--                 -->
        <xsl:for-each select="$ParamSplits[1]">
         <xsl:variable name="VarTopicByTextSplit" select="key('wwsplits-splits-by-id', $VarTopicByText/@splitID)[@documentID = $VarTopicByText/@documentID][1]" />

         <!-- CUSTOM: include the HTML anchor too.-->
         <!--
         <xsl:variable name="VarLinkTarget" select="wwuri:GetRelativeTo($VarTopicByTextSplit/@path, $ParamSplit/@path)" />
         -->
         <xsl:variable name="VarLinkTargetTmp" select="wwuri:GetRelativeTo($VarTopicByTextSplit/@path, $ParamSplit/@path)" />
         <xsl:variable name="VarLinkTarget" select="concat($VarLinkTargetTmp, '#', $VarTopicByTextSplit/@id)" />


         <xsl:variable name="VarAbsoluteLinkTarget">
          <xsl:value-of select="$VarLinkRoot" />
          <xsl:value-of select="$VarLinkTarget" />
         </xsl:variable>

         <!-- CUSTOM -->
         <!-- Use custom label if it exists. -->
         <xsl:choose>
          <xsl:when test="string-length($VarContextLabel) &gt; 0 " >
           <econtexts:topic label="{$VarContextLabel}" href="{$VarAbsoluteLinkTarget}" />
          </xsl:when>
          <xsl:otherwise>
            <econtexts:topic label="{$VarTopicByTextSplit/@title}" href="{$VarAbsoluteLinkTarget}" />
          </xsl:otherwise>
         </xsl:choose>

        </xsl:for-each>
       </xsl:for-each>
      </econtexts:context>
     </xsl:if>
    </xsl:for-each>
   </xsl:for-each>
  </econtexts:contexts>
 </xsl:template>
</xsl:stylesheet>
