<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 a member to a Java class</xd:short>
    
<xd:detail>
      
<ul> 
        
<li>srcML element:     <code>src:class</code></li>
        
<li>advice type:       <code>add</code></li>
        
<li>codeModifier type: <code>member</code></li>
      
</ul>
      
      Adds a member to the first possible location of the following choices:
      
<ul>
        
<li>Before the first method</li>
        
<li>As the last member in the class</li>
      
</ul>
      A member is anything that can be a member of a class. E.g a method, a field, an inner class.
      
<h2>Example 1</h2>
      
<h3>Base Code</h3>
      
<div class="source">
<pre>class Foo {
  public void foo() {}
}
</pre>
      
</div>
      
<h3>Code Modifier</h3>
      
<xd:xml>
<codeModifier type="declaration">
    
<accessModifier type="public" />
    
<text>public void foo_new() {}</text>
</codeModifier>
      
</xd:xml>
      
<h3>Modified Code</h3>
      
<div class="source">
      
<pre>class Foo {
  
<span class="highlightedCode">public void foo_new() {}</span>
  public void foo() {}
}
</pre>
      
</div>
      
<xd:weaverRuleLinks pointcutType="src:class" adviceType="add" codeModifierType="member" />
    
</xd:detail>
    
<xd:cvsId>$Id$</xd:cvsId>
    
<xd:author>ibirrer</xd:author>
    
<xd:copyright>2004-2005, P&amp;P Software GmbH</xd:copyright>
  
</xd:doc>

  
  
<!-- Preserve Whitespaces -->
  
<xsl:preserve-space elements="*" />
  
    
     <xd:doc>
      Adds a member to the first possible location of the following choices:
      
<ul>
        
<li>Before the first method</li>
        
<li>As the last member in the class</li>
      
</ul>
  
</xd:doc>

  
<xsl:template match="src:class[ax:advice[@type='add' and position() = 1]/ax:codeModifier[@type='member']]" mode="weaving">
    
<xsl:copy>
      
<xsl:copy-of select="@*" />
      
<!-- The current advice has to be deleted. Others must be preserved.  -->
      
<xsl:copy-of select="ax:advice[position() != 1]" />
      
<xsl:choose>
        
<xsl:when test=".//src:function">
          
<!-- Method found. Weave it before the method.-->
          
<xsl:apply-templates select="child::node()[not(self::ax:advice)]" mode="weaving_class_add_member_function">
            
<xsl:with-param name="advice" select="ax:advice[1]" />
          
</xsl:apply-templates>
        
</xsl:when>
        
<xsl:otherwise>
          
<!-- Weave everything in unit -->
          
<xsl:apply-templates select="child::node()[not(self::ax:advice)]" mode="weaving" />
          
          
<!-- Add newline -->
          
<xsl:text>
</xsl:text>
          
          
<!-- add the declaration(s) -->
          
<xsl:value-of select="xw:getCodeModifierContent(ax:advice[1]/ax:codeModifier)" />
        
</xsl:otherwise>
      
</xsl:choose>
    
</xsl:copy>
  
</xsl:template>

  
     <xd:doc>
     This template adds the member before the first method that occurs in the source file.
    
<xd:param name="advice" type="node-set">The advice element which will be used for weaving the unit.</xd:param>
  
</xd:doc>

  
<xsl:template match="src:function[1]" mode="weaving_class_add_member_function">
    
<xsl:param name="advice" />
    
<xsl:if test="not(exists(preceding-sibling::*[position() = 1 and self::src:comment]))">
      
<!-- If there is a comment directly before this function, the declarations have been added already by the template below -->
      
<xsl:value-of select="xw:getCodeModifierContent($advice/ax:codeModifier)" />
      
<xsl:text>
</xsl:text>
      
<xsl:value-of select="util:getIndent(.)" />
    
</xsl:if>
    
    
<!-- copy the function -->
    
<xsl:copy>
      
<xsl:copy-of select="@*" />
      
<!-- Copy advices of this function -->
      
<xsl:copy-of select="ax:advice" />
      
<!-- Copy the rest -->
      
<xsl:apply-templates mode="weaving" />
    
</xsl:copy>
  
</xsl:template>
  
  
     <xd:doc>
    If a function has a comment attached, the declaration is added before the comment
    
<xd:param name="advice" type="node-set">The advice element which will be used for weaving the class.</xd:param>
  
</xd:doc>

  
<xsl:template match="src:comment[following-sibling::*[position() = 1 and self::src:function]][1]" mode="weaving_class_add_member_function">
    
<xsl:param name="advice" />
    
<!-- If the class has a comment, the include is woven here -->
    
<!-- Add new includes -->
    
<xsl:value-of select="xw:getCodeModifierContent($advice/ax:codeModifier)" />
    
<xsl:text>
</xsl:text>
    
<xsl:value-of select="util:getIndent(.)" />
    
<xsl:copy>
      
<xsl:copy-of select="@*" />
      
<!-- Copy all advices of this class -->
      
<xsl:copy-of select="ax:advice" />
      
<xsl:apply-templates mode="weaving" />
    
</xsl:copy>
  
</xsl:template>

  
  
     <xd:doc>
     Default copy template for adding declarations before a function definiton
    
<xd:param name="advice" type="node-set">The advice element which will be used for weaving the unit.</xd:param>
  
</xd:doc>

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






































v