Как вложить ту же настраиваемую директиву в FreeMarker? - PullRequest
0 голосов
/ 07 августа 2020

Это мой шаблон:

<@base64>head://<@base64>{"add":"addr","aid":"0","host":"sefse.sdf.xyz"}</@base64></@base64>

Это моя директива:

class Base64Directive : TemplateDirectiveModel {


    @Throws(TemplateException::class, IOException::class)
    override fun execute(env: Environment,
                         params: Map<*, *>, loopVars: Array<TemplateModel?>,
                         body: TemplateDirectiveBody?) { // Check if no parameters were given:
        if (!params.isEmpty()) {
            throw TemplateModelException(
                    "This directive doesn't allow parameters.")
        }
        if (loopVars.size != 0) {
            throw TemplateModelException(
                    "This directive doesn't allow loop variables.")
        }
        // If there is non-empty nested content:
        if (body != null) {
            // Executes the nested body. Same as <#nested> in FTL, except
            // that we use our own writer instead of the current output writer.
            body.render(Base64FilterWriter(env.out))
        } else {
            throw RuntimeException("missing body")
        }
    }

    /**
     * A [Writer] that transforms the character stream to upper case
     * and forwards it to another [Writer].
     */
    private class Base64FilterWriter internal constructor(out: Writer) : Writer() {
        private val out: Writer = out
        @Throws(IOException::class)
        override fun write(cbuf: CharArray, off: Int, len: Int) {
            var transformedCbuf = String(Base64.getEncoder().encode(String(cbuf).toByteArray(Charsets.UTF_8).clone()))
            out.write(transformedCbuf)
        }

        @Throws(IOException::class)
        override fun flush() {
            out.flush()
        }

        @Throws(IOException::class)
        override fun close() {
            out.close()
        }

    }
}

Есть две директивы, которые вложены друг в друга. Когда я запускаю свой код, он не может скомпилировать пару директив изнутри (<@ base64> {"add": "addr", "aid": "0", "host": "sefse.sdf.xyz"} / @ base64) наружу (<@ base64> head: // base64-after / @ base64).

Как я могу заставить эту компиляцию работать изнутри наружу?

...