org.beetl.core.Template Java Examples

The following examples show how to use org.beetl.core.Template. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleVATest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testUserAttribute() throws Exception
{

	User user = User.getTestUser();

	Template t = gt.getTemplate("/va/va_simple_template.html");
	this.bind(t, "user", user);
	String str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/va/va_simple_expected.html"), str);

	t = gt.getTemplate("/va/va_simple_template.html");
	this.bind(t, "user", user);
	str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/va/va_simple_expected.html"), str);

}
 
Example #2
Source File: RpcDocBuilderTemplate.java    From smart-doc with Apache License 2.0 6 votes vote down vote up
/**
 * Merge all api doc into one document
 *
 * @param apiDocList         list  data of Api doc
 * @param config             api config
 * @param javaProjectBuilder JavaProjectBuilder
 * @param template           template
 * @param outPutFileName     output file
 */
public void buildAllInOne(List<RpcApiDoc> apiDocList, ApiConfig config, JavaProjectBuilder javaProjectBuilder, String template, String outPutFileName) {
    String outPath = config.getOutPath();
    String strTime = DateTimeUtil.long2Str(now, DateTimeUtil.DATE_FORMAT_SECOND);
    String rpcConfig = config.getRpcConsumerConfig();
    String rpcConfigConfigContent = null;
    if (Objects.nonNull(rpcConfig)) {
        rpcConfigConfigContent = FileUtil.getFileContent(rpcConfig);
    }
    FileUtil.mkdirs(outPath);
    List<ApiErrorCode> errorCodeList = errorCodeDictToList(config);
    Template tpl = BeetlTemplateUtil.getByName(template);
    tpl.binding(TemplateVariable.API_DOC_LIST.getVariable(), apiDocList);
    tpl.binding(TemplateVariable.ERROR_CODE_LIST.getVariable(), errorCodeList);
    tpl.binding(TemplateVariable.VERSION_LIST.getVariable(), config.getRevisionLogs());
    tpl.binding(TemplateVariable.DEPENDENCY_LIST.getVariable(), config.getRpcApiDependencies());
    tpl.binding(TemplateVariable.VERSION.getVariable(), now);
    tpl.binding(TemplateVariable.CREATE_TIME.getVariable(), strTime);
    tpl.binding(TemplateVariable.PROJECT_NAME.getVariable(), config.getProjectName());
    tpl.binding(TemplateVariable.RPC_CONSUMER_CONFIG.getVariable(), rpcConfigConfigContent);
    setDirectoryLanguageVariable(config, tpl);
    FileUtil.nioWriteFile(tpl.render(), outPath + FILE_SEPARATOR + outPutFileName);
}
 
Example #3
Source File: DocBuilderTemplate.java    From smart-doc with Apache License 2.0 6 votes vote down vote up
/**
 * Merge all api doc into one document
 *
 * @param apiDocList         list  data of Api doc
 * @param config             api config
 * @param javaProjectBuilder JavaProjectBuilder
 * @param template           template
 * @param outPutFileName     output file
 */
public void buildAllInOne(List<ApiDoc> apiDocList, ApiConfig config, JavaProjectBuilder javaProjectBuilder, String template, String outPutFileName) {
    String outPath = config.getOutPath();
    String strTime = DateTimeUtil.long2Str(now, DateTimeUtil.DATE_FORMAT_SECOND);
    FileUtil.mkdirs(outPath);
    List<ApiErrorCode> errorCodeList = errorCodeDictToList(config);

    Template tpl = BeetlTemplateUtil.getByName(template);
    tpl.binding(TemplateVariable.API_DOC_LIST.getVariable(), apiDocList);
    tpl.binding(TemplateVariable.ERROR_CODE_LIST.getVariable(), errorCodeList);
    tpl.binding(TemplateVariable.VERSION_LIST.getVariable(), config.getRevisionLogs());
    tpl.binding(TemplateVariable.VERSION.getVariable(), now);
    tpl.binding(TemplateVariable.CREATE_TIME.getVariable(), strTime);
    tpl.binding(TemplateVariable.PROJECT_NAME.getVariable(), config.getProjectName());
    if (CollectionUtil.isEmpty(errorCodeList)) {
        tpl.binding(TemplateVariable.DICT_ORDER.getVariable(), apiDocList.size() + 1);
    } else {
        tpl.binding(TemplateVariable.DICT_ORDER.getVariable(), apiDocList.size() + 2);
    }
    setDirectoryLanguageVariable(config, tpl);
    List<ApiDocDict> apiDocDictList = buildDictionary(config, javaProjectBuilder);
    tpl.binding(TemplateVariable.DICT_LIST.getVariable(), apiDocDictList);
    FileUtil.nioWriteFile(tpl.render(), outPath + FILE_SEPARATOR + outPutFileName);
}
 
Example #4
Source File: SimpleVATest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testFunctionAttribute() throws Exception
{

	gt.registerFunction("userArray", new Function() {

		@Override
		public Object call(Object[] paras, Context ctx)
		{
			// TODO Auto-generated method stub
			return new Object[]
			{ User.getTestUser(), "a" };
		}

	});
	Template t = gt.getTemplate("/va/va_fun_template.html");
	String str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/va/va_fun_expected.html"), str);

	t = gt.getTemplate("/va/va_fun_template.html");
	str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/va/va_fun_expected.html"), str);

}
 
Example #5
Source File: RpcHtmlBuilder.java    From smart-doc with Apache License 2.0 6 votes vote down vote up
/**
 * build ever controller api
 *
 * @param apiDocList list of api doc
 * @param outPath    output path
 */
private static void buildDoc(List<RpcApiDoc> apiDocList, String outPath) {
    FileUtil.mkdirs(outPath);
    Template htmlApiDoc;
    String strTime = DateTimeUtil.long2Str(now, DateTimeUtil.DATE_FORMAT_SECOND);
    for (RpcApiDoc rpcDoc : apiDocList) {
        Template apiTemplate = BeetlTemplateUtil.getByName(RPC_API_DOC_MD_TPL);
        apiTemplate.binding(TemplateVariable.DESC.getVariable(), rpcDoc.getDesc());
        apiTemplate.binding(TemplateVariable.NAME.getVariable(), rpcDoc.getName());
        apiTemplate.binding(TemplateVariable.LIST.getVariable(), rpcDoc.getList());
        apiTemplate.binding(TemplateVariable.PROTOCOL.getVariable(),rpcDoc.getProtocol());
        apiTemplate.binding(TemplateVariable.AUTHOR.getVariable(),rpcDoc.getAuthor());
        apiTemplate.binding(TemplateVariable.VERSION.getVariable(),rpcDoc.getVersion());
        apiTemplate.binding(TemplateVariable.URI.getVariable(),rpcDoc.getUri());

        String html = MarkDownUtil.toHtml(apiTemplate.render());
        htmlApiDoc = BeetlTemplateUtil.getByName(HTML_API_DOC_TPL);
        htmlApiDoc.binding(TemplateVariable.HTML.getVariable(), html);
        htmlApiDoc.binding(TemplateVariable.TITLE.getVariable(), rpcDoc.getDesc());
        htmlApiDoc.binding(TemplateVariable.CREATE_TIME.getVariable(), strTime);
        htmlApiDoc.binding(TemplateVariable.VERSION.getVariable(), now);
        FileUtil.nioWriteFile(htmlApiDoc.render(), outPath + FILE_SEPARATOR + rpcDoc.getAlias() + ".html");
    }
}
 
Example #6
Source File: GeneralNumberTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testDiv() throws Exception
{
	Template t = gt.getTemplate("/exp/general_div_template.html");
	this.bind(t, "d1", d1, "d2", d2);
	String str = t.render();

	AssertJUnit.assertEquals(this.getFileContent("/exp/general_div_expected.html"), str);

	t = gt.getTemplate("/exp/general_div_template.html");
	this.bind(t, "d1", d1, "d2", d2);
	str = t.render();

	AssertJUnit.assertEquals(this.getFileContent("/exp/general_div_expected.html"), str);

}
 
Example #7
Source File: FunctionTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testCore() throws Exception
{

	Map map = new HashMap();
	map.put("a", "hi");
	map.put("b", 1);

	Template t = gt.getTemplate("/function/function_template.html");
	t.binding(map);
	String str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/function/function_expected.html"), str);

	t = gt.getTemplate("/function/function_template.html");
	t.binding(map);
	str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/function/function_expected.html"), str);
}
 
Example #8
Source File: BigNumberTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testMinus() throws Exception
{
	Template t = gt.getTemplate("/exp/bignumber/number_minus_template.html");
	this.bind(t, "d1", d1, "d2", d2);
	String str = t.render();
	String expected = getFileContent("/exp/bignumber/number_minus_expected.html");
	AssertJUnit.assertEquals(expected, str);

	t = gt.getTemplate("/exp/bignumber/number_minus_template.html");
	this.bind(t, "d1", d1, "d2", d2);
	str = t.render();

	AssertJUnit.assertEquals(this.getFileContent("/exp/bignumber/number_minus_expected.html"), str);

}
 
Example #9
Source File: BeetlTemplateUtil.java    From ApplicationPower with Apache License 2.0 6 votes vote down vote up
/**
 * @param path
 * @param params
 * @return
 */
public static Map<String, String> getTemplatesRendered(String path, Map<String, Object> params) {
    Map<String, String> templateMap = new HashMap<>();
    File[] files = FileUtil.getResourceFolderFiles(path);
    GroupTemplate gt = getGroupTemplate(path);
    for (File f : files) {
        if (f.isFile()) {
            String fileName = f.getName();
            Template tp = gt.getTemplate(fileName);
            if (null != params) {
                tp.binding(params);
            }
            templateMap.put(fileName, tp.render());
        }
    }
    return templateMap;
}
 
Example #10
Source File: GradleCodeBuilder.java    From ApplicationPower with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, String> handleTemplates() {
    //key is path ,value is template content
    Map<String, String> templates = new HashMap<>();
    //init settings
    Template settingsTpl = BeetlTemplateUtil.getByName(SETTINGS_TPL);
    settingsTpl.binding(GeneratorConstant.COMMON_VARIABLE);
    String settingsOut = PathUtil.connectPath(getBasePath(), "settings.gradle");
    templates.put(settingsOut, settingsTpl.render());

    Template buildTpl = BeetlTemplateUtil.getByName(BUILD_TPL);
    buildTpl.binding("basePackage", GeneratorProperties.basePackage());
    buildTpl.binding("springBootVersion", "${springBootVersion}");
    buildTpl.binding("useJTA", GeneratorProperties.isJTA());
    buildTpl.binding("isMultipleDataSource", GeneratorProperties.isMultipleDataSource());
    buildTpl.binding("jdkVersion", "${java.version}");
    buildTpl.binding("isUseDocker", GeneratorProperties.useDocker());
    String buildOut = PathUtil.connectPath(getBasePath(), "build.gradle");
    templates.put(buildOut, buildTpl.render());

    return templates;
}
 
Example #11
Source File: SetTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testPojo() throws Exception
{
	Template t = gt.getTemplate("/exp/set_pojo_template.html");
	User user = new User("joelli");
	User lover = new User("lucymiao");
	t.binding("user", user);
	t.binding("lover", lover);
	String str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/exp/set_pojo_expected.html"), str);

	t = gt.getTemplate("/exp/set_pojo_template.html");
	t.binding("user", user);
	t.binding("lover", lover);
	str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/exp/set_pojo_expected.html"), str);
}
 
Example #12
Source File: ModelBuilder.java    From ApplicationPower with Apache License 2.0 6 votes vote down vote up
@Override
public String generateTemplate(TableInfo tableInfo) {
    String tableName = tableInfo.getName();
    String tableTemp = StringUtil.removePrefix(tableName, GeneratorProperties.tablePrefix());
    String entitySimpleName = StringUtil.toCapitalizeCamelCase(tableTemp);//类名
    Map<String, Column> columnMap = tableInfo.getColumnsInfo();
    String fields = generateFields(columnMap);
    String gettersAndSetters = this.generateSetAndGetMethods(columnMap);
    String imports = this.generateImport(columnMap);
    String toString = this.generateToStringMethod(entitySimpleName, columnMap);
    String templateName = GeneratorProperties.getDbTemplatePath()+"/"+ConstVal.TPL_ENTITY;
    Template template = BeetlTemplateUtil.getByName(templateName);
    template.binding(GeneratorConstant.COMMON_VARIABLE);//作者
    template.binding(GeneratorConstant.ENTITY_SIMPLE_NAME, entitySimpleName);//类名
    template.binding(GeneratorConstant.FIELDS, fields);//字段
    template.binding(GeneratorConstant.GETTERS_AND_SETTERS, gettersAndSetters);//get和set方法
    template.binding(GeneratorConstant.TABLE_COMMENT, tableInfo.getRemarks());//表注释
    template.binding(GeneratorConstant.TO_STRING, toString);
    template.binding(GeneratorConstant.LOMBOK, GeneratorProperties.useLombok());
    template.binding(GeneratorConstant.TABLE_NAME,tableName);
    template.binding("SerialVersionUID", String.valueOf(UUID.randomUUID().getLeastSignificantBits()));
    template.binding("modelImports", imports);
    return template.render();
}
 
Example #13
Source File: GeneralNumberTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testCompare() throws Exception
{
	Template t = gt.getTemplate("/exp/general_compare_template.html");
	this.bind(t, "d1", d1, "d2", d2);
	String str = t.render();

	AssertJUnit.assertEquals(this.getFileContent("/exp/general_compare_expected.html"), str);

	t = gt.getTemplate("/exp/general_compare_template.html");
	this.bind(t, "d1", d1, "d2", d2);
	str = t.render();

	AssertJUnit.assertEquals(this.getFileContent("/exp/general_compare_expected.html"), str);

}
 
Example #14
Source File: HtmlVarBinddingTagTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testEmptyTag() throws Exception
{

	//将默认搜索路径更改到tag目录下
	gt.registerTag("tagbinding", VarBindingSampleTag.class);

	Template t = gt.getTemplate("/tag/binding/tagbinding_template.html");
	String str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/tag/binding/tagbinding_expected.html"), str);

	t = gt.getTemplate("/tag/binding/tagbinding_template.html");
	str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/tag/binding/tagbinding_expected.html"), str);

}
 
Example #15
Source File: HtmlVarBinddingTagTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testLoopTag() throws Exception
{

	//将默认搜索路径更改到tag目录下
	gt.registerTag("tagloopbinding", VarBindingLoopSampleTag.class);
	//todo 会多出一个空行
	Template t = gt.getTemplate("/tag/binding/tagloopbinding_template.html");
	String str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/tag/binding/tagloopbinding_expected.html"), str);

	t = gt.getTemplate("/tag/binding/tagloopbinding_template.html");
	str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/tag/binding/tagloopbinding_expected.html"), str);

}
 
Example #16
Source File: LayoutTagTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testLayout() throws Exception
{

	User user = User.getTestUser();

	Template t = gt.getTemplate("/tag/layout_template.html");
	this.bind(t, "user", user);
	String str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/tag/layout_expected.html"), str);
	
			t = gt.getTemplate("/tag/layout_template.html");
			this.bind(t, "user", user);
			str = t.render();
			AssertJUnit.assertEquals(this.getFileContent("/tag/layout_expected.html"), str);
	
}
 
Example #17
Source File: HTMLTagSupportWrapper.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void callHtmlTag(String path)
{
	Template t = null;

	t = gt.getHtmlFunctionOrTagTemplate(path, this.ctx.getResourceId());

	t.binding(ctx.globalVar);
	t.dynamic(ctx.objectKeys);

	if (args.length == 2)
	{
		Map<String, Object> map = (Map<String, Object>) args[1];
		for (Entry<String, Object> entry : map.entrySet())
		{
			t.binding(entry.getKey(), entry.getValue());

		}
	}

	BodyContent bodyContent = super.getBodyContent();
	t.binding("tagBody", bodyContent);

	t.renderTo(ctx.byteWriter);
}
 
Example #18
Source File: GroupTemplateTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void main(String[] args) throws Exception {

		String home = System.getProperty("user.dir") + File.separator
				+ "template" + File.separator;
		Configuration cf = Configuration.defaultConfiguration();
		cf.setStatementStart("<!--:");
		cf.setStatementEnd("-->");
		FileResourceLoader rs = new FileResourceLoader(home, cf.getCharset());
		GroupTemplate gt = new GroupTemplate(rs, cf);

		List<StockModel> list = StockModel.dummyItems();

		Template t = gt.getTemplate("/helloworld.html");
		t.binding("items", list);
		StringWriter sw = new StringWriter();
		t.renderTo(sw);
		System.out.println(sw.toString());

		// 第二次
		t = gt.getTemplate("/helloworld.html");
		t.binding("items", list);
		sw = new StringWriter();
		t.renderTo(sw);
		System.out.println(sw.toString());

	}
 
Example #19
Source File: FormatterTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testCore() throws Exception
{

	gt.registerFormat("date.short", new ShortDateFormatter());

	Map map = new HashMap();
	map.put("a", 1.12);
	Template t = gt.getTemplate("/formatter/formatter_template.html");
	t.binding(map);
	String str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/formatter/formatter_expected.html"), str);

	t = gt.getTemplate("/formatter/formatter_template.html");
	t.binding(map);
	str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/formatter/formatter_expected.html"), str);
}
 
Example #20
Source File: GroupTemplateTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void main(String[] args) throws Exception {

		String home = System.getProperty("user.dir") + File.separator
				+ "template" + File.separator;
		Configuration cf = Configuration.defaultConfiguration();
		cf.setStatementStart("<!--:");
		cf.setStatementEnd("-->");
		FileResourceLoader rs = new FileResourceLoader(home, cf.getCharset());
		GroupTemplate gt = new GroupTemplate(rs, cf);

		List<StockModel> list = StockModel.dummyItems();

		Template t = gt.getTemplate("/helloworld.html");
		t.binding("items", list);
		StringWriter sw = new StringWriter();
		t.renderTo(sw);
		System.out.println(sw.toString());

		// 第二次
		t = gt.getTemplate("/helloworld.html");
		t.binding("items", list);
		sw = new StringWriter();
		t.renderTo(sw);
		System.out.println(sw.toString());

	}
 
Example #21
Source File: DocBuilderTemplate.java    From smart-doc with Apache License 2.0 5 votes vote down vote up
/**
 * Generate a single controller api document
 *
 * @param projectBuilder projectBuilder
 * @param controllerName controller name
 * @param template       template
 * @param fileExtension  file extension
 */
public void buildSingleApi(ProjectDocConfigBuilder projectBuilder, String controllerName, String template, String fileExtension) {
    ApiConfig config = projectBuilder.getApiConfig();
    FileUtil.mkdirs(config.getOutPath());
    IDocBuildTemplate<ApiDoc> docBuildTemplate = new SpringBootDocBuildTemplate();
    ApiDoc doc = docBuildTemplate.getSingleApiData(projectBuilder, controllerName);
    Template mapper = BeetlTemplateUtil.getByName(template);
    mapper.binding(TemplateVariable.DESC.getVariable(), doc.getDesc());
    mapper.binding(TemplateVariable.NAME.getVariable(), doc.getName());
    mapper.binding(TemplateVariable.LIST.getVariable(), doc.getList());
    FileUtil.writeFileNotAppend(mapper.render(), config.getOutPath() + FILE_SEPARATOR + doc.getName() + fileExtension);
}
 
Example #22
Source File: ServiceImplBuilder.java    From ApplicationPower with Apache License 2.0 5 votes vote down vote up
@Override
public String generateTemplate(TableInfo tableInfo) {
    String tableTemp = StringUtil.removePrefix(tableInfo.getName(), GeneratorProperties.tablePrefix());
    String entityName = StringUtil.toCapitalizeCamelCase(tableTemp);
    String entitySimpleName = StringUtil.toCapitalizeCamelCase(entityName);//类名
    String firstLowName = StringUtil.firstToLowerCase(entitySimpleName);
    String templateName = GeneratorProperties.getDbTemplatePath()+"/"+ConstVal.TPL_SERVICEIMPL;
    Template serviceImplTemplate = BeetlTemplateUtil.getByName(templateName);
    serviceImplTemplate.binding(GeneratorConstant.PRIMARY_KEY_TYPE, tableInfo.getPrimaryKeyType());
    serviceImplTemplate.binding(GeneratorConstant.COMMON_VARIABLE);//作者
    serviceImplTemplate.binding(GeneratorConstant.FIRST_LOWER_NAME, firstLowName);
    serviceImplTemplate.binding(GeneratorConstant.ENTITY_SIMPLE_NAME, entitySimpleName);//类名
    serviceImplTemplate.binding(GeneratorProperties.getGenerateMethods());//过滤方法
    return serviceImplTemplate.render();
}
 
Example #23
Source File: MapperBuilder.java    From ApplicationPower with Apache License 2.0 5 votes vote down vote up
@Override
public String generateTemplate(TableInfo tableInfo) {
    String tableName = tableInfo.getName();
    String tableTemp = StringUtil.removePrefix(tableName, GeneratorProperties.tablePrefix());
    String entitySimpleName = StringUtil.toCapitalizeCamelCase(tableTemp);//类名
    String firstLowName = StringUtil.firstToLowerCase(entitySimpleName);
    Map<String, Column> columnMap = tableInfo.getColumnsInfo();
    String insertSql = generateInsertSql(columnMap, tableName);
    String batchInsertSql = generateBatchInsertSql(columnMap, tableName);
    String updateSql = generateConditionUpdateSql(tableInfo);
    String batchUpdateSql = generateBatchUpdateSql(tableInfo);
    String selectSql = generateSelectSql(tableInfo);
    String results = generateResultMap(columnMap);
    String primaryKey = getPrimaryKey(columnMap);
    String template = GeneratorProperties.getDbTemplatePath()+"/"+ConstVal.TPL_MAPPER;
    Template mapper = BeetlTemplateUtil.getByName(template);
    String idType = TypeConvert.mybatisType(tableInfo.getPrimaryKeyType());
    mapper.binding(GeneratorConstant.PRIMARY_KEY_TYPE, idType);
    mapper.binding(GeneratorConstant.FIRST_LOWER_NAME, firstLowName);
    mapper.binding(GeneratorConstant.ENTITY_SIMPLE_NAME, entitySimpleName);//类名
    mapper.binding(GeneratorConstant.BASE_PACKAGE, GeneratorProperties.basePackage());//基包名
    mapper.binding(GeneratorConstant.INSERT_SQL, insertSql);
    mapper.binding(GeneratorConstant.BATCH_INSERT_SQL, batchInsertSql);
    mapper.binding(GeneratorConstant.UPDATE_SQL, updateSql);
    // mapper.binding(GeneratorConstant.BATCH_UPDATE_SQL,batchUpdateSql);
    mapper.binding(GeneratorConstant.SELECT_SQL, selectSql);
    mapper.binding(GeneratorConstant.RESULT_MAP, results);
    mapper.binding(GeneratorConstant.IS_RESULT_MAP, GeneratorProperties.getResultMap());
    mapper.binding(GeneratorConstant.TABLE_NAME, tableName);
    mapper.binding(GeneratorConstant.PRIMARY_KEY, primaryKey);
    mapper.binding(GeneratorProperties.getGenerateMethods());//过滤方法
    return mapper.render();
}
 
Example #24
Source File: ServiceTestBuilder.java    From ApplicationPower with Apache License 2.0 5 votes vote down vote up
@Override
public String generateTemplate(TableInfo tableInfo) {
    String tableTemp = StringUtil.removePrefix(tableInfo.getName(), GeneratorProperties.tablePrefix());
    String entitySimpleName = StringUtil.toCapitalizeCamelCase(tableTemp);//类名
    String firstLowName = StringUtil.firstToLowerCase(entitySimpleName);
    String templateName = GeneratorProperties.getDbTemplatePath()+"/"+ConstVal.TPL_SERVICE_TEST;
    Template serviceTestTemplate = BeetlTemplateUtil.getByName(templateName);
    serviceTestTemplate.binding(GeneratorConstant.COMMON_VARIABLE);//作者
    serviceTestTemplate.binding(GeneratorConstant.FIRST_LOWER_NAME, firstLowName);
    serviceTestTemplate.binding(GeneratorConstant.ENTITY_SIMPLE_NAME, entitySimpleName);//类名
    serviceTestTemplate.binding(GeneratorProperties.getGenerateMethods());//过滤方法
    return serviceTestTemplate.render();
}
 
Example #25
Source File: NativeTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testMethod() throws Exception
{
	NativeTest test = new NativeTest();
	Template t = gt.getTemplate("/nat/nat_method_template.html");
	this.bind(t, "test", test);
	String str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/nat/nat_method_expected.html"), str);

	t = gt.getTemplate("/nat/nat_method_template.html");
	this.bind(t, "test", test);
	str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/nat/nat_method_expected.html"), str);
}
 
Example #26
Source File: CodeWriter.java    From ApplicationPower with Apache License 2.0 5 votes vote down vote up
private void writeDbSourceAndJTACode(ConfigBuilder config, SpringBootProjectConfig projectConfig) {
        String basePackage = GeneratorProperties.basePackage();
        Map<String, String> dirMap = config.getPathInfo();
        Set<String> dataSources = GeneratorProperties.getMultipleDataSource();
        if (GeneratorProperties.isJTA() || dataSources.size() > 0) {
            Template jtaTpl = BeetlTemplateUtil.getByName(ConstVal.TPL_JTA);
            jtaTpl.binding(GeneratorConstant.COMMON_VARIABLE);
//            FileUtil.writeFileNotAppend(jtaTpl.render(),dirMap.get(ConstVal.DATA_SOURCE_FIG)+"\\TransactionManagerConfig.java");
        }
        if (dataSources.size() > 0) {
            String configPath = dirMap.get(ConstVal.DATA_SOURCE_FIG);
            Template aspectTpl = BeetlTemplateUtil.getByName(ConstVal.TPL_DATASOURCE_ASPECT);
            aspectTpl.binding(GeneratorConstant.COMMON_VARIABLE);
            FileUtil.writeFileNotAppend(aspectTpl.render(), dirMap.get(ConstVal.ASPECT) + ConstVal.FILE_SEPARATOR + "DbAspect.java");

            DataSourceKeyBuilder sourceKeyBuilder = new DataSourceKeyBuilder();
            String dataSourceTpl = sourceKeyBuilder.builderDataSourceKey(dataSources);
            FileUtil.writeFileNotAppend(dataSourceTpl, dirMap.get(ConstVal.CONSTANTS) + ConstVal.FILE_SEPARATOR + "DataSourceKey.java");

            Template abstractCfg = BeetlTemplateUtil.getByName(ConstVal.TPL_DATASOURCE_CFG);
            abstractCfg.binding(GeneratorConstant.COMMON_VARIABLE);
            FileUtil.writeFileNotAppend(abstractCfg.render(), configPath + ConstVal.FILE_SEPARATOR + "AbstractDataSourceConfig.java");

            SpringBootMybatisCfgBuilder builder = new SpringBootMybatisCfgBuilder();
            String mybatisCfgTpl = builder.createMybatisCfg(dataSources);
            FileUtil.writeFileNotAppend(mybatisCfgTpl, configPath + ConstVal.FILE_SEPARATOR + "MyBatisConfig.java");
        }
    }
 
Example #27
Source File: RpcHtmlBuilder.java    From smart-doc with Apache License 2.0 5 votes vote down vote up
/**
 * build error_code html
 *
 * @param errorCodeList list of error code
 * @param outPath
 */
private static void buildErrorCodeDoc(List<ApiErrorCode> errorCodeList, String outPath) {
    if (CollectionUtil.isNotEmpty(errorCodeList)) {
        Template error = BeetlTemplateUtil.getByName(ERROR_CODE_LIST_MD_TPL);
        error.binding(TemplateVariable.LIST.getVariable(), errorCodeList);
        String errorHtml = MarkDownUtil.toHtml(error.render());
        Template errorCodeDoc = BeetlTemplateUtil.getByName(HTML_API_DOC_TPL);
        errorCodeDoc.binding(TemplateVariable.VERSION.getVariable(), now);
        errorCodeDoc.binding(TemplateVariable.HTML.getVariable(), errorHtml);
        errorCodeDoc.binding(TemplateVariable.TITLE.getVariable(), ERROR_CODE_LIST_EN_TITLE);
        errorCodeDoc.binding(TemplateVariable.CREATE_TIME.getVariable(), DateTimeUtil.long2Str(now, DateTimeUtil.DATE_FORMAT_SECOND));
        FileUtil.nioWriteFile(errorCodeDoc.render(), outPath + FILE_SEPARATOR + "error_code.html");
    }
}
 
Example #28
Source File: PojoTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSimple() throws Exception
{

	Pojo p = new Pojo();
	Template t = gt.getTemplate("/lang/pojo_template.html");
	t.binding("p",p);
	String str = t.render();
	AssertJUnit.assertEquals(this.getFileContent("/lang/pojo_expected.html"), str);

}
 
Example #29
Source File: WordprocessingMLBeetlTemplate.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * 使用Beetl模板引擎渲染模板
 * @param template :模板内容
 * @param variables :变量
 * @return {@link WordprocessingMLPackage} 对象
 * @throws Exception :异常对象
 */
@Override
public WordprocessingMLPackage process(String template, Map<String, Object> variables) throws Exception {
	//使用Beetl模板引擎渲染模板
	Template beeTemplate = getEngine().getTemplate(template);
	beeTemplate.binding(variables);
	//获取模板渲染后的结果
	String html = beeTemplate.render();
	//使用HtmlTemplate进行渲染
	return mlHtmlTemplate.process(html, variables);
}
 
Example #30
Source File: ControllerTestBuilder.java    From ApplicationPower with Apache License 2.0 5 votes vote down vote up
@Override
public String generateTemplate(TableInfo tableInfo) {
    Map<String, Column> columnMap = tableInfo.getColumnsInfo();
    //实体名需要移除表前缀
    String tableTemp = StringUtil.removePrefix(tableInfo.getName(), GeneratorProperties.tablePrefix());
    String entitySimpleName = StringUtil.toCapitalizeCamelCase(tableTemp);//类名
    String firstLowName = StringUtil.firstToLowerCase(entitySimpleName);//类实例变量名
    Template controllerTemplate = BeetlTemplateUtil.getByName(ConstVal.TPL_CONTROLLER_TEST);
    controllerTemplate.binding(GeneratorConstant.COMMON_VARIABLE);//作者
    controllerTemplate.binding(GeneratorConstant.FIRST_LOWER_NAME, firstLowName);
    controllerTemplate.binding(GeneratorConstant.ENTITY_SIMPLE_NAME, entitySimpleName);//类名
    controllerTemplate.binding(controllerTestParams, generateParams(columnMap));
    controllerTemplate.binding(GeneratorProperties.getGenerateMethods());
    return controllerTemplate.render();
}