Advertisement
Guest User

Untitled

a guest
Dec 19th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 17.68 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <!--
  4.    License Agreement
  5.    
  6.    Copyright (C) 2009. The MITRE Corporation (http://www.mitre.org/). All Rights Reserved.
  7.    
  8.    Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  9.    
  10.    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  11.    
  12.    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  13.    
  14.    * The US Government will not be charged any license fee and/or royalties related to this software.
  15.    
  16.    * Neither name of The MITRE Corporation; nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  17.    
  18.    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19.    `AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20.    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21.    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
  22.    HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23.    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24.    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  25.    OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26.    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  27.    TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  28.    USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.    
  29. -->
  30.            
  31. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  32.    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  33.    xmlns:oval-def="http://oval.mitre.org/XMLSchema/oval-definitions-5"
  34.    version="2.0"
  35.    xmlns:ovalfn="http://oval.mitre.org/xsl/functions"
  36.    xmlns="http://oval.mitre.org/ovalutils/oval_splitter">
  37.  
  38.    
  39.     <xsl:output method="xml" indent="yes" omit-xml-declaration="no" encoding="UTF-8"/>
  40.  
  41.     <xsl:variable name="debugMode" as="xsd:boolean" select="false()"/>
  42.  
  43.     <!-- Get the file that holds the definition and the id of the definition -->
  44.     <xsl:param name="paramOvalId" as="xsd:string" required="no" select="string('.')" />
  45.     <xsl:param name="paramSplitLevel" as="xsd:string" required="no" select="string('.')" />
  46.     <xsl:param name="paramOutputDir" as="xsd:string" required="no" select="string('.')" />
  47.    
  48.    
  49.     <!-- Get the definition document and oval id node-->
  50.     <xsl:variable name="definitionDoc" select="oval-def:oval_definitions"/>
  51.  
  52.     <!--                                                                                -->
  53.     <!-- Root of the processing of the oval definitions document                        -->
  54.     <!--                                                                                -->
  55.     <xsl:template match="oval-def:oval_definitions">
  56.        
  57.         <!-- Retrieve all split level nodes -->
  58.        
  59.         <xsl:variable name="items" select="*[local-name()=$paramSplitLevel]/* | $definitionDoc//*[@id=$paramOvalId]" />
  60.        
  61.         <!-- Extract each item by id and output in its own file -->
  62.         <xsl:for-each select="$items">
  63.  
  64.             <!-- The id of the item to extract and the filename to output it -->
  65.             <xsl:variable name="ovalId" select="@id" />
  66.            
  67.             <xsl:variable name="outputDir">
  68.                 <xsl:choose>
  69.                     <xsl:when test="string-length($paramOutputDir) > 0">
  70.                         <xsl:value-of select="concat($paramOutputDir, '/')" />
  71.                     </xsl:when>
  72.                     <xsl:otherwise>
  73.                         <xsl:value-of select="string('.')" />
  74.                     </xsl:otherwise>
  75.                 </xsl:choose>
  76.             </xsl:variable>
  77.            
  78.             <xsl:variable name="filename" select="concat($outputDir, replace($ovalId, ':', '.'), '.xml')" />
  79.            
  80.             <!-- Create the result document -->
  81.             <xsl:result-document href="{$filename}">
  82.                
  83.                 <xsl:variable name="allIds" select="ovalfn:getAllIds($definitionDoc//oval-def:definitions/oval-def:definition[@id = 'no mathing id']/@id, $ovalId)"/>
  84.                
  85.                 <xsl:if test="$debugMode">
  86.                     <xsl:comment>
  87.                         <xsl:for-each select="$allIds">
  88.                             <xsl:sort select="." order="ascending"/>
  89.                             <xsl:text>
  90.                                
  91.                             </xsl:text>
  92.                             <xsl:value-of select="."></xsl:value-of>
  93.                         </xsl:for-each>
  94.                     </xsl:comment>
  95.                 </xsl:if>
  96.                
  97.                 <xsl:copy>
  98.                     <xsl:copy-of select="@*"/>
  99.                    
  100.                     <xsl:copy-of select="$definitionDoc//oval-def:generator"/>
  101.                    
  102.                     <!-- write the needed definitions -->
  103.                     <xsl:if test="ovalfn:set-contains-substr($allIds, ':def:')">
  104.                         <xsl:element name="definitions" namespace="http://oval.mitre.org/XMLSchema/oval-definitions-5">
  105.                            
  106.                             <xsl:for-each select="$definitionDoc//oval-def:definitions/oval-def:definition[@id = $allIds]">
  107.                                 <xsl:copy-of select="."/>
  108.                             </xsl:for-each>
  109.                            
  110.                             <xsl:if test="$debugMode">
  111.                                 <xsl:for-each select="$definitionDoc//oval-def:definitions/oval-def:definition">
  112.                                     <xsl:comment>
  113.                                         <xsl:text>Checking id: </xsl:text>
  114.                                         <xsl:value-of select="@id"/>
  115.                                     </xsl:comment>
  116.                                     <xsl:if test="ovalfn:member-of($allIds, @id)">
  117.                                         <xsl:copy-of select="."/>
  118.                                     </xsl:if>
  119.                                 </xsl:for-each>
  120.                             </xsl:if>
  121.                            
  122.                         </xsl:element>
  123.                     </xsl:if>
  124.                    
  125.                     <!-- write the needed tests -->
  126.                     <xsl:if test="ovalfn:set-contains-substr($allIds, ':tst:')">
  127.                         <xsl:element name="tests" namespace="http://oval.mitre.org/XMLSchema/oval-definitions-5">
  128.                             <xsl:for-each select="$definitionDoc//oval-def:tests/child::*[@id = $allIds]">
  129.                                 <xsl:copy-of select="."/>
  130.                             </xsl:for-each>
  131.                            
  132.                             <xsl:if test="$debugMode">    
  133.                                 <xsl:for-each select="$definitionDoc//oval-def:tests/child::*">
  134.                                     <xsl:comment>
  135.                                         <xsl:text>Checking id: </xsl:text>
  136.                                         <xsl:value-of select="@id"/>
  137.                                     </xsl:comment>
  138.                                     <xsl:if test="ovalfn:member-of($allIds, @id)">
  139.                                         <xsl:copy-of select="."/>
  140.                                     </xsl:if>
  141.                                 </xsl:for-each>
  142.                             </xsl:if>
  143.                            
  144.                         </xsl:element>
  145.                     </xsl:if>                    
  146.                    
  147.                     <!-- write objects -->
  148.                     <xsl:if test="ovalfn:set-contains-substr($allIds, ':obj:')">
  149.                         <xsl:element name="objects" namespace="http://oval.mitre.org/XMLSchema/oval-definitions-5">
  150.                             <xsl:for-each select="$definitionDoc//oval-def:objects/child::*[@id = $allIds]">
  151.                                 <xsl:copy-of select="."/>
  152.                             </xsl:for-each>
  153.                            
  154.                             <xsl:if test="$debugMode">
  155.                                 <xsl:for-each select="$definitionDoc//oval-def:objects/child::*">
  156.                                     <xsl:comment>
  157.                                         <xsl:text>Checking id: </xsl:text>
  158.                                         <xsl:value-of select="@id"/>
  159.                                     </xsl:comment>
  160.                                     <xsl:if test="ovalfn:member-of($allIds, @id)">
  161.                                         <xsl:copy-of select="."/>
  162.                                     </xsl:if>                        
  163.                                 </xsl:for-each>
  164.                             </xsl:if>
  165.                            
  166.                         </xsl:element>
  167.                     </xsl:if>                    
  168.                    
  169.                     <!-- write states -->
  170.                     <xsl:if test="ovalfn:set-contains-substr($allIds, ':ste:')">
  171.                         <xsl:element name="states" namespace="http://oval.mitre.org/XMLSchema/oval-definitions-5">
  172.                             <xsl:for-each select="$definitionDoc//oval-def:states/child::*[@id = $allIds]">
  173.                                 <xsl:copy-of select="."/>
  174.                             </xsl:for-each>
  175.                            
  176.                             <xsl:if test="$debugMode">
  177.                                 <xsl:for-each select="$definitionDoc//oval-def:states/child::*">
  178.                                     <xsl:comment>
  179.                                         <xsl:text>Checking id: </xsl:text>
  180.                                         <xsl:value-of select="@id"/>
  181.                                     </xsl:comment>
  182.                                     <xsl:if test="ovalfn:member-of($allIds, @id)">
  183.                                         <xsl:copy-of select="."/>
  184.                                     </xsl:if>
  185.                                 </xsl:for-each>
  186.                             </xsl:if>
  187.                            
  188.                         </xsl:element>
  189.                     </xsl:if>
  190.                    
  191.                     <!-- write variables -->
  192.                     <xsl:if test="ovalfn:set-contains-substr($allIds, ':var:')">
  193.                         <xsl:element name="variables" namespace="http://oval.mitre.org/XMLSchema/oval-definitions-5">
  194.                             <xsl:for-each select="$definitionDoc//oval-def:variables/child::*[@id = $allIds]">
  195.                                 <xsl:copy-of select="."/>
  196.                             </xsl:for-each>
  197.                            
  198.                             <xsl:if test="$debugMode">
  199.                                 <xsl:for-each select="$definitionDoc//oval-def:variables/child::*">
  200.                                     <xsl:comment>
  201.                                         <xsl:text>Checking id: </xsl:text>
  202.                                         <xsl:value-of select="@id"/>
  203.                                     </xsl:comment>
  204.                                     <xsl:if test="ovalfn:member-of($allIds, @id)">
  205.                                         <xsl:copy-of select="."/>
  206.                                     </xsl:if>                    
  207.                                 </xsl:for-each>
  208.                             </xsl:if>
  209.                            
  210.                         </xsl:element>
  211.                     </xsl:if>
  212.                    
  213.                 </xsl:copy>
  214.                
  215.             </xsl:result-document>
  216.            
  217.         </xsl:for-each>
  218.        
  219.     </xsl:template>
  220.  
  221.     <!--                             -->
  222.     <!-- Functions                   -->
  223.     <!--                             -->
  224.  
  225.     <!--
  226.        definitions -> definitions
  227.        definitions -> tests
  228.        states -> variables
  229.        variables -> variables
  230.        variables -> objects
  231.        objects -> variables
  232.        objects -> objects
  233.        objects -> states
  234.        objects -> variables      
  235.    -->
  236.  
  237.     <!-- recursively get all the ids needed for an id -->
  238.     <xsl:function name="ovalfn:getAllIds" as="item()*">
  239.         <xsl:param name="processedIds" as="item()*"/>
  240.         <xsl:param name="inputId" as="item()"/>
  241.        
  242.         <xsl:if test="$debugMode">
  243.             <xsl:comment>
  244.                 <xsl:text>looking for id: </xsl:text>
  245.                 <xsl:value-of select="$inputId"/>
  246.             </xsl:comment>
  247.         </xsl:if>
  248.  
  249.         <xsl:choose>
  250.             <xsl:when test="not(ovalfn:member-of($processedIds, $inputId))">
  251.  
  252.                 <xsl:choose>
  253.  
  254.                     <xsl:when test="contains($inputId, ':def:')">
  255.  
  256.                         <xsl:for-each select="$definitionDoc//oval-def:definition[@id = $inputId]//oval-def:extend_definition/@definition_ref |
  257.                            $definitionDoc//oval-def:definition[@id = $inputId]//oval-def:criterion/@test_ref">
  258.                            
  259.                             <xsl:variable name="returnedIds" select="ovalfn:getAllIds(($processedIds | $inputId), .)"/>
  260.                             <xsl:sequence select="$returnedIds | $processedIds | $inputId | ."/>
  261.                         </xsl:for-each>
  262.  
  263.                     </xsl:when>
  264.  
  265.                     <xsl:when test="contains($inputId, ':tst:')">
  266.  
  267.                         <xsl:for-each select="$definitionDoc//oval-def:tests/child::*[@id = $inputId]/*:object/@object_ref |    $definitionDoc//oval-def:definition[@id = $inputId]//oval-def:criterion/@test_ref">
  268.                            
  269.                             <xsl:variable name="returnedIds" select="ovalfn:getAllIds(($processedIds | $inputId), .)"/>
  270.                             <xsl:sequence select="$returnedIds | $processedIds | $inputId | ."/>
  271.                         </xsl:for-each>
  272.  
  273.                     </xsl:when>
  274.  
  275.                     <xsl:when test="contains($inputId, ':obj:')">
  276.  
  277.                         <xsl:for-each select="$definitionDoc//oval-def:objects/child::*[@id = $inputId]/descendant::*/*:object_reference/text() | $definitionDoc//oval-def:objects/child::*[@id = $inputId]/descendant::*/*:filter/text() |
  278.                            $definitionDoc//oval-def:objects/child::*[@id = $inputId]/descendant::*/*:var_ref/text() |
  279.                             $definitionDoc//oval-def:objects/child::*[@id = $inputId]/descendant::*/@var_ref">             
  280.                            
  281.                             <xsl:variable name="returnedIds" select="ovalfn:getAllIds(($processedIds | $inputId), .)"/>
  282.                             <xsl:sequence select="$returnedIds | $processedIds | $inputId | ."/>
  283.                         </xsl:for-each>
  284.  
  285.                     </xsl:when>
  286.  
  287.                     <xsl:when test="contains($inputId, ':ste:')">
  288.  
  289.                         <xsl:for-each select="$definitionDoc//oval-def:states/child::*[@id = $inputId]/descendant::*/@var_ref">
  290.                            
  291.                             <xsl:variable name="returnedIds" select="ovalfn:getAllIds(($processedIds | $inputId), .)"/>
  292.                             <xsl:sequence select="$returnedIds | $processedIds | $inputId | ."/>
  293.                         </xsl:for-each>
  294.  
  295.                     </xsl:when>
  296.  
  297.                     <xsl:when test="contains($inputId, ':var:')">
  298.  
  299.                         <xsl:for-each select="$definitionDoc//oval-def:variables/child::*[@id = $inputId]/descendant::*/@object_ref |
  300.                            $definitionDoc//oval-def:variables/child::*[@id = $inputId]/descendant::*/@var_ref |
  301.                            $definitionDoc//oval-def:variables/child::*[@id = $inputId]/descendant::*/@object_ref">
  302.                            
  303.                             <xsl:variable name="returnedIds" select="ovalfn:getAllIds(($processedIds | $inputId), .)"/>
  304.                             <xsl:sequence select="$returnedIds | $processedIds | $inputId | ."/>
  305.                         </xsl:for-each>
  306.  
  307.                     </xsl:when>
  308.  
  309.                 </xsl:choose>
  310.  
  311.             </xsl:when>
  312.             <xsl:otherwise>
  313.                 <xsl:sequence select="$processedIds"/>
  314.             </xsl:otherwise>
  315.         </xsl:choose>
  316.  
  317.     </xsl:function>
  318.  
  319.     <xsl:function name="ovalfn:set-contains-substr" as="xsd:boolean">
  320.         <xsl:param name="set" as="item()*"/>
  321.         <xsl:param name="subString" as="xsd:string"/>
  322.         <xsl:variable name="contains-substr" as="xsd:boolean*" select="for $test in $set return if (contains($test, $subString)) then true() else ()"/>
  323.        
  324.         <xsl:sequence select="not(empty($contains-substr))"/>
  325.     </xsl:function>
  326.    
  327.     <xsl:function name="ovalfn:element-equality" as="xsd:boolean">
  328.         <xsl:param name="item1" as="item()?"/>
  329.         <xsl:param name="item2" as="item()?"/>
  330.         <xsl:sequence select="$item1 = $item2"/>
  331.     </xsl:function>
  332.  
  333.     <xsl:function name="ovalfn:member-of" as="xsd:boolean">
  334.         <xsl:param name="set" as="item()*"/>
  335.         <xsl:param name="elem" as="item()"/>
  336.         <xsl:variable name="member-of" as="xsd:boolean*" select="for $test in $set return if (ovalfn:element-equality($test, $elem)) then true() else ()"/>
  337.  
  338.         <xsl:sequence select="not(empty($member-of))"/>
  339.     </xsl:function>
  340.  
  341.     <xsl:function name="ovalfn:difference" as="item()*">
  342.         <xsl:param name="nodes1" as="item()*"/>
  343.         <xsl:param name="nodes2" as="item()*"/>
  344.         <xsl:sequence select="for $test in $nodes1 return if (ovalfn:member-of($nodes2,$test)) then () else $test"/>
  345.     </xsl:function>
  346.  
  347. </xsl:stylesheet>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement