<xsl:transform version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
                                                                                
<!--
	The service-context to poco modeling transformation stylesheet.

	Copyright 2009 by Pocomatic Software LLC, All Rights Reserved.

	Author: Ke Jin <kejin@pocomatic.com>
-->

<xsl:output doctype-system =
       "http://www.pocomatic.com/poco-application-context.dtd"/>
<xsl:output method="xml" encoding="UTF-8"/>

<!-- ensure recursive template match -->
<xsl:template match="*|/">
  <xsl:apply-templates/>
</xsl:template>

<!-- copy all unspecified nodes -->
<xsl:template match="*">
  <xsl:copy>
    <xsl:if test="dynamic-property and not(@id)">
      <xsl:attribute name="id">
        <xsl:value-of select="concat('poco-bean-id:', generate-id(.))"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<!-- poco-dynamic-context is mapped to poco-application-context -->
<xsl:template match="/poco-dynamic-context">
  <poco-application-context>
    <header>#include "ServiceRegistry.h"</header>
    <load library="./ServiceRegistry.$dll"/>
    <xsl:apply-templates/>
    <bean class="*void*" factory-method="(void*)" id="poco-dynamic-resync" singleton="false" lazy-init="true">
      <method-arg type="array">
      <array type="bean" class="*void*">
      <xsl:apply-templates select="bean/dynamic-property" mode="dynamic-entry"/>
      </array>
      </method-arg>
    </bean>
  </poco-application-context>
</xsl:template>

<xsl:template match="service">
  <bean class="ServiceRegistry" factory-method="ServiceRegistry::instance"
        lazy-init="false" singleton="true">
    <xsl:variable name="publish-class-type">
      <xsl:if test="@publish-type">
        <xsl:value-of select="@publish-type"/>
      </xsl:if>
      <xsl:if test="not(@publish-type)">
        <xsl:value-of select="@class"/>
      </xsl:if>
    </xsl:variable>

    <ioc method="setService">
      <method-arg type="string">
        <xsl:attribute name="value">
          <xsl:value-of select="@publish-name"/>
        </xsl:attribute>
      </method-arg>
      <method-arg type="string">
        <xsl:attribute name="value">
          <xsl:value-of select="$publish-class-type"/>
        </xsl:attribute>
      </method-arg>
      <method-arg>
        <xsl:attribute name="class">
          <xsl:value-of select="$publish-class-type"/>
        </xsl:attribute>
        <bean>
          <xsl:copy-of select="@class|@factory-bean|@factory-method"/>
        </bean>
      </method-arg>
    </ioc>
  </bean>
</xsl:template>

<xsl:template match="bean/dynamic-property">
</xsl:template>

<xsl:template match="bean/dynamic-property" mode="dynamic-entry">
  <item>
  <bean singleton="false">
    <xsl:attribute name="class">
      <xsl:value-of select="../@class"/>
    </xsl:attribute>
    <xsl:attribute name="factory-method">
      <xsl:value-of select="concat('(', ../@class, '*)')"/>
    </xsl:attribute>
    <method-arg>
      <xsl:attribute name="ref">
        <xsl:if test="../@id">
          <xsl:value-of select="../@id"/>
        </xsl:if>
        <xsl:if test="not(../@id)">
          <xsl:value-of select="concat('poco-bean-id:', generate-id(..))"/>
        </xsl:if>
      </xsl:attribute>
    </method-arg>
    <ioc>
      <xsl:attribute name="method">
        <xsl:value-of select="@name"/>
      </xsl:attribute>
      <method-arg>
        <bean singleton="false"> 
          <xsl:attribute name="class">
            <xsl:value-of select="@reference-type"/>
          </xsl:attribute>
          <xsl:attribute name="factory-method">
            <xsl:value-of select="concat('(', @reference-type, '*)ServiceRegistry::instance()->getService')"/>
          </xsl:attribute>
          <method-arg type="string">
            <xsl:attribute name="value">
              <xsl:value-of select="@reference-name"/>
            </xsl:attribute>
          </method-arg>
          <method-arg type="string">
            <xsl:attribute name="value">
              <xsl:value-of select="@reference-type"/>
            </xsl:attribute>
          </method-arg>
        </bean>
      </method-arg>
    </ioc>
  </bean>
  </item>
</xsl:template>
  
</xsl:transform>

