buildframework/helium/external/helium-antlib/sysdef/demo/data/sf/os/buildtools/bldsystemtools/buildsystemtools/joinsysdef-module.xsl
changeset 179 d8ac696cc51f
equal deleted inserted replaced
1:be27ed110b50 179:d8ac696cc51f
       
     1 <?xml version="1.0"?>
       
     2 <!-- 
       
     3 ============================================================================ 
       
     4 Name        : joinsysdef-module.xsl 
       
     5 Part of     : Helium AntLib
       
     6 
       
     7 Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     8 All rights reserved.
       
     9 This component and the accompanying materials are made available
       
    10 under the terms of the License "Eclipse Public License v1.0"
       
    11 which accompanies this distribution, and is available
       
    12 at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    13 
       
    14 Initial Contributors:
       
    15 Nokia Corporation - initial contribution.
       
    16 
       
    17 Contributors:
       
    18 
       
    19 Description:
       
    20 
       
    21 ============================================================================
       
    22 -->
       
    23  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common" exclude-result-prefixes="exslt">
       
    24 	<!-- save SF namespace as a constant to avoid the risk of typos-->
       
    25  <xsl:variable name="defaultns">http://www.symbian.org/system-definition</xsl:variable>
       
    26  
       
    27 <!-- create a stand-alone sysdef from a linked set of fragments -->
       
    28 
       
    29 <xsl:template match="/*" mode="join">
       
    30 	<xsl:message terminate="yes">Cannot process this document</xsl:message>
       
    31 </xsl:template>
       
    32 
       
    33 
       
    34 <xsl:template match="/SystemDefinition[@schema='3.0.0' and count(*)=1]" mode="join">
       
    35 	<xsl:param name="origin" select="/.."/>
       
    36 	<xsl:param name="root"/>
       
    37 	<xsl:param name="filename"/>
       
    38 	<xsl:param name="namespaces"/>
       
    39 	<xsl:choose>
       
    40 		<xsl:when test="$origin">	<!-- this sysdef fragment was linked from a parent sysdef -->
       
    41 			<xsl:for-each select="*"> <!-- can be only one -->
       
    42 				<xsl:variable name="upid"><xsl:apply-templates select="$origin/@id" mode="my-id"/></xsl:variable>		<!-- namespaceless ID of this in parent doc -->
       
    43 				<xsl:variable name="id"><xsl:apply-templates select="@id" mode="my-id"/></xsl:variable>						<!-- namespaceless ID of this here -->
       
    44 				<xsl:variable name="upns"><xsl:apply-templates select="$origin/@id" mode="my-namespace"/></xsl:variable>	<!-- ID's namespace in parent doc -->
       
    45 				<xsl:variable name="ns"><xsl:apply-templates select="@id" mode="my-namespace"/></xsl:variable>	<!-- ID's namespace -->
       
    46 				<xsl:if test="$id!=$upid or $ns!=$upns">
       
    47 					<xsl:message terminate="yes">Linked ID "<xsl:value-of select="$id"/>" (<xsl:value-of select="$ns"/>) must match linking document "<xsl:value-of select="$upid"/>" (<xsl:value-of select="$upns"/>)</xsl:message>
       
    48 				</xsl:if>
       
    49 				<!-- copy any attributes not already defined (parent doc overrides child doc)-->
       
    50 				<xsl:for-each select="@*">
       
    51 					<xsl:variable name="n" select="name()"/>
       
    52 					<xsl:choose>
       
    53 						<xsl:when test="$n='id'"/> <!-- never copy this, always set -->
       
    54 						<xsl:when test="$origin/@*[name()=$n]"> <!-- don't copy if already set -->
       
    55 							<xsl:message>Cannot set "<xsl:value-of select="$n"/>", already set</xsl:message>
       
    56 						</xsl:when>
       
    57 						<xsl:when test="$n='before'">
       
    58 							<!-- ensure ns is correct (if any future attribtues will ever use an ID, process it here too)-->
       
    59 							<xsl:apply-templates select="." mode="join">
       
    60 								<xsl:with-param name="namespaces" select="$namespaces"/>
       
    61 							</xsl:apply-templates>
       
    62 						</xsl:when> 
       
    63 						<xsl:otherwise><xsl:copy-of select="."/></xsl:otherwise> <!-- just copy anything else -->
       
    64 					</xsl:choose>
       
    65 				</xsl:for-each>
       
    66 				<xsl:copy-of select="../namespace::*[not(.=$namespaces)]"/> <!-- set any namespaces not already set (they should all alreayd be, but some XSLT processors are quirky) -->
       
    67 				<xsl:apply-templates select="*|comment()" mode="join">
       
    68 					<xsl:with-param name="root" select="$root"/>
       
    69 					<xsl:with-param name="filename" select="$filename"/>
       
    70 					<xsl:with-param name="namespaces" select="$namespaces | ../namespace::*[not(.=$namespaces)]"/>
       
    71 				</xsl:apply-templates>
       
    72 			</xsl:for-each>
       
    73 		</xsl:when>
       
    74 		<xsl:when test="function-available('exslt:node-set')">
       
    75 			<!--try to put all namespaces in root element -->
       
    76 			<xsl:variable name="nss">
       
    77 				<!-- contains node set of namespaces to add to root element.
       
    78 					May panic if there are too many single-letter namespaces and this can't create a new one -->
       
    79 				<xsl:call-template name="needed-namespaces">
       
    80 					<xsl:with-param name="foundns">
       
    81 						<xsl:apply-templates select="//*[(self::component or self::collection or self::package or self::layer) and @href]" mode="scan-for-namespaces"/>
       
    82 					</xsl:with-param>
       
    83 				</xsl:call-template>
       
    84 			</xsl:variable>
       
    85 			<xsl:variable name="ns" select="@id-namespace | namespace::* | exslt:node-set($nss)/*"/>
       
    86 			<xsl:copy><xsl:copy-of select="@*"/>
       
    87 				<xsl:for-each select="exslt:node-set($nss)/*"> <!-- add namespace definitions -->
       
    88 					<xsl:attribute name="xmlns:{name()}">
       
    89 						<xsl:value-of select="."/>
       
    90 					</xsl:attribute>
       
    91 				</xsl:for-each>
       
    92 				<xsl:apply-templates select="*|comment()" mode="join">
       
    93 					<xsl:with-param name="namespaces" select="$ns"/>
       
    94 					<xsl:with-param name="root" select="$root"/>
       
    95 					<xsl:with-param name="filename" select="$filename"/>
       
    96 				</xsl:apply-templates>
       
    97 			</xsl:copy>
       
    98 		</xsl:when>
       
    99 		<xsl:otherwise> <!-- can't handle node-set() so put the namespaces in the document instead of the root -->
       
   100 			<xsl:variable name="ns" select="@id-namespace | namespace::*"/>
       
   101 			<xsl:copy><xsl:copy-of select="@*"/>
       
   102 				<xsl:apply-templates select="*|comment()" mode="join">
       
   103 					<xsl:with-param name="namespaces" select="$ns"/>
       
   104 					<xsl:with-param name="root" select="$root"/>
       
   105 					<xsl:with-param name="filename" select="$filename"/>
       
   106 				</xsl:apply-templates>
       
   107 			</xsl:copy>
       
   108 		</xsl:otherwise>
       
   109 	</xsl:choose>
       
   110 </xsl:template>
       
   111 
       
   112 <xsl:template match="*" mode="scan-for-namespaces"/> <!-- just in case of errors, consider replacing by terminate -->
       
   113 <xsl:template match="*[@href and not(self::meta)]" mode="scan-for-namespaces">
       
   114 	<!-- produce a list of namespace-prefix namespace pairs separated by newlines, in reverse order found in documents 
       
   115 		reverse order so we can try to use the first namespace prefix defined if it's available-->
       
   116 	<xsl:for-each select="document(@href,.)/*">
       
   117 		<xsl:apply-templates select="//*[(self::component or self::collection or self::package or self::layer) and @href]" mode="scan-for-namespaces"/>
       
   118 		<xsl:for-each select="//namespace::* | @id-namespace">
       
   119 			<xsl:value-of select="concat(name(),' ',.,'&#xa;')"/>
       
   120 		</xsl:for-each>
       
   121 	</xsl:for-each>			
       
   122 </xsl:template>
       
   123 
       
   124 <xsl:template name="needed-namespaces">
       
   125 	<xsl:param name="foundns"/>
       
   126 	<xsl:param name="usedpre"/>
       
   127 	<xsl:variable name="line" select="substring-before($foundns,'&#xa;')"/> <!-- always has trailing newline -->
       
   128 	<xsl:variable name="name" select="substring-after($line,' ')"/> <!-- namespace prefix -->
       
   129 	<xsl:variable name="remainder" select="substring-after($foundns,'&#xa;')"/>
       
   130 	<xsl:variable name="newprefix">
       
   131 		<xsl:if test="not(contains(concat('&#xa;',$remainder),concat('&#xa;',$line,'&#xa;'))) and
       
   132 			not(//namespace::*[.=$name] or @id-namespace[.=$name] or (not(@id-namespace) and $defaultns=$name))">
       
   133 					<xsl:apply-templates select="." mode="ns-prefix">
       
   134 						<xsl:with-param name="ns" select="$name"/>
       
   135 						<xsl:with-param name="pre" select="substring-before($line,' ')"/>
       
   136 						<xsl:with-param name="dontuse" select="$usedpre"/>
       
   137 					</xsl:apply-templates>
       
   138 		</xsl:if>
       
   139 	</xsl:variable>
       
   140 	<xsl:if test="$newprefix!=''">
       
   141 		<!-- can treat this as if it were a namespace node -->
       
   142 		<xsl:element name="{$newprefix}">
       
   143 			<xsl:value-of select="$name"/>
       
   144 		</xsl:element>
       
   145 	</xsl:if>
       
   146 	<xsl:if test="$remainder!=''">
       
   147 		<xsl:call-template name="needed-namespaces">
       
   148 			<xsl:with-param name="foundns" select="$remainder"/>
       
   149 			<xsl:with-param name="usedpre" select="concat($usedpre,' ',$newprefix,' ')"/>
       
   150 		</xsl:call-template>
       
   151 	</xsl:if>
       
   152 </xsl:template>
       
   153 
       
   154 <xsl:template match="/SystemDefinition" mode="ns-prefix">
       
   155 	<!-- should be able to replace this with mechanism that uses the XSLT processor's own ability to generate namespaces -->
       
   156 	<xsl:param name="ns"/>
       
   157 	<xsl:param name="pre"/>
       
   158 	<xsl:param name="dontuse"/>
       
   159 	<xsl:param name="chars">ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz</xsl:param>
       
   160 	<xsl:variable name="name" select="substring(substring-after($ns,'http://www.'),1,1)"/>
       
   161 	<xsl:choose>
       
   162 		<xsl:when test="$pre!='' and $pre!='id-namespace' and not(//namespace::*[name()=$pre]) and not(contains($dontuse,concat(' ',$pre,' ')))">
       
   163 			<xsl:value-of select="$pre"/>
       
   164 		</xsl:when>
       
   165 		<xsl:when test="$ns='' and $chars=''">
       
   166 			<xsl:message terminate="yes">Cannot create namespace prefix for downstream default namespace</xsl:message>
       
   167 		</xsl:when>
       
   168 		<xsl:when test="$name!='' and not(contains($dontuse,concat(' ',$name,' ')))"><xsl:value-of select="$name"/></xsl:when>
       
   169 		<xsl:when test="namespace::*[name()=substring($chars,1,1)] or contains($dontuse,concat(' ',substring($chars,1,1),' '))">
       
   170 			<xsl:apply-templates mode="ns-prefix">
       
   171 				<xsl:with-param name="chars" select="substring($chars,2)"/>
       
   172 			</xsl:apply-templates>
       
   173 		</xsl:when>
       
   174 		<xsl:otherwise>
       
   175 			<xsl:value-of select="substring($chars,1,1)"/>
       
   176 		</xsl:otherwise>
       
   177 	</xsl:choose>
       
   178 </xsl:template>
       
   179 
       
   180 
       
   181 <xsl:template match="unit" mode="join">	<xsl:param name="root"/><xsl:param name="filename"/>
       
   182 	<xsl:element name="{name()}">
       
   183 		<xsl:apply-templates select="@*" mode="join">
       
   184 			<xsl:with-param name="root" select="$root"/>
       
   185 			<xsl:with-param name="filename" select="$filename"/>
       
   186 		</xsl:apply-templates>
       
   187 	</xsl:element>
       
   188 </xsl:template>
       
   189 
       
   190 <!-- copy metadata verbatim
       
   191 	Should add mechanism to selectively include or filter metadata sections -->
       
   192 <xsl:template match="meta" priority="2">
       
   193 	<xsl:element name="{name()}">
       
   194 		<xsl:apply-templates select="@*" mode="join"/>
       
   195 		<xsl:choose>
       
   196 			<xsl:when test="@href">
       
   197 				<xsl:copy-of select="document(@href,.)/*"/> 
       
   198 			</xsl:when>
       
   199 			<xsl:otherwise>
       
   200 				<xsl:copy-of select="*|comment()"/>
       
   201 			</xsl:otherwise>
       
   202 		</xsl:choose>
       
   203 	</xsl:element>
       
   204 </xsl:template>
       
   205 
       
   206 <xsl:template match="*" mode="join">	<xsl:param name="root"/><xsl:param name="filename"/><xsl:param name="namespaces"/>
       
   207 	<xsl:element name="{name()}"> <!-- use this instead of <copy> so xalan doesn't add extra wrong namespaces -->
       
   208 		<xsl:apply-templates select="@*" mode="join">
       
   209 			<xsl:with-param name="namespaces" select="$namespaces"/>
       
   210 		</xsl:apply-templates>
       
   211 		
       
   212 		<xsl:choose>
       
   213 			<xsl:when test="@href">
       
   214 				<xsl:variable name="origin" select="."/>
       
   215 				<xsl:apply-templates select="document(@href,.)/*" mode="join">
       
   216 					<xsl:with-param name="origin" select="$origin"/>
       
   217 					<xsl:with-param name="namespaces" select="$namespaces"/>
       
   218 					<xsl:with-param name="filename">
       
   219 						<xsl:call-template name="joinpath">
       
   220 							<xsl:with-param name="file" select="$filename"/>
       
   221 							<xsl:with-param name="rel" select="$origin/@href"/>
       
   222 						</xsl:call-template>					
       
   223 					</xsl:with-param>
       
   224 					<xsl:with-param name="root">
       
   225 							<xsl:value-of select="$root"/>/<xsl:call-template name="lastbefore">
       
   226 							<xsl:with-param name="string" select="$origin/@href"/>
       
   227 						</xsl:call-template>
       
   228 					</xsl:with-param>
       
   229 				</xsl:apply-templates> 
       
   230 			</xsl:when>
       
   231 			<xsl:otherwise>
       
   232 				<xsl:apply-templates select="*|comment()" mode="join">
       
   233 					<xsl:with-param name="root" select="$root"/>
       
   234 					<xsl:with-param name="filename" select="$filename"/>
       
   235 					<xsl:with-param name="namespaces" select="$namespaces"/>
       
   236 				</xsl:apply-templates>
       
   237 			</xsl:otherwise>
       
   238 		</xsl:choose>
       
   239 	</xsl:element>
       
   240 </xsl:template>
       
   241 
       
   242 <xsl:template match="@mrp[starts-with(.,'/')] | @bldFile[starts-with(.,'/')] | @base[starts-with(.,'/')]" mode="join">
       
   243 	<xsl:copy-of select="."/>
       
   244 </xsl:template>
       
   245 
       
   246 <xsl:template match="@mrp|@bldFile|@base" mode="join">	<xsl:param name="root"/><xsl:param name="filename"/>
       
   247 	<xsl:attribute name="{name()}">
       
   248 		<xsl:call-template name="joinpath">
       
   249 			<xsl:with-param name="file" select="$filename"/>
       
   250 			<xsl:with-param name="rel" select="."/>
       
   251 		</xsl:call-template>	
       
   252 	</xsl:attribute>	
       
   253 </xsl:template>
       
   254 
       
   255 
       
   256 <xsl:template match="@href" mode="join"/>
       
   257 
       
   258 <xsl:template match="@*" mode="my-namespace"> <!-- the namespace of an ID -->
       
   259 	<xsl:choose>
       
   260 		<xsl:when test="contains(.,':')">
       
   261 			<xsl:value-of select="ancestor::*/namespace::*[name()=substring-before(current(),':')]"/>
       
   262 		</xsl:when>
       
   263 		<xsl:when test="/SystemDefinition/@id-namespace">
       
   264 			<xsl:value-of select="/SystemDefinition/@id-namespace"/>
       
   265 		</xsl:when>
       
   266 		<xsl:otherwise>
       
   267 			<xsl:value-of select="$defaultns"/>
       
   268 		</xsl:otherwise>
       
   269 	</xsl:choose>
       
   270 </xsl:template>
       
   271 
       
   272 
       
   273 <xsl:template match="@*" mode="my-id"> <!-- the ID with namespace prefix removed -->
       
   274 	<xsl:choose>
       
   275 		<xsl:when test="contains(.,':')">
       
   276 			<xsl:value-of select="substring-after(.,':')"/>
       
   277 		</xsl:when>
       
   278 		<xsl:otherwise>
       
   279 			<xsl:value-of select="."/>
       
   280 		</xsl:otherwise>
       
   281 	</xsl:choose>
       
   282 </xsl:template>
       
   283 
       
   284 
       
   285 
       
   286 <xsl:template match="@id|@before" mode="join">
       
   287 	<xsl:param name="namespaces"/>
       
   288 	<!-- this will change the namespace prefixes for all IDs to match the root document -->
       
   289 	<xsl:variable name="ns">
       
   290 		<xsl:apply-templates select="." mode="my-namespace"/>
       
   291 	</xsl:variable>
       
   292 	<xsl:if test="$ns=''">
       
   293 		<xsl:message terminate="yes">Could not find namespace for <xsl:value-of select="."/>
       
   294 		</xsl:message>
       
   295 	</xsl:if>
       
   296 	<xsl:variable name="prefix" select="name($namespaces[.=$ns])"/>
       
   297 	<xsl:attribute name="{name()}">
       
   298 	<xsl:choose>
       
   299 		<xsl:when test="$prefix = 'id-namespace' or  (not($namespaces[name()='id-prefix']) and $ns=$defaultns)"/> <!-- it's the default namespace, no prefix -->
       
   300 		<xsl:when test="$prefix='' and contains(.,':')">
       
   301 			<!-- complex: copy id and copy namespace (namespace should be copied already)-->
       
   302 			<xsl:value-of select="."/>
       
   303 		</xsl:when>
       
   304 		<xsl:when test="$prefix='' and $ns=$defaultns"/> <!-- no prefix and it's the default --> 
       
   305 		<xsl:when test="$prefix!=''">			<!-- just change the prefix -->
       
   306 			<xsl:value-of select="concat($prefix,':')"/>
       
   307 		</xsl:when>
       
   308 		<xsl:otherwise>
       
   309 		<xsl:message terminate="yes">Error</xsl:message>
       
   310 		</xsl:otherwise>
       
   311 	</xsl:choose>
       
   312 		<xsl:apply-templates select="." mode="my-id"/>
       
   313 	</xsl:attribute>
       
   314 </xsl:template>
       
   315 
       
   316 
       
   317 
       
   318 <xsl:template match="@*|comment()" mode="join"><xsl:copy-of select="."/></xsl:template>
       
   319 
       
   320 
       
   321 <!-- path handling follows -->
       
   322 
       
   323  <xsl:template name="lastbefore"><xsl:param name="string"/><xsl:param name="substr" select="'/'"/>
       
   324         <xsl:if test="contains($string,$substr)">
       
   325                 <xsl:value-of select="substring-before($string,$substr)"/>
       
   326                 <xsl:if test="contains(substring-after($string,$substr),$substr)">
       
   327 	                <xsl:value-of select="$substr"/>
       
   328 	              </xsl:if>
       
   329         <xsl:call-template name="lastbefore">
       
   330                 <xsl:with-param name="string" select="substring-after($string,$substr)"/>
       
   331                 <xsl:with-param name="substr" select="$substr"/>
       
   332         </xsl:call-template>
       
   333         </xsl:if>
       
   334 </xsl:template>
       
   335 
       
   336  <xsl:template name="joinpath"><xsl:param name="file"/><xsl:param name="rel"/>
       
   337         <xsl:call-template name="reducepath">
       
   338         <xsl:with-param name="file">
       
   339 	        <xsl:call-template name="lastbefore">
       
   340 	                <xsl:with-param name="string" select="$file"/>
       
   341 	        </xsl:call-template>
       
   342 	        <xsl:text>/</xsl:text>
       
   343 	        <xsl:value-of select="$rel"/>
       
   344 	       </xsl:with-param>
       
   345 	      </xsl:call-template>
       
   346  </xsl:template>
       
   347 
       
   348 <xsl:template name="reducepath"><xsl:param name="file"/>
       
   349 	<xsl:call-template name="reducedotdotpath">
       
   350     	<xsl:with-param name="file">
       
   351 			<xsl:call-template name="reducedotpath">
       
   352 		    	<xsl:with-param name="file" select="$file"/>
       
   353 		    </xsl:call-template>
       
   354 		</xsl:with-param>
       
   355 	</xsl:call-template>
       
   356 </xsl:template>
       
   357 
       
   358 <xsl:template name="reducedotdotpath"><xsl:param name="file"/>
       
   359 	<xsl:choose>
       
   360 		<xsl:when test="starts-with($file,'../')">
       
   361 			<xsl:text>../</xsl:text>
       
   362 			<xsl:call-template name="reducedotdotpath">
       
   363         		<xsl:with-param name="file" select="substring($file,4)"/>
       
   364 			</xsl:call-template>
       
   365 		</xsl:when>
       
   366 		<xsl:when test="contains($file,'/../')">							
       
   367 			<xsl:call-template name="reducepath">
       
   368         		<xsl:with-param name="file">
       
   369 			        <xsl:call-template name="lastbefore">
       
   370 			                <xsl:with-param name="string" select="substring-before($file,'/../')"/>
       
   371 			        </xsl:call-template>
       
   372 			        <xsl:text>/</xsl:text>
       
   373 					<xsl:value-of select="substring-after($file,'/../')"/>
       
   374 				</xsl:with-param>
       
   375 			</xsl:call-template>
       
   376 		</xsl:when>
       
   377 		<xsl:otherwise><xsl:value-of select="$file"/></xsl:otherwise>
       
   378 	</xsl:choose>
       
   379  </xsl:template>
       
   380 
       
   381 <xsl:template name="reducedotpath"><xsl:param name="file"/>
       
   382 	<xsl:choose>	
       
   383 		<xsl:when test="starts-with($file,'./')">
       
   384 			<xsl:call-template name="reducedotpath">
       
   385         		<xsl:with-param name="file" select="substring($file,3)"/>
       
   386 			</xsl:call-template>
       
   387 		</xsl:when>
       
   388 		<xsl:when test="contains($file,'/./')">
       
   389 			<xsl:call-template name="reducepath">
       
   390         		<xsl:with-param name="file">
       
   391 	                <xsl:value-of select="substring-before($file,'/./')"/>
       
   392 			        <xsl:text>/</xsl:text>
       
   393 					<xsl:value-of select="substring-after($file,'/./')"/>
       
   394 				</xsl:with-param>
       
   395 			</xsl:call-template>
       
   396 		</xsl:when>
       
   397 		<xsl:otherwise><xsl:value-of select="$file"/></xsl:otherwise>
       
   398 	</xsl:choose>
       
   399  </xsl:template>
       
   400 
       
   401 
       
   402 </xsl:stylesheet>