<commit_before><!--
    Copyright (C) 2008 Orbeon, Inc.

    This program is free software; you can redistribute it and/or modify it under the terms of the
    GNU Lesser General Public License as published by the Free Software Foundation; either version
    2.1 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 Lesser General Public License for more details.

    The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
-->
<xsl:stylesheet
    xmlns:p="http://www.orbeon.com/oxf/pipeline"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xh="http://www.w3.org/1999/xhtml"
    xmlns:frf="java:org.orbeon.oxf.fr.FormRunner"
    xmlns:fr="http://orbeon.org/oxf/xml/form-runner"
    xmlns:metadata="java:org.orbeon.oxf.fr.FormRunnerMetadata"

    version="2.0">

    <xsl:variable name="data"         select="/*"                          as="element()"/>
    <xsl:variable name="xhtml"        select="doc('input:xhtml')/*"        as="element(xh:html)"/>
    <xsl:variable name="request"      select="doc('input:request')/*"      as="element(request)"/>
    <xsl:variable name="fr-resources" select="doc('input:fr-resources')/*" as="element(resources)"/>
    <xsl:variable name="attachments"  select="doc('input:attachments')/*"  as="element(attachments)"/>

    <xsl:variable name="metadata"     select="frf:metadataInstanceRootOpt($xhtml)" as="element(metadata)"/>

    <!-- Language requested -->
    <xsl:variable
        name="request-language"
        select="$request/parameters/parameter[name = 'fr-language']/value"
        as="xs:string"/>

    <!-- App and form -->
    <xsl:variable name="app"  select="doc('input:parameters')/*/app"  as="xs:string"/>
    <xsl:variable name="form" select="doc('input:parameters')/*/form" as="xs:string"/>

    <xsl:function name="fr:find-emails" as="xs:string*">

        <xsl:param name="class-name"    as="xs:string"/>
        <xsl:param name="property-name" as="xs:string"/>

        <xsl:sequence
            select="
                distinct-values(
                    for $holder in
                        (
                            frf:searchHoldersForClassTopLevelOnly       (                $xhtml/xh:body, $data, $class-name),
                            frf:searchHoldersForClassUseSectionTemplates($xhtml/xh:head, $xhtml/xh:body, $data, $class-name),
                            p:property(string-join(($property-name, $app, $form), '.'))
                        )
                    return
                        for $raw-email in
                            tokenize(
                                $holder,
                                '(,|\s)\s*'
                            )
                        return
                            normalize-space($raw-email)[. != '']
                )
            "
        />
    </xsl:function>

    <xsl:function name="fr:build-message" as="xs:string">

        <xsl:param name="type"    as="xs:string"/>
        <xsl:param name="is-html" as="xs:boolean"/>

        <xsl:variable name="metadata-elem" select="$metadata/email/*[local-name() = $type]"/>
        <xsl:variable name="resource-elem" select="$fr-resources/resource[@xml:lang = $request-language]/email/*[local-name() = $type]"/>

        <xsl:choose>
            <xsl:when test="exists($metadata-elem/template) and empty($metadata-elem/fr:param)">
                <!-- Just a template -->
                <xsl:value-of select="$metadata-elem/template[@xml:lang = $request-language]"/>
            </xsl:when>
            <xsl:when test="exists($metadata-elem/fr:param)">
                <!-- Parameters to a template -->
                <xsl:variable
                    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
                    xmlns:saxon="http://saxon.sf.net/"
                    name="string-value"
                    select="
                        p:process-template(
                            (
                                $metadata-elem/template[@xml:lang = $request-language],
                                $resource-elem
                            )[1],
                            'en',
                            map:merge(
                                for $p in $metadata-elem/fr:param
                                return
                                    map:entry(
                                        $p/fr:name,
                                        if ($p/@type = 'ExpressionParam') then
                                            string(($data/saxon:evaluate($p/fr:expr))[1])   (: NOTE: Form Runner function library is not in scope. :)
                                        else if ($p/@type = 'ControlValueParam') then
                                            error()                                         (: TODO: Not implemented yet. :)
                                        else if ($p/@type = 'AllControlValuesParam') then
                                            metadata:findAllControlsWithValues($is-html)
                                        else
                                            error()
                                    )
                            )
                        )
                    "/>

                <xsl:value-of select="concat('', $string-value, '')"/>

            </xsl:when>
            <xsl:otherwise>
                <!-- Use the value from resources or properties -->
                <xsl:value-of select="$resource-elem"/>
            </xsl:otherwise><commit_msg>For #2428: remove unneeded `concat()`<commit_after><!--
    Copyright (C) 2008 Orbeon, Inc.

    This program is free software; you can redistribute it and/or modify it under the terms of the
    GNU Lesser General Public License as published by the Free Software Foundation; either version
    2.1 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 Lesser General Public License for more details.

    The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
-->
<xsl:stylesheet
    xmlns:p="http://www.orbeon.com/oxf/pipeline"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xh="http://www.w3.org/1999/xhtml"
    xmlns:frf="java:org.orbeon.oxf.fr.FormRunner"
    xmlns:fr="http://orbeon.org/oxf/xml/form-runner"
    xmlns:metadata="java:org.orbeon.oxf.fr.FormRunnerMetadata"

    version="2.0">

    <xsl:variable name="data"         select="/*"                          as="element()"/>
    <xsl:variable name="xhtml"        select="doc('input:xhtml')/*"        as="element(xh:html)"/>
    <xsl:variable name="request"      select="doc('input:request')/*"      as="element(request)"/>
    <xsl:variable name="fr-resources" select="doc('input:fr-resources')/*" as="element(resources)"/>
    <xsl:variable name="attachments"  select="doc('input:attachments')/*"  as="element(attachments)"/>

    <xsl:variable name="metadata"     select="frf:metadataInstanceRootOpt($xhtml)" as="element(metadata)"/>

    <!-- Language requested -->
    <xsl:variable
        name="request-language"
        select="$request/parameters/parameter[name = 'fr-language']/value"
        as="xs:string"/>

    <!-- App and form -->
    <xsl:variable name="app"  select="doc('input:parameters')/*/app"  as="xs:string"/>
    <xsl:variable name="form" select="doc('input:parameters')/*/form" as="xs:string"/>

    <xsl:function name="fr:find-emails" as="xs:string*">

        <xsl:param name="class-name"    as="xs:string"/>
        <xsl:param name="property-name" as="xs:string"/>

        <xsl:sequence
            select="
                distinct-values(
                    for $holder in
                        (
                            frf:searchHoldersForClassTopLevelOnly       (                $xhtml/xh:body, $data, $class-name),
                            frf:searchHoldersForClassUseSectionTemplates($xhtml/xh:head, $xhtml/xh:body, $data, $class-name),
                            p:property(string-join(($property-name, $app, $form), '.'))
                        )
                    return
                        for $raw-email in
                            tokenize(
                                $holder,
                                '(,|\s)\s*'
                            )
                        return
                            normalize-space($raw-email)[. != '']
                )
            "
        />
    </xsl:function>

    <xsl:function name="fr:build-message" as="xs:string">

        <xsl:param name="type"    as="xs:string"/>
        <xsl:param name="is-html" as="xs:boolean"/>

        <xsl:variable name="metadata-elem" select="$metadata/email/*[local-name() = $type]"/>
        <xsl:variable name="resource-elem" select="$fr-resources/resource[@xml:lang = $request-language]/email/*[local-name() = $type]"/>

        <xsl:choose>
            <xsl:when test="exists($metadata-elem/template) and empty($metadata-elem/fr:param)">
                <!-- Just a template -->
                <xsl:value-of select="$metadata-elem/template[@xml:lang = $request-language]"/>
            </xsl:when>
            <xsl:when test="exists($metadata-elem/fr:param)">

                <!-- Parameters to a template -->
                <xsl:variable
                    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
                    xmlns:saxon="http://saxon.sf.net/"
                    name="string-value"
                    select="
                        p:process-template(
                            (
                                $metadata-elem/template[@xml:lang = $request-language],
                                $resource-elem
                            )[1],
                            'en',
                            map:merge(
                                for $p in $metadata-elem/fr:param
                                return
                                    map:entry(
                                        $p/fr:name,
                                        if ($p/@type = 'ExpressionParam') then
                                            string(($data/saxon:evaluate($p/fr:expr))[1])   (: NOTE: Form Runner function library is not in scope. :)
                                        else if ($p/@type = 'ControlValueParam') then
                                            error()                                         (: TODO: Not implemented yet. :)
                                        else if ($p/@type = 'AllControlValuesParam') then
                                            metadata:findAllControlsWithValues($is-html)
                                        else
                                            error()
                                    )
                            )
                        )
                    "/>

                <xsl:value-of select="$string-value"/>

            </xsl:when>
            <xsl:otherwise>
                <!-- Use the value from resources or properties -->
                <xsl:value-of select="$resource-elem"/>
            </xsl:otherwise>
{'fraction_non_alphanumeric': 0.13635969851858268, 'fraction_numerical': 0.005544485835571342, 'mean_word_length': 1.4467994913098772, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 12, 'xml': 26}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2368948', 'n_tokens_mistral': 3125, 'n_tokens_neox': 2876, 'n_words': 574}