Search in sources :

Example 1 with PlainTextOutputFormat

use of freemarker.core.PlainTextOutputFormat in project freemarker by apache.

the class Configuration method setRegisteredCustomOutputFormats.

/**
 * Sets the custom output formats that can be referred by their unique name ({@link OutputFormat#getName()}) from
 * templates. Names are also used to look up the {@link OutputFormat} for standard file extensions; see them at
 * {@link #setRecognizeStandardFileExtensions(boolean)}.
 *
 * <p>
 * When there's a clash between a custom output format name and a standard output format name, the custom format
 * will win, thus you can override the meaning of standard output format names. Except, it's not allowed to override
 * {@link UndefinedOutputFormat} and {@link PlainTextOutputFormat}.
 *
 * <p>
 * The default value is an empty collection.
 *
 * @param registeredCustomOutputFormats
 *            The collection of the {@link OutputFormat}-s, each must be different and has a unique name (
 *            {@link OutputFormat#getName()}) within this collection.
 *
 * @throws IllegalArgumentException
 *             When multiple different {@link OutputFormat}-s have the same name in the parameter collection. When
 *             the same {@link OutputFormat} object occurs for multiple times in the collection. If an
 *             {@link OutputFormat} name is 0 long. If an {@link OutputFormat} name doesn't start with letter or
 *             digit. If an {@link OutputFormat} name contains {@code '+'} or <code>'{'</code> or <code>'}'</code>.
 *             If an {@link OutputFormat} name equals to {@link UndefinedOutputFormat#getName()} or
 *             {@link PlainTextOutputFormat#getName()}.
 *
 * @since 2.3.24
 */
public void setRegisteredCustomOutputFormats(Collection<? extends OutputFormat> registeredCustomOutputFormats) {
    NullArgumentException.check(registeredCustomOutputFormats);
    Map<String, OutputFormat> m = new LinkedHashMap<String, OutputFormat>(registeredCustomOutputFormats.size() * 4 / 3, 1f);
    for (OutputFormat outputFormat : registeredCustomOutputFormats) {
        String name = outputFormat.getName();
        if (name.equals(UndefinedOutputFormat.INSTANCE.getName())) {
            throw new IllegalArgumentException("The \"" + name + "\" output format can't be redefined");
        }
        if (name.equals(PlainTextOutputFormat.INSTANCE.getName())) {
            throw new IllegalArgumentException("The \"" + name + "\" output format can't be redefined");
        }
        if (name.length() == 0) {
            throw new IllegalArgumentException("The output format name can't be 0 long");
        }
        if (!Character.isLetterOrDigit(name.charAt(0))) {
            throw new IllegalArgumentException("The output format name must start with letter or digit: " + name);
        }
        if (name.indexOf('+') != -1) {
            throw new IllegalArgumentException("The output format name can't contain \"+\" character: " + name);
        }
        if (name.indexOf('{') != -1) {
            throw new IllegalArgumentException("The output format name can't contain \"{\" character: " + name);
        }
        if (name.indexOf('}') != -1) {
            throw new IllegalArgumentException("The output format name can't contain \"}\" character: " + name);
        }
        OutputFormat replaced = m.put(outputFormat.getName(), outputFormat);
        if (replaced != null) {
            if (replaced == outputFormat) {
                throw new IllegalArgumentException("Duplicate output format in the collection: " + outputFormat);
            }
            throw new IllegalArgumentException("Clashing output format names between " + replaced + " and " + outputFormat + ".");
        }
    }
    this.registeredCustomOutputFormats = Collections.unmodifiableMap(m);
    clearTemplateCache();
}
Also used : UndefinedOutputFormat(freemarker.core.UndefinedOutputFormat) CSSOutputFormat(freemarker.core.CSSOutputFormat) HTMLOutputFormat(freemarker.core.HTMLOutputFormat) CombinedMarkupOutputFormat(freemarker.core.CombinedMarkupOutputFormat) JavaScriptOutputFormat(freemarker.core.JavaScriptOutputFormat) OutputFormat(freemarker.core.OutputFormat) XMLOutputFormat(freemarker.core.XMLOutputFormat) MarkupOutputFormat(freemarker.core.MarkupOutputFormat) PlainTextOutputFormat(freemarker.core.PlainTextOutputFormat) XHTMLOutputFormat(freemarker.core.XHTMLOutputFormat) RTFOutputFormat(freemarker.core.RTFOutputFormat) JSONOutputFormat(freemarker.core.JSONOutputFormat) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

CSSOutputFormat (freemarker.core.CSSOutputFormat)1 CombinedMarkupOutputFormat (freemarker.core.CombinedMarkupOutputFormat)1 HTMLOutputFormat (freemarker.core.HTMLOutputFormat)1 JSONOutputFormat (freemarker.core.JSONOutputFormat)1 JavaScriptOutputFormat (freemarker.core.JavaScriptOutputFormat)1 MarkupOutputFormat (freemarker.core.MarkupOutputFormat)1 OutputFormat (freemarker.core.OutputFormat)1 PlainTextOutputFormat (freemarker.core.PlainTextOutputFormat)1 RTFOutputFormat (freemarker.core.RTFOutputFormat)1 UndefinedOutputFormat (freemarker.core.UndefinedOutputFormat)1 XHTMLOutputFormat (freemarker.core.XHTMLOutputFormat)1 XMLOutputFormat (freemarker.core.XMLOutputFormat)1 LinkedHashMap (java.util.LinkedHashMap)1