<?xml version="1.0"?>
<!-- 

  Copyright 2006 The Apache Software Foundation.
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
      http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  $Id$

--> 
<xsl:stylesheet       version="1.0"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  xmlns:xalan="org.apache.xalan.xslt.extensions.Redirect"
   extension-element-prefixes="xalan">

    <xsl:output method="xml"
                indent="yes"
              encoding="iso-8859-1" />

    <xsl:template match="dialogs">
<xsl:comment> 

  Copyright 2006 The Apache Software Foundation.
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
      http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  $Id$
</xsl:comment>
<xsl:comment>
This file was generated by a stylesheet.
</xsl:comment>
        <scxmldialogs>
        <xsl:apply-templates select="dialog"/>
        </scxmldialogs>
    </xsl:template>

    <xsl:template match="dialog">
        <xsl:variable name="name" select="@name"/>
        <xsl:variable name="start" select="@start"/>
        <dialog name="{$name}" scxmlconfig="{concat($name,'.xml')}"/>
        <xalan:write select="concat($name,'.xml')">
<xsl:comment> 

  Copyright 2006 The Apache Software Foundation.
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
      http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  $Id$
</xsl:comment>
<xsl:comment>
This file was generated by a stylesheet.
</xsl:comment>
        <xsl:choose>
            <xsl:when test="count(child::transition) &gt; 0">
            <scxml version="1.0"
              initialstate="dialog">
                <state id="dialog">
                    <initial>
                        <transition target="{$start}"/>
                    </initial>
            <xsl:apply-templates select="transition"/>
            <xsl:apply-templates select="action"/>
            <xsl:apply-templates select="view"/>
            <xsl:apply-templates select="end"/>
            <xsl:apply-templates select="subdialog"/>
                </state>
            </scxml>
            </xsl:when>
            <xsl:otherwise>
            <scxml version="1.0"
              initialstate="{$start}">
            <xsl:apply-templates select="transition"/>
            <xsl:apply-templates select="action"/>
            <xsl:apply-templates select="view"/>
            <xsl:apply-templates select="end"/>
            <xsl:apply-templates select="subdialog"/>
            </scxml>
            </xsl:otherwise>
        </xsl:choose>
        </xalan:write>
    </xsl:template>

    <xsl:template match="transition">
        <xsl:param name="parent" />
        <xsl:param name="parentid" />
        <xsl:variable name="target" select="@target"/>
        <xsl:variable name="outcome" select="@outcome"/>
        <xsl:variable name="begin" select="'${outcome eq '"/>
        <xsl:variable name="end" select="'}'"/>
        <xsl:variable name="quote" select="&quot;'&quot;"/>
        <xsl:variable name="cond" 
                    select="concat($begin,$quote,$outcome,$quote,$end)"/>
        <xsl:choose>
            <xsl:when test="$parent = 'action'">
            <transition cond="{$cond}"
                      target="{$target}"/>
            </xsl:when>
            <xsl:when test="$parent = 'view'">
            <transition cond="{$cond}"
                       event="faces.outcome"
                      target="{$target}"/>
            </xsl:when>
            <xsl:when test="$parent = 'subdialog'">
            <transition cond="{$cond}"
                       event="{$parentid}.done"
                      target="{$target}"/>
            </xsl:when>
            <xsl:when test="$parent = 'end'">
            <transition target="{$target}"/>
            </xsl:when>
            <xsl:otherwise>
            <transition cond="{$cond}"
                       event="faces.outcome"
                      target="{$target}"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="action">
        <xsl:variable name="stateid" select="@name"/>
        <xsl:variable name="mbexpr" select="@method"/>
        <state id="{$stateid}">
            <onentry>
                <var name="outcome" expr="{$mbexpr}"/>
            </onentry>
        <xsl:apply-templates select="transition">
            <xsl:with-param name="parent" select="'action'"/>
        </xsl:apply-templates>
        </state>
    </xsl:template>

    <xsl:template match="view">
        <xsl:variable name="stateid" select="@name"/>
        <xsl:variable name="viewid" select="@viewId"/>
        <state id="{$stateid}">
            <invoke targettype="x-jsf-view" target="{$viewid}" />
        <xsl:apply-templates select="transition">
            <xsl:with-param name="parent" select="'view'"/>
        </xsl:apply-templates>
        </state>
    </xsl:template>

    <xsl:template match="end">
        <xsl:variable name="stateid" select="@name"/>
        <xsl:variable name="viewid" select="@viewId"/>
        <state id="{$stateid}" final="true">
            <invoke targettype="x-jsf-view" target="{$viewid}" />
        <xsl:apply-templates select="transition">
            <xsl:with-param name="parent" select="'end'"/>
        </xsl:apply-templates>
        </state>
    </xsl:template>

    <xsl:template match="subdialog">
        <xsl:variable name="stateid" select="@name"/>
        <xsl:variable name="subdialogid" select="@dialogName"/>
        <state id="{$stateid}" src="{$subdialogid}.xml">
        <xsl:apply-templates select="transition">
            <xsl:with-param name="parent" select="'subdialog'"/>
            <xsl:with-param name="parentid" select="@name"/>
        </xsl:apply-templates>
        </state>
    </xsl:template>

</xsl:stylesheet>
