use of freemarker.core.OutputFormat in project freemarker by apache.
the class Configuration method setSetting.
@Override
public void setSetting(String name, String value) throws TemplateException {
boolean unknown = false;
try {
if ("TemplateUpdateInterval".equalsIgnoreCase(name)) {
name = TEMPLATE_UPDATE_DELAY_KEY;
} else if ("DefaultEncoding".equalsIgnoreCase(name)) {
name = DEFAULT_ENCODING_KEY;
}
if (DEFAULT_ENCODING_KEY_SNAKE_CASE.equals(name) || DEFAULT_ENCODING_KEY_CAMEL_CASE.equals(name)) {
if (JVM_DEFAULT.equalsIgnoreCase(value)) {
setDefaultEncoding(getJVMDefaultEncoding());
} else {
setDefaultEncoding(value);
}
} else if (LOCALIZED_LOOKUP_KEY_SNAKE_CASE.equals(name) || LOCALIZED_LOOKUP_KEY_CAMEL_CASE.equals(name)) {
setLocalizedLookup(StringUtil.getYesNo(value));
} else if (STRICT_SYNTAX_KEY_SNAKE_CASE.equals(name) || STRICT_SYNTAX_KEY_CAMEL_CASE.equals(name)) {
setStrictSyntaxMode(StringUtil.getYesNo(value));
} else if (WHITESPACE_STRIPPING_KEY_SNAKE_CASE.equals(name) || WHITESPACE_STRIPPING_KEY_CAMEL_CASE.equals(name)) {
setWhitespaceStripping(StringUtil.getYesNo(value));
} else if (AUTO_ESCAPING_POLICY_KEY_SNAKE_CASE.equals(name) || AUTO_ESCAPING_POLICY_KEY_CAMEL_CASE.equals(name)) {
if ("enable_if_default".equals(value) || "enableIfDefault".equals(value)) {
setAutoEscapingPolicy(ENABLE_IF_DEFAULT_AUTO_ESCAPING_POLICY);
} else if ("enable_if_supported".equals(value) || "enableIfSupported".equals(value)) {
setAutoEscapingPolicy(ENABLE_IF_SUPPORTED_AUTO_ESCAPING_POLICY);
} else if ("disable".equals(value)) {
setAutoEscapingPolicy(DISABLE_AUTO_ESCAPING_POLICY);
} else {
throw invalidSettingValueException(name, value);
}
} else if (OUTPUT_FORMAT_KEY_SNAKE_CASE.equals(name) || OUTPUT_FORMAT_KEY_CAMEL_CASE.equals(name)) {
if (value.equalsIgnoreCase(DEFAULT)) {
unsetOutputFormat();
} else {
OutputFormat stdOF = STANDARD_OUTPUT_FORMATS.get(value);
setOutputFormat(stdOF != null ? stdOF : (OutputFormat) _ObjectBuilderSettingEvaluator.eval(value, OutputFormat.class, true, _SettingEvaluationEnvironment.getCurrent()));
}
} else if (REGISTERED_CUSTOM_OUTPUT_FORMATS_KEY_SNAKE_CASE.equals(name) || REGISTERED_CUSTOM_OUTPUT_FORMATS_KEY_CAMEL_CASE.equals(name)) {
List list = (List) _ObjectBuilderSettingEvaluator.eval(value, List.class, true, _SettingEvaluationEnvironment.getCurrent());
for (Object item : list) {
if (!(item instanceof OutputFormat)) {
throw new _MiscTemplateException(getEnvironment(), "Invalid value for setting ", new _DelayedJQuote(name), ": List items must be " + OutputFormat.class.getName() + " instances, in: ", value);
}
}
setRegisteredCustomOutputFormats(list);
} else if (RECOGNIZE_STANDARD_FILE_EXTENSIONS_KEY_SNAKE_CASE.equals(name) || RECOGNIZE_STANDARD_FILE_EXTENSIONS_KEY_CAMEL_CASE.equals(name)) {
if (value.equalsIgnoreCase(DEFAULT)) {
unsetRecognizeStandardFileExtensions();
} else {
setRecognizeStandardFileExtensions(StringUtil.getYesNo(value));
}
} else if (CACHE_STORAGE_KEY_SNAKE_CASE.equals(name) || CACHE_STORAGE_KEY_CAMEL_CASE.equals(name)) {
if (value.equalsIgnoreCase(DEFAULT)) {
unsetCacheStorage();
}
if (value.indexOf('.') == -1) {
int strongSize = 0;
int softSize = 0;
Map map = StringUtil.parseNameValuePairList(value, String.valueOf(Integer.MAX_VALUE));
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry ent = (Map.Entry) it.next();
String pname = (String) ent.getKey();
int pvalue;
try {
pvalue = Integer.parseInt((String) ent.getValue());
} catch (NumberFormatException e) {
throw invalidSettingValueException(name, value);
}
if ("soft".equalsIgnoreCase(pname)) {
softSize = pvalue;
} else if ("strong".equalsIgnoreCase(pname)) {
strongSize = pvalue;
} else {
throw invalidSettingValueException(name, value);
}
}
if (softSize == 0 && strongSize == 0) {
throw invalidSettingValueException(name, value);
}
setCacheStorage(new MruCacheStorage(strongSize, softSize));
} else {
setCacheStorage((CacheStorage) _ObjectBuilderSettingEvaluator.eval(value, CacheStorage.class, false, _SettingEvaluationEnvironment.getCurrent()));
}
} else if (TEMPLATE_UPDATE_DELAY_KEY_SNAKE_CASE.equals(name) || TEMPLATE_UPDATE_DELAY_KEY_CAMEL_CASE.equals(name)) {
long multipier;
String valueWithoutUnit;
if (value.endsWith("ms")) {
multipier = 1;
valueWithoutUnit = rightTrim(value.substring(0, value.length() - 2));
} else if (value.endsWith("s")) {
multipier = 1000;
valueWithoutUnit = rightTrim(value.substring(0, value.length() - 1));
} else if (value.endsWith("m")) {
multipier = 1000 * 60;
valueWithoutUnit = rightTrim(value.substring(0, value.length() - 1));
} else if (value.endsWith("h")) {
multipier = 1000 * 60 * 60;
valueWithoutUnit = rightTrim(value.substring(0, value.length() - 1));
} else {
// Default is seconds for backward compatibility
multipier = 1000;
valueWithoutUnit = value;
}
setTemplateUpdateDelayMilliseconds(Integer.parseInt(valueWithoutUnit) * multipier);
} else if (TAG_SYNTAX_KEY_SNAKE_CASE.equals(name) || TAG_SYNTAX_KEY_CAMEL_CASE.equals(name)) {
if ("auto_detect".equals(value) || "autoDetect".equals(value)) {
setTagSyntax(AUTO_DETECT_TAG_SYNTAX);
} else if ("angle_bracket".equals(value) || "angleBracket".equals(value)) {
setTagSyntax(ANGLE_BRACKET_TAG_SYNTAX);
} else if ("square_bracket".equals(value) || "squareBracket".equals(value)) {
setTagSyntax(SQUARE_BRACKET_TAG_SYNTAX);
} else {
throw invalidSettingValueException(name, value);
}
} else if (INTERPOLATION_SYNTAX_KEY_SNAKE_CASE.equals(name) || INTERPOLATION_SYNTAX_KEY_CAMEL_CASE.equals(name)) {
if ("legacy".equals(value)) {
setInterpolationSyntax(LEGACY_INTERPOLATION_SYNTAX);
} else if ("dollar".equals(value)) {
setInterpolationSyntax(DOLLAR_INTERPOLATION_SYNTAX);
} else if ("square_bracket".equals(value) || "squareBracket".equals(value)) {
setInterpolationSyntax(SQUARE_BRACKET_INTERPOLATION_SYNTAX);
} else {
throw invalidSettingValueException(name, value);
}
} else if (NAMING_CONVENTION_KEY_SNAKE_CASE.equals(name) || NAMING_CONVENTION_KEY_CAMEL_CASE.equals(name)) {
if ("auto_detect".equals(value) || "autoDetect".equals(value)) {
setNamingConvention(AUTO_DETECT_NAMING_CONVENTION);
} else if ("legacy".equals(value)) {
setNamingConvention(LEGACY_NAMING_CONVENTION);
} else if ("camel_case".equals(value) || "camelCase".equals(value)) {
setNamingConvention(CAMEL_CASE_NAMING_CONVENTION);
} else {
throw invalidSettingValueException(name, value);
}
} else if (TAB_SIZE_KEY_SNAKE_CASE.equals(name) || TAB_SIZE_KEY_CAMEL_CASE.equals(name)) {
setTabSize(Integer.parseInt(value));
} else if (INCOMPATIBLE_IMPROVEMENTS_KEY_SNAKE_CASE.equals(name) || INCOMPATIBLE_IMPROVEMENTS_KEY_CAMEL_CASE.equals(name)) {
setIncompatibleImprovements(new Version(value));
} else if (INCOMPATIBLE_ENHANCEMENTS.equals(name)) {
setIncompatibleEnhancements(value);
} else if (TEMPLATE_LOADER_KEY_SNAKE_CASE.equals(name) || TEMPLATE_LOADER_KEY_CAMEL_CASE.equals(name)) {
if (value.equalsIgnoreCase(DEFAULT)) {
unsetTemplateLoader();
} else {
setTemplateLoader((TemplateLoader) _ObjectBuilderSettingEvaluator.eval(value, TemplateLoader.class, true, _SettingEvaluationEnvironment.getCurrent()));
}
} else if (TEMPLATE_LOOKUP_STRATEGY_KEY_SNAKE_CASE.equals(name) || TEMPLATE_LOOKUP_STRATEGY_KEY_CAMEL_CASE.equals(name)) {
if (value.equalsIgnoreCase(DEFAULT)) {
unsetTemplateLookupStrategy();
} else {
setTemplateLookupStrategy((TemplateLookupStrategy) _ObjectBuilderSettingEvaluator.eval(value, TemplateLookupStrategy.class, false, _SettingEvaluationEnvironment.getCurrent()));
}
} else if (TEMPLATE_NAME_FORMAT_KEY_SNAKE_CASE.equals(name) || TEMPLATE_NAME_FORMAT_KEY_CAMEL_CASE.equals(name)) {
if (value.equalsIgnoreCase(DEFAULT)) {
unsetTemplateNameFormat();
} else if (value.equalsIgnoreCase("default_2_3_0")) {
setTemplateNameFormat(TemplateNameFormat.DEFAULT_2_3_0);
} else if (value.equalsIgnoreCase("default_2_4_0")) {
setTemplateNameFormat(TemplateNameFormat.DEFAULT_2_4_0);
} else {
throw invalidSettingValueException(name, value);
}
} else if (TEMPLATE_CONFIGURATIONS_KEY_SNAKE_CASE.equals(name) || TEMPLATE_CONFIGURATIONS_KEY_CAMEL_CASE.equals(name)) {
if (value.equals(NULL)) {
setTemplateConfigurations(null);
} else {
setTemplateConfigurations((TemplateConfigurationFactory) _ObjectBuilderSettingEvaluator.eval(value, TemplateConfigurationFactory.class, false, _SettingEvaluationEnvironment.getCurrent()));
}
} else {
unknown = true;
}
} catch (Exception e) {
throw settingValueAssignmentException(name, value, e);
}
if (unknown) {
super.setSetting(name, value);
}
}
use of freemarker.core.OutputFormat 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();
}
}
use of freemarker.core.OutputFormat 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;
}
}
use of freemarker.core.OutputFormat 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;
}
use of freemarker.core.OutputFormat 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();
}
Aggregations