Search in sources :

Example 1 with MarkupOutputFormat

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

the class Configuration method getOutputFormat.

/**
 * Returns the output format for a name.
 *
 * @param name
 *            Either the name of the output format as it was registered with
 *            {@link Configuration#setRegisteredCustomOutputFormats(Collection)}, or a combined output format name.
 *            A combined output format is created ad-hoc from the registered formats. For example, if you need RTF
 *            embedded into HTML, the name will be <code>HTML{RTF}</code>, where "HTML" and "RTF" refer to the
 *            existing formats. This logic can be used recursively, so for example <code>XML{HTML{RTF}}</code> is
 *            also valid.
 *
 * @return Not {@code null}.
 *
 * @throws UnregisteredOutputFormatException
 *             If there's no output format registered with the given name.
 * @throws IllegalArgumentException
 *             If the usage of <code>{</code> and <code>}</code> in the name is syntactically wrong, or if not all
 *             {@link OutputFormat}-s are {@link MarkupOutputFormat}-s in the <code>...{...}</code> expression.
 *
 * @since 2.3.24
 */
public OutputFormat getOutputFormat(String name) throws UnregisteredOutputFormatException {
    if (name.length() == 0) {
        throw new IllegalArgumentException("0-length format name");
    }
    if (name.charAt(name.length() - 1) == '}') {
        // Combined markup
        int openBrcIdx = name.indexOf('{');
        if (openBrcIdx == -1) {
            throw new IllegalArgumentException("Missing opening '{' in: " + name);
        }
        MarkupOutputFormat outerOF = getMarkupOutputFormatForCombined(name.substring(0, openBrcIdx));
        MarkupOutputFormat innerOF = getMarkupOutputFormatForCombined(name.substring(openBrcIdx + 1, name.length() - 1));
        return new CombinedMarkupOutputFormat(name, outerOF, innerOF);
    } else {
        OutputFormat custOF = registeredCustomOutputFormats.get(name);
        if (custOF != null) {
            return custOF;
        }
        OutputFormat stdOF = STANDARD_OUTPUT_FORMATS.get(name);
        if (stdOF == null) {
            StringBuilder sb = new StringBuilder();
            sb.append("Unregistered output format name, ");
            sb.append(StringUtil.jQuote(name));
            sb.append(". The output formats registered in the Configuration are: ");
            Set<String> registeredNames = new TreeSet<String>();
            registeredNames.addAll(STANDARD_OUTPUT_FORMATS.keySet());
            registeredNames.addAll(registeredCustomOutputFormats.keySet());
            boolean first = true;
            for (String registeredName : registeredNames) {
                if (first) {
                    first = false;
                } else {
                    sb.append(", ");
                }
                sb.append(StringUtil.jQuote(registeredName));
            }
            throw new UnregisteredOutputFormatException(sb.toString());
        }
        return stdOF;
    }
}
Also used : UnregisteredOutputFormatException(freemarker.core.UnregisteredOutputFormatException) TreeSet(java.util.TreeSet) 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) CombinedMarkupOutputFormat(freemarker.core.CombinedMarkupOutputFormat) MarkupOutputFormat(freemarker.core.MarkupOutputFormat) CombinedMarkupOutputFormat(freemarker.core.CombinedMarkupOutputFormat)

Example 2 with MarkupOutputFormat

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

the class Configuration method getMarkupOutputFormatForCombined.

private MarkupOutputFormat getMarkupOutputFormatForCombined(String outerName) throws UnregisteredOutputFormatException {
    OutputFormat of = getOutputFormat(outerName);
    if (!(of instanceof MarkupOutputFormat)) {
        throw new IllegalArgumentException("The \"" + outerName + "\" output format can't be used in " + "...{...} expression, because it's not a markup format.");
    }
    MarkupOutputFormat outerOF = (MarkupOutputFormat) of;
    return outerOF;
}
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) CombinedMarkupOutputFormat(freemarker.core.CombinedMarkupOutputFormat) MarkupOutputFormat(freemarker.core.MarkupOutputFormat)

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 UnregisteredOutputFormatException (freemarker.core.UnregisteredOutputFormatException)1 TreeSet (java.util.TreeSet)1