<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text"/>

    <xsl:template match="/">
        <xsl:for-each select="wurfl/devices/device[@id='samsung_e640_ver1']"> <!-- TODO remove this device after testing -->
            <xsl:variable name="max_jar_size">
                <xsl:call-template name="getCapability">
                    <xsl:with-param name="deviceNode" select="."/>
                    <xsl:with-param name="capabilityName" select="'j2me_max_jar_size'"/>
                </xsl:call-template>
            </xsl:variable>
            
            Max jar size: <xsl:value-of select="$max_jar_size"/>
        </xsl:for-each>
    </xsl:template>
    
    <!-- templates -->
    
    <!-- PS, call named templates like this:
    <xsl:call-template name="templateName">
        <xsl:with-param name="arg0" select="'the argument value'"/>
    </xsl:template>
    -->
    
    <!--
    Return the named capability of the provided node. If the node does not have the capability defined
    (e.g. /device/group[@id='GROUP_ID']/capability[@name='CAPABILITY_NAME']/@value
    does not exist for that device), then we will need to inspect the device's parent
    (referenced by device[@fall_back='PARENT_ID']) and see if that parent has the property.
    We will do this recursively until we find the ancestor with the property. 
   
    TEST CASE: the device "samsung_e640_ver1" does not have the capability  "j2me_max_jar_size" (in group=j2me).
    Iterating through the ancestors yields:
    opwv_v62_generic (does not have j2me_max_jar_size)
    opwv_v61_generic (does not have j2me_max_jar_size)
    opwv_v6_generic (does not have j2me_max_jar_size)
    upgui_generic (does not have j2me_max_jar_size)
    generic (j2me_max_jar_size = 0)
    
    -->
    <xsl:template name="getCapability">
        <xsl:param name="deviceNode"/>
        <xsl:param name="capabilityName"/>
        
        <!-- first, get device[id='deviceNode[call_back='XXX']']
            i.e. the device as referenced by the argument's "call_back" value.
        -->
        <xsl:variable name="fallBackId">
            <xsl:value-of select="$deviceNode/@fall_back"/>
        </xsl:variable>
        <!-- ignore the fall_back if it's "root" -->
        <xsl:choose>
            <xsl:when test="$fallBackId='root'">
                <!-- return the current node's capability -->
                <xsl:value-of select="$deviceNode/group/capability[@name='$capabilityName']/@value"/>
            </xsl:when>
            <xsl:otherwise>
                <!-- see if device has capability -->
                <xsl:choose>
                    <xsl:when test="$deviceNode/group/capability[@name='$capabilityName']/@value">
                        <xsl:value-of select="$deviceNode/group/capability[@name='$capabilityName']/@value"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <!-- get device by fall_back id and recurse -->
                        <xsl:variable name="fallBackDevice">
                            <xsl:copy-of select="//device[@id=$fallBackId]"/>
                        </xsl:variable>
                        
                        <xsl:call-template name="getCapability">
                            <xsl:with-param name="deviceNode" select="$fallBackDevice"/>
                            <xsl:with-param name="capabilityName" select="$capabilityName"/>
                        </xsl:call-template>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

	<!--<xsl:template match="/wurfl/devices/device/@id">
		<xsl:value-of select="."/>
	</xsl:template>-->
    
</xsl:stylesheet>



<!-- Stylus Studio meta-information - (c) 2004-2006. Progress Software Corporation. All rights reserved.
<metaInformation>
<scenarios ><scenario default="yes" name="wurfl" userelativepaths="yes" externalpreview="no" url="wurfl.xml" htmlbaseurl="" outputurl="" processortype="internal" useresolver="yes" profilemode="0" profiledepth="" profilelength="" urlprofilexml="" commandline="" additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext="" validateoutput="no" validator="internal" customvalidator=""/></scenarios><MapperMetaTag><MapperInfo srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no"/><MapperBlockPosition></MapperBlockPosition><TemplateContext></TemplateContext><MapperFilter side="source"></MapperFilter></MapperMetaTag>
</metaInformation>
-->