Search in sources :

Example 1 with IStandardJavaScriptSerializer

use of org.thymeleaf.standard.serializer.IStandardJavaScriptSerializer in project thymeleaf by thymeleaf.

the class LazyEscapingCharSequence method produceEscapedOutput.

private void produceEscapedOutput(final Writer writer) {
    /*
         * Producing ESCAPED output is somewhat simple in HTML or XML modes, as it simply consists of converting
         * input into a String and HTML-or-XML-escaping it.
         *
         * But for JavaScript or CSS, it becomes a bit more complicated than that. JavaScript will output a complete
         * literal (incl. single quotes) if input is a String or a non-recognized value type, but will print input
         * as an object, number, boolean, etc. if it is recognized to be of one of these types. CSS will output
         * quoted literals.
         *
         * Note that, when in TEXT mode, HTML escaping will be used (as TEXT is many times used for
         * processing HTML templates)
         */
    try {
        switch(templateMode) {
            case TEXT:
            // fall-through
            case HTML:
                if (this.input != null) {
                    HtmlEscape.escapeHtml4Xml(this.input.toString(), writer);
                }
                return;
            case XML:
                if (this.input != null) {
                    // Note we are outputting a body content here, so it is important that we use the version
                    // of XML escaping meant for content, not attributes (slight differences)
                    XmlEscape.escapeXml10(this.input.toString(), writer);
                }
                return;
            case JAVASCRIPT:
                final IStandardJavaScriptSerializer javaScriptSerializer = StandardSerializers.getJavaScriptSerializer(this.configuration);
                javaScriptSerializer.serializeValue(this.input, writer);
                return;
            case CSS:
                final IStandardCSSSerializer cssSerializer = StandardSerializers.getCSSSerializer(this.configuration);
                cssSerializer.serializeValue(this.input, writer);
                return;
            case RAW:
                if (this.input != null) {
                    writer.write(this.input.toString());
                }
                return;
            default:
                throw new TemplateProcessingException("Unrecognized template mode " + templateMode + ". Cannot produce escaped output for " + "this template mode.");
        }
    } catch (final IOException e) {
        throw new TemplateProcessingException("An error happened while trying to produce escaped output", e);
    }
}
Also used : IStandardJavaScriptSerializer(org.thymeleaf.standard.serializer.IStandardJavaScriptSerializer) IStandardCSSSerializer(org.thymeleaf.standard.serializer.IStandardCSSSerializer) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)1 IStandardCSSSerializer (org.thymeleaf.standard.serializer.IStandardCSSSerializer)1 IStandardJavaScriptSerializer (org.thymeleaf.standard.serializer.IStandardJavaScriptSerializer)1