需求:
这又是一个非常有趣的需求,我们需要一个目录有以下固定的结构:
目录
-css
--1.css
--abc.css
-js
-js1.js
-js2.js
-image
-image1.jpg
-image2.gif
也就是说:
子目录必须是css,js,image,如果有其他的子目录,则说明这个目录结构不合法
实现:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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);
-
- }
-
-
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void checkFolderStructure(String source) throws Exception {
-
-
-
-
-
- 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");
-
- }
-
-
-
-
-
- File[] files = rootFolder.listFiles();
-
-
-
-
-
-
-
- List<File> subFoldersInRootFolder = new ArrayList<File>();
-
- for (File file : files) {
-
-
-
- if (file.isDirectory())
-
- subFoldersInRootFolder.add(file);
-
- }
-
-
-
-
-
-
-
-
-
- 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");
-
- }
-
-
-
-
-
- for (File subFolder : subFoldersInRootFolder) {
-
-
-
-
-
- String subFolderName = subFolder.getName();
-
-
-
- if (logger.isDebugEnabled()) {
-
- logger.debug("checking the subFolder named:" + subFolderName);
-
- }
-
-
-
-
-
-
-
-
-
- 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");
-
- }
-
-
-
- }
-
-
-
- }
-
-
-
-
-
-
- }
核心方法是从第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,如需转载请自行联系原作者