formatting
This commit is contained in:
parent
e7daad3f95
commit
3267a980b3
2 changed files with 170 additions and 183 deletions
42
traceur.xsd
42
traceur.xsd
|
@ -1,24 +1,24 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
||||||
<xs:element name="traceur">
|
<xs:element name="traceur">
|
||||||
<xs:complexType>
|
<xs:complexType>
|
||||||
<xs:sequence>
|
<xs:sequence>
|
||||||
<!-- Peut contenir plusieurs "LINETO" ou "MOVETO" -->
|
<!-- Peut contenir plusieurs "LINETO" ou "MOVETO" -->
|
||||||
<xs:choice maxOccurs="unbounded">
|
<xs:choice maxOccurs="unbounded">
|
||||||
<xs:element name="LINETO">
|
<xs:element name="LINETO">
|
||||||
<xs:complexType>
|
<xs:complexType>
|
||||||
<xs:attribute name="x" type="xs:decimal" use="required" />
|
<xs:attribute name="x" type="xs:decimal" use="required" />
|
||||||
<xs:attribute name="y" type="xs:decimal" use="required" />
|
<xs:attribute name="y" type="xs:decimal" use="required" />
|
||||||
</xs:complexType>
|
</xs:complexType>
|
||||||
</xs:element>
|
</xs:element>
|
||||||
<xs:element name="MOVETO">
|
<xs:element name="MOVETO">
|
||||||
<xs:complexType>
|
<xs:complexType>
|
||||||
<xs:attribute name="x" type="xs:decimal" use="required" />
|
<xs:attribute name="x" type="xs:decimal" use="required" />
|
||||||
<xs:attribute name="y" type="xs:decimal" use="required" />
|
<xs:attribute name="y" type="xs:decimal" use="required" />
|
||||||
</xs:complexType>
|
</xs:complexType>
|
||||||
</xs:element>
|
</xs:element>
|
||||||
</xs:choice>
|
</xs:choice>
|
||||||
</xs:sequence>
|
</xs:sequence>
|
||||||
</xs:complexType>
|
</xs:complexType>
|
||||||
</xs:element>
|
</xs:element>
|
||||||
</xs:schema>
|
</xs:schema>
|
||||||
|
|
311
traceur.xsl
311
traceur.xsl
|
@ -1,177 +1,164 @@
|
||||||
<xsl:transform version="3.0"
|
<xsl:transform version="3.0"
|
||||||
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
||||||
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
|
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
|
||||||
xmlns:xs="http://www.w3.org/2001/XMLSchema"
|
xmlns:xs="http://www.w3.org/2001/XMLSchema"
|
||||||
exclude-result-prefixes="xs math">
|
exclude-result-prefixes="xs math">
|
||||||
<xsl:output method="xml" indent="yes" />
|
<xsl:output method="xml" indent="yes" />
|
||||||
|
|
||||||
<!-- Déclaration des variables initiales -->
|
<!-- Déclaration des variables initiales -->
|
||||||
<xsl:variable name="initial-x" select="0" as="xs:double" />
|
<xsl:variable name="initial-x" select="0" as="xs:double" />
|
||||||
<xsl:variable
|
<xsl:variable
|
||||||
name="initial-y" select="0" as="xs:double" />
|
name="initial-y" select="0" as="xs:double" />
|
||||||
<xsl:variable name="initial-angle" select="0"
|
<xsl:variable name="initial-angle" select="0"
|
||||||
as="xs:double" />
|
as="xs:double" />
|
||||||
|
|
||||||
<xsl:template match="/tortue">
|
<xsl:template match="/tortue">
|
||||||
<traceur>
|
<traceur>
|
||||||
<!-- Appel du template pour traiter les commandes avec les valeurs initiales des
|
<!-- Appel du template pour traiter les commandes avec les valeurs initiales des
|
||||||
paramètres -->
|
paramètres -->
|
||||||
<xsl:call-template name="process-commands">
|
<xsl:call-template name="process-commands">
|
||||||
<xsl:with-param name="commands" select="*" />
|
<xsl:with-param name="commands" select="*" />
|
||||||
<xsl:with-param name="x"
|
<xsl:with-param name="x" select="$initial-x" />
|
||||||
select="$initial-x" />
|
<xsl:with-param
|
||||||
<xsl:with-param name="y" select="$initial-y" />
|
name="y" select="$initial-y" />
|
||||||
<xsl:with-param
|
<xsl:with-param
|
||||||
name="angle" select="$initial-angle" />
|
name="angle" select="$initial-angle" />
|
||||||
<xsl:with-param name="states" select="()" />
|
<xsl:with-param name="states" select="()" />
|
||||||
</xsl:call-template>
|
</xsl:call-template>
|
||||||
</traceur>
|
</traceur>
|
||||||
</xsl:template>
|
</xsl:template>
|
||||||
|
|
||||||
<!-- Template récursif pour traiter les commandes individuelles -->
|
<!-- Template récursif pour traiter les commandes individuelles -->
|
||||||
<xsl:template
|
<xsl:template
|
||||||
name="process-commands">
|
name="process-commands">
|
||||||
<xsl:param name="commands" />
|
<xsl:param name="commands" />
|
||||||
<xsl:param name="x" />
|
<xsl:param name="x" />
|
||||||
<xsl:param name="y" />
|
<xsl:param name="y" />
|
||||||
|
<xsl:param name="angle" />
|
||||||
<xsl:param
|
<xsl:param
|
||||||
name="angle" />
|
name="states" />
|
||||||
<xsl:param name="states" />
|
|
||||||
|
|
||||||
<!-- Si il reste des commandes à traiter -->
|
<!-- Si il reste des commandes à traiter -->
|
||||||
<xsl:if test="$commands">
|
<xsl:if test="$commands">
|
||||||
<!-- Sélection de la première commande à traiter -->
|
<!-- Sélection de la première commande à traiter -->
|
||||||
<xsl:variable
|
<xsl:variable
|
||||||
name="current-command" select="$commands[1]" />
|
name="current-command" select="$commands[1]" />
|
||||||
<xsl:choose>
|
<xsl:choose>
|
||||||
<!-- Si la commande est une ligne -->
|
<!-- Si la commande est une ligne -->
|
||||||
<xsl:when test="local-name($current-command) = 'line'">
|
<xsl:when test="local-name($current-command) = 'line'">
|
||||||
<!-- Calcul de la distance à parcourir et conversion de l'angle en radians -->
|
<!-- Calcul de la distance à parcourir et conversion de l'angle en radians -->
|
||||||
<xsl:variable name="distance"
|
<xsl:variable name="distance"
|
||||||
select="number($current-command)" />
|
select="number($current-command)" />
|
||||||
<xsl:variable name="radians"
|
<xsl:variable name="radians"
|
||||||
select="$angle * math:pi() div 180" />
|
select="$angle * math:pi() div 180" />
|
||||||
<xsl:variable name="new-x"
|
<xsl:variable name="new-x"
|
||||||
select="$x + $distance * math:cos($radians)" />
|
select="$x + $distance * math:cos($radians)" />
|
||||||
<xsl:variable name="new-y"
|
<xsl:variable name="new-y"
|
||||||
select="$y + $distance * math:sin($radians)" />
|
select="$y + $distance * math:sin($radians)" />
|
||||||
<!-- Création de l'élément LINETO avec les nouvelles coordonnées calculées -->
|
<!-- Création de l'élément LINETO avec les nouvelles coordonnées calculées -->
|
||||||
<LINETO
|
<LINETO
|
||||||
x="{format-number($new-x, '#.######')}"
|
x="{format-number($new-x, '#.######')}"
|
||||||
y="{format-number($new-y, '#.######')}" />
|
y="{format-number($new-y, '#.######')}" />
|
||||||
<!-- Rappel récursif du template avec la mise à jour des coordonnées et des
|
<!-- Rappel récursif du template avec la mise à jour des coordonnées et des
|
||||||
commandes restantes -->
|
commandes restantes -->
|
||||||
<xsl:call-template
|
<xsl:call-template name="process-commands">
|
||||||
name="process-commands">
|
<xsl:with-param name="commands" select="$commands[position() > 1]" />
|
||||||
<xsl:with-param name="commands" select="$commands[position() > 1]" />
|
<xsl:with-param
|
||||||
<xsl:with-param
|
name="x" select="$new-x" />
|
||||||
name="x" select="$new-x" />
|
<xsl:with-param name="y" select="$new-y" />
|
||||||
<xsl:with-param name="y" select="$new-y" />
|
<xsl:with-param
|
||||||
<xsl:with-param
|
name="angle" select="$angle" />
|
||||||
name="angle" select="$angle" />
|
<xsl:with-param name="states" select="$states" />
|
||||||
<xsl:with-param name="states"
|
</xsl:call-template>
|
||||||
select="$states" />
|
</xsl:when>
|
||||||
</xsl:call-template>
|
<!-- Si la commande est un turn -->
|
||||||
</xsl:when>
|
<xsl:when test="local-name($current-command) = 'turn'">
|
||||||
<!-- Si la commande est un turn -->
|
<!-- Calcul du nouvel angle avec modulo 360 pour rester dans un cercle complet -->
|
||||||
<xsl:when test="local-name($current-command) = 'turn'">
|
<xsl:variable
|
||||||
<!-- Calcul du nouvel angle avec modulo 360 pour rester dans un cercle complet -->
|
name="new-angle" select="($angle + number($current-command)) mod 360" />
|
||||||
<xsl:variable
|
<!-- Rappel récursif du template sans changer de position mais en mettant à jour
|
||||||
name="new-angle" select="($angle + number($current-command)) mod 360" />
|
|
||||||
<!-- Rappel récursif du template sans changer de position mais en mettant à jour
|
|
||||||
l'angle -->
|
l'angle -->
|
||||||
<xsl:call-template
|
<xsl:call-template
|
||||||
name="process-commands">
|
name="process-commands">
|
||||||
<xsl:with-param name="commands" select="$commands[position() > 1]" />
|
<xsl:with-param name="commands" select="$commands[position() > 1]" />
|
||||||
<xsl:with-param
|
<xsl:with-param
|
||||||
name="x" select="$x" />
|
name="x" select="$x" />
|
||||||
<xsl:with-param name="y" select="$y" />
|
<xsl:with-param name="y" select="$y" />
|
||||||
<xsl:with-param
|
<xsl:with-param
|
||||||
name="angle" select="$new-angle" />
|
name="angle" select="$new-angle" />
|
||||||
<xsl:with-param name="states"
|
<xsl:with-param name="states" select="$states" />
|
||||||
select="$states" />
|
</xsl:call-template>
|
||||||
</xsl:call-template>
|
</xsl:when>
|
||||||
</xsl:when>
|
<!-- Si la commande est un MOVE -->
|
||||||
<!-- Si la commande est un MOVE -->
|
<xsl:when test="local-name($current-command) = 'move'">
|
||||||
<xsl:when test="local-name($current-command) = 'move'">
|
<!-- Calcul de la distance à parcourir et conversion de l'angle en radians -->
|
||||||
<!-- Calcul de la distance à parcourir et conversion de l'angle en radians -->
|
<xsl:variable name="distance"
|
||||||
<xsl:variable name="distance"
|
select="number($current-command)" />
|
||||||
select="number($current-command)" />
|
<xsl:variable name="radians"
|
||||||
<xsl:variable name="radians"
|
select="$angle * math:pi() div 180" />
|
||||||
select="$angle * math:pi() div 180" />
|
<xsl:variable name="new-x"
|
||||||
<xsl:variable name="new-x"
|
select="$x + $distance * math:cos($radians)" />
|
||||||
select="$x + $distance * math:cos($radians)" />
|
<xsl:variable name="new-y"
|
||||||
<xsl:variable name="new-y"
|
select="$y + $distance * math:sin($radians)" />
|
||||||
select="$y + $distance * math:sin($radians)" />
|
<!-- Création de l'élément MOVETO avec les nouvelles coordonnées calculées -->
|
||||||
<!-- Création de l'élément MOVETO avec les nouvelles coordonnées calculées -->
|
<MOVETO
|
||||||
<MOVETO
|
x="{format-number($new-x, '#.######')}"
|
||||||
x="{format-number($new-x, '#.######')}"
|
y="{format-number($new-y, '#.######')}" />
|
||||||
y="{format-number($new-y, '#.######')}" />
|
<!-- Rappel récursif du template avec la mise à jour des coordonnées et des
|
||||||
<!-- Rappel récursif du template avec la mise à jour des coordonnées et des
|
|
||||||
commandes restantes -->
|
commandes restantes -->
|
||||||
<xsl:call-template
|
<xsl:call-template name="process-commands">
|
||||||
name="process-commands">
|
<xsl:with-param name="commands" select="$commands[position() > 1]" />
|
||||||
<xsl:with-param name="commands" select="$commands[position() > 1]" />
|
<xsl:with-param
|
||||||
<xsl:with-param
|
name="x" select="$new-x" />
|
||||||
name="x" select="$new-x" />
|
<xsl:with-param name="y" select="$new-y" />
|
||||||
<xsl:with-param name="y" select="$new-y" />
|
<xsl:with-param
|
||||||
<xsl:with-param
|
name="angle" select="$angle" />
|
||||||
name="angle" select="$angle" />
|
<xsl:with-param name="states" select="$states" />
|
||||||
<xsl:with-param name="states"
|
</xsl:call-template>
|
||||||
select="$states" />
|
</xsl:when>
|
||||||
</xsl:call-template>
|
<!-- Si la commande est un store -->
|
||||||
</xsl:when>
|
<xsl:when test="local-name($current-command) = 'store'">
|
||||||
<!--Si
|
<!-- On enregistre le nouvelle état-->
|
||||||
la commande est un store -->
|
<xsl:variable name="new-state">
|
||||||
<xsl:when test="local-name($current-command) = 'store'">
|
<state x="{$x}" y="{$y}" angle="{$angle}" />
|
||||||
<!--On
|
</xsl:variable>
|
||||||
enregistre le nouvelle état-->
|
<!-- On l'ajoute au précédent -->
|
||||||
<xsl:variable
|
<xsl:variable
|
||||||
name="new-state">
|
name="new-states" select="($states, $new-state)" />
|
||||||
<state x="{$x}" y="{$y}" angle="{$angle}" />
|
<xsl:call-template
|
||||||
</xsl:variable>
|
name="process-commands">
|
||||||
<!--On
|
<xsl:with-param name="commands" select="$commands[position() > 1]" />
|
||||||
l'ajoute au précédent -->
|
<xsl:with-param
|
||||||
<xsl:variable
|
name="x" select="$x" />
|
||||||
name="new-states" select="($states, $new-state)" />
|
<xsl:with-param name="y" select="$y" />
|
||||||
<xsl:call-template
|
<xsl:with-param
|
||||||
name="process-commands">
|
name="angle" select="$angle" />
|
||||||
<xsl:with-param name="commands" select="$commands[position() > 1]" />
|
<xsl:with-param name="states" select="$new-states" />
|
||||||
<xsl:with-param
|
</xsl:call-template>
|
||||||
name="x" select="$x" />
|
</xsl:when>
|
||||||
<xsl:with-param name="y" select="$y" />
|
<!-- Si la commande est un restore et que au moins 1 état est enregistré : -->
|
||||||
<xsl:with-param
|
<xsl:when test="local-name($current-command) = 'restore' and count($states)>=1">
|
||||||
name="angle" select="$angle" />
|
<!-- On séléctionne notre state-->
|
||||||
<xsl:with-param name="states"
|
<xsl:variable
|
||||||
select="$new-states" />
|
name="state" select="$states/state[1]" />
|
||||||
</xsl:call-template>
|
<xsl:variable
|
||||||
</xsl:when>
|
name="remaining-states"
|
||||||
<!--Si
|
select="subsequence($states/state, 1, count($states/state) - 1)" />
|
||||||
la commande est un restore et que au moins 1 état est enregistré : -->
|
|
||||||
<xsl:when test="local-name($current-command) = 'restore' and count($states)>=1">
|
|
||||||
<!--On
|
|
||||||
séléctionne notre state-->
|
|
||||||
<xsl:variable
|
|
||||||
name="state" select="$states/state[1]" />
|
|
||||||
<xsl:variable
|
|
||||||
name="remaining-states"
|
|
||||||
select="subsequence($states/state, 1, count($states/state) - 1)" />
|
|
||||||
|
|
||||||
<xsl:call-template
|
<xsl:call-template
|
||||||
name="process-commands">
|
name="process-commands">
|
||||||
<xsl:with-param name="commands" select="$commands[position() > 1]" />
|
<xsl:with-param name="commands" select="$commands[position() > 1]" />
|
||||||
<xsl:with-param
|
<xsl:with-param
|
||||||
name="x" select="($state/@x)[last()]" />
|
name="x" select="($state/@x)[last()]" />
|
||||||
<xsl:with-param name="y"
|
<xsl:with-param name="y"
|
||||||
select="($state/@y)[last()]" />
|
select="($state/@y)[last()]" />
|
||||||
<xsl:with-param name="angle"
|
<xsl:with-param name="angle"
|
||||||
select="($state/@angle)[last()]" />
|
select="($state/@angle)[last()]" />
|
||||||
<xsl:with-param name="states"
|
<xsl:with-param name="states"
|
||||||
select="$remaining-states" />
|
select="$remaining-states" />
|
||||||
</xsl:call-template>
|
</xsl:call-template>
|
||||||
</xsl:when>
|
</xsl:when>
|
||||||
|
</xsl:choose>
|
||||||
</xsl:choose>
|
</xsl:if>
|
||||||
</xsl:if>
|
</xsl:template>
|
||||||
</xsl:template>
|
|
||||||
</xsl:transform>
|
</xsl:transform>
|
||||||
|
|
Reference in a new issue