Search in sources :

Example 1 with UndefinedOutputFormat

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

the class Configuration method setOutputFormat.

/**
 * Sets the default output format. Usually, you should leave this on its default, which is
 * {@link UndefinedOutputFormat#INSTANCE}, and then use standard file extensions like "ftlh" (for HTML) or "ftlx"
 * (for XML) and ensure that {@link #setRecognizeStandardFileExtensions(boolean)} is {@code true} (see more there).
 * Where you can't use the standard extensions, templates still can be associated to output formats with
 * patterns matching their name (their path) using {@link #setTemplateConfigurations(TemplateConfigurationFactory)}.
 * But if all templates will have the same output format, you may use {@link #setOutputFormat(OutputFormat)} after
 * all, to a value like {@link HTMLOutputFormat#INSTANCE}, {@link XMLOutputFormat#INSTANCE}, etc. Also note
 * that templates can specify their own output format like {@code
 * <#ftl output_format="HTML">}, which overrides any configuration settings.
 *
 * <p>
 * The output format is mostly important because of auto-escaping (see {@link #setAutoEscapingPolicy(int)}), but
 * maybe also used by the embedding application to set the HTTP response MIME type, etc.
 *
 * @see #setRegisteredCustomOutputFormats(Collection)
 * @see #setTemplateConfigurations(TemplateConfigurationFactory)
 * @see #setRecognizeStandardFileExtensions(boolean)
 * @see #setAutoEscapingPolicy(int)
 *
 * @since 2.3.24
 */
public void setOutputFormat(OutputFormat outputFormat) {
    if (outputFormat == null) {
        throw new NullArgumentException("outputFormat", "You may meant: " + UndefinedOutputFormat.class.getSimpleName() + ".INSTANCE");
    }
    OutputFormat prevOutputFormat = getOutputFormat();
    this.outputFormat = outputFormat;
    outputFormatExplicitlySet = true;
    if (prevOutputFormat != outputFormat) {
        clearTemplateCache();
    }
}
Also used : UndefinedOutputFormat(freemarker.core.UndefinedOutputFormat) 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) NullArgumentException(freemarker.template.utility.NullArgumentException)

Example 2 with UndefinedOutputFormat

use of freemarker.core.UndefinedOutputFormat 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)2 CombinedMarkupOutputFormat (freemarker.core.CombinedMarkupOutputFormat)2 HTMLOutputFormat (freemarker.core.HTMLOutputFormat)2 JSONOutputFormat (freemarker.core.JSONOutputFormat)2 JavaScriptOutputFormat (freemarker.core.JavaScriptOutputFormat)2 MarkupOutputFormat (freemarker.core.MarkupOutputFormat)2 OutputFormat (freemarker.core.OutputFormat)2 PlainTextOutputFormat (freemarker.core.PlainTextOutputFormat)2 RTFOutputFormat (freemarker.core.RTFOutputFormat)2 UndefinedOutputFormat (freemarker.core.UndefinedOutputFormat)2 XHTMLOutputFormat (freemarker.core.XHTMLOutputFormat)2 XMLOutputFormat (freemarker.core.XMLOutputFormat)2 NullArgumentException (freemarker.template.utility.NullArgumentException)1 LinkedHashMap (java.util.LinkedHashMap)1