如何在Java中创建基于模板的文件? [英] How to create Template based files in Java?

查看:310
本文介绍了如何在Java中创建基于模板的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在java和Im中使用Eclipse IDE创建一个模板文件。我想创建一个模板文件,使得一个程序从用户获取参数,应该可以将这些参数粘贴到模板文件中,然后将其另存为一个文件。我该怎么做?



请指导我。



感谢
NB

解决方案

以下是我在开源模板引擎中执行此操作的方式, Chunk

  import com.x5.template.Theme; 
import com.x5.template.Chunk;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

...

public void writeTemplatedFile()throws IOException
{
Theme theme = new Theme();
Chunk chunk = theme.makeChunk(my_template,txt);

//用用户输入替换下面的静态值
chunk.set(name,Lancelot);
chunk.set(favorite_color,blue);

String outfilePath = getFilePath();
文件文件= new File(outfilePath);
FileWriter out = new FileWriter(file);

chunk.render(out);

out.flush();
out.close();
}

my_template.txt(只需放在themes / my_template.txt中的classpath中)

 我的名字是{$ name}。 

我最喜欢的颜色是{$ favorite_color}。

输出:

 我的名字是Lancelot。 

我最喜欢的颜色是蓝色。

通过添加|过滤器和:默认为您的标签,您的模板可以更加明智。 p>

my_template.txt - 示例2

 我的名字是{$ name | defang:[未提供]}。 

我最喜欢的颜色是{$ favorite_color | defang | lc:[未提供]}。

在此示例中,defang过滤器将删除可能有助于形成XSS攻击的任何字符。 lc过滤器将文本更改为小写。如果值为空,则输出冒号后的任何内容。



有一个 Eclipse插件,可直接在Eclipse IDE中编辑Chunk模板。该插件提供语法高亮和模板文档的大纲视图。



块可以做更多的事情,仔细浏览文档以进行快速浏览。全面披露:我喜欢Chunk,部分是因为我创建了它。


I want to create a template file in java and Im using Eclipse IDE. I want to create a template file such that one program which gets the parameters from the users, it should be able to paste these parameters into the template file and then save it as a separate file. How can I do this ?

Please guide me.

Thanks N.B

解决方案

Here's how you would do this in my open-sourced template engine, Chunk.

 import com.x5.template.Theme;
 import com.x5.template.Chunk;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;

 ...

 public void writeTemplatedFile() throws IOException
 {
     Theme theme = new Theme();
     Chunk chunk = theme.makeChunk("my_template","txt");

     // replace static values below with user input
     chunk.set("name", "Lancelot");         
     chunk.set("favorite_color", "blue");

     String outfilePath = getFilePath();
     File file = new File(outfilePath);
     FileWriter out = new FileWriter(file);

     chunk.render( out );

     out.flush();
     out.close();
 }

my_template.txt (simply place in the classpath in themes/my_template.txt)

My name is {$name}.

My favorite color is {$favorite_color}.

Output:

My name is Lancelot.

My favorite color is blue.

Your template can be made a bit smarter by adding |filters and :defaults to your tags.

my_template.txt - example 2

My name is {$name|defang:[not provided]}.

My favorite color is {$favorite_color|defang|lc:[not provided]}.

In this example, the defang filter removes any characters that might help form an XSS attack. The lc filter changes the text to lowercase. And anything after the colon will be output in case the value is null.

There is an Eclipse plugin available for editing Chunk templates directly in the Eclipse IDE. The plugin provides syntax highlighting and an outline view for template documents.

Chunk can do a lot more, take a peek at the docs for a quick tour. Full disclosure: I love Chunk in part because I created it.

这篇关于如何在Java中创建基于模板的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆