View Javadoc
1 /* 2 * Created on 2003-5-30 10:50:40 by joel guo 3 * 4 * vTradEx Information Technology Inc. 5 */ 6 package com.cyclops.albumbuilder; 7 import java.io.File; 8 import java.io.FileFilter; 9 import java.util.Map; 10 11 import org.apache.commons.collections.MultiHashMap; 12 import org.apache.commons.configuration.Configuration; 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.apache.regexp.RE; 16 import org.apache.regexp.REUtil; 17 import org.apache.stratum.lifecycle.Configurable; 18 import org.apache.stratum.lifecycle.Initializable; 19 /*** 20 * Add description for this class <font color="red">HERE</font>! 21 * <big><font face="黑体">惩前毖后,治病救人</font></big> 22 * 23 * @author <a href="mailto:joeblack.guo@vtradex.com">joel guo</a> 24 * @company <a href="http://www.vtradex.com">vTradEx</a> 25 * @since 2003-5-30 10:50:40 26 */ 27 public class AlbumBuilderComponent implements Configurable, Initializable { 28 /*** Logger object*/ 29 protected Log logger = LogFactory.getLog(getClass()); 30 private Configuration rootConfiguration; 31 private MultiHashMap sourceRepository = new MultiHashMap(); 32 /*** Override method configure() of super class 33 * @see org.apache.stratum.lifecycle.Configurable#configure(org.apache.commons.configuration.Configuration) 34 */ 35 public void configure(Configuration conf) { 36 rootConfiguration = conf; 37 } 38 /*** Method getConfiguration() in class AlbumBuilderComponent 39 * @return Configuration object 40 */ 41 public Configuration getConfiguration() { 42 return rootConfiguration; 43 } 44 /*** Method getSourceRepository() 45 * @return All source resources in a multi map 46 */ 47 protected Map getSourceRepository() { 48 return sourceRepository; 49 } 50 /*** Override method initialize() of super class 51 * @see org.apache.stratum.lifecycle.Initializable#initialize() 52 */ 53 public void initialize() { 54 String[] roots = getConfiguration().getStringArray("source.directory"); 55 String[] patterns = 56 getConfiguration().getStringArray("source.filepattern"); 57 boolean recursive = 58 getConfiguration().getBoolean("source.recursive", false); 59 for (int i = 0; i < roots.length; i++) { 60 String rootDir = roots[i]; 61 scanResource(rootDir, "/", recursive, patterns); 62 } 63 } 64 private boolean scanResource( 65 String rootDir, 66 String relativePath, 67 final boolean recursive, 68 final String[] filePatterns) { 69 File dir = new File(rootDir, relativePath); 70 if (!dir.isDirectory()) { 71 return false; 72 } 73 File[] files = dir.listFiles(new FileFilter() { 74 //用是否递归和文件名模式来过滤当前目录下的文件和子目录 75 //通过一个匿名内部类实现 76 public boolean accept(File file) { 77 if (recursive && file.isDirectory()) { 78 return true; 79 } else { 80 boolean ret = false; 81 for (int i = 0; i < filePatterns.length; i++) { 82 String filePattern = filePatterns[i]; 83 try { 84 RE re = REUtil.createRE("<" + filePattern + ">"); 85 if (re.match("<" + file.getName() + ">")) { 86 ret = true; 87 break; 88 } 89 } catch (Exception e) { 90 logger.warn("Invalid RegExp pattern", e); 91 } 92 } 93 return ret; 94 } 95 } 96 }); 97 boolean ret = false; 98 for (int i = 0; i < files.length; i++) { 99 File file = files[i]; 100 //递归调用子目录 101 if (file.isDirectory()) { 102 ret 103 |= scanResource( 104 rootDir, 105 relativePath + file.getName() + "/", 106 recursive, 107 filePatterns); 108 } else { 109 //记住得到的源图片文件 110 SourceResource sr = 111 new SourceResource(rootDir, relativePath + file.getName()); 112 sourceRepository.put(relativePath, sr); 113 ret = true; 114 } 115 } 116 //如果一个目录有有效的子目录但没有文件,也要把这个目录记下来 117 if (ret && !sourceRepository.containsKey(relativePath)) { 118 sourceRepository.put(relativePath, null); 119 } 120 return ret; 121 } 122 }

This page was automatically generated by Maven