<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>

    <xsl:template match="*[local-name()='portlet-app']">
        <aop>
			<xsl:for-each select="//*[local-name()='supports']/*[local-name()='portlet-mode']">
				<xsl:call-template name="aspects"/>
			</xsl:for-each>
			<aspect name="ru.spbstu.ecd.ics.aopportlet168.testapp.AccessCheckAspect" class="ru.spbstu.ecd.ics.aopportlet168.testapp.AccessCheckAspect"/>
			<aspect name="ru.spbstu.ecd.ics.aopportlet168.testapp.DataSourceAspect" class="ru.spbstu.ecd.ics.aopportlet168.testapp.DataSourceAspect"/>
            <bind pointcut="execution(public * ru.spbstu.ecd.ics.aopportlet168.testapp.WeatherDataSource->getLondonWeather())">
                <advice name="getRandomInt" aspect="ru.spbstu.ecd.ics.aopportlet168.testapp.DataSourceAspect"/>
            </bind>
            <xsl:for-each select="//*[local-name()='portlet-class']">
				<xsl:element name="bind">
					<xsl:attribute name="pointcut">execution(public * <xsl:value-of select="."/>->render(..))</xsl:attribute>
					<xsl:element name="advice">
						<xsl:attribute name="name">checkAccess</xsl:attribute>
						<xsl:attribute name="aspect">ru.spbstu.ecd.ics.aopportlet168.testapp.AccessCheckAspect</xsl:attribute>
					</xsl:element>
				</xsl:element>
			</xsl:for-each>

			<xsl:for-each select="//*[local-name()='supports']/*[local-name()='portlet-mode']">
				<xsl:call-template name="bind"/>
			</xsl:for-each>
		</aop>
    </xsl:template>

    <xsl:template name="bind">
        <xsl:element name="bind">
            <xsl:attribute name="pointcut">execution(public * <xsl:value-of select="../../*[local-name()='portlet-class']"/>->render(..))</xsl:attribute>
            <xsl:element name="advice">
                <xsl:attribute name="name">render</xsl:attribute>
                <xsl:attribute name="aspect"><xsl:call-template name="prePortletStr"/><xsl:value-of select="."/>Aspect</xsl:attribute>
            </xsl:element>
        </xsl:element>
        <xsl:element name="bind">
            <xsl:attribute name="pointcut">execution(public * <xsl:value-of select="../../*[local-name()='portlet-class']"/>->processAction(..))</xsl:attribute>
            <xsl:element name="advice">
                <xsl:attribute name="name">processAction</xsl:attribute>
                <xsl:attribute name="aspect"><xsl:call-template name="prePortletStr"/><xsl:value-of select="."/>Aspect</xsl:attribute>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <xsl:template name="aspects">
        <xsl:element name="aspect">
            <xsl:attribute name="name"><xsl:call-template name="prePortletStr"/><xsl:value-of select="."/>Aspect</xsl:attribute>
            <xsl:attribute name="class"><xsl:call-template name="prePortletStr"/><xsl:value-of select="."/>Aspect</xsl:attribute>
        </xsl:element>
    </xsl:template>

    <xsl:template name="prePortletStr">
        <xsl:value-of select="substring-before(../../*[local-name()='portlet-class'],'Portlet')"/>
    </xsl:template>

</xsl:stylesheet>

