在上面文章http://supercharles888.blog.51cto.com/609344/980309 中我们已经提到可以用StringTemplateParser来动态的利用可配置规则,来吧一个有占位符的字符串替换为没有占位符的字符串。所以我们这里提到的类 FileParser,就是利用了StringTemplateParser,它读取有占位符的文件,然后转为没有占位符的文件。
直接上代码:
- ...
- //忽略所有import语句
- /**
- *
- * The class can do the file parse work as follows: first it will read the
- * content of the input file from the location , then,it will invoke the
- * StringTemplateParser utility method to do the parsing work(replacing the
- * placeholders) finally, it will generate a new file with the new contents
- *
- * @author cwang58
- * @created date: Aug 2, 2012
- */
- public class FileParser implements IFileParser {
- private static Logger logger = LoggerFactory.getLogger(FileParser.class);
- private IStringTemplateParser stp;
- public FileParser() throws Exception {
- stp = new StringTemplateParser();
- }
- public IStringTemplateParser getStp() {
- return stp;
- }
- public void setStp(IStringTemplateParser stp) {
- this.stp = stp;
- }
- /**
- * this method will read the content from the file designated by parameter
- *
- * @param source
- * the location of the file that we read from
- * @return the string content of the file
- * @throws Exception
- */
- public String readContentFromFile(String source) throws Exception {
- File file = new File(source);
- // first make sure the source is a file ,not a directory
- if (file.isDirectory()) {
- if (logger.isErrorEnabled()) {
- logger.error("the source file can not be a directory");
- }
- throw new ParameterInvalidException(FILE_REPLACEMENT_ERROR);
- }
- // open a file reader to read the file content
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader(file));
- StringBuffer bufferedFileContent = new StringBuffer();
- String line = null;
- while ((line = reader.readLine()) != null) {
- bufferedFileContent.append(line + LINE_SEPARATOR);
- }
- reader.close();
- return bufferedFileContent.toString();
- } catch (FileNotFoundException ex) {
- if (logger.isErrorEnabled()) {
- logger.error("File is not found");
- }
- throw new FileReplacerException(FILE_REPLACEMENT_ERROR, ex);
- } catch (IOException ex) {
- if (logger.isErrorEnabled()) {
- logger.error("IO didn't open or closed");
- }
- throw new FileReplacerException(FILE_REPLACEMENT_ERROR, ex);
- }
- }
- /**
- * This method is used for writing the string content to a file given by
- * parameter
- *
- * @param fileContent
- * the string content
- * @param destination
- * the path of the destination file which we want to write to
- * @throws Exception
- *
- */
- public void writeContentToFile(String fileContent, String destination)
- throws Exception {
- File file = new File(destination);
- // first make sure the source is a file ,not a directory
- if (file.isDirectory()) {
- if (logger.isErrorEnabled()) {
- logger.error("the destination file can not be a directory");
- }
- throw new ParameterInvalidException("the destination file"
- + destination + "can not be a directory");
- }
- // open a file writer to write the file content
- BufferedWriter writer = null;
- try {
- writer = new BufferedWriter(new FileWriter(file, false));
- writer.write(fileContent);
- writer.flush();
- } catch (FileNotFoundException ex) {
- if (logger.isErrorEnabled()) {
- logger.error("File is not found");
- }
- throw new FileReplacerException("the file:" + destination
- + " can't be found", ex);
- } catch (IOException ex) {
- if (logger.isErrorEnabled()) {
- logger.error("IO didn't open or closed");
- }
- throw new FileReplacerException(
- "can't write the contents into file: " + destination, ex);
- }finally{
- writer.close();
- }
- }
- /**
- * the process for replacing the giving files placeholder
- *
- * @param source
- * the location of the file that has place holder
- * @param destination
- * the location that put the parsed file
- */
- public void parseFile(String source, String destination) throws Exception {
- if (logger.isDebugEnabled()) {
- logger.debug("the file location which contains placeholder is: "
- + source);
- logger.debug("the file location that we put back is: "
- + destination);
- }
- String placeHolderFileContent = readContentFromFile(source);
- if (logger.isDebugEnabled()) {
- logger.debug("the content in this placeholder's file is: "
- + placeHolderFileContent);
- }
- if (logger.isDebugEnabled()) {
- logger.debug("Begin to use rule builder to replace the placeholders in the source file");
- }
- String noPlaceHolderFileContent = stp
- .parseStringTemplateWithRuleBuilder(placeHolderFileContent);
- if (logger.isDebugEnabled()) {
- logger.debug("After parsing ,the file content with no place holder is: "
- + noPlaceHolderFileContent);
- }
- writeContentToFile(noPlaceHolderFileContent, destination);
- if (logger.isDebugEnabled()) {
- logger.debug("writing no placeholder file to " + destination
- + " successfully");
- }
- }
- }
从这里可以看出,很一目了然的3个方法:
(1)readContentFromFile:
这个方法是很基础的一个io操作类,它的目的是吧一个内含有占位符的文件内容读到一个字符串中,因为我们上一篇文章中提到的StringTemplateParser是关于字符串的操作。
(2)writeContentToFile:
这个方法做了和readContentFromFile相反的事情,它是吧一个字符串内容(一般来说是已经被替换过后的内容)写入指定的文件中。
(3)parseFile:
这个方法就是把(1)和(2)结合了起来,它先调用readContentFromFile方法吧一个有占位符的文件的内容读入一个字符串变量,然后它调用StringTemplateParser类的parseStringTemplateWithRuleBuilder方法吧这个有占位符的字符串转为没有占位符的字符串,最后把这个没有占位符的字符串通过writeContentToFile方法写入到一个指定的文件中。