在上面文章http://supercharles888.blog.51cto.com/609344/980309 中我们已经提到可以用StringTemplateParser来动态的利用可配置规则,来吧一个有占位符的字符串替换为没有占位符的字符串。所以我们这里提到的类 FileParser,就是利用了StringTemplateParser,它读取有占位符的文件,然后转为没有占位符的文件。

 

直接上代码:

 

 
  
  1. ...
  2. //忽略所有import语句
  3.  
  4.   
  5.  
  6. /** 
  7.  
  8. * 
  9.  
  10.  * The class can do the file parse work as follows: first it will read the 
  11.  
  12. * content of the input file from the location , then,it will invoke the 
  13.  
  14. * StringTemplateParser utility method to do the parsing work(replacing the 
  15.  
  16. * placeholders) finally, it will generate a new file with the new contents 
  17.  
  18. * 
  19.  
  20.  * @author cwang58 
  21.  
  22. * @created date: Aug 2, 2012 
  23.  
  24. */ 
  25.  
  26. public class FileParser implements IFileParser { 
  27.  
  28.   
  29.  
  30.          private static Logger logger = LoggerFactory.getLogger(FileParser.class); 
  31.  
  32.   
  33.  
  34.          private IStringTemplateParser stp; 
  35.  
  36.   
  37.  
  38.          public FileParser() throws Exception { 
  39.  
  40.   
  41.  
  42.                  stp = new StringTemplateParser(); 
  43.  
  44.   
  45.  
  46.          } 
  47.  
  48.   
  49.  
  50.          public IStringTemplateParser getStp() { 
  51.  
  52.                  return stp; 
  53.  
  54.          } 
  55.  
  56.   
  57.  
  58.          public void setStp(IStringTemplateParser stp) { 
  59.  
  60.                  this.stp = stp; 
  61.  
  62.          } 
  63.  
  64.   
  65.  
  66.          /** 
  67.  
  68.          * this method will read the content from the file designated by parameter 
  69.  
  70.          * 
  71.  
  72.           * @param source 
  73.  
  74.          *            the location of the file that we read from 
  75.  
  76.          * @return the string content of the file 
  77.  
  78.          * @throws Exception 
  79.  
  80.          */ 
  81.  
  82.          public String readContentFromFile(String source) throws Exception { 
  83.  
  84.   
  85.  
  86.                  File file = new File(source); 
  87.  
  88.   
  89.  
  90.                  // first make sure the source is a file ,not a directory 
  91.  
  92.                  if (file.isDirectory()) { 
  93.  
  94.   
  95.  
  96.                           if (logger.isErrorEnabled()) { 
  97.  
  98.                                    logger.error("the source file can not be a directory"); 
  99.  
  100.                           } 
  101.  
  102.   
  103.  
  104.                           throw new ParameterInvalidException(FILE_REPLACEMENT_ERROR); 
  105.  
  106.                  } 
  107.  
  108.   
  109.  
  110.                  // open a file reader to read the file content 
  111.  
  112.                  BufferedReader reader = null
  113.  
  114.                  try { 
  115.  
  116.                           reader = new BufferedReader(new FileReader(file)); 
  117.  
  118.   
  119.  
  120.                           StringBuffer bufferedFileContent = new StringBuffer(); 
  121.  
  122.                           String line = null
  123.  
  124.   
  125.  
  126.                           while ((line = reader.readLine()) != null) { 
  127.  
  128.                                    bufferedFileContent.append(line + LINE_SEPARATOR); 
  129.  
  130.                           } 
  131.  
  132.                           reader.close(); 
  133.  
  134.   
  135.  
  136.                           return bufferedFileContent.toString(); 
  137.  
  138.   
  139.  
  140.                  } catch (FileNotFoundException ex) { 
  141.  
  142.   
  143.  
  144.                           if (logger.isErrorEnabled()) { 
  145.  
  146.                                    logger.error("File is not found"); 
  147.  
  148.   
  149.  
  150.                           } 
  151.  
  152.                           throw new FileReplacerException(FILE_REPLACEMENT_ERROR, ex); 
  153.  
  154.                  } catch (IOException ex) { 
  155.  
  156.   
  157.  
  158.                           if (logger.isErrorEnabled()) { 
  159.  
  160.                                    logger.error("IO didn't open or closed"); 
  161.  
  162.                           } 
  163.  
  164.                           throw new FileReplacerException(FILE_REPLACEMENT_ERROR, ex); 
  165.  
  166.   
  167.  
  168.                  } 
  169.  
  170.   
  171.  
  172.          } 
  173.  
  174.   
  175.  
  176.          /** 
  177.  
  178.          * This method is used for writing the string content to a file given by 
  179.  
  180.          * parameter 
  181.  
  182.          * 
  183.  
  184.           * @param fileContent 
  185.  
  186.          *            the string content 
  187.  
  188.          * @param destination 
  189.  
  190.          *            the path of the destination file which we want to write to 
  191.  
  192.          * @throws Exception 
  193.  
  194.          * 
  195.  
  196.           */ 
  197.  
  198.          public void writeContentToFile(String fileContent, String destination) 
  199.  
  200.                           throws Exception { 
  201.  
  202.   
  203.  
  204.                  File file = new File(destination); 
  205.  
  206.   
  207.  
  208.                  // first make sure the source is a file ,not a directory 
  209.  
  210.                  if (file.isDirectory()) { 
  211.  
  212.   
  213.  
  214.                           if (logger.isErrorEnabled()) { 
  215.  
  216.                                    logger.error("the destination file can not be a directory"); 
  217.  
  218.                           } 
  219.  
  220.   
  221.  
  222.                           throw new ParameterInvalidException("the destination file" 
  223.  
  224.                                             + destination + "can not be a directory"); 
  225.  
  226.                  } 
  227.  
  228.   
  229.  
  230.                  // open a file writer to write the file content 
  231.  
  232.                  BufferedWriter writer = null
  233.  
  234.                  try { 
  235.  
  236.                           writer = new BufferedWriter(new FileWriter(file, false)); 
  237.  
  238.   
  239.  
  240.                           writer.write(fileContent); 
  241.  
  242.                           writer.flush(); 
  243.  
  244.   
  245.  
  246.                  } catch (FileNotFoundException ex) { 
  247.  
  248.   
  249.  
  250.                           if (logger.isErrorEnabled()) { 
  251.  
  252.                                    logger.error("File is not found"); 
  253.  
  254.   
  255.  
  256.                           } 
  257.  
  258.                           throw new FileReplacerException("the file:" + destination 
  259.  
  260.                                             + " can't be found", ex); 
  261.  
  262.   
  263.  
  264.                  } catch (IOException ex) { 
  265.  
  266.   
  267.  
  268.                           if (logger.isErrorEnabled()) { 
  269.  
  270.                                    logger.error("IO didn't open or closed"); 
  271.  
  272.   
  273.  
  274.                           } 
  275.  
  276.                           throw new FileReplacerException( 
  277.  
  278.                                             "can't write the contents into file: " + destination, ex); 
  279.  
  280.   
  281.  
  282.                  }finally
  283.  
  284.                           writer.close(); 
  285.  
  286.                  } 
  287.  
  288.   
  289.  
  290.          } 
  291.  
  292.   
  293.  
  294.          /** 
  295.  
  296.          * the process for replacing the giving files placeholder 
  297.  
  298.          * 
  299.  
  300.           * @param source 
  301.  
  302.          *            the location of the file that has place holder 
  303.  
  304.          * @param destination 
  305.  
  306.          *            the location that put the parsed file 
  307.  
  308.          */ 
  309.  
  310.          public void parseFile(String source, String destination) throws Exception { 
  311.  
  312.   
  313.  
  314.                  if (logger.isDebugEnabled()) { 
  315.  
  316.                           logger.debug("the file location which contains placeholder is: " 
  317.  
  318.                                             + source); 
  319.  
  320.                           logger.debug("the file location that we put back is: " 
  321.  
  322.                                             + destination); 
  323.  
  324.                  } 
  325.  
  326.   
  327.  
  328.                  String placeHolderFileContent = readContentFromFile(source); 
  329.  
  330.                  if (logger.isDebugEnabled()) { 
  331.  
  332.                           logger.debug("the content in this placeholder's file is: " 
  333.  
  334.                                             + placeHolderFileContent); 
  335.  
  336.                  } 
  337.  
  338.   
  339.  
  340.                  if (logger.isDebugEnabled()) { 
  341.  
  342.                           logger.debug("Begin to use rule builder to replace the placeholders in the source file"); 
  343.  
  344.                  } 
  345.  
  346.                  String noPlaceHolderFileContent = stp 
  347.  
  348.                                    .parseStringTemplateWithRuleBuilder(placeHolderFileContent); 
  349.  
  350.   
  351.  
  352.                  if (logger.isDebugEnabled()) { 
  353.  
  354.                           logger.debug("After parsing ,the file content with no place holder is: " 
  355.  
  356.                                             + noPlaceHolderFileContent); 
  357.  
  358.                  } 
  359.  
  360.   
  361.  
  362.                  writeContentToFile(noPlaceHolderFileContent, destination); 
  363.  
  364.   
  365.  
  366.                  if (logger.isDebugEnabled()) { 
  367.  
  368.                           logger.debug("writing no placeholder file to " + destination 
  369.  
  370.                                             + " successfully"); 
  371.  
  372.                  } 
  373.  
  374.          } 
  375.  
  376.   
  377.  

 

从这里可以看出,很一目了然的3个方法:

(1)readContentFromFile:

这个方法是很基础的一个io操作类,它的目的是吧一个内含有占位符的文件内容读到一个字符串中,因为我们上一篇文章中提到的StringTemplateParser是关于字符串的操作。

(2)writeContentToFile:

这个方法做了和readContentFromFile相反的事情,它是吧一个字符串内容(一般来说是已经被替换过后的内容)写入指定的文件中。

(3)parseFile:

这个方法就是把(1)和(2)结合了起来,它先调用readContentFromFile方法吧一个有占位符的文件的内容读入一个字符串变量,然后它调用StringTemplateParser类的parseStringTemplateWithRuleBuilder方法吧这个有占位符的字符串转为没有占位符的字符串,最后把这个没有占位符的字符串通过writeContentToFile方法写入到一个指定的文件中。