当前位置: 首页>>代码示例>>Java>>正文


Java StringTemplateLoader类代码示例

本文整理汇总了Java中freemarker.cache.StringTemplateLoader的典型用法代码示例。如果您正苦于以下问题:Java StringTemplateLoader类的具体用法?Java StringTemplateLoader怎么用?Java StringTemplateLoader使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


StringTemplateLoader类属于freemarker.cache包,在下文中一共展示了StringTemplateLoader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import freemarker.cache.StringTemplateLoader; //导入依赖的package包/类
public static void main(String[] args) {
    Map<String,Object> tmp = new HashMap<>();
    tmp.put("user","邢天宇");
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
    StringTemplateLoader loader = new StringTemplateLoader();
    loader.putTemplate(NAME,"hello ${user}");
    cfg.setTemplateLoader(loader);
    cfg.setDefaultEncoding("UTF-8");
    try {
        Template template = cfg.getTemplate(NAME);
        StringWriter writer = new StringWriter();
        template.process(tmp,writer);
        System.out.println(writer.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }

}
 
开发者ID:rpgmakervx,项目名称:slardar,代码行数:19,代码来源:TemplateParser.java

示例2: before

import freemarker.cache.StringTemplateLoader; //导入依赖的package包/类
public void before() {
	configuration = new Configuration(Configuration.VERSION_2_3_23);
	// configuration.setObjectWrapper(new
	// DefaultObjectWrapper(Configuration.VERSION_2_3_23));
	try {
		// File resource = ResourceUtils.getResourceAsFile("/");
		// logger.debug("path:" + resource.getAbsolutePath());
		StringTemplateLoader templateLoader = new StringTemplateLoader();
		configuration.setTemplateLoader(templateLoader);
		Resource[] resources = ResourceUtils.getResources("classpath*:/template/*.ftl");
		for (Resource resource : resources) {
			String fileName = resource.getFilename();
			String value = IOUtils.toString(resource.getInputStream());
			templateLoader.putTemplate(fileName, value);
		}
		// templateLoader.putTemplate();
		
		// configuration.setDirectoryForTemplateLoading(resource);
	} catch (IOException e) {
		logger.error(e.getMessage(), e);
	}
}
 
开发者ID:xuegongzi,项目名称:rabbitframework,代码行数:23,代码来源:GeneratorConfig.java

示例3: process

import freemarker.cache.StringTemplateLoader; //导入依赖的package包/类
public static String process(String resource, String queryName, Map<String, Object> paramMap) throws Exception {
	DynamicHql dynamicHql = loadResource(resource);	
	if (null == dynamicHql) {
		logger.error( "no dynamic hql resource." );			
		throw new Exception( "no dynamic hql resource." );
	}
	String hql = "";
	for (int i=0; i<dynamicHql.getQuery().size() && hql.length()<1; i++) {
		Query queryObj = dynamicHql.getQuery().get(i);
		if (!queryObj.getName().equals(queryName)) {
			continue;
		}			
		StringTemplateLoader templateLoader = new StringTemplateLoader();
		templateLoader.putTemplate(TEMPLATE_ID, queryObj.getContent());
		Configuration cfg = new Configuration( Configuration.VERSION_2_3_21 );
		cfg.setTemplateLoader(templateLoader);
		Template template = cfg.getTemplate(TEMPLATE_ID, Constants.BASE_ENCODING);
		Writer out = new StringWriter();
		template.process(paramMap, out);			
		hql = out.toString();
	}
	if (StringUtils.isBlank(hql)) {
		logger.warn( "hql is blank." );			
	}
	return hql;		
}
 
开发者ID:billchen198318,项目名称:bamboobsc,代码行数:27,代码来源:DynamicHqlUtils.java

示例4: freemarkerProcess

import freemarker.cache.StringTemplateLoader; //导入依赖的package包/类
public static String freemarkerProcess(Map input, String templateStr) {  
    try {
    	StringTemplateLoader stringLoader = new StringTemplateLoader();  
	    String template = "content";  
	    stringLoader.putTemplate(template, templateStr);  
	    Configuration cfg = new Configuration();  
	    cfg.setTemplateLoader(stringLoader);  

	    Template templateCon = cfg.getTemplate(template);  
	    StringWriter writer = new StringWriter();  
	    templateCon.process(input, writer);  
	    return writer.toString();
	} catch (Exception e) {
		e.printStackTrace();
	}
    return null;
}
 
开发者ID:ImKarl,项目名称:ccshop,代码行数:18,代码来源:FreemarkerTemplateUtil.java

示例5: initStringTemplateLoader

import freemarker.cache.StringTemplateLoader; //导入依赖的package包/类
private static void initStringTemplateLoader(StringTemplateLoader loader) {
	// text 文本消息
	loader.putTemplate(OutTextMsg.class.getSimpleName(), OutTextMsg.TEMPLATE);
	// news 图文消息
	loader.putTemplate(OutNewsMsg.class.getSimpleName(), OutNewsMsg.TEMPLATE);
	// image 图片消息
	loader.putTemplate(OutImageMsg.class.getSimpleName(), OutImageMsg.TEMPLATE);
	//voice 语音消息
	loader.putTemplate(OutVoiceMsg.class.getSimpleName(), OutVoiceMsg.TEMPLATE);
	// video 视频消息
	loader.putTemplate(OutVideoMsg.class.getSimpleName(), OutVideoMsg.TEMPLATE);
	// music 音乐消息
	loader.putTemplate(OutMusicMsg.class.getSimpleName(), OutMusicMsg.TEMPLATE);
	// 转发多客服消息
	loader.putTemplate(OutCustomMsg.class.getSimpleName(), OutCustomMsg.TEMPLATE);
}
 
开发者ID:JackFish,项目名称:jfinal-weixin,代码行数:17,代码来源:OutMsgXmlBuilder.java

示例6: setUp

import freemarker.cache.StringTemplateLoader; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
    freemarkerConfiguration = new Configuration(Configuration.VERSION_2_3_23);

    templateLoader = new StringTemplateLoader();

    freemarkerConfiguration.setTemplateLoader(templateLoader);
    freemarkerConfiguration.setDefaultEncoding("UTF-8");
    freemarkerConfiguration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    freemarkerConfiguration.setNumberFormat("0.######");

    Map<String, TemplateDirectiveModel> qlDirectives = new HashMap<String, TemplateDirectiveModel>();
    qlDirectives.put("trim", new TrimDirective());
    qlDirectives.put("where", new WhereDirective());
    qlDirectives.put("set", new SetDirective());

    freemarkerConfiguration.setSharedVariable("ql", qlDirectives);

    dataModel = new HashMap<String, Object>();
}
 
开发者ID:kwon37xi,项目名称:freemarker-dynamic-ql-builder,代码行数:21,代码来源:AbstractQlDirectiveTest.java

示例7: setUp

import freemarker.cache.StringTemplateLoader; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
    freemarkerConfiguration = new Configuration(Configuration.VERSION_2_3_23);

    templateLoader = new StringTemplateLoader();

    freemarkerConfiguration.setTemplateLoader(templateLoader);
    freemarkerConfiguration.setDefaultEncoding("UTF-8");
    freemarkerConfiguration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    freemarkerConfiguration.setNumberFormat("0.######");

    parameterConverters = new HashMap<String, ParameterConverter>();
    parameterConverters.put("booleanToYn", booleanToYn);
    parameterConverters.put("dateToTime", dateToTime);

    unwrapper = new TemplateModelObjectUnwrapperDefaultImpl();
    paramMethod = new ParamMethod(unwrapper, parameterConverters, withPositionalIndex);

    dataModel = new HashMap<String, Object>();
}
 
开发者ID:kwon37xi,项目名称:freemarker-dynamic-ql-builder,代码行数:21,代码来源:ParamMethodTest.java

示例8: generateString

import freemarker.cache.StringTemplateLoader; //导入依赖的package包/类
public  String generateString(
         Map<String, Object> data,  String tplStr) {
 String result = null;
 String name="myStrTpl";
 try { 
  StringTemplateLoader stringTemplateLoader= new StringTemplateLoader();
  stringTemplateLoader.putTemplate(name, tplStr);
          cfg.setTemplateLoader(stringTemplateLoader); 
         Template template = cfg.getTemplate(name,defaultCharacter);
          StringWriter out = new StringWriter(); 
          template.process(data, out);  
          out.flush();  
          result= out.toString();
          out.close(); 
      } catch (Exception e) {  
          e.printStackTrace();  
      }  
 return result;
}
 
开发者ID:VonChange,项目名称:haloDao-Hibernate3,代码行数:20,代码来源:FreemarkerUtils.java

示例9: testRenameWithTabs

import freemarker.cache.StringTemplateLoader; //导入依赖的package包/类
@Test
public void testRenameWithTabs() throws IOException, URISyntaxException {
    loadTemplate(TEMPLATE_FILE2);

    List<VariableInfo> results = new VariableFinder(
            TemplateIntrospector.getRootNode(template))
                    .seek()
                    .getVariableInfo();

    TemplateEditor editor = new TemplateEditor(templateText);
    for (VariableInfo vi : results) {
        if (vi.getName().equals("somevariable")) {
            for (Expr var : vi.getVariables()) {
                editor.replace(var, "othervariable");
            }
        }
    }

    String modifiedTemplate = editor.apply().getModifiedTemplate();
    StringTemplateLoader tloader = new StringTemplateLoader();
    tloader.putTemplate("t", modifiedTemplate);
    templateConfig = new Configuration();
    templateConfig.setTemplateLoader(tloader);
    template = templateConfig.getTemplate("t");
}
 
开发者ID:cwong15,项目名称:freemarker-introspection,代码行数:26,代码来源:TemplateModificationTests.java

示例10: generateUpdateClass

import freemarker.cache.StringTemplateLoader; //导入依赖的package包/类
public void generateUpdateClass(File outJavaFile, String outJavaPackage) throws IOException {

		String template =
				new String(IOUtils.toCharArray(UpdateClassGenerator.class.getResourceAsStream("/UpdateClass.template.java")));

		Configuration cfg = new Configuration();
		StringTemplateLoader loader = new StringTemplateLoader();
		loader.putTemplate("template", template);
		cfg.setTemplateLoader(loader);
		cfg.setObjectWrapper(new DefaultObjectWrapper());

		Template temp = cfg.getTemplate("template");

		FileWriter fw = new FileWriter(outJavaFile);
		BufferedWriter bw = new BufferedWriter(fw);

		try {
			temp.process(this.buildTemplateModel(outJavaPackage), bw);
		} catch (TemplateException e) {
			throw new Error(e);
		}

		bw.close();
		fw.close();
	}
 
开发者ID:plum-umd,项目名称:rubah,代码行数:26,代码来源:UpdateClassGenerator.java

示例11: getTemplate

import freemarker.cache.StringTemplateLoader; //导入依赖的package包/类
protected Template getTemplate() {
    try {
        String templateContent = IOUtils.toString(reportTemplate.getDocumentContent(), StandardCharsets.UTF_8);
        StringTemplateLoader stringLoader = new StringTemplateLoader();
        stringLoader.putTemplate(reportTemplate.getDocumentName(), templateContent);

        Configuration fmConfiguration = new Configuration();
        fmConfiguration.setTemplateLoader(stringLoader);
        fmConfiguration.setDefaultEncoding("UTF-8");

        Template htmlTemplate = fmConfiguration.getTemplate(reportTemplate.getDocumentName());
        htmlTemplate.setObjectWrapper(objectWrapper);
        return htmlTemplate;
    } catch (Exception e) {
        throw wrapWithReportingException("An error occurred while creating freemarker template", e);
    }
}
 
开发者ID:cuba-platform,项目名称:yarg,代码行数:18,代码来源:HtmlFormatter.java

示例12: parseTemplate

import freemarker.cache.StringTemplateLoader; //导入依赖的package包/类
public static String parseTemplate(String template, Map<String, Object> params) {
    Configuration configuration = new Configuration(FREEMARKER_VERSION);
    configuration.setObjectWrapper(new DefaultObjectWrapper(FREEMARKER_VERSION));

    StringTemplateLoader templateLoader = new StringTemplateLoader();
    templateLoader.putTemplate("template", template);
    configuration.setTemplateLoader(templateLoader);

    try {
        Template templateObj = configuration.getTemplate("template");
        StringWriter out = new StringWriter();
        templateObj.process(params, out);
        out.flush();
        return out.toString();
    } catch (IOException | TemplateException e) {
        throw new IllegalArgumentException("Can't parse template.", e);
    }
}
 
开发者ID:Codeforces,项目名称:codeforces-commons,代码行数:19,代码来源:TemplateUtil.java

示例13: FreemarkerSecondPassStatementBuilder

import freemarker.cache.StringTemplateLoader; //导入依赖的package包/类
public FreemarkerSecondPassStatementBuilder(Statement statement, DatabaseDialect dialect, Object value) {
    this.statement = statement;
    this.dialect = dialect;
    this.value = value;
    final StringTemplateLoader loader = new StringTemplateLoader();
    String sql = statement.getSql();
    sql = sql.replaceAll("<%(.*?)>", "<#$1>");
    sql = sql.replaceAll("</%(.*?)>", "</#$1>");
    sql = sql.replaceAll("%\\{(.*?)\\}", "\\${$1}");
    loader.putTemplate("sql", sql);
    final Configuration configuration = new Configuration();
    configuration.setTemplateLoader(loader);
    try {
        template = configuration.getTemplate("sql");
    } catch (IOException ignored) {
    }
}
 
开发者ID:mmnaseri,项目名称:dragonfly,代码行数:18,代码来源:FreemarkerSecondPassStatementBuilder.java

示例14: getStringConfig

import freemarker.cache.StringTemplateLoader; //导入依赖的package包/类
/**
 * FreeMarker configuration for loading the specified template directly from a String
 * 
 * @param path      Pseudo Path to the template
 * @param template  Template content
 * 
 * @return FreeMarker configuration
 */
protected Configuration getStringConfig(String path, String template)
{
    Configuration config = new Configuration();
    
    // setup template cache
    config.setCacheStorage(new MruCacheStorage(2, 0));
    
    // use our custom loader to load a template directly from a String
    StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
    stringTemplateLoader.putTemplate(path, template);
    config.setTemplateLoader(stringTemplateLoader);
    
    // use our custom object wrapper that can deal with QNameMap objects directly
    config.setObjectWrapper(qnameObjectWrapper);
    
    // rethrow any exception so we can deal with them
    config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    
    // set default template encoding
    if (defaultEncoding != null)
    {
        config.setDefaultEncoding(defaultEncoding);
    }
    config.setIncompatibleImprovements(new Version(2, 3, 20));
    config.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);
    
    return config;
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:37,代码来源:FreeMarkerProcessor.java

示例15: TemplateParser

import freemarker.cache.StringTemplateLoader; //导入依赖的package包/类
public TemplateParser(String templateContent, org.easyarch.slardar.session.Configuration configuration) {
    this.sqlMapCache = SqlMapCacheFactory.getInstance()
            .createCache(configuration.getCacheEntity());
    StringTemplateLoader loader = new StringTemplateLoader();
    loader.putTemplate(NAME,templateContent);
    cfg.setTemplateLoader(loader);
    cfg.setDefaultEncoding("UTF-8");
}
 
开发者ID:rpgmakervx,项目名称:slardar,代码行数:9,代码来源:TemplateParser.java


注:本文中的freemarker.cache.StringTemplateLoader类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。