metatools/sysdeftools/lib/path-module.xsl
changeset 624 f70b728ea30c
equal deleted inserted replaced
621:96fee2635b19 624:f70b728ea30c
       
     1 <?xml version="1.0"?>
       
     2 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       
     3 <!--Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 	All rights reserved.
       
     5 	This component and the accompanying materials are made available
       
     6 	under the terms of the License "Eclipse Public License v1.0"
       
     7 	which accompanies this distribution, and is available
       
     8 	at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 
       
    10 	Initial Contributors:
       
    11 	Nokia Corporation - initial contribution.
       
    12 	Contributors:
       
    13 	Description:
       
    14 	XSLT module which contains named templates which process file paths
       
    15 -->
       
    16 
       
    17  <xsl:template name="lastbefore"><xsl:param name="string"/><xsl:param name="substr" select="'/'"/>
       
    18         <xsl:if test="contains($string,$substr)">
       
    19                 <xsl:value-of select="substring-before($string,$substr)"/>
       
    20                 <xsl:if test="contains(substring-after($string,$substr),$substr)">
       
    21 	                <xsl:value-of select="$substr"/>
       
    22 	              </xsl:if>
       
    23         <xsl:call-template name="lastbefore">
       
    24                 <xsl:with-param name="string" select="substring-after($string,$substr)"/>
       
    25                 <xsl:with-param name="substr" select="$substr"/>
       
    26         </xsl:call-template>
       
    27         </xsl:if>
       
    28 </xsl:template>
       
    29 
       
    30  <xsl:template name="joinpath"><xsl:param name="file"/><xsl:param name="rel"/>
       
    31 	<xsl:choose>
       
    32 		<xsl:when test="(contains($rel,'://') and not(contains(substring-before($rel,'://'),'/'))) or starts-with($rel,'/')"> <!-- absolute URI or absolute path-->
       
    33 			<xsl:value-of select="$rel"/>
       
    34 		</xsl:when>
       
    35 		<xsl:otherwise> <!-- relative link -->
       
    36 			<xsl:call-template name="reducepath">
       
    37 				<xsl:with-param name="file">
       
    38 					<xsl:call-template name="lastbefore">
       
    39 						<xsl:with-param name="string" select="$file"/>
       
    40 					</xsl:call-template>
       
    41 					<xsl:text>/</xsl:text>
       
    42 					<xsl:value-of select="$rel"/>
       
    43 				</xsl:with-param>
       
    44 			</xsl:call-template>
       
    45 		</xsl:otherwise>
       
    46 	</xsl:choose>
       
    47  </xsl:template>
       
    48 
       
    49 <xsl:template name="reducepath"><xsl:param name="file"/>
       
    50 	<xsl:call-template name="reducedotdotpath">
       
    51     	<xsl:with-param name="file">
       
    52 			<xsl:call-template name="reducedotpath">
       
    53 		    	<xsl:with-param name="file" select="$file"/>
       
    54 		    </xsl:call-template>
       
    55 		</xsl:with-param>
       
    56 	</xsl:call-template>
       
    57 </xsl:template>
       
    58 
       
    59 <xsl:template name="reducedotdotpath"><xsl:param name="file"/>
       
    60 	<xsl:variable name="pre">
       
    61 		<xsl:call-template name="lastbefore">
       
    62 			    <xsl:with-param name="string" select="substring-before($file,'/../')"/>
       
    63 		</xsl:call-template>
       
    64 	</xsl:variable>
       
    65 	<xsl:choose>
       
    66 		<xsl:when test="starts-with($file,'../')">
       
    67 			<xsl:text>../</xsl:text>
       
    68 			<xsl:call-template name="reducedotdotpath">
       
    69         		<xsl:with-param name="file" select="substring($file,4)"/>
       
    70 			</xsl:call-template>
       
    71 		</xsl:when>
       
    72 		<xsl:when test="contains($file,'/../') and $pre='' and not(starts-with($file,'/'))"> <!-- if file is a relative path and the dotdots go up to the top dir, don't start with a slash -->
       
    73 			<xsl:call-template name="reducepath">
       
    74         		<xsl:with-param name="file" select="substring-after($file,'/../')"/>
       
    75 			</xsl:call-template>
       
    76 		</xsl:when>
       
    77 		<xsl:when test="contains($file,'/../')">
       
    78 			<xsl:call-template name="reducepath">
       
    79         		<xsl:with-param name="file" select="concat($pre,'/',substring-after($file,'/../'))"/>
       
    80 			</xsl:call-template>
       
    81 		</xsl:when>
       
    82 		<xsl:otherwise><xsl:value-of select="$file"/></xsl:otherwise>
       
    83 	</xsl:choose>
       
    84  </xsl:template>
       
    85 
       
    86 <xsl:template name="reducedotpath"><xsl:param name="file"/>
       
    87 	<xsl:choose>	
       
    88 		<xsl:when test="starts-with($file,'./')">
       
    89 			<xsl:call-template name="reducedotpath">
       
    90         		<xsl:with-param name="file" select="substring($file,3)"/>
       
    91 			</xsl:call-template>
       
    92 		</xsl:when>
       
    93 
       
    94 		<xsl:when test="contains($file,'/./')">
       
    95 			<xsl:call-template name="reducepath">
       
    96         		<xsl:with-param name="file">
       
    97 	                <xsl:value-of select="substring-before($file,'/./')"/>
       
    98 			        <xsl:text>/</xsl:text>
       
    99 					<xsl:value-of select="substring-after($file,'/./')"/>
       
   100 				</xsl:with-param>
       
   101 			</xsl:call-template>
       
   102 		</xsl:when>
       
   103 		<xsl:when test="substring($file,string-length($file) - 1) = '/.'">
       
   104            <xsl:value-of select="substring($file,1,string-length($file) - 2)"/>
       
   105 		</xsl:when>
       
   106 		<xsl:otherwise><xsl:value-of select="$file"/></xsl:otherwise>
       
   107 	</xsl:choose>
       
   108  </xsl:template>
       
   109 
       
   110 </xsl:stylesheet>