<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: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 comment into another block comment.</xd:short>
    
<xd:detail>
      
<ul> 
        
<li>srcML element:     <code>src:comment[@type='block']</code></li>
        
<li>advice type:       <code>end</code></li>
        
<li>codeModifier type: <code>comment</code></li>
      
</ul>
      Only 
<strong>block</strong> comments are allowed!
    
<h2>Example 1</h2>
      
<h3>Base Code</h3>
      
<div class="source">
<pre>/**
 * A comment
 */
</pre>
      
</div>
      
<h3>Code Modifier</h3>
      
<xd:xml>
<codeModifier type="comment">
  
<text>Additional Comment</text>
</codeModifier>
      
</xd:xml>
      
<h3>Modified Code</h3>
      
<div class="source">
      
<pre>/**
 * A comment
 * 
<span class="highlightedCode">Additional Comment</span>
 */
</pre>
      
</div>
      
      
<h2>Example 2</h2>
      
<p>@-tags stay at the end of a comment.</p>
      
<h3>Base Code</h3>
      
<div class="source">
<pre>/**
 * A comment
 * @param int test
 */
</pre>
      
</div>
      
<h3>Code Modifier</h3>
      
<xd:xml>
<codeModifier type="comment">
  
<text>Additional Comment Line 1</text>
  
<text>Additional Comment Line 2</text>
</codeModifier>
      
</xd:xml>
      
<h3>Modified Code</h3>
      
<div class="source">
      
<pre>/**
 * A comment
 * 
<span class="highlightedCode">Additional Comment Line 1</span>
 * 
<span class="highlightedCode">Additional Comment Line 2</span>
 * @param int test
 */
</pre>
      
</div>
      
<xd:weaverRuleLinks pointcutType="src:comment" adviceType="end" codeModifierType="comment" />
    
</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>
    Uses XSLT 2.0 Regular Expressions to do the weaving
  
</xd:doc>

  
<xsl:template match="src:comment[(@type='block')and(ax:advice[@type='end' and position() = 1]/ax:codeModifier[@type='comment'])]" mode="weaving">
    
<xsl:variable name="comment" select="text()[1]" />
    
<xsl:copy>
      
<xsl:copy-of select="@*" />
      
      
<!-- The current advice is deleted. Others are preserved.  -->
      
<xsl:copy-of select="ax:advice[position() != 1]" />
      
<xsl:variable name="comments" select="ax:advice[1]/ax:codeModifier/ax:text" />
      
      
<!-- 
        Compute the indentation of the comment 
      
-->
      
<xsl:variable name="indent">
        
<xsl:choose>
          
<xsl:when test="not(contains($comment,' '))">
            
<!-- single line comment, the indentation is taken from the text() element preceding the comment element -->
            
<xsl:value-of select="replace(preceding-sibling::text()[1], '(.* )([^ ]*)', '$2', 's')" />
          
</xsl:when>
          
<xsl:otherwise>
            
<!-- multi line comment, the indentation is taken from the the indentation of the star charater (*). -->
            
<xsl:analyze-string select="tokenize($comment , ' ')[2]" regex="([ \t]*)\*">
              
<xsl:matching-substring>
                
<xsl:value-of select="substring(regex-group(1), 1, string-length(regex-group(1) ) -1 )" />
              
</xsl:matching-substring>
            
</xsl:analyze-string>
          
</xsl:otherwise>
        
</xsl:choose>
      
</xsl:variable>
                        
      
<xsl:analyze-string select="$comment" regex="(/\*+)(.*?)(@.*?)?(\*+/)" flags="s">
        
<xsl:matching-substring>
          
<xsl:variable name="beginOfComment" select="regex-group(1)" />
          
<xsl:variable name="commentContent" select="regex-group(2)" />
          
<xsl:variable name="commentTags" select="regex-group(3)" />
          
<xsl:variable name="endOfComment" select="regex-group(4)" />
          
          
<!-- Print /** -->
          
<xsl:value-of select="$beginOfComment" />
          
          
<!-- Print newline if comment was a single line comment -->
          
<xsl:if test="not(contains($comment,' '))">
            
<xsl:value-of select="concat(' ', $indent, ' *')" />
          
</xsl:if>
          
          
<!-- Print original comment contens (before @ tags) -->
          
<xsl:value-of select="$commentContent" />

          
<xsl:choose>
            
<!-- Print newline if original comment was a single line comment -->
            
<xsl:when test="not(contains($comment,' '))">
              
<xsl:value-of select="concat(' ', $indent, ' * ' )" />
            
</xsl:when>
            
<!-- Print star if there were no tags in original comment -->
            
<xsl:when test="string-length($commentTags) = 0">
              
<xsl:text></xsl:text>
            
</xsl:when>
          
</xsl:choose>
          
          
<!-- Weave new comment -->
<!--           <xsl:variable name="commentString">
            <xsl:for-each select="$comments">
              <xsl:value-of select="util:xmlToString(child::node())"/>
              <xsl:if test="position() != last()">
                <xsl:value-of select="concat('&#xA;', $indent, ' * ')"/>
              </xsl:if>
            </xsl:for-each>
          </xsl:variable> 
-->
          
<xsl:for-each select="$comments">
            
<xsl:variable name="commentContent" select="util:xmlToString(child::node())" />
            
<xsl:choose>
              
<xsl:when test="contains($commentContent, ' ')">
              
<xsl:for-each select="tokenize($commentContent,' ')">
                
<xsl:value-of select="." />
                
<xsl:if test="position() != last()">
                  
<xsl:value-of select="concat(' ', $indent, ' * ')" />
                
</xsl:if>
              
</xsl:for-each>
              
</xsl:when>
              
<xsl:otherwise>
                
<xsl:value-of select="." />
              
</xsl:otherwise>
            
</xsl:choose>
            
<xsl:if test="position() != last()">
              
<xsl:value-of select="concat(' ', $indent, ' * ')" />
            
</xsl:if>
          
</xsl:for-each>
          
          
<xsl:choose>
            
<xsl:when test="string-length($commentTags) != 0">
              
<xsl:value-of select="concat(' ', $indent, ' * ')" />
            
</xsl:when>
          
</xsl:choose>
          
          
<!-- Print original comment tags -->
          
<xsl:value-of select="$commentTags" />
          
          
<!-- Add newline if original comment contained no tags -->
          
<xsl:choose>
            
<xsl:when test="string-length($commentTags) = 0">
              
<xsl:value-of select="concat(' ', $indent, ' ')" />
            
</xsl:when>
          
</xsl:choose>

          
<xsl:value-of select="$endOfComment" />
        
</xsl:matching-substring>
    
         
<xsl:non-matching-substring>
          
<xsl:message terminate="yes">[XWeaver] ERROR: Comment weaving failed at weaverRules/comment_end_comment.xsl</xsl:message>
        
</xsl:non-matching-substring>
      
</xsl:analyze-string>
    
</xsl:copy>
  
</xsl:template>
  
  
     <xd:doc>
    If the user tries to weave a line comment, a warning is being raised.
  
</xd:doc>

  
<xsl:template match="src:comment[(@type='line')and(ax:advice[@type='end' and position() = 1]/ax:codeModifier[@type='comment'])]" mode="weaving">
    
<xsl:variable name="comment" select="text()[1]" />
    
<xsl:copy>
      
<xsl:copy-of select="@*" />
      
<xsl:message terminate="no">[XWeaver] WARNING: Ignoring the advice "<xsl:value-of select="ax:advice/@name" />" in one-line comment (@type='line'). Only JavaDoc-style comments (/** */) are supported.</xsl:message>
      
<!-- The current advice is deleted. Others are preserved.  -->
      
<xsl:copy-of select="ax:advice[position() != 1]" />
      
<!-- Apply templates to all child nodes exluding advices -->
      
<xsl:apply-templates select="node()[not(self::ax:advice)]" mode="weaving" />
    
</xsl:copy>
  
</xsl:template>
  
</xsl:stylesheet>






































v