<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns="http://cygnets.co.uk/schemas/adl-1.2"
                xmlns:a="http://cygnets.co.uk/schemas/adl-1.2"
                exclude-result-prefixes="a">
  <!--
      Application Description Framework
      adl2adl.xsl
      
      (c) 2008 Cygnet Solutions Ltd
      
      Transform ADL to Verbose (or canonical) ADL
      Most defaults are replaced with their actual values.
      
      $Author: af $
      $Revision: 1.1 $
  -->
  <xsl:import href="types.xslt"/>
  
  <xsl:output indent="yes" method="xml" encoding="utf-8"/>

  <xsl:template match="a:application">
      <xsl:copy>
        <xsl:apply-templates select="@*" />
        <xsl:comment>
          ***************************************************************************
          *
          *	©2007 Cygnet Solutions Ltd
          *
          *	THIS FILE IS AUTOMATICALLY GENERATED AND SHOULD NOT
          *	BE MANUALLY EDITED. 
          *
          *	Generated using adl2adl.xsl revision <xsl:value-of select="substring('$Revision: 1.1 $', 12)"/>
          *
          ***************************************************************************
        </xsl:comment>
        <xsl:apply-templates select="a:type | a:typedef | a:entity | comment()"/>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="a:entity">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:if test="not(a:key)">
        <xsl:if test=".//a:property[@name='Id']">
          <xsl:message terminate="yes">Class "<xsl:value-of select="@name"/>" has an 'Id' field, but no key. This is not allowed.</xsl:message>
        </xsl:if>
        <key>
          <property name="Id" type="Integer-32" access="field.pascalcase-underscore">
            <generator class="identity"/>
          </property>
        </key>
      </xsl:if>
      <xsl:apply-templates select="*"/>
    </xsl:copy>
  </xsl:template>
  
  <!-- Expand any 'default' forms -->
  <xsl:template match="a:form[not(@properties)]">
    <form properties="listed">
      <xsl:apply-templates select="@*" />
      <xsl:for-each select="../a:key/a:property|../a:property">
        <field>
          <xsl:attribute name="property">
            <xsl:value-of select="@name"/>
          </xsl:attribute>
        </field>
      </xsl:for-each>
    </form>
  </xsl:template>

  <!-- Expand any 'default' lists -->
  <xsl:template match="a:list[not(@properties)]">
    <list properties="listed">
      <xsl:apply-templates select="@*" />
      <pragma name="with-pagination-control" value="true"/>
      <pragma name="with-can-add" value="true"/>
      <xsl:for-each select="../a:key/a:property|../a:property">
        <field>
          <xsl:attribute name="property">
            <xsl:value-of select="@name"/>
          </xsl:attribute>
        </field>
      </xsl:for-each>
    </list>
  </xsl:template>

  <xsl:template match="a:property">
    <property>
      <xsl:choose>
        <xsl:when test="@type='entity' or @type='list' or @type='link'">
          <xsl:attribute name="type"><xsl:value-of select="@type" /></xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <xsl:variable name="typename">
            <xsl:call-template name="type-name" />
          </xsl:variable>
          <xsl:if test="$typename">
            <xsl:attribute name="type">
              <xsl:value-of select="$typename"/>
            </xsl:attribute>
          </xsl:if>
          <!--xsl:call-template name="add-type-attr-if-avail">
            <xsl:with-param name="attr" select="'name'" />
          </xsl:call-template-->
        </xsl:otherwise>
      </xsl:choose>
      <xsl:apply-templates select="@*[name()!='type']|*" />
    </property>
  </xsl:template>

  <xsl:template name="add-type-attr-if-avail">
    <xsl:param name="attr"/>
    <xsl:variable name="val">
      <xsl:call-template name="type-attr">
        <xsl:with-param name="attr" select="$attr" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="$val">
      <xsl:attribute name="{$attr}">
        <xsl:value-of select="$val"/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>