<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 weaves a code fragment before a return statement. </xd:short>
    
<xd:detail>
      
<ul> 
        
<li>srcML element:     <code>src:return</code></li>
        
<li>advice type:       <code>before</code></li>
        
<li>codeModifier type: <code>codeFragment</code></li>
      
</ul>
      
<h2>Example 1</h2>
      
<div class="source">
<pre>int foo() {
  int i = 10;
  return i;
}
</pre>
      
</div>
      
<h3>Code Modifier</h3>
      
<xd:xml>
<codeModifier type="codeFragment">
  
<text>i = i * 2;</text>
</codeModifier>
      
</xd:xml>
      
<h3>Modified Code</h3>
      
<div class="source">
<pre>int foo() {
  int i = 10;
  
<span class="highlightedCode">i = i * 2;</span>
  return i;
}
</pre>
      
</div>
      
<h2>Example 2</h2>
      
<p>If the return statement is inside an if/for/while/do without a block, a block is added by the weaver.</p>
      
<div class="source">
<pre>int foo() {
  if( true )
    return 10;
  else
    return 20;
}
</pre>
      
</div>
      
<h3>Code Modifier</h3>
      
<xd:xml>
<codeModifier type="codeFragment">
  
<text>printf("function returns");</text>
</codeModifier>
      
</xd:xml>
      
<h3>Modified Code</h3>
      
<div class="source">
<pre>int foo() {
  if( true ) {
    
<span class="highlightedCode">printf("function returns");</span>
    return 10;
  } else {
    
<span class="highlightedCode">printf("function returns");</span>
    return 20;
  }
}
</pre>
      
</div>
        
<xd:weaverRuleLinks pointcutType="src:return" adviceType="before" codeModifierType="codeFragment" />
    
</xd:detail>
    
<xd:cvsId>$Id$</xd:cvsId>
    
<xd:author>ibirrer</xd:author>
    
<xd:copyright>2004, P&amp;P Software GmbH</xd:copyright>
  
</xd:doc>

  
  
                   <xd:doc>
    Adds the code fragment before the return statement.
  
</xd:doc>

  
<xsl:template match="src:return[ax:advice[@type='before' and position() = 1]/ax:codeModifier[@type='codeFragment']]" mode="weaving">
    
<!-- Test if the return element is enclosed in a block -->
    
<xsl:variable name="isEnclosedInBlock" select="exists(parent::src:block)" />
    
<xsl:variable name="result">
      
<!-- Find out indentation -->
      
<xsl:variable name="indent" select="util:repeatString($defaultIndentation, count(ancestor::src:block) + 1 - count(parent::src:block))" />
      
      
<!-- If return is not enclosed in a block, add opening curly brace and newline -->
      
<xsl:if test="not($isEnclosedInBlock)">
        
<xsl:value-of select="concat( ' {', ' ', $indent )" />
      
</xsl:if>
      
      
<!-- Add codeFragment before the return element -->
      
<ax:inserted><xsl:value-of select="util:indentText(xw:getCodeModifierContent(ax:advice[1]/ax:codeModifier), $indent, false())" /></ax:inserted>
      
<xsl:value-of select="concat(' ',$indent)" />
      
      
<!-- copy the return element -->
      
<xsl:copy>
        
<xsl:copy-of select="@*" />
        
<!-- The current advice has to be deleted. All the others must be preserved.  -->
        
<xsl:copy-of select="ax:advice[position() != 1]" />
        
<xsl:copy-of select="node()[not(self::ax:advice)]" />
      
</xsl:copy>
      
<xsl:if test="not($isEnclosedInBlock)">
        
<xsl:value-of select="concat( ' ', substring-after($indent, $defaultIndentation) , '}' )" />
      
</xsl:if>
    
</xsl:variable>
    
<xsl:choose>
      
<xsl:when test="not($isEnclosedInBlock)">
        
<src:block><xsl:copy-of select="$result" /></src:block>
      
</xsl:when>
      
<xsl:otherwise>
        
<xsl:copy-of select="$result" />
      
</xsl:otherwise>
    
</xsl:choose>
  
</xsl:template>
  
  
<xsl:template match="text()[following-sibling::src:return[ax:advice[@type='before' and position() = 1]/ax:codeModifier[@type='codeFragment']]][position() = last()]" mode="weaving">
    
<xsl:choose>
      
<xsl:when test="exists(following-sibling::src:return/parent::src:block)">
        
<xsl:value-of select="." />
      
</xsl:when>
      
<xsl:otherwise>
        
<xsl:value-of select="normalize-space(.)" />
      
</xsl:otherwise>
    
</xsl:choose>
  
</xsl:template>
  
  
<xsl:template match="text()[following-sibling::src:else[src:return[ax:advice[@type='before' and position() = 1]/ax:codeModifier[@type='codeFragment']]]][position() = last()]" mode="weaving">
    
<xsl:choose>
      
<xsl:when test="exists(following-sibling::src:else/src:return/parent::src:block)">
        
<xsl:value-of select="." />
      
</xsl:when>
      
<xsl:otherwise>
        
<xsl:value-of select="concat(' ', normalize-space(.))" />
      
</xsl:otherwise>
    
</xsl:choose>
  
</xsl:template>
  
  
<xsl:template match="text()[following-sibling::src:else/src:if/src:then[src:return[ax:advice[@type='before' and position() = 1]/ax:codeModifier[@type='codeFragment']]]][position() = last()]" mode="weaving">
    
<xsl:choose>
      
<xsl:when test="exists(following-sibling::src:else/src:return/parent::src:block)">
        
<xsl:value-of select="." />
      
</xsl:when>
      
<xsl:otherwise>
        
<xsl:value-of select="concat(' ', normalize-space(.))" />
      
</xsl:otherwise>
    
</xsl:choose>
  
</xsl:template>
</xsl:stylesheet>






































v