示例#1
0
 @SuppressWarnings("unchecked")
 public AbstractTemplate(
     Engine engine,
     Filter filter,
     Formatter<?> formatter,
     Map<Class<?>, Object> functions,
     Map<String, Template> importMacros) {
   this.engine = engine;
   this.filter = filter;
   this.formatter = (Formatter<Object>) formatter;
   this.importMacros = importMacros;
   this.macros = initMacros(engine, filter, formatter, functions, importMacros);
   if (formatter instanceof MultiFormatter) {
     MultiFormatter multi = (MultiFormatter) formatter;
     this.numberFormatter = multi.get(Number.class);
     this.booleanFormatter = multi.get(Boolean.class);
     this.byteFormatter = getFormatter(multi, Byte.class, numberFormatter);
     this.charFormatter = multi.get(Character.class);
     this.shortFormatter = getFormatter(multi, Short.class, numberFormatter);
     this.intFormatter = getFormatter(multi, Integer.class, numberFormatter);
     this.longFormatter = getFormatter(multi, Long.class, numberFormatter);
     this.floatFormatter = getFormatter(multi, Float.class, numberFormatter);
     this.doubleFormatter = getFormatter(multi, Double.class, numberFormatter);
     this.dateFormatter = multi.get(Date.class);
   } else {
     this.numberFormatter = null;
     this.booleanFormatter = null;
     this.byteFormatter = null;
     this.charFormatter = null;
     this.shortFormatter = null;
     this.intFormatter = null;
     this.longFormatter = null;
     this.floatFormatter = null;
     this.doubleFormatter = null;
     this.dateFormatter = null;
   }
   this.nullValue = engine.getProperty(NULL_VALUE, "");
   this.trueValue = engine.getProperty(TRUE_VALUE, "true");
   this.falseValue = engine.getProperty(FALSE_VALUE, "false");
   this.outputEncoding = engine.getProperty(OUTPUT_ENCODING);
   this.outputCharset =
       outputEncoding == null || outputEncoding.length() == 0
           ? null
           : Charset.forName(outputEncoding);
 }
示例#2
0
 private String findMessageByLocale(String key, Locale locale) {
   String file = messageBasename + (locale == null ? "" : "_" + locale) + messageSuffix;
   EncodingProperties properties = messageCache.get(file);
   if ((properties == null || reloadable) && engine.hasResource(file)) {
     if (properties == null) {
       properties = new EncodingProperties();
       EncodingProperties old = messageCache.putIfAbsent(file, properties);
       if (old != null) {
         properties = old;
       }
     }
     try {
       Resource resource = engine.getResource(file);
       if (properties.getLastModified() < resource.getLastModified()) {
         String encoding = (StringUtils.isEmpty(messageEncoding) ? "UTF-8" : messageEncoding);
         properties.load(resource.openStream(), encoding, resource.getLastModified());
       }
     } catch (IOException e) {
       if (logger != null && logger.isErrorEnabled()) {
         logger.error(
             "Failed to load httl message file "
                 + file
                 + " with locale "
                 + locale
                 + ", cause: "
                 + e.getMessage(),
             e);
       }
     }
   }
   if (properties != null) {
     String value = properties.getProperty(key);
     if (StringUtils.isNotEmpty(value)) {
       return value;
     }
   }
   if (locale != null) {
     return findMessageByLocale(key, LocaleUtils.getParentLocale(locale));
   }
   return null;
 }