001 package org.kuali.common.util;
002
003 import java.io.File;
004 import java.io.IOException;
005 import java.util.ArrayList;
006 import java.util.Arrays;
007 import java.util.Collections;
008 import java.util.List;
009 import java.util.Properties;
010
011 import org.apache.commons.io.FileUtils;
012 import org.codehaus.plexus.util.StringUtils;
013 import org.slf4j.Logger;
014 import org.slf4j.LoggerFactory;
015
016 public class MetaInfUtils {
017
018 private static final Logger logger = LoggerFactory.getLogger(MetaInfUtils.class);
019
020 public static void scanAndCreateFiles(List<MetaInfContext> contexts) throws IOException {
021 for (MetaInfContext context : contexts) {
022 List<File> files = getFiles(context);
023 List<MetaInfResource> resources = getResources(context, files);
024 doLocations(context, resources);
025 if (context.isAddPropertiesFile()) {
026 doProperties(context, resources);
027 }
028 }
029 }
030
031 public static List<File> getFiles(MetaInfContext context) throws IOException {
032 Assert.notNull(context.getBaseDir(), "baseDir is null");
033 Assert.notNull(context.getOutputFile(), "outputFile is null");
034 List<String> includes = context.getIncludes();
035 List<String> excludes = context.getExcludes();
036 logger.info("Examining - " + context.getBaseDir().getCanonicalPath());
037 logger.info("Include - " + CollectionUtils.getSpaceSeparatedString(includes));
038 logger.info("Exclude - " + CollectionUtils.getSpaceSeparatedString(excludes));
039 SimpleScanner scanner = new SimpleScanner(context.getBaseDir(), includes, excludes);
040 List<File> files = scanner.getFiles();
041 logger.info("Located " + files.size() + " files");
042 return files;
043 }
044
045 public static void doLocations(MetaInfContext context, List<MetaInfResource> resources) throws IOException {
046 List<String> locations = getLocations(resources);
047 if (context.isSort()) {
048 Collections.sort(locations);
049 }
050 logger.info("Creating " + context.getOutputFile().getCanonicalPath());
051 FileUtils.writeLines(context.getOutputFile(), locations);
052 }
053
054 public static void doProperties(MetaInfContext context, List<MetaInfResource> resources) {
055 logger.debug("doProperties()");
056 Properties properties = getProperties(context, resources);
057 File propertiesFile = new File(LocationUtils.getCanonicalPath(context.getOutputFile()) + ".properties");
058 PropertyUtils.store(properties, propertiesFile, "UTF-8");
059 }
060
061 public static Properties getProperties(MetaInfContext context, List<MetaInfResource> resources) {
062 Properties properties = new Properties();
063 for (MetaInfResource resource : resources) {
064 String sizeKey = resource.getKey() + ".size";
065 properties.setProperty(sizeKey, resource.getSize() + "");
066 if (context.isAddLineCount()) {
067 String linesKey = resource.getKey() + ".lines";
068 properties.setProperty(linesKey, resource.getLines() + "");
069 }
070 }
071 return properties;
072 }
073
074 public static String getPropertyKey(String location) {
075 String key = StringUtils.replace(location, ":", ".");
076 return StringUtils.replace(key, "/", ".");
077 }
078
079 public static void scanAndCreateFile(MetaInfContext context) throws IOException {
080 scanAndCreateFiles(Arrays.asList(context));
081 }
082
083 public static List<MetaInfResource> getResources(MetaInfContext context, List<File> files) throws IOException {
084 List<MetaInfResource> resources = new ArrayList<MetaInfResource>();
085 for (int i = 0; i < files.size(); i++) {
086 MetaInfResource resource = getResource(context, files.get(i));
087 resources.add(resource);
088 }
089 return resources;
090 }
091
092 public static List<String> getLocations(List<MetaInfResource> resources) {
093 List<String> locations = new ArrayList<String>();
094 for (MetaInfResource resource : resources) {
095 locations.add(resource.getLocation());
096 }
097 return locations;
098 }
099
100 public static List<String> getLocations(File baseDir, List<File> files, String prefix) throws IOException {
101 List<String> locations = new ArrayList<String>();
102 for (int i = 0; i < files.size(); i++) {
103 String location = getLocation(baseDir, files.get(i), prefix);
104 locations.add(location);
105 }
106 return locations;
107 }
108
109 public static MetaInfResource getResource(MetaInfContext context, File file) throws IOException {
110 String location = getLocation(context.getBaseDir(), file, context.getPrefix());
111 long size = file.length();
112 long lines = -1;
113 if (context.isAddLineCount()) {
114 lines = LocationUtils.getLineCount(file);
115 }
116 String key = getPropertyKey(location);
117
118 MetaInfResource resource = new MetaInfResource();
119 resource.setLocation(location);
120 resource.setSize(size);
121 resource.setKey(key);
122 resource.setLines(lines);
123 return resource;
124 }
125
126 public static String getLocation(File baseDir, File file, String prefix) throws IOException {
127 String dir = baseDir.getCanonicalPath();
128 String path = file.getCanonicalPath();
129 int pos = dir.length() + 1;
130 return prefix + StringUtils.substring(path, pos);
131 }
132
133 }