Exemple #1
0
  @SuppressWarnings("unchecked")
  public static String parseTemplate(
      Map paramMap,
      String templateName,
      String templateDirectory,
      boolean isDirectory,
      String encoding)
      throws IOException, TemplateException {
    if (configuration == null) {
      configuration = new Configuration();
    }
    if (StringUtils.isNotBlank(encoding)) {
      configuration.setEncoding(Locale.getDefault(), encoding);
    } else {
      configuration.setEncoding(Locale.getDefault(), DEFAULT_ENCODING);
    }
    ByteArrayOutputStream bos = null;
    OutputStreamWriter out = null;
    try {
      if (isDirectory) {
        configuration.setDirectoryForTemplateLoading(new File(templateDirectory));
      } else {
        configuration.setClassForTemplateLoading(TemplateParser.class, templateDirectory);
      }
      bos = new ByteArrayOutputStream();
      out = new OutputStreamWriter(bos);
      configuration.getTemplate(templateName).process(paramMap, out);
      return bos.toString();
    } catch (IOException e) {
      throw e;
    } catch (TemplateException e) {
      throw e;
    } finally {
      try {
        out.close();
      } catch (IOException e) {

      } finally {
        out = null;
      }

      try {
        bos.close();
      } catch (IOException e) {

      } finally {
        bos = null;
      }
    }
  }
Exemple #2
0
 public static void init() {
   cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
   cfg.setCacheStorage(new freemarker.cache.MruCacheStorage(20, 250));
   cfg.setNumberFormat("#");
   cfg.setEncoding(Locale.getDefault(), "UTF-8");
   cfg.setObjectWrapper(new DefaultObjectWrapper());
   cfg.clearTemplateCache();
 }
 public TemplateLoaderImpl(BundleContext ctx) {
   menuBuilder = new MenuBuilder(ctx);
   config = new Configuration();
   // config.setTemplateLoader(new ClassTemplateLoader(Activator.class, "/templates/"));
   config.setTemplateLoader(new BundleTemplateLoader(ctx, "/templates/"));
   config.setObjectWrapper(new DefaultObjectWrapper());
   config.setEncoding(Locale.getDefault(), "UTF-8");
   config.setURLEscapingCharset("UTF-8");
 }
Exemple #4
0
 /**
  * インスタンスを生成します。
  *
  * @param templateEncoding テンプレートファイルのエンコーディング
  * @param templatePrimaryDir テンプレートファイルを格納したプライマリディレクトリ、プライマリディレクトリを使用しない場合{@code null}
  */
 public Generator(String templateEncoding) {
   if (templateEncoding == null) {
     throw new NullPointerException("templateFileEncoding");
   }
   this.configuration = new Configuration();
   configuration.setObjectWrapper(ObjectWrapper.DEFAULT_WRAPPER);
   configuration.setEncoding(Locale.getDefault(), templateEncoding);
   configuration.setTemplateLoader(
       new ClassTemplateLoader(this.getClass(), "/" + DEFAULT_TEMPLATE_DIR_NAME));
 }
 public static void main(String[] args) throws Exception {
   Configuration cfg = new Configuration();
   cfg.setDirectoryForTemplateLoading(new File(Main.SAMPLE_DIR));
   cfg.setEncoding(Locale.US, "UTF8");
   Template template = cfg.getTemplate("contact.ftl");
   Map map = new HashMap();
   // 位置
   map.put("pos", "Contact Us");
   PrintWriter pw = new PrintWriter(new FileOutputStream(Main.YW_DIR + "contact.html"));
   template.process(map, pw);
   pw.close();
 }
Exemple #6
0
 public static Configuration cfg(String templatePath) {
   if (configuration == null) {
     Configuration cfg = new Configuration();
     cfg.setEncoding(Locale.CHINA, "utf-8");
     try {
       cfg.setDirectoryForTemplateLoading(
           new File(ServiceManager.getInstance().getWebContextPath() + templatePath));
     } catch (IOException e) {
       logger.error(e);
       throw new RuntimeException("设置模板文件路径失败!");
     }
     // 设置定义的编码
     cfg.setObjectWrapper(new DefaultObjectWrapper());
     return cfg;
   } else return configuration;
 }
Exemple #7
0
 /**
  * 生成文件
  *
  * @param templateName 模板文件
  * @param fileName 生成文件
  * @param root 参数
  */
 private static void buildFile(String templateName, String fileName, Map root) {
   Configuration freemarkerCfg = new Configuration();
   freemarkerCfg.setClassForTemplateLoading(GenerateJavaFile.class, "/");
   freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8");
   Template template;
   try {
     template = freemarkerCfg.getTemplate(templateName);
     template.setEncoding("UTF-8");
     File htmlFile = new File(fileName);
     Writer out =
         new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8"));
     template.process(root, out);
     out.flush();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  public static Configuration getCfg() {
    if (cfg == null) {
      cfg = new Configuration();
      cfg.setTemplateUpdateDelay(6000);
      cfg.setCacheStorage(new freemarker.cache.MruCacheStorage(20, 250));

      DateFormateDirectiveModel df = new DateFormateDirectiveModel();
      cfg.setSharedVariable("dateFormat", df);

      cfg.setObjectWrapper(new DefaultObjectWrapper());
      cfg.setDefaultEncoding("UTF-8");
      cfg.setLocale(java.util.Locale.CHINA);
      cfg.setEncoding(java.util.Locale.CHINA, "UTF-8");
    }

    return cfg;
  }
Exemple #9
0
  private Configuration getCfg() {

    if (cfg == null) {
      cfg = FreeMarkerUtil.getCfg();
    }

    pathPrefix = pathPrefix == null ? "" : pathPrefix;

    if (pageFolder == null) { // 默认使用挂件所在文件夹
      // System.out.println(" folder null use "+ this.clazz.getName() );
      cfg.setClassForTemplateLoading(this.clazz, pathPrefix);
    } else {
      // System.out.println(" folder not null use "+ pageFolder);
      cfg.setServletContextForTemplateLoading(
          ThreadContextHolder.getHttpRequest().getSession().getServletContext(), pageFolder);
    }
    cfg.setObjectWrapper(new DefaultObjectWrapper());
    cfg.setDefaultEncoding("UTF-8");
    cfg.setLocale(java.util.Locale.CHINA);
    cfg.setEncoding(java.util.Locale.CHINA, "UTF-8");
    return cfg;
  }