<!---->
<
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:ax="
http://control.ee.ethz.ch/XWeaver/AspectX"
version="
2.0">
<
xd:
doc type="
stylesheet">
<
xd:
short>
Weaving Rule that adds a declaration to a unit. </
xd:
short>
<
xd:
detail>
<
ul>
<
li>
srcML element: <
code>
src:unit</
code></
li>
<
li>
advice type: <
code>
add</
code></
li>
<
li>
codeModifier type: <
code>
declaration</
code></
li>
</
ul>
<
p>
The declaration is added to the first possible location of the following choices:</
p>
<
ol>
<
li>
Before first function definition</
li>
<
li>
At the end of the unit</
li>
</
ol>
<
h2>
Example</
h2>
<
div class="
source">
<
pre>
#include <stdio.h>
void Foo:foo() {
printf("foo");
}</
pre>
</
div>
<
h3>
Code Modifier</
h3>
<
xd:
xml>
<
codeModifier type="
declaration">
<
text>
const int MAX_WIDTH = 10;</
text>
</
codeModifier>
</
xd:
xml>
<
h3>
Modified Code</
h3>
<
div class="
source">
<
pre>
#include <stdio.h>
<
span class="
highlightedCode">
const int MAX_WIDTH = 10;</
span>
void Foo:foo() {
printf("foo");
}</
pre>
</
div>
<
xd:
weaverRuleLinks pointcutType="
src:unit"
adviceType="
add"
codeModifierType="
declaration" />
</
xd:
detail>
<
xd:
cvsId>
$Id$</
xd:
cvsId>
<
xd:
author>
ibirrer</
xd:
author>
<
xd:
copyright>
2004, P&P Software GmbH</
xd:
copyright>
</
xd:
doc>
<!---->
<
xsl:
preserve-space elements="
*" />
<
xd:
doc>
Adds a declaration to the first possible location of the following choices:
<
ul>
<
li>
Before first function definition</
li>
<
li>
At the end of the unit</
li>
</
ul>
</
xd:
doc>
<
xsl:
template match="
src:unit[ax:advice[@type='add' and position() = 1]/ax:codeModifier[@type='declaration']]"
mode="
weaving">
<
xsl:
copy>
<
xsl:
copy-of select="
@*" />
<!---->
<
xsl:
copy-of select="
ax:advice[position() != 1]" />
<
xsl:
choose>
<
xsl:
when test="
.//src:function">
<!---->
<
xsl:
apply-templates select="
child::node()[not(self::ax:advice)]"
mode="
weaving_unit_add_declaration_function">
<
xsl:
with-param name="
advice"
select="
ax:advice[1]" />
</
xsl:
apply-templates>
</
xsl:
when>
<
xsl:
otherwise>
<!---->
<
xsl:
apply-templates select="
child::node()[not(self::ax:advice)]"
mode="
weaving" />
<!---->
<
xsl:
text>
</
xsl:
text>
<!---->
<
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 declaration before the first function 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_unit_add_declaration_function">
<
xsl:
param name="
advice" />
<
xsl:
if test="
preceding-sibling::*[position() = 1 and not(self::src:comment)]">
<!---->
<
xsl:
value-of select="
xw:getCodeModifierContent($advice/ax:codeModifier)" />
<
xsl:
text>
</
xsl:
text>
</
xsl:
if>
<!---->
<
xsl:
copy>
<
xsl:
copy-of select="
@*" />
<!---->
<
xsl:
copy-of select="
ax:advice" />
<!---->
<
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 unit.</
xd:
param>
</
xd:
doc>
<
xsl:
template match="
src:comment[following-sibling::*[position() = 1 and self::src:function]][1]"
mode="
weaving_unit_add_declaration_function">
<
xsl:
param name="
advice" />
<!---->
<!---->
<
xsl:
value-of select="
xw:getCodeModifierContent($advice/ax:codeModifier)" />
<
xsl:
text>
</
xsl:
text>
<
xsl:
text>
</
xsl:
text>
<
xsl:
copy>
<
xsl:
copy-of select="
@*" />
<!---->
<
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_unit_add_declaration_function">
<
xsl:
param name="
advice" />
<
xsl:
copy>
<
xsl:
copy-of select="
@*" />
<
xsl:
apply-templates mode="
weaving_unit_add_declaration_function">
<
xsl:
with-param name="
advice"
select="
$advice" />
</
xsl:
apply-templates>
</
xsl:
copy>
</
xsl:
template>
</
xsl:
stylesheet>
v