博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IO Foundation 4- 目录结构检查器 FolderStructureChecker
阅读量:7105 次
发布时间:2019-06-28

本文共 3853 字,大约阅读时间需要 12 分钟。

 

需求:

这又是一个非常有趣的需求,我们需要一个目录有以下固定的结构:

 

目录

-css

 --1.css

 --abc.css

 

-js

 -js1.js

 -js2.js

 

-image

 -image1.jpg

 -image2.gif

 

也就是说:

子目录必须是css,js,image,如果有其他的子目录,则说明这个目录结构不合法

 

实现:

 
  1. /** 
  2.  
  3. * This class is used for checking whether the folder has the correct folder 
  4.  
  5. * structure 
  6.  
  7. * 
  8.  
  9.  * @author cwang58 
  10.  
  11. * @created date: Aug 3, 2012 
  12.  
  13. */ 
  14.  
  15. public class FolderStructureChecker implements IFolderStructureChecker { 
  16.  
  17.   
  18.  
  19.          private final Logger logger = LoggerFactory 
  20.  
  21.                           .getLogger(FolderStructureChecker.class); 
  22.  
  23.   
  24.  
  25.          private static ConfigureUtility configureUtility; 
  26.  
  27.   
  28.  
  29.          private String javascriptFolderName; 
  30.  
  31.          private String cssFolderName; 
  32.  
  33.          private String imageFolderName; 
  34.  
  35.   
  36.  
  37.          public FolderStructureChecker() throws Exception { 
  38.  
  39.   
  40.  
  41.                  try { 
  42.  
  43.                           configureUtility = ConfigureUtility.getInstance(); 
  44.  
  45.   
  46.  
  47.                           setJavascriptFolderName(configureUtility 
  48.  
  49.                                             .getProperty(JAVASCRIPT_FOLDER)); 
  50.  
  51.                           setCssFolderName(configureUtility.getProperty(CSS_FOLDER)); 
  52.  
  53.                           setImageFolderName(configureUtility.getProperty(IMAGE_FOLDER)); 
  54.  
  55.   
  56.  
  57.                  } catch (Exception ex) { 
  58.  
  59.   
  60.  
  61.                           if (logger.isErrorEnabled()) { 
  62.  
  63.                                    logger.error("fail when parsing the zip bundle configureation file"); 
  64.  
  65.   
  66.  
  67.                           } 
  68.  
  69.   
  70.  
  71.                           throw new ConfigureSystemException( 
  72.  
  73.                                             "failed when parsing the properties file ", ex); 
  74.  
  75.                  } 
  76.  
  77.   
  78.  
  79.          } 
  80.  
  81.   
  82.  
  83.          /** 
  84.  
  85.          * this file can be used for checking the zip file structure ,if it matches 
  86.  
  87.          * the same structure as following 
  88.  
  89.          * 
  90.  
  91.           * --js --js1.js --js2.js --css --css1.css --css2.css --image --a.jpg 
  92.  
  93.          * --b.png --c.git index.htm 
  94.  
  95.          * 
  96.  
  97.           * 
  98.  
  99.           * then ok 
  100.  
  101.          * 
  102.  
  103.           * if the file name doens't match the one listed here,or if the root has 
  104.  
  105.          * extra folder name ,it throws Exception 
  106.  
  107.          * 
  108.  
  109.           * 
  110.  
  111.           * @param source 
  112.  
  113.          *            the path of the folder that we need to check the folder 
  114.  
  115.          *            structure 
  116.  
  117.          */ 
  118.  
  119.          public void checkFolderStructure(String source) throws Exception { 
  120.  
  121.                  // TODO Auto-generated method stub 
  122.  
  123.   
  124.  
  125.                  File rootFolder = new File(source); 
  126.  
  127.                  if (!rootFolder.isDirectory()) { 
  128.  
  129.   
  130.  
  131.                           if (logger.isErrorEnabled()) { 
  132.  
  133.                                    logger.error("The root should be a directory not a file"); 
  134.  
  135.                           } 
  136.  
  137.                           throw new ParameterInvalidException("The folder root:" + source 
  138.  
  139.                                             + " should be a directory not a file"); 
  140.  
  141.                  } 
  142.  
  143.   
  144.  
  145.                  // get all the directory sub-folder of the root folder 
  146.  
  147.                  File[] files = rootFolder.listFiles(); 
  148.  
  149.   
  150.  
  151.                  // this list is only used for storing all the subFolders inside the root 
  152.  
  153.                  // folder 
  154.  
  155.                  List<File> subFoldersInRootFolder = new ArrayList<File>(); 
  156.  
  157.                  for (File file : files) { 
  158.  
  159.   
  160.  
  161.                           if (file.isDirectory()) 
  162.  
  163.                                    subFoldersInRootFolder.add(file); 
  164.  
  165.                  } 
  166.  
  167.   
  168.  
  169.                  // first ,check the number of sub-folders whether it is 3( has 
  170.  
  171.                  // "css","js","image" 3 folders totally) 
  172.  
  173.                  // if the number is not 3, then the check returns false 
  174.  
  175.                  if (subFoldersInRootFolder.size() != 3) { 
  176.  
  177.   
  178.  
  179.                           if (logger.isErrorEnabled()) { 
  180.  
  181.                                    logger.error("The number of the sub folders of root foler doesn't match"); 
  182.  
  183.                           } 
  184.  
  185.                           throw new InvalidFolderStructureException( 
  186.  
  187.                                             "In folder struture check,the number of the sub folders of root foler doesn't match"); 
  188.  
  189.                  } 
  190.  
  191.   
  192.  
  193.                  // traverse all the subFolders 
  194.  
  195.                  for (File subFolder : subFoldersInRootFolder) { 
  196.  
  197.   
  198.  
  199.                           // get the folder name of the sub-folder 
  200.  
  201.                           String subFolderName = subFolder.getName(); 
  202.  
  203.   
  204.  
  205.                           if (logger.isDebugEnabled()) { 
  206.  
  207.                                    logger.debug("checking the subFolder named:" + subFolderName); 
  208.  
  209.                           } 
  210.  
  211.   
  212.  
  213.                           // execute the comparation ,if the subFolderName doesn't match any 
  214.  
  215.                           // one of {"image","js","css"} 
  216.  
  217.                           // then ,the check will return false 
  218.  
  219.                           if ((!subFolderName.trim().equals(getJavascriptFolderName())) 
  220.  
  221.                                             && (!subFolderName.trim().equals(getCssFolderName())) 
  222.  
  223.                                             && (!subFolderName.trim().equals(getImageFolderName()))) { 
  224.  
  225.   
  226.  
  227.                                    if (logger.isErrorEnabled()) { 
  228.  
  229.                                             logger.error("the folder name: " + subFolderName 
  230.  
  231.                                                              + " is not what we expected"); 
  232.  
  233.                                    } 
  234.  
  235.   
  236.  
  237.                                    throw new InvalidFolderStructureException("the folder name: " 
  238.  
  239.                                                     + subFolderName + " is not what we expected"); 
  240.  
  241.                           } 
  242.  
  243.   
  244.  
  245.                  } 
  246.  
  247.   
  248.  
  249.          } 
  250.  
  251.   
  252.  
  253.          //setter,getter这里略去 
  254.   
  255.  

 

核心方法是从第119行开始的checkFolderStructure方法,它会

(1)先在第127-141行对参数进行判断,看它是否为一个目录

(2)然后对于目录,第145-165吧根目录的所有子目录读入数组中

(3)第169-189,看子目录的数目是否为3,因为我们已经约定,只可以允许css,js,image这3个目录,多一个不能少一个不行。

(4)第193-245行则是遍历每个目录的目录名,看他们的名字是否为css,js,image中的一个。如果不是,则出错。

 

本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/980313,如需转载请自行联系原作者
你可能感兴趣的文章
OpenResty(nginx扩展)实现防cc攻击
查看>>
Struts2 学习系列 (4) ValueStack和OGNL
查看>>
H2数据库使用
查看>>
转一篇文章:MySQL 通过idb文件恢复Innodb 数据
查看>>
002SilverFox的介绍(01)
查看>>
蓝牙1.1~5.0不同版本特性简介(技术扫盲贴)
查看>>
名词王国里的死刑(翻译) - A Story of Hello World
查看>>
跟我学网站开发框架CodeIgniter之url篇
查看>>
ssh connect no route
查看>>
LifecycleMBeanBase
查看>>
转载:AbstractQueuedSynchronizer的实现分析(下)
查看>>
Spring IDE 添加xml文件
查看>>
“三缺一”微信谈判猜想 运营商毫无“底牌”
查看>>
base64 加密解密 golang (转)
查看>>
E: Could not get lock /var/lib/dpkg/lock
查看>>
Linux-Java线上故障排查一(高cpu使用率)
查看>>
MySQL的内部XA事务
查看>>
vi 常用命令行
查看>>
优酷去掉广告代码
查看>>
iptables简单设置
查看>>