Search in sources :

Example 1 with UnregisteredOutputFormatException

use of freemarker.core.UnregisteredOutputFormatException 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)

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