需求:
这又是一个非常有趣的需求,我们需要一个目录有以下固定的结构:
目录
-css
--1.css
--abc.css
-js
-js1.js
-js2.js
-image
-image1.jpg
-image2.gif
也就是说:
子目录必须是css,js,image,如果有其他的子目录,则说明这个目录结构不合法
实现:
- /**
- * This class is used for checking whether the folder has the correct folder
- * structure
- *
- * @author cwang58
- * @created date: Aug 3, 2012
- */
- public class FolderStructureChecker implements IFolderStructureChecker {
- private final Logger logger = LoggerFactory
- .getLogger(FolderStructureChecker.class);
- private static ConfigureUtility configureUtility;
- private String javascriptFolderName;
- private String cssFolderName;
- private String imageFolderName;
- public FolderStructureChecker() throws Exception {
- try {
- configureUtility = ConfigureUtility.getInstance();
- setJavascriptFolderName(configureUtility
- .getProperty(JAVASCRIPT_FOLDER));
- setCssFolderName(configureUtility.getProperty(CSS_FOLDER));
- setImageFolderName(configureUtility.getProperty(IMAGE_FOLDER));
- } catch (Exception ex) {
- if (logger.isErrorEnabled()) {
- logger.error("fail when parsing the zip bundle configureation file");
- }
- throw new ConfigureSystemException(
- "failed when parsing the properties file ", ex);
- }
- }
- /**
- * this file can be used for checking the zip file structure ,if it matches
- * the same structure as following
- *
- * --js --js1.js --js2.js --css --css1.css --css2.css --image --a.jpg
- * --b.png --c.git index.htm
- *
- *
- * then ok
- *
- * if the file name doens't match the one listed here,or if the root has
- * extra folder name ,it throws Exception
- *
- *
- * @param source
- * the path of the folder that we need to check the folder
- * structure
- */
- public void checkFolderStructure(String source) throws Exception {
- // TODO Auto-generated method stub
- File rootFolder = new File(source);
- if (!rootFolder.isDirectory()) {
- if (logger.isErrorEnabled()) {
- logger.error("The root should be a directory not a file");
- }
- throw new ParameterInvalidException("The folder root:" + source
- + " should be a directory not a file");
- }
- // get all the directory sub-folder of the root folder
- File[] files = rootFolder.listFiles();
- // this list is only used for storing all the subFolders inside the root
- // folder
- List<File> subFoldersInRootFolder = new ArrayList<File>();
- for (File file : files) {
- if (file.isDirectory())
- subFoldersInRootFolder.add(file);
- }
- // first ,check the number of sub-folders whether it is 3( has
- // "css","js","image" 3 folders totally)
- // if the number is not 3, then the check returns false
- if (subFoldersInRootFolder.size() != 3) {
- if (logger.isErrorEnabled()) {
- logger.error("The number of the sub folders of root foler doesn't match");
- }
- throw new InvalidFolderStructureException(
- "In folder struture check,the number of the sub folders of root foler doesn't match");
- }
- // traverse all the subFolders
- for (File subFolder : subFoldersInRootFolder) {
- // get the folder name of the sub-folder
- String subFolderName = subFolder.getName();
- if (logger.isDebugEnabled()) {
- logger.debug("checking the subFolder named:" + subFolderName);
- }
- // execute the comparation ,if the subFolderName doesn't match any
- // one of {"image","js","css"}
- // then ,the check will return false
- if ((!subFolderName.trim().equals(getJavascriptFolderName()))
- && (!subFolderName.trim().equals(getCssFolderName()))
- && (!subFolderName.trim().equals(getImageFolderName()))) {
- if (logger.isErrorEnabled()) {
- logger.error("the folder name: " + subFolderName
- + " is not what we expected");
- }
- throw new InvalidFolderStructureException("the folder name: "
- + subFolderName + " is not what we expected");
- }
- }
- }
- //setter,getter这里略去
- }
核心方法是从第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,如需转载请自行联系原作者