/** * @param templateName * Must be root relative */ private LazilyInitializedNamespace(String templateName) { super(null); this.templateName = templateName; // Make snapshot of all settings that influence template resolution: this.locale = getLocale(); this.encoding = getIncludedTemplateEncoding(); this.customLookupCondition = getIncludedTemplateCustomLookupCondition(); }
Collator getCollator() { if (cachedCollator == null) { cachedCollator = Collator.getInstance(getLocale()); } return cachedCollator; }
@Override TemplateModel calculateResult(String s, Environment env) { return new SimpleScalar(s.toUpperCase(env.getLocale())); } }
@Override TemplateModel calculateResult(String s, Environment env) { return new SimpleScalar(s.toLowerCase(env.getLocale())); } }
@Override protected String calculateInterpolatedStringOrMarkup(Environment env) throws TemplateException { Number num = expression.evalToNumber(env); FormatHolder fmth = formatCache; // atomic sampling if (fmth == null || !fmth.locale.equals(env.getLocale())) { synchronized (this) { fmth = formatCache; if (fmth == null || !fmth.locale.equals(env.getLocale())) { NumberFormat fmt = NumberFormat.getNumberInstance(env.getLocale()); if (hasFormat) { fmt.setMinimumFractionDigits(minFracDigits); fmt.setMaximumFractionDigits(maxFracDigits); } else { fmt.setMinimumFractionDigits(0); fmt.setMaximumFractionDigits(50); } fmt.setGroupingUsed(false); formatCache = new FormatHolder(fmt, env.getLocale()); fmth = formatCache; } } } // We must use Format even if hasFormat == false. // Some locales may use non-Arabic digits, thus replacing the // decimal separator in the result of toString() is not enough. String s = fmth.format.format(num); return s; }
/** * Gets the {@link TemplateNumberFormat} <em>for the current locale</em>. * * @param formatString * Not {@code null} * @param cacheResult * If the results should stored in the {@link Environment}-level cache. It will still try to get the * result from the cache regardless of this parameter. */ private TemplateNumberFormat getTemplateNumberFormat(String formatString, boolean cacheResult) throws TemplateValueFormatException { if (cachedTemplateNumberFormats == null) { if (cacheResult) { cachedTemplateNumberFormats = new HashMap<String, TemplateNumberFormat>(); } } else { TemplateNumberFormat format = cachedTemplateNumberFormats.get(formatString); if (format != null) { return format; } } TemplateNumberFormat format = getTemplateNumberFormatWithoutCache(formatString, getLocale()); if (cacheResult) { cachedTemplateNumberFormats.put(formatString, format); } return format; }
public String getAsString() throws TemplateModelException { Environment env = Environment.getCurrentEnvironment(); Locale locale = env.getLocale(); return getLocalizedString(locale); }
private void doAutoIncludes(Environment env, Template t) throws TemplateException, IOException, TemplateNotFoundException, MalformedTemplateNameException, ParseException { // We can't store autoIncludes in LinkedHashSet-s because setAutoIncludes(List) allows duplicates, // unfortunately. Yet we have to prevent duplicates among Configuration levels, with the lowest levels having // priority. So we build some Set-s to do that, but we avoid the most common cases where they aren't needed. List<String> tAutoIncludes = t.getAutoIncludesWithoutFallback(); List<String> envAutoIncludes = env.getAutoIncludesWithoutFallback(); for (String templateName : getAutoIncludesWithoutFallback()) { if ((tAutoIncludes == null || !tAutoIncludes.contains(templateName)) && (envAutoIncludes == null || !envAutoIncludes.contains(templateName))) { env.include(getTemplate(templateName, env.getLocale())); } } if (tAutoIncludes != null) { for (String templateName : tAutoIncludes) { if (envAutoIncludes == null || !envAutoIncludes.contains(templateName)) { env.include(getTemplate(templateName, env.getLocale())); } } } if (envAutoIncludes != null) { for (String templateName : envAutoIncludes) { env.include(getTemplate(templateName, env.getLocale())); } } }
/** * Returns the number format as {@link TemplateNumberFormat}, for the given format string and locale. To get a * number format for the current locale, use {@link #getTemplateNumberFormat(String)} instead. * * <p> * Note on performance (which was true at least for 2.3.24): Unless the locale happens to be equal to the current * locale, the {@link Environment}-level format cache can't be used, so the format string has to be parsed and the * matching factory has to be get an invoked, which is much more expensive than getting the format from the cache. * Thus the returned format should be stored by the caller for later reuse (but only within the current thread and * in relation to the current {@link Environment}), if it will be needed frequently. * * @param formatString * A string that you could also use as the value of the {@code numberFormat} configuration setting. * @param locale * The locale of the number format; not {@code null}. * * @since 2.3.24 */ public TemplateNumberFormat getTemplateNumberFormat(String formatString, Locale locale) throws TemplateValueFormatException { if (locale.equals(getLocale())) { getTemplateNumberFormat(formatString); } return getTemplateNumberFormatWithoutCache(formatString, locale); }
static Template getTemplate(String systemId) throws IOException { Environment env = Environment.getCurrentEnvironment(); String encoding = env.getTemplate().getEncoding(); if (encoding == null) { encoding = env.getConfiguration().getEncoding(env.getLocale()); } String templatePath = env.getTemplate().getName(); int lastSlash = templatePath.lastIndexOf('/'); templatePath = lastSlash == -1 ? "" : templatePath.substring(0, lastSlash + 1); systemId = env.toFullTemplateName(templatePath, systemId); Template raw = env.getConfiguration().getTemplate(systemId, env.getLocale(), encoding, false); return raw; }
private void initialize() throws IOException, TemplateException { setTemplate(configuration.getTemplate( templateName, locale, customLookupCondition, encoding, true, false)); Locale lastLocale = getLocale(); try { setLocale(locale); initializeImportLibNamespace(this, getTemplate()); } finally { setLocale(lastLocale); } }
private String getIncludedTemplateEncoding() { String encoding; // This branch shouldn't exist, as it doesn't make much sense to inherit encoding. But we have to keep BC. encoding = getTemplate().getEncoding(); if (encoding == null) { encoding = configuration.getEncoding(this.getLocale()); } return encoding; }
throws IOException { return configuration.getTemplate( name, getLocale(), getIncludedTemplateCustomLookupCondition(), encoding != null ? encoding : getIncludedTemplateEncoding(), parseAsFTL,
@Override public void setLocale(Locale locale) { Locale prevLocale = getLocale(); super.setLocale(locale); if (!locale.equals(prevLocale)) { cachedTemplateNumberFormats = null; if (cachedTemplateNumberFormat != null && cachedTemplateNumberFormat.isLocaleBound()) { cachedTemplateNumberFormat = null; } if (cachedTempDateFormatArray != null) { for (int i = 0; i < CACHED_TDFS_LENGTH; i++) { final TemplateDateFormat f = cachedTempDateFormatArray[i]; if (f != null && f.isLocaleBound()) { cachedTempDateFormatArray[i] = null; } } } cachedTempDateFormatsByFmtStrArray = null; cachedCollator = null; } }
dateType, getLocale(), useSQLDTTimeZone ? getSQLDateAndTimeTimeZone() : getTimeZone(), zonelessInput); if (cacheResult) {
int dateType, Locale locale, TimeZone timeZone, boolean zonelessInput) throws TemplateValueFormatException { Locale currentLocale = getLocale(); if (locale.equals(currentLocale)) { int equalCurrentTZ;
@Override public Locale getLocale() throws Exception { return environment.getLocale(); }
@Override public Locale getLocale() throws Exception { return environment.getLocale(); }
interpretedTemplate.setLocale(env.getLocale()); return new TemplateProcessorModel(interpretedTemplate);