sysmodellibs/sysmodelgen/core/overlay-module.xsl
changeset 7 3c36c452f013
equal deleted inserted replaced
6:5b32dc297d05 7:3c36c452f013
       
     1 <?xml version="1.0"?>
       
     2  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       
     3  	<xsl:output method="xml" indent="yes"/>
       
     4 	<xsl:key name="named" match="*" use="@name"/>
       
     5  
       
     6 <xsl:template match="sysdef" mode="overlay-attributes" priority="1">  <xsl:param name="item"/>
       
     7 	<xsl:choose>
       
     8 		<xsl:when test="$item[self::SystemDefinition]">
       
     9 			<!--  add global attribtues to document node since we canalways easily find it -->
       
    10 			<xsl:apply-templates select="/model" mode="global-attributes"/>
       
    11 		</xsl:when>
       
    12 		<xsl:when test="$item/@id">
       
    13 			<!-- follwing vars to save trouble of recalulating each time -->
       
    14 			<xsl:variable name="id"><xsl:apply-templates select="$item/@id" mode="my-id"/></xsl:variable>			<!-- namespaceless ID of this here -->
       
    15 			<xsl:variable name="ns"><xsl:apply-templates select="$item/@id" mode="my-namespace"/></xsl:variable>	<!-- ID's namespace -->
       
    16 			<xsl:apply-templates select="info|../layout/*" mode="overlay-attributes">
       
    17 				<xsl:with-param name="id" select="$id"/>
       
    18 				<xsl:with-param name="ns" select="$ns"/>
       
    19 				<xsl:with-param name="item" select="$item"/>
       
    20 			</xsl:apply-templates>
       
    21 		</xsl:when>
       
    22 	</xsl:choose>
       
    23 </xsl:template>
       
    24 
       
    25 <xsl:template match="sysdef" mode="overlay-meta" priority="1"><xsl:param name="item"/>
       
    26 	<xsl:if test="$item[parent::SystemDefinition]">
       
    27 		<!-- the root model item gets the global meta stuff -->
       
    28 		<xsl:apply-templates select="/model" mode="global-meta"/>
       
    29 	</xsl:if>
       
    30 	<xsl:if test="$item/@id">
       
    31 		<!-- follwing vars to save trouble of recalulating each time -->
       
    32 		<xsl:variable name="id"><xsl:apply-templates select="$item/@id" mode="my-id"/></xsl:variable>			<!-- namespaceless ID of this here -->
       
    33 		<xsl:variable name="ns"><xsl:apply-templates select="$item/@id" mode="my-namespace"/></xsl:variable>	<!-- ID's namespace -->
       
    34 		<xsl:apply-templates select="info|../layout/info|document(../@deps,.)/*" mode="overlay-meta">
       
    35 			<xsl:with-param name="id" select="$id"/>
       
    36 			<xsl:with-param name="ns" select="$ns"/>
       
    37 			<xsl:with-param name="item" select="$item"/>
       
    38 		</xsl:apply-templates>
       
    39 	</xsl:if>
       
    40 </xsl:template>
       
    41 
       
    42 <xsl:template match="*" mode="global-meta"/>
       
    43 <xsl:template match="/model" mode="global-meta">
       
    44 	<xsl:apply-templates select="*[not(self::sysdef)]" mode="global-meta"/>
       
    45 </xsl:template>
       
    46 
       
    47 <xsl:template match="* | @*" mode="global-attributes"/>
       
    48 <xsl:template match="/model" mode="global-attributes">
       
    49 	<xsl:apply-templates select="*[not(self::sysdef)] | @*" mode="global-attributes"/>
       
    50 </xsl:template>
       
    51 
       
    52 <xsl:template mode="global-attributes" match="@version | /model/@*">
       
    53 	<xsl:copy-of select="."/> 
       
    54 </xsl:template>
       
    55 
       
    56 <xsl:template mode="global-attributes" match="/model/@link">
       
    57 	<xsl:attribute name="base"><xsl:value-of select="."/></xsl:attribute>
       
    58 </xsl:template>
       
    59 
       
    60 <xsl:template mode="global-attributes" match="/SystemModelDeps/@name"/>
       
    61 <xsl:template mode="global-attributes" match="/SystemModelDeps/@number">
       
    62 	<xsl:attribute name="build"><xsl:value-of select="."/></xsl:attribute>
       
    63 </xsl:template>
       
    64 <xsl:template mode="global-attributes" match="/model/@deps">
       
    65 	<xsl:apply-templates select="document(.,.)/*/@*" mode="global-attributes"/>
       
    66 </xsl:template>
       
    67 
       
    68 
       
    69 <xsl:template match="/info" mode="overlay-meta" priority="2">
       
    70 	<xsl:param name="ns"/><xsl:param name="id"/><xsl:param name="from"/>
       
    71 	<xsl:for-each select="item[@ref and (@ref=$id or substring-after(@ref,':')=$id)]"> <!-- potential match of IDs -->
       
    72 		<xsl:variable name="myns"><xsl:apply-templates select="@ref" mode="my-namespace"/></xsl:variable>
       
    73 		<xsl:if test="$myns = $ns"> <!-- match -->
       
    74 			<meta><xsl:apply-templates select="/info/@data-type" mode="overlay-meta"/>
       
    75 				<xsl:if test="not(/info/@data-type)"><xsl:copy-of select="$from/@rel | $from/@type[.!='extra']"/></xsl:if>
       
    76 				<xsl:copy-of select="*|comment()"/>
       
    77 			</meta>
       
    78 		</xsl:if>
       
    79 	</xsl:for-each>	
       
    80 </xsl:template>
       
    81 
       
    82 
       
    83 <xsl:template match="info[@type='abbrev']" mode="overlay-meta" priority="2"/> <!-- only sets attributes, never content -->
       
    84 <xsl:template match="info[@href]" mode="overlay-meta" priority="1"><xsl:param name="item"/><xsl:param name="ns"/><xsl:param name="id"/>
       
    85 	<xsl:apply-templates select="document(@href,.)/*" mode="overlay-meta">
       
    86 		<xsl:with-param name="id" select="$id"/>
       
    87 		<xsl:with-param name="ns" select="$ns"/>
       
    88 		<xsl:with-param name="item" select="$item"/>
       
    89 		<xsl:with-param name="from" select="current()"/>
       
    90 	</xsl:apply-templates>
       
    91 </xsl:template>
       
    92 
       
    93 <xsl:template match="info[@href]" mode="overlay-attributes" priority="1"><xsl:param name="item"/><xsl:param name="ns"/><xsl:param name="id"/>
       
    94 	<xsl:apply-templates select="document(@href,.)/*" mode="overlay-attributes">
       
    95 		<xsl:with-param name="id" select="$id"/>
       
    96 		<xsl:with-param name="ns" select="$ns"/>
       
    97 		<xsl:with-param name="item" select="$item"/>
       
    98 		<xsl:with-param name="from" select="current()"/>
       
    99 	</xsl:apply-templates>
       
   100 </xsl:template>
       
   101 
       
   102 
       
   103 <!-- generic info syntax for attaching data to IDs --> 
       
   104 <xsl:template match="/info" mode="overlay-meta" priority="2">
       
   105 	<xsl:param name="ns"/><xsl:param name="id"/><xsl:param name="from"/>
       
   106 	<xsl:for-each select="item[@ref and (@ref=$id or substring-after(@ref,':')=$id)]"> <!-- potential match of IDs -->
       
   107 		<xsl:variable name="myns"><xsl:apply-templates select="@ref" mode="my-namespace"/></xsl:variable>
       
   108 		<xsl:if test="$myns = $ns"> <!-- match -->
       
   109 			<xsl:copy-of select="meta[count(@rel|@type)=count(@*)]"/> <!--copy all standard meta sections verbatim and first -->
       
   110 			<xsl:if test="*[not(self::meta[count(@rel|@type)=count(@*)])]">
       
   111 				<!-- anything not in a meta will be put in a generic meta, comments inclued. --> 
       
   112 				<meta><xsl:apply-templates select="/info/@data-type" mode="overlay-meta"/>
       
   113 					<xsl:if test="not(/info/@data-type)"><xsl:copy-of select="$from/@rel | $from/@type[.!='extra']"/></xsl:if>
       
   114 					<xsl:copy-of select="*[not(self::meta[count(@rel|@type)=count(@*)])]|comment()"/>
       
   115 				</meta>
       
   116 			</xsl:if>
       
   117 		</xsl:if>
       
   118 	</xsl:for-each>	
       
   119 </xsl:template>
       
   120 
       
   121 <xsl:template match="/info" mode="overlay-attributes" priority="2">
       
   122 	<xsl:param name="ns"/><xsl:param name="id"/><xsl:param name="from"/>
       
   123 	<xsl:for-each select="item[@ref and (@ref=$id or substring-after(@ref,':')=$id)]"> <!-- potential match of IDs -->
       
   124 		<xsl:variable name="myns"><xsl:apply-templates select="@ref" mode="my-namespace"/></xsl:variable>
       
   125 		<xsl:if test="$myns = $ns"> <!-- match -->
       
   126 			<xsl:copy-of select="@*[name()!='id' and name()!='ref' ]"/> <!-- cannot override ID, don't include ref -->
       
   127 		</xsl:if>
       
   128 	</xsl:for-each>	
       
   129 </xsl:template>
       
   130 
       
   131 <xsl:template match="/info/@data-type" mode="overlay-meta">
       
   132 	<xsl:attribute name="rel"><xsl:value-of select="."/></xsl:attribute>
       
   133 </xsl:template>
       
   134 
       
   135 
       
   136 
       
   137 <!-- S12 is well deprecated, but should still support for now. Note that use of osd attribute is slightly different from before --> 
       
   138 <xsl:template match="/Schedule12" mode="overlay-meta">
       
   139 	<xsl:param name="ns"/><xsl:param name="id"/>
       
   140 	<xsl:for-each select="//system_model[@entry=$id or substring-after(@entry,':')=$id]"> <!-- potential match of IDs -->	
       
   141 		<xsl:variable name="myns"><xsl:apply-templates select="@entry" mode="my-namespace"/></xsl:variable>
       
   142 		<xsl:if test="$myns = $ns"> <!-- match -->
       
   143 			<meta rel="Schedule12">
       
   144 				<s12>
       
   145 					<xsl:attribute name="ver"><xsl:value-of select="/Schedule12/@OS_version"/></xsl:attribute>
       
   146 					<xsl:attribute name="osd"><xsl:value-of select="name(..)"/></xsl:attribute>
       
   147 					<xsl:copy-of select="@name"/>
       
   148 				</s12>
       
   149 			</meta>
       
   150 		</xsl:if>
       
   151 	</xsl:for-each>
       
   152 </xsl:template>
       
   153 
       
   154 
       
   155 <xsl:template match="/SystemModelDeps" mode="overlay-meta">
       
   156 	<xsl:param name="item"/><xsl:param name="ns"/><xsl:param name="id"/>
       
   157 	<xsl:if test="$item[self::component]"> <!-- only valid for components for now-->
       
   158 		<xsl:variable name="matches" select="//Executable[@component=$id or substring-after(@component,':')=$id]"/>
       
   159 		<xsl:if test="$matches">
       
   160 			<meta rel="Dependencies" type="depmodel">	 <!-- there might be none, but tools should be able to live with an empty meta -->
       
   161 				<xsl:for-each select="$matches"> <!-- potential match of IDs -->
       
   162 					<xsl:variable name="myns"><xsl:apply-templates select="@component" mode="my-namespace"/></xsl:variable>
       
   163 					<xsl:if test="$myns = $ns"> <!-- actual match -->
       
   164 						<Bin><xsl:copy-of select="@*[name()!='component']|*"/></Bin>
       
   165 					</xsl:if>
       
   166 				</xsl:for-each>
       
   167 			</meta>
       
   168 		</xsl:if>
       
   169 	</xsl:if>
       
   170 	<xsl:variable name="matches" select="
       
   171 		Layers[$item[self::layer]]/Layer[@name=$item/@name] |
       
   172 		Blocks[$item[self::package]]/Block[@name=$item/@name] |
       
   173 		SubBlocks[$item[self::package]]/SubBlock[@name=$item/@name] | 
       
   174 		Collections[$item[self::collection]]/Collection[@name=$item/@name] |
       
   175 		Components[$item[self::component]]/Component[@name=$item/@name]"/>
       
   176 	<xsl:if test="$matches">
       
   177 		<meta rel="Dependencies" type="generic">
       
   178 			<xsl:for-each select="$matches/dep">
       
   179 				<xsl:variable name="dep" select="."/>
       
   180 				<xsl:for-each select="$item">
       
   181 					<xsl:for-each select="key('named',$dep/@name)[1]">
       
   182 						<dep ref="{@id}">
       
   183 							<xsl:copy-of select="$dep/@type"/>
       
   184 						</dep>
       
   185 					</xsl:for-each>
       
   186 				</xsl:for-each>
       
   187 			</xsl:for-each>
       
   188 		</meta>
       
   189 	</xsl:if>
       
   190 </xsl:template>
       
   191 
       
   192 
       
   193 
       
   194 <xsl:template match="/display-names" mode="overlay-attributes">
       
   195 	<xsl:param name="item"/><xsl:param name="ns"/><xsl:param name="id"/>
       
   196 	<xsl:variable name="match"> <!-- get the values of the refs that match $item --> 
       
   197 		<xsl:for-each select="abbrev[@ref and (@ref=$id or substring-after(@ref,':')=$id)]"> <!-- potential match of IDs -->
       
   198 			<xsl:variable name="myns"><xsl:apply-templates select="@ref" mode="my-namespace"/></xsl:variable>
       
   199 			<xsl:if test="$myns = $ns"> <!-- match -->
       
   200 				<xsl:value-of select="concat(@ref,' ')"/>
       
   201 			</xsl:if>
       
   202 		</xsl:for-each>
       
   203 	</xsl:variable>
       
   204 	
       
   205 	<xsl:choose>
       
   206 		<xsl:when test="$match!=''">
       
   207 			<xsl:for-each select="abbrev[@ref=substring-before($match,' ')]"> <!-- match the first in the doc -->
       
   208 				<xsl:copy-of select="@abbrev|@font|../@font"/>
       
   209 			</xsl:for-each>
       
   210 		</xsl:when>
       
   211 		<xsl:otherwise>
       
   212 			<xsl:for-each select="key('named',$item/@name)[1]"> <!-- match the first in the doc -->
       
   213 				<xsl:copy-of select="@abbrev|@font|../@font"/>
       
   214 			</xsl:for-each>
       
   215 		</xsl:otherwise>
       
   216 	</xsl:choose>
       
   217 </xsl:template>
       
   218 
       
   219   
       
   220  
       
   221 <!-- unsupported proprietary format. Uses name and tag name  to match
       
   222 It's only real use is no longer needed now, but do we need to handle all the old files?
       
   223 -->
       
   224 <xsl:template match="/attributes" mode="overlay-attributes"><xsl:param name="item"/>
       
   225 	<xsl:copy-of select="key('named',$item/ancestor::layer/@name)[self::layer and @inherit='yes']/attrs/@*"/>
       
   226 	<xsl:copy-of select="key('named',$item/ancestor::package/@name)[self::block or self::package and @inherit='yes']/attrs/@*"/>
       
   227 	<xsl:copy-of select="key('named',$item/ancestor::collection/@name)[self::coll and @inherit='yes']/attrs/@*"/>
       
   228 	<xsl:copy-of select="key('named',$item/@name)[starts-with(local-name($item),local-name()) and (not(@location) or (@location=$item/../@name))]/attrs/@*"/>
       
   229 </xsl:template>
       
   230 
       
   231 
       
   232 <!-- consider supporting a techstream-like org document where it copies all data under the item and all data above it -->
       
   233 
       
   234   <!-- no symsym support, but consider some kind of SQL lookup-->
       
   235 
       
   236 <!-- special code for delaing with links follows -->
       
   237 
       
   238 <xsl:template match="*" mode="link" priority="1"><xsl:param name="data"/>
       
   239 	<xsl:apply-templates select="$data" mode="link">
       
   240 		<xsl:with-param name="item" select="current()"/>
       
   241 	</xsl:apply-templates>
       
   242 </xsl:template>
       
   243 
       
   244 <xsl:template match="sysdef" mode="link" priority="1"><xsl:param name="item"/>
       
   245 	<xsl:variable name="newhref">
       
   246 		<xsl:if test="$item/@id">
       
   247 			<!-- follwing vars to save trouble of recalulating each time -->
       
   248 			<xsl:variable name="id"><xsl:apply-templates select="$item/@id" mode="my-id"/></xsl:variable>			<!-- namespaceless ID of this here -->
       
   249 			<xsl:variable name="ns"><xsl:apply-templates select="$item/@id" mode="my-namespace"/></xsl:variable>	<!-- ID's namespace -->
       
   250 			<xsl:for-each select="document(info/@href,.)/info/item[@href and (@ref=$id or substring-after(@ref,':')=$id)]">
       
   251 				<xsl:variable name="myns"><xsl:apply-templates select="@ref" mode="my-namespace"/></xsl:variable>
       
   252 				<xsl:if test="$myns = $ns"> <!-- match -->
       
   253 					<xsl:value-of select="concat(@href,'&#xa;')"/>
       
   254 				</xsl:if> 
       
   255 			</xsl:for-each>
       
   256 		</xsl:if>
       
   257 	</xsl:variable>
       
   258 	
       
   259 	<xsl:value-of select="substring-before($newhref,'&#xa;')"/> <!-- use first defined -->
       
   260 	<xsl:if test="$newhref='' "><xsl:value-of select="$item/@href"/></xsl:if>
       
   261 </xsl:template>
       
   262 
       
   263 </xsl:stylesheet>