View Javadoc

1   package org.kuali.common.util;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.ArrayList;
6   import java.util.Arrays;
7   import java.util.Collections;
8   import java.util.List;
9   
10  import org.apache.commons.io.FileUtils;
11  import org.codehaus.plexus.util.StringUtils;
12  import org.slf4j.Logger;
13  import org.slf4j.LoggerFactory;
14  
15  public class MetaInfUtils {
16  
17  	private static final Logger logger = LoggerFactory.getLogger(MetaInfUtils.class);
18  
19  	public static void scanAndCreateFiles(List<MetaInfContext> contexts) throws IOException {
20  		for (MetaInfContext context : contexts) {
21  			Assert.notNull(context.getBaseDir(), "baseDir is null");
22  			Assert.notNull(context.getOutputFile(), "outputFile is null");
23  			List<String> includes = context.getIncludes();
24  			List<String> excludes = context.getExcludes();
25  			logger.info("Examining - " + context.getBaseDir().getCanonicalPath());
26  			logger.info("Include - " + CollectionUtils.getSpaceSeparatedString(includes));
27  			logger.info("Exclude - " + CollectionUtils.getSpaceSeparatedString(excludes));
28  			SimpleScanner scanner = new SimpleScanner(context.getBaseDir(), includes, excludes);
29  			List<File> files = scanner.getFiles();
30  			logger.info("Located " + files.size() + " files");
31  			List<String> locations = getLocations(context.getBaseDir(), files, context.getPrefix());
32  			if (context.isSort()) {
33  				Collections.sort(locations);
34  			}
35  			logger.info("Creating " + context.getOutputFile().getCanonicalPath());
36  			FileUtils.writeLines(context.getOutputFile(), locations);
37  		}
38  	}
39  
40  	public static void scanAndCreateFile(MetaInfContext context) throws IOException {
41  		scanAndCreateFiles(Arrays.asList(context));
42  	}
43  
44  	public static List<String> getLocations(File baseDir, List<File> files, String prefix) throws IOException {
45  		List<String> locations = new ArrayList<String>();
46  		for (int i = 0; i < files.size(); i++) {
47  			String location = getLocation(baseDir, files.get(i), prefix);
48  			locations.add(location);
49  		}
50  		return locations;
51  	}
52  
53  	public static String getLocation(File baseDir, File file, String prefix) throws IOException {
54  		String dir = baseDir.getCanonicalPath();
55  		String path = file.getCanonicalPath();
56  		int pos = dir.length() + 1;
57  		return prefix + StringUtils.substring(path, pos);
58  	}
59  
60  }