tame/src/current/include/util.xsl

204 lines
5.5 KiB
XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Utility templates
Copyright (C) 2016 R-T Specialty, LLC.
This file is part of TAME.
TAME is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
<http://www.gnu.org/licenses/>.
-->
<xsl:stylesheet version="1.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:util="http://www.lovullo.com/util"
xmlns:lv="http://www.lovullo.com/rater"
xmlns:lvm="http://www.lovullo.com/rater/map"
xmlns:c="http://www.lovullo.com/calc"
xmlns:ext="http://www.lovullo.com/ext">
<xsl:variable name="_chrlower" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="_chrupper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<!--
Path to map directory
-->
<xsl:param name="map-root" select="'../map/'" />
<xsl:param name="retmap-root" select="concat( $map-root, 'return/' )" />
<xsl:template name="util:load-package">
<xsl:param name="package" />
<xsl:param name="self" />
<xsl:variable name="path" select="concat($package, '.xml')" />
<xsl:variable name="pkg" select="document( $path, $self )/lv:package" />
<ext:package name="{$pkg/@name}">
<!-- path, including extension -->
<xsl:attribute name="path">
<xsl:value-of select="$path" />
</xsl:attribute>
<!-- path, excluding extension (as it appears in @package) -->
<xsl:attribute name="import-path">
<xsl:value-of select="@package" />
</xsl:attribute>
<xsl:copy-of select="$pkg" />
</ext:package>
</xsl:template>
<xsl:template match="lvm:*" mode="util:map-expand" priority="1">
<xsl:param name="path" select="'.'" />
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates mode="util:map-expand">
<xsl:with-param name="path" select="$path" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<!-- recursively inline imports -->
<xsl:template match="lvm:import" mode="util:map-expand" priority="5">
<xsl:param name="path" select="'.'" />
<xsl:apply-templates
select="document( concat( @path, '.xml' ), . )/lvm:*/*"
mode="util:map-expand">
<xsl:with-param name="path" select="concat( $path, '/', @path )" />
</xsl:apply-templates>
</xsl:template>
<!-- must be lower priority than lv:import -->
<xsl:template match="/lvm:*/lvm:*" mode="util:map-expand" priority="3">
<xsl:param name="path" select="'.'" />
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:attribute name="__src" select="$path" />
<xsl:apply-templates mode="util:map-expand">
<xsl:with-param name="path" select="$path" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<!--
Converts first character to uppercase
Functions like ucfirst in PHP
@param string str string to ucfirst
@return string provided string with first character converted to uppercase
-->
<xsl:template name="util:ucfirst">
<xsl:param name="str" />
<!-- convert first char to uppercase -->
<xsl:value-of
select="translate( substring( $str, 1, 1 ), $_chrlower, $_chrupper )" />
<!-- remainder of string as it was provided -->
<xsl:value-of select="substring( $str, 2 )" />
</xsl:template>
<!--
Converts a string to uppercase
-->
<xsl:template name="util:uppercase">
<xsl:param name="str" />
<xsl:value-of select="translate( $str, $_chrlower, $_chrupper )" />
</xsl:template>
<!--
Converts a string to lowercase
-->
<xsl:template name="util:lowercase">
<xsl:param name="str" />
<xsl:value-of select="translate( $str, $_chrupper, $_chrlower )" />
</xsl:template>
<xsl:template name="util:json">
<xsl:param name="id" />
<xsl:param name="value" />
<xsl:param name="obj" />
<xsl:param name="array" />
<xsl:if test="$id">
<xsl:text>"</xsl:text>
<xsl:call-template name="util:json-escape">
<xsl:with-param name="string" select="$id" />
</xsl:call-template>
<xsl:text>":</xsl:text>
</xsl:if>
<xsl:choose>
<xsl:when test="$array">
<xsl:text>[</xsl:text>
<xsl:for-each select="$array/*">
<xsl:if test="position() > 1">
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:value-of select="." />
</xsl:for-each>
<xsl:text>]</xsl:text>
</xsl:when>
<xsl:when test="$obj">
<xsl:text>{</xsl:text>
<xsl:for-each select="$obj/*">
<xsl:if test="position() > 1">
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:value-of select="." />
</xsl:for-each>
<xsl:text>}</xsl:text>
</xsl:when>
<xsl:when test="$value">
<xsl:text>"</xsl:text>
<xsl:call-template name="util:json-escape">
<xsl:with-param name="string" select="$value" />
</xsl:call-template>
<xsl:text>"</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="yes">[util] !!! invalid util:json</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="util:json-escape">
<xsl:param name="string" />
<xsl:value-of select="$string" />
</xsl:template>
</xsl:stylesheet>