metatools/sysdeftools/sysdefdowngrade.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 	Downgrade a 3.x system definition into the 2.0.1 syntax
       
    15 -->
       
    16 	<xsl:output method="xml" indent="yes"/>
       
    17 
       
    18 
       
    19 <!--Description:Downgrades a 3.x system definition into the 2.0.1 syntax
       
    20  -->
       
    21 
       
    22 <!--Input:<sysdef> - (required) The system definition XML file to process, in the 3.0 format, and can be a pkgdef or stand-alone sysdef.-->
       
    23 <!--Output:<sysdef> - (optional) The system definition XML file to save the output as. If not present it will write to stdout.-->
       
    24 
       
    25 	<xsl:param name="Path">os/deviceplatformrelease/foundation_system/system_model</xsl:param>
       
    26 	<!-- <oldpath> - The directory containing the root system definition XML file in the 2.0 sysdef style. This must not end in /
       
    27 		This is used to compute the absolute paths the 2.0 syntax needs. -->
       
    28 	<xsl:param name="Root"/> <!--<list> - (optional) A space separated list of root variables in the form "VAR1=value1 VAR=value2" --> 
       
    29 	<xsl:param name="Strict" select="0"/> <!--1 - (optional) If present, namespaced extensions are stripped out --> 
       
    30 	<xsl:variable name="root" select="concat(' ',$Root,' ')"/> <!-- sort of hack to allow absolute paths in downgraded output -->
       
    31 	<xsl:variable name="srcroot" select="substring-before(substring-after($root,' SRCROOT='),' ')"/> <!-- the default path prefix -->
       
    32 
       
    33 <xsl:template match="/*">
       
    34 	<xsl:message terminate="yes">ERROR: Cannot process this document</xsl:message>
       
    35 </xsl:template>
       
    36 
       
    37 <!-- can only handle 3.0.x to 2.0.1 transforms
       
    38 	Assumes only packages are using href
       
    39  -->
       
    40 <xsl:template match="/SystemDefinition[starts-with(@schema,'3.0.')]"> 
       
    41 	<!-- process root system definition or package definition-->
       
    42 	<xsl:call-template name="DTD"/> <!-- insert 2.0.01 DTD -->
       
    43   <SystemDefinition name="{*/@name}" schema="2.0.1">
       
    44   	<xsl:apply-templates select="*|comment()"/>
       
    45   </SystemDefinition>
       
    46 </xsl:template>
       
    47 
       
    48 <xsl:template match="/SystemDefinition[starts-with(@schema,'3.0.') and systemModel]"> 
       
    49 	<xsl:call-template name="DTD"/> <!-- insert 2.0.01 DTD -->
       
    50   <SystemDefinition name="{systemModel/@name}" schema="2.0.1">
       
    51   	<xsl:apply-templates select="*|comment()"/>
       
    52   </SystemDefinition>
       
    53 </xsl:template>
       
    54 
       
    55 <xsl:template match="/SystemDefinition/package[@href]" priority="2">
       
    56 	<xsl:message terminate="yes">ERROR: Package definition (<xsl:value-of select="@id"/>) cannot link another package</xsl:message>
       
    57 </xsl:template>
       
    58 
       
    59 <xsl:template match="/SystemDefinition/package" priority="1">
       
    60 	<!-- process package definition file--> 
       
    61   <systemModel>
       
    62   	<layer name="anonymous"> <!-- fake layer -->
       
    63 		<block>
       
    64 			<xsl:apply-templates mode="copy" select="@id|@name|@span|@level|@levels"/><!--  valid attribuites for 2.0 -->
       
    65 		  	<xsl:apply-templates select="*|comment()">
       
    66 		  		<xsl:with-param name="path" select="concat($Path,'/')"/> <!-- need to keep tack of where the current document is -->
       
    67 		  	</xsl:apply-templates>			
       
    68 		</block>  
       
    69    	</layer>
       
    70   </systemModel>
       
    71 </xsl:template>
       
    72 
       
    73 
       
    74 
       
    75 <xsl:template match="/"><xsl:apply-templates select="*"/></xsl:template>
       
    76 <xsl:template match="@*|comment()"><xsl:copy-of select="."/></xsl:template> 
       
    77 	<!-- comments are copied verbatim. Attribtues are copied by default -->
       
    78 
       
    79 <xsl:template match="systemModel">
       
    80 	<systemModel>
       
    81   	<xsl:apply-templates select="*|comment()"> <!-- no attributes -->
       
    82   		<xsl:with-param name="path" select="$Path"/> <!-- need to keep tack of where the current document is -->
       
    83   	</xsl:apply-templates>
       
    84 	</systemModel>
       
    85 </xsl:template>
       
    86 
       
    87 <xsl:template mode="copy" match="@*">
       
    88 	<xsl:copy-of select="."/>
       
    89 </xsl:template>
       
    90 
       
    91 <xsl:template mode="copy" match="@id"> <!-- id in 3.0 is name in 2.0 -->
       
    92 	<xsl:attribute name="name"><xsl:value-of select="."/></xsl:attribute>
       
    93 </xsl:template>
       
    94 
       
    95 <xsl:template mode="copy" match="@name">  <!-- name in 3.0 is long-name in 2.0.1 -->
       
    96 	<xsl:if test=".!=../@id"> <!-- don't bother if it will be the same as name -->
       
    97 		<xsl:attribute name="long-name"><xsl:value-of select="."/></xsl:attribute>
       
    98 	</xsl:if>
       
    99 </xsl:template>
       
   100 	
       
   101 <xsl:template match="layer"><xsl:param name="path"/>
       
   102 	<layer>
       
   103 		<xsl:apply-templates mode="copy" select="@id|@name|@span|@levels"/> <!--  valid attribuites for 2.0 -->
       
   104 		<xsl:apply-templates select="*|comment()"> 
       
   105 			<xsl:with-param name="path" select="$path"/> 
       
   106 		</xsl:apply-templates>
       
   107 	</layer>
       
   108 </xsl:template>
       
   109 
       
   110 <xsl:template match="layer/package"><!-- translates to block -->
       
   111 	<xsl:param name="path"/>
       
   112 	<block>
       
   113 		<xsl:apply-templates mode="copy" select="@id|@name|@span|@level|@levels"/><!--  valid attribuites for 2.0 -->
       
   114 		<xsl:choose>
       
   115 			<xsl:when test="@href">
       
   116 				<xsl:variable name="this" select="."/>
       
   117 				<xsl:variable name="prefixmap" select="ancestor::SystemDefinition/*/meta[@rel='link-mapping']/map-prefix[starts-with(current()/@href,@link)]"/>
       
   118 				<xsl:for-each select="document(@href,.)/SystemDefinition/*">
       
   119 					<xsl:variable name="my-id"><xsl:apply-templates mode="normalize-id" select="@id"/></xsl:variable>
       
   120 					<xsl:variable name="other-id"><xsl:apply-templates mode="normalize-id" select="$this/@id"/></xsl:variable>
       
   121 					<xsl:if test="$my-id != $other-id">
       
   122 						<xsl:message terminate="yes">ERROR: Package IDs do not match: <xsl:value-of select="$my-id"/> vs <xsl:value-of select="$other-id"/></xsl:message>
       
   123 					</xsl:if>
       
   124 					<xsl:if test="@name and @name!=@id and not($this/@name and $this/@name=$this/@id)">
       
   125 						<!-- set long-name only if name is different from the id and not set in child doc -->
       
   126 						<xsl:attribute name="long-name"><xsl:value-of select="@name"/></xsl:attribute>
       
   127 					</xsl:if>						
       
   128 					<xsl:for-each select="@span|@levels|@level">
       
   129 						<!-- copy only if not set in child doc -->
       
   130 						<xsl:if test="not(this/@*[name()=name(current())])">
       
   131 							<xsl:copy-of select="."/>
       
   132 						</xsl:if>
       
   133 					</xsl:for-each>
       
   134 					<xsl:apply-templates select="*|comment()">
       
   135 	  					<xsl:with-param name="path">
       
   136 							<xsl:choose>
       
   137 								<xsl:when test="$prefixmap">
       
   138 	  								<xsl:call-template name="normpath">
       
   139 	  									<xsl:with-param name="path">
       
   140 										<xsl:apply-templates select="$prefixmap/@to"/>
       
   141 										<xsl:value-of select="substring-after($this/@href,$prefixmap/@link)"/>
       
   142 									 </xsl:with-param>
       
   143 	  								</xsl:call-template>
       
   144 								</xsl:when>
       
   145 								<xsl:when test="starts-with($this/@href,'/')">  <!-- absolute path -->
       
   146 	  								<xsl:call-template name="normpath">
       
   147 	  									<xsl:with-param name="path" select="$this/@href"/>
       
   148 	  								</xsl:call-template>
       
   149 								</xsl:when>
       
   150 								<xsl:when test="contains($this/@href,'://')">  <!-- generic URI -->
       
   151 	  								<xsl:call-template name="normpath">
       
   152 	  									<xsl:with-param name="path" select="substring-after($this/@href,'://')"/>
       
   153 	  								</xsl:call-template>
       
   154 								</xsl:when>
       
   155 								<xsl:otherwise>
       
   156 	  								<xsl:call-template name="normpath">
       
   157 	  									<xsl:with-param name="path" select="concat($path,'/',$this/@href)"/>
       
   158 	  								</xsl:call-template>
       
   159 								</xsl:otherwise>
       
   160 							</xsl:choose>
       
   161 	  					</xsl:with-param> 
       
   162 	  				</xsl:apply-templates>
       
   163 				</xsl:for-each>
       
   164 			</xsl:when>
       
   165 			<xsl:otherwise>
       
   166 				<xsl:apply-templates select="*|comment()">
       
   167   					<xsl:with-param name="path" select="$path"/> 
       
   168   				</xsl:apply-templates>
       
   169 			</xsl:otherwise>
       
   170 		</xsl:choose>
       
   171 	</block>
       
   172 </xsl:template>
       
   173 
       
   174 <xsl:template match="package/package">	<!-- translates to subblock --><xsl:param name="path"/>
       
   175 	<subblock>
       
   176 		<xsl:apply-templates mode="copy" select="@id|@name"/>
       
   177 		<xsl:apply-templates select="*|comment()">
       
   178 			<xsl:with-param name="path" select="$path"/> 
       
   179 		</xsl:apply-templates>
       
   180 	</subblock>
       
   181 </xsl:template>
       
   182 
       
   183 <xsl:template match="package/package/pacakge"> <!-- cannot nest this deep --><xsl:param name="path"/>
       
   184 	<xsl:message>WARNING: Excessive nesting of packages: Ignoring <xsl:value-of select="@id"/></xsl:message>
       
   185 	<xsl:apply-templates select="*|comment()">
       
   186 		<xsl:with-param name="path" select="$path"/> 
       
   187 	</xsl:apply-templates>
       
   188 </xsl:template>
       
   189 
       
   190 
       
   191 <xsl:template match="collection"><xsl:param name="path"/>
       
   192 	<collection>
       
   193 		<xsl:apply-templates mode="copy" select="@id|@name|@level"/>
       
   194 		<xsl:apply-templates select="*|comment()">
       
   195 			<xsl:with-param name="path" select="$path"/> 
       
   196 		</xsl:apply-templates>
       
   197 	</collection>
       
   198 </xsl:template>
       
   199 
       
   200 
       
   201 <xsl:template match="component"><xsl:param name="path"/>
       
   202 	<component>
       
   203 		<xsl:apply-templates mode="copy" select="@id|@name|@deprecated|@introduced|@filter|@purpose"/>
       
   204 		<xsl:if test="contains(concat(' ',@class,' '),' plugin ')">
       
   205 			<xsl:attribute name="plugin">Y</xsl:attribute>
       
   206 		</xsl:if>
       
   207 		<xsl:call-template name="class">
       
   208 			<xsl:with-param name="remove">plugin</xsl:with-param>
       
   209 			<xsl:with-param name="add">
       
   210 				<xsl:if test="not(*) and comment()">placeholder</xsl:if>
       
   211 				<xsl:if test="@target='desktop'"> PC</xsl:if>
       
   212 			</xsl:with-param>
       
   213 		</xsl:call-template>
       
   214 	  	<xsl:apply-templates select="*|comment()">
       
   215 			<xsl:with-param name="path" select="$path"/> 
       
   216 		</xsl:apply-templates>
       
   217 	</component>
       
   218 </xsl:template>
       
   219 
       
   220 <xsl:template match="unit[@base and not(@mrp or @bldFile)]"/>
       
   221 
       
   222 <xsl:template match="unit"><xsl:param name="path"/>
       
   223 	<unit>
       
   224 		<xsl:apply-templates select="@mrp|@bldFile|@late">
       
   225 			<xsl:with-param name="path" select="$path"/> 
       
   226 		</xsl:apply-templates>
       
   227 		<xsl:copy-of select="@filter|@root[not(contains($root,concat(' ',.,'=')))]|@version|@prebuilt|@priority"/>
       
   228 		<xsl:for-each select="@*[contains(name(),':') and not($Strict)]">
       
   229 			<xsl:attribute name="{local-name()}">
       
   230 				<xsl:value-of select="."/>
       
   231 			</xsl:attribute>
       
   232 		</xsl:for-each>
       
   233 	</unit>
       
   234 </xsl:template>
       
   235 
       
   236 <xsl:template match="unit/@late"> <!-- 2.0 uses Y/N, 3.0 uses yes/no -->
       
   237 	<xsl:attribute name="{name()}">
       
   238 		<xsl:choose>
       
   239 			<xsl:when test=".='yes'">Y</xsl:when>
       
   240 			<xsl:when test=".='no'">N</xsl:when>
       
   241 		</xsl:choose>
       
   242 	</xsl:attribute>
       
   243 </xsl:template>
       
   244 
       
   245 <xsl:template match="@mrp|@bldFile"><xsl:param name="path"/>
       
   246 	<xsl:attribute name="{name()}">
       
   247 		<xsl:choose>
       
   248 			<xsl:when test="../@root">
       
   249 				<xsl:variable name="pre" select="substring-before(substring-after($root,concat(' ',../@root,'=')),' ')"/>
       
   250 				<xsl:value-of select="$pre"/>
       
   251 				<xsl:if test="$pre!='' and $pre!='/'">/</xsl:if>
       
   252 			</xsl:when>
       
   253 			<xsl:when test="$srcroot='/'"> <!-- treat all paths as absolute -->
       
   254 				<xsl:value-of select="$srcroot"/>
       
   255 			</xsl:when>
       
   256 			<xsl:when test="$srcroot!=''">
       
   257 				<xsl:value-of select="concat($srcroot,'/')"/>
       
   258 			</xsl:when>
       
   259 		</xsl:choose>
       
   260 		<xsl:choose>
       
   261 		<xsl:when test="starts-with(.,'/')"> <!-- keep absolute paths verbatim (barring the leading / ) -->
       
   262 			<xsl:value-of select="substring-after(.,'/')"/>
       
   263 		</xsl:when>
       
   264 		<xsl:otherwise>	
       
   265 			<xsl:call-template name="normpath">
       
   266   				<xsl:with-param name="path">
       
   267   					<xsl:call-template name="before">
       
   268 						<xsl:with-param name="text" select="$path"/>
       
   269 					</xsl:call-template>
       
   270 					<xsl:value-of select="."/>
       
   271   				</xsl:with-param>
       
   272   			</xsl:call-template>
       
   273 		</xsl:otherwise>
       
   274 	</xsl:choose>
       
   275 	</xsl:attribute>
       
   276 </xsl:template>
       
   277 
       
   278 <xsl:template match="meta"/> <!-- strip all meta tags -->
       
   279 
       
   280 <xsl:template match="meta[info/@contract]"> <!-- except contract -->
       
   281 	<xsl:copy-of select="info/@contract"/>
       
   282 </xsl:template>
       
   283 
       
   284 
       
   285 <xsl:template match="meta[@rel='link-mapping']/map-prefix/@to">
       
   286 	<xsl:choose>
       
   287 		<xsl:when test="starts-with(.,'/')"><xsl:value-of select="substring(.,2)"/></xsl:when> <!-- absolute paths in 3.0 are relative in 2.0 -->
       
   288 		<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
       
   289 	</xsl:choose>
       
   290 </xsl:template>
       
   291 
       
   292 
       
   293 <xsl:template match="@id" mode="normalize-id">
       
   294 	<xsl:choose>
       
   295 		<xsl:when test="contains(@id,':')"><xsl:value-of select="substring-after(@id,':')"/></xsl:when>
       
   296 		<xsl:otherwise><xsl:value-of select="@id"/></xsl:otherwise>
       
   297 	</xsl:choose>
       
   298 </xsl:template>
       
   299 
       
   300 <xsl:template name="class"><xsl:param name="remove"/><xsl:param name="add"/>
       
   301 <!--  returns the value of the class attribute with the space-separated list of names in $remove taken out and those in $add added on (does not check for duplicates) -->
       
   302 	<xsl:param name="class" select="normalize-space(@class)"/>
       
   303 	<xsl:variable name="r">
       
   304 		<xsl:text> </xsl:text>
       
   305 		<xsl:choose>
       
   306 			<xsl:when test="contains($remove,' ')"><xsl:value-of select="substring-before($remove,' ')"/></xsl:when>
       
   307 			<xsl:otherwise><xsl:value-of select="$remove"/></xsl:otherwise>
       
   308 		</xsl:choose>
       
   309 		<xsl:text> </xsl:text>
       
   310 	</xsl:variable>
       
   311 	<xsl:variable name="c">
       
   312 		<xsl:choose>
       
   313 			<xsl:when test="contains(concat(' ',$class,' '),$r)">
       
   314 				<xsl:value-of select="substring-before(concat(' ',$class,' '),$r)"/>
       
   315 				<xsl:text> </xsl:text>
       
   316 				<xsl:value-of select="substring-after(concat(' ',$class,' '),$r)"/>
       
   317 			</xsl:when>
       
   318 			<xsl:otherwise><xsl:value-of select="$class"/></xsl:otherwise>
       
   319 		</xsl:choose>
       
   320 		<xsl:if test="normalize-space($add)!=''"><xsl:value-of select="concat(' ',normalize-space($add))"/></xsl:if>
       
   321 	</xsl:variable>
       
   322 	<xsl:choose>
       
   323 		<xsl:when test="contains($remove,' ')">
       
   324 			<xsl:call-template name="class">
       
   325 				<xsl:with-param name="remove" select="substring-after($remove,' ')"/>
       
   326 				<xsl:with-param name="class" select="$c"/>
       
   327 			</xsl:call-template>
       
   328 		</xsl:when>
       
   329 		<xsl:when test="normalize-space($c)!=''">
       
   330 			<xsl:attribute name="class">
       
   331 				<xsl:value-of select="normalize-space($c)"/>
       
   332 			</xsl:attribute>
       
   333 		</xsl:when>
       
   334 	</xsl:choose>
       
   335 </xsl:template>
       
   336 
       
   337 <xsl:template name="normpath"><xsl:param name="path"/>
       
   338 <!-- normalize out any ".." in the path in $path  -->
       
   339 <xsl:choose>
       
   340 	<xsl:when test="contains($path,'/../')">
       
   341 	<xsl:call-template name="normpath">
       
   342 		<xsl:with-param name="path">
       
   343 		<xsl:call-template name="before">
       
   344 			<xsl:with-param name="text" select="substring-before($path,'/../')"/>
       
   345 		</xsl:call-template>
       
   346 		<xsl:value-of select="substring-after($path,'/../')"/>
       
   347 		</xsl:with-param>
       
   348 		</xsl:call-template>
       
   349 	</xsl:when>
       
   350 	<xsl:otherwise><xsl:value-of select="$path"/></xsl:otherwise>
       
   351 </xsl:choose>
       
   352 </xsl:template>
       
   353 
       
   354 <!-- return all text before the last / -->
       
   355 <xsl:template name="before"><xsl:param name="text"/>
       
   356 <xsl:if test="contains($text,'/')">
       
   357 	<xsl:value-of select="substring-before($text,'/')"/>/<xsl:call-template name="before"><xsl:with-param name="text" select="substring-after($text,'/')"/></xsl:call-template>
       
   358 	</xsl:if>
       
   359 </xsl:template>
       
   360 
       
   361 <xsl:template name="DTD">
       
   362 <xsl:text disable-output-escaping="yes"><![CDATA[<!DOCTYPE SystemDefinition [
       
   363 <!ELEMENT SystemDefinition ( systemModel )>
       
   364 <!ATTLIST SystemDefinition
       
   365   name CDATA #REQUIRED
       
   366   schema CDATA #REQUIRED
       
   367 >
       
   368 <!-- all paths are relative to the environment variable specified by the root attribute, or SOURCEROOT if not.  -->
       
   369 
       
   370 <!-- System Model Section of DTD -->
       
   371 <!ELEMENT systemModel (layer+)>
       
   372 
       
   373 <!ELEMENT layer (block* | collection*)*>
       
   374 <!-- Kernel Services, Base Services, OS Services, Etc -->
       
   375 <!ATTLIST layer
       
   376   name CDATA #REQUIRED
       
   377   long-name CDATA #IMPLIED
       
   378   levels NMTOKENS #IMPLIED
       
   379   span CDATA #IMPLIED
       
   380 >
       
   381 
       
   382 <!ELEMENT block (subblock* | collection*)*>
       
   383  <!-- Generic OS services, Comms Services, etc -->
       
   384 <!ATTLIST block
       
   385   levels NMTOKENS #IMPLIED
       
   386   span CDATA #IMPLIED
       
   387   level NMTOKEN #IMPLIED
       
   388   name CDATA #REQUIRED
       
   389   long-name CDATA #IMPLIED
       
   390 >
       
   391 
       
   392 <!ELEMENT subblock (collection)*>
       
   393 <!-- Cellular Baseband Services, Networking Services, etc -->
       
   394 <!ATTLIST subblock
       
   395   name CDATA #REQUIRED
       
   396   long-name CDATA #IMPLIED
       
   397 >
       
   398 
       
   399 <!ELEMENT collection (component)*>
       
   400 <!-- Screen Driver, Content Handling, etc -->
       
   401 <!ATTLIST collection
       
   402   name CDATA #REQUIRED
       
   403   long-name CDATA #IMPLIED
       
   404   level NMTOKEN #IMPLIED
       
   405 >
       
   406 
       
   407 <!ELEMENT component (unit)*>
       
   408 <!-- contains units or is a  package or prebuilt -->
       
   409 <!ATTLIST component
       
   410   name CDATA #REQUIRED
       
   411   long-name CDATA #IMPLIED
       
   412   deprecated CDATA #IMPLIED
       
   413   introduced CDATA #IMPLIED
       
   414   contract CDATA #IMPLIED
       
   415   plugin (Y|N) "N"
       
   416   filter CDATA #IMPLIED
       
   417   class NMTOKENS #IMPLIED
       
   418   supports CDATA #IMPLIED
       
   419   purpose ( optional | mandatory | development ) "optional"
       
   420 >
       
   421 
       
   422 <!ELEMENT unit EMPTY >
       
   423 <!-- must be buildable (bld.inf) -->
       
   424 <!-- bldFile  may someday be removed in favour of mrp -->
       
   425 <!ATTLIST unit
       
   426   mrp CDATA #IMPLIED
       
   427   filter CDATA #IMPLIED
       
   428   bldFile CDATA #IMPLIED
       
   429   root CDATA #IMPLIED
       
   430   version NMTOKEN #IMPLIED
       
   431   prebuilt NMTOKEN #IMPLIED
       
   432   late (Y|N) #IMPLIED
       
   433   priority CDATA #IMPLIED
       
   434 >
       
   435 ]>
       
   436 ]]></xsl:text>
       
   437 </xsl:template>
       
   438 </xsl:stylesheet>