<xsl:stylesheet xmlns:src="http://www.sdml.info/srcML/src" xmlns:cpp="http://www.sdml.info/srcML/cpp" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xw="http://www.pnp-software.com/XWeaver" xmlns:xd="http://www.pnp-software.com/XSLTdoc" xmlns:util="http://www.pnp-software.com/util" xmlns:ax="http://control.ee.ethz.ch/XWeaver/AspectX" version="2.0">
  
  
     <xd:doc type="stylesheet">
    
<xd:short>Weaving Rule that adds an initializer to a constructor</xd:short>
    
<xd:detail>
      
<ul> 
        
<li>srcML element:     <code>src:constructor</code></li>
        
<li>advice type:       <code>add</code></li>
        
<li>codeModifier type: <code>initializer</code></li>
      
</ul>
      
<h2>Example</h2>
      
<h3>Base Code</h3>
      
<div class="source">
        
<pre>Foo:Foo() { }</pre>
      
</div>
      
<h3>Code Modifier</h3>
      
<xd:xml>
<codeModifier type="initializer">
  
<text>_x(10)</text>
</codeModifier>
      
</xd:xml>
      
<h3>Modified Code</h3>
      
<div class="source">
        
<pre>Foo:Foo() <span class="highlightedCode">: _x(10)</span> { }</pre>
      
</div>
      
<xd:weaverRuleLinks pointcutType="src:constructor" adviceType="add" codeModifierType="initializer" />
    
</xd:detail>
    
<xd:cvsId>$Id$</xd:cvsId>
    
<xd:author>ibirrer</xd:author>
    
<xd:copyright>2004, P&amp;P Software GmbH</xd:copyright>
  
</xd:doc>

  
  
<!-- Preserve Whitespaces -->
  
<xsl:preserve-space elements="*" />
  
  
     <xd:doc>
    
<xd:detail>
      This template does the following:
      
<ul>
        
<li>Checks if an initializer list (src:member_list) exist for the current constructor. If not, an initializer list is added including the advice </li>
        
<li>If yes the advice is copied to the initializer list</li>
      
</ul>
    
</xd:detail>
  
</xd:doc>

  
<xsl:template match="src:constructor[ax:advice[@type='add' and position() = 1]/ax:codeModifier[@type='initializer']]" mode="weaving">
    
<xsl:variable name="advice" select="ax:advice[1]" />
    
<!-- copy all advices except the ones to be moved -->
    
<xsl:copy-of select="ax:advice[not(@type='add' and ax:codeModifier/@type='initializer')]" />
    
<!-- Check for member_list -->
    
<xsl:choose>
      
<xsl:when test="src:member_list">
        
<xsl:copy>
          
<xsl:copy-of select="@*" />
          
<!-- Copy the advice to the member list -->
          
<xsl:apply-templates select="child::node()[not(self::ax:advice)]" mode="constructor_move_initializerList">
            
<xsl:with-param name="advices" select="ax:advice[@type='add' and ax:codeModifier/@type='initializer']" />
          
</xsl:apply-templates>
        
</xsl:copy>
      
</xsl:when>
      
<xsl:otherwise>
        
<xsl:copy>
          
<xsl:copy-of select="@*" />
          
<xsl:copy-of select="src:block/preceding-sibling::node()[not(self::ax:advice)]" />
          
<src:member_list>
            
<xsl:text></xsl:text>
            
<xsl:copy-of select="ax:advice[@type='add' and ax:codeModifier/@type='initializer']" />
          
</src:member_list>
          
<xsl:copy-of select="src:block" />
        
</xsl:copy>
      
</xsl:otherwise>
    
</xsl:choose>
  
</xsl:template>
  
  
     <xd:doc>
    Move advices to the member_list element element.
  
</xd:doc>

  
<xsl:template match="src:member_list" mode="constructor_move_initializerList">
    
<xsl:param name="advices" />
    
<xsl:copy>
      
<xsl:copy-of select="@*" />
      
<xsl:copy-of select="$advices" />
      
<!-- 
        Copy everything else. <xsl:apply-templates mode="weaving"/> would not work properly
        here, because it would not copy any advices present for the member_list element ot their subelements 
      
-->
      
<xsl:copy-of select="child::node()" />
    
</xsl:copy>
  
</xsl:template>
  
  
  
     <xd:doc>
    Default copy template for constructor_move_initializerList.
  
</xd:doc>

  
<xsl:template match="*" mode="constructor_move_initializerList">
    
<xsl:param name="advices" />
    
<xsl:copy>
      
<xsl:copy-of select="@*" />
      
<xsl:apply-templates mode="constructor_move_initializerList">
        
<xsl:with-param name="advices" select="$advices" />
      
</xsl:apply-templates>
    
</xsl:copy>
  
</xsl:template>
</xsl:stylesheet>






































v