@Override
 protected void currentConfiguration(Configuration cfg) {
   channelService = mock(ChannelDaoable.class);
   Channel channel = new Channel();
   channel.setId(1L);
   when(channelService.findPublishByUri(any(Long.class), any(String.class))).thenReturn(channel);
   templateService = mock(TemplateDaoable.class);
   when(templateService.findUniquePath(any(Long.class), any(Long.class), any(String.class)))
       .thenReturn("2/1/include.html");
   IncludeDirective directive = new IncludeDirective(channelService, templateService);
   cfg.setSharedVariable("include", directive);
 }
Esempio n. 2
0
  public void init(ServletConfig config) throws ServletException {
    super.init(config);

    try {
      String appPath = config.getServletContext().getRealPath("");
      // 是否为开发mode
      debug = "true".equals(config.getInitParameter("development"));
      // 读取log4j.xml配置文件
      DOMConfigurator.configure(appPath + "/WEB-INF/log4j.xml");

      logger.info("Starting JForum. Debug mode is " + debug);
      //
      ConfigLoader.startSystemglobals(appPath);
      // 启动缓存引擎
      ConfigLoader.startCacheEngine();

      // Configure the template engine
      Configuration templateCfg = new Configuration();
      templateCfg.setTemplateUpdateDelay(2);
      templateCfg.setSetting("number_format", "#");
      templateCfg.setSharedVariable("startupTime", new Long(new Date().getTime()));

      // Create the default template loader
      String defaultPath = SystemGlobals.getApplicationPath() + "/templates";
      FileTemplateLoader defaultLoader = new FileTemplateLoader(new File(defaultPath));

      String extraTemplatePath = SystemGlobals.getValue(ConfigKeys.FREEMARKER_EXTRA_TEMPLATE_PATH);

      if (StringUtils.isNotBlank(extraTemplatePath)) {
        // An extra template path is configured, we need a MultiTemplateLoader
        FileTemplateLoader extraLoader = new FileTemplateLoader(new File(extraTemplatePath));
        TemplateLoader[] loaders = new TemplateLoader[] {extraLoader, defaultLoader};
        MultiTemplateLoader multiLoader = new MultiTemplateLoader(loaders);
        templateCfg.setTemplateLoader(multiLoader);
      } else {
        // An extra template path is not configured, we only need the default loader
        templateCfg.setTemplateLoader(defaultLoader);
      }
      // 载入模块
      ModulesRepository.init(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR));

      this.loadConfigStuff();

      if (!this.debug) {
        templateCfg.setTemplateUpdateDelay(3600);
      }

      JForumExecutionContext.setTemplateConfig(templateCfg);
    } catch (Exception e) {
      throw new ForumStartupException("Error while starting JForum", e);
    }
  }
 public CCodeGenerator() throws TemplateModelException {
   config.setDefaultEncoding("UTF-8");
   config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
   config.setClassForTemplateLoading(CCodeGenerator.class, "/templates");
   config.setSharedVariable("cTypeHelper", C_TypeHelper.getInstance());
   config.setSharedVariable("enumHelper", EnumHelper.getInstance());
   config.setSharedVariable("requestHelper", RequestHelper.getInstance());
   config.setSharedVariable("helper", Helper.getInstance());
   config.setSharedVariable("structHelper", StructHelper.getInstance());
   config.setSharedVariable("structMemberHelper", StructMemberHelper.getInstance());
   config.setSharedVariable("parameterHelper", ParameterHelper.getInstance());
 }
  public static Configuration getConfiguration()
      throws SQLException, TemplateModelException, IOException {
    if (cfg == null) {
      cfg = new Configuration(new Version("2.3.23"));
      cfg.setDefaultEncoding("UTF-8");
      cfg.setSharedVariable(
          "portalAddr", SystemConfigDao.getSystemConfig(Config.TemplateInfo.CONFIG_ID));

      // 获取资源路径
      String classPath = cfg.getClass().getResource("/").getFile().toString();
      cfg.setDirectoryForTemplateLoading(new File(classPath));
    }

    return cfg;
  }
  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;
  }
 public void configureSharedVariables(Configuration configuration) {
   configuration.setSharedVariable("templateExists", new TemplateExistsMethod());
   configuration.setSharedVariable("getXmlDataModelSchema", new XmlDataModelSchemaMethod());
   configuration.setSharedVariable("getClassForName", new ClassReferenceMethod());
   configuration.setSharedVariable("getClassSourceForName", new ClassSourceMethod());
   configuration.setSharedVariable("getClassInstanceForName", new ClassInstanceMethod());
   configuration.setSharedVariable("getAntBuildProject", new AntBuildProjectMethod());
   configuration.setSharedVariable("getFile", new GetFileMethod());
   configuration.setSharedVariable("getXmlDoc", new GetXmlDocumentMethod());
   configuration.setSharedVariable(
       "getInputSourceDependencies", new InputSourceDependenciesMethod());
   configuration.setSharedVariable("executeCommand", new ExecuteCommandMethod());
   configuration.setSharedVariable("getQueryResultSet", new GetQueryResultSetMethod());
   configuration.setSharedVariable("getQueryResultsAsMatrix", new GetQueryResultsAsMatrixMethod());
   configuration.setSharedVariable(
       "getQueryResultsAsMapArray", new GetQueryResultsAsMapArrayMethod(false));
   configuration.setSharedVariable(
       "getQueryResultsAsMapArrayWithLabelAsKey", new GetQueryResultsAsMapArrayMethod(true));
   configuration.setSharedVariable(
       "getQueryResultsSingleRowAsMap", new GetQueryResultsSingleRowAsMapMethod(false));
   configuration.setSharedVariable(
       "getQueryResultsSingleRowAsMapWithLabelAsKey",
       new GetQueryResultsSingleRowAsMapMethod(true));
   configuration.setSharedVariable(
       "getFileContentsSyntaxHighlighted", new GetFileContentsSyntaxHighlightedMethod());
   configuration.setSharedVariable("panel", new PanelTransform());
   configuration.setSharedVariable("command", new CommandTransform());
   configuration.setSharedVariable("statics", BeansWrapper.getDefaultInstance().getStaticModels());
   SyntaxHighlightTransform.registerTransforms(configuration);
 }