<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5"
  xmlns:unix-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#unix"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:cdf="http://checklists.nist.gov/xccdf/1.1" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:oval-res="http://oval.mitre.org/XMLSchema/oval-results-5"
  xmlns:oval-sc="http://oval.mitre.org/XMLSchema/oval-system-characteristics-5"
  xmlns:unix-sc="http://oval.mitre.org/XMLSchema/oval-system-characteristics-5#unix"
  xmlns:ind-def="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent"
  xmlns:ind-sc="http://oval.mitre.org/XMLSchema/oval-system-characteristics-5#independent"
  exclude-result-prefixes="oval-def xsl cdf xsd oval-res oval-sc unix-sc unix-def ind-sc ind-def">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <!-- This library is to handle the all Mac OS X patches with follwoing criteria:
          
          Logic :-
          
          <criteria operator="AND">
          <criterion comment="Test for the existence of the file /etc/ntp.conf" test_ref="oval:com.mcafee.oval:tst:243238" />
          <criterion test_ref="oval:com.mcafee.oval:tst:954014" negate="true" comment="Mac OS X NTP Security Update is installed"/>
          </criteria>
          
          
          Ex. OVAL ID - oval:com.mcafee.oval:def:954013
  -->

  <xsl:param name="defid" />
  <xsl:template match="/">
    <xsl:call-template name="getFindings">
      <xsl:with-param name="definitionId" select="$defid"/>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template name="getFindings">
    <xsl:param name="definitionId"/>
    <xsl:variable name="result" select="/oval-res:oval_results/oval-res:results/oval-res:system/oval-res:definitions/oval-res:definition[@definition_id=$definitionId]/@result"/>
    
    <xsl:variable name="xmlfileContentTest_id" select="/oval-res:oval_results/oval-def:oval_definitions/oval-def:tests/ind-def:xmlfilecontent_test/@id"/>
    <xsl:variable name="result2" select="/oval-res:oval_results/oval-res:results/oval-res:system/oval-res:definitions/oval-res:definition[@definition_id=$definitionId]/descendant::oval-res:criterion[@test_ref=$xmlfileContentTest_id]/@result"/>
    
    <xsl:variable name="definitionXmlTextFileTest" select="/oval-res:oval_results/oval-def:oval_definitions/oval-def:tests/ind-def:xmlfilecontent_test[@id=$xmlfileContentTest_id]"/>
    <xsl:variable name="xmlfilePath" select="/oval-res:oval_results/oval-def:oval_definitions/oval-def:objects/ind-def:xmlfilecontent_object[@id=$definitionXmlTextFileTest/ind-def:object/@object_ref]/ind-def:path/text()" />
    <xsl:variable name="xmlfileName" select="/oval-res:oval_results/oval-def:oval_definitions/oval-def:objects/ind-def:xmlfilecontent_object[@id=$definitionXmlTextFileTest/ind-def:object/@object_ref]/ind-def:filename/text()" />
    
    <xsl:variable name="fileTest_id" select="/oval-res:oval_results/oval-def:oval_definitions/oval-def:tests/unix-def:file_test/@id"/>        
    <xsl:variable name="fileTest" select="/oval-res:oval_results/oval-def:oval_definitions/oval-def:tests/unix-def:file_test[@id=$fileTest_id]"/>
    <xsl:variable name="filePath" select="/oval-res:oval_results/oval-def:oval_definitions/oval-def:objects/unix-def:file_object[@id=$fileTest/unix-def:object/@object_ref]/unix-def:path/text()" />
    <xsl:variable name="fileName" select="/oval-res:oval_results/oval-def:oval_definitions/oval-def:objects/unix-def:file_object[@id=$fileTest/unix-def:object/@object_ref]/unix-def:filename/text()" />
    <xsl:variable name="fileInfo" select="concat($filePath,'/', $fileName)"/>
    
    <xsl:variable name="result1" select="/oval-res:oval_results/oval-res:results/oval-res:system/oval-res:definitions/oval-res:definition[@definition_id=$definitionId]/descendant::oval-res:criterion[@test_ref=$fileTest_id]/@result"/>     
    
    <xsl:variable name="title" select="/oval-res:oval_results/oval-def:oval_definitions/oval-def:definitions/oval-def:definition[ @id = $definitionId ]/oval-def:metadata/oval-def:title"/>
    
    <xsl:variable name="isViolationValue">
      <xsl:choose>
        <xsl:when test="$result = 'false' ">
          <xsl:text>false</xsl:text>
        </xsl:when>
        <xsl:when test="$result = 'true' ">
          <xsl:text>true</xsl:text>
        </xsl:when>
      </xsl:choose>
    </xsl:variable>
    
    
    <xsl:variable name="message_id">
      <!-- Check if result is false ......................................... -->
      <xsl:if test="$result='false'">
        <xsl:choose>
          <xsl:when test="$result1='false'">
            <xsl:text>com.mcafee.pa.msg.macosFilePresent</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>com.mcafee.pa.msg.macosUpdateInstalled</xsl:text>    
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>            
      <!-- Check if result is true .......................................-->
      <xsl:if test="$result='true'">
        <xsl:text>com.mcafee.pa.msg.macosUpdateInstalled</xsl:text>
      </xsl:if>               
    </xsl:variable>
    
    <xsl:element name="findings" namespace="http://results.pa.mcafee.com/findings/5.2">
      <xsl:attribute name="id">
        <xsl:value-of select="$definitionId"/>
      </xsl:attribute>
      <xsl:element name="finding" namespace="http://results.pa.mcafee.com/findings/5.2">
        <xsl:attribute name="messageId">
          <xsl:value-of select="$message_id"/>
        </xsl:attribute>
        <xsl:attribute name="isViolation">
          <xsl:value-of select="$isViolationValue"/>
        </xsl:attribute>
        <xsl:element name="instanceValue" namespace="http://results.pa.mcafee.com/findings/5.2">
          <xsl:attribute name="key">patch_name</xsl:attribute>
          <xsl:value-of select="$title"/>
        </xsl:element>
        <xsl:element name="instanceValue" namespace="http://results.pa.mcafee.com/findings/5.2">
          <xsl:attribute name="key">filePath</xsl:attribute>
          <xsl:value-of select="$filePath"/>
        </xsl:element>
        <xsl:element name="instanceValue" namespace="http://results.pa.mcafee.com/findings/5.2">
          <xsl:attribute name="key">fileName</xsl:attribute>
          <xsl:value-of select="$fileName"/>
        </xsl:element>
      </xsl:element>
    </xsl:element>
</xsl:template>
</xsl:stylesheet>
