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    
010    import org.apache.commons.io.FileUtils;
011    import org.codehaus.plexus.util.StringUtils;
012    import org.slf4j.Logger;
013    import org.slf4j.LoggerFactory;
014    
015    public class MetaInfUtils {
016    
017            private static final Logger logger = LoggerFactory.getLogger(MetaInfUtils.class);
018    
019            public static void scanAndCreateFiles(List<MetaInfContext> contexts) throws IOException {
020                    for (MetaInfContext context : contexts) {
021                            Assert.notNull(context.getBaseDir(), "baseDir is null");
022                            Assert.notNull(context.getOutputFile(), "outputFile is null");
023                            List<String> includes = context.getIncludes();
024                            List<String> excludes = context.getExcludes();
025                            logger.info("Examining - " + context.getBaseDir().getCanonicalPath());
026                            logger.info("Include - " + CollectionUtils.getSpaceSeparatedString(includes));
027                            logger.info("Exclude - " + CollectionUtils.getSpaceSeparatedString(excludes));
028                            SimpleScanner scanner = new SimpleScanner(context.getBaseDir(), includes, excludes);
029                            List<File> files = scanner.getFiles();
030                            logger.info("Located " + files.size() + " files");
031                            List<String> locations = getLocations(context.getBaseDir(), files, context.getPrefix());
032                            if (context.isSort()) {
033                                    Collections.sort(locations);
034                            }
035                            logger.info("Creating " + context.getOutputFile().getCanonicalPath());
036                            FileUtils.writeLines(context.getOutputFile(), locations);
037                    }
038            }
039    
040            public static void scanAndCreateFile(MetaInfContext context) throws IOException {
041                    scanAndCreateFiles(Arrays.asList(context));
042            }
043    
044            public static List<String> getLocations(File baseDir, List<File> files, String prefix) throws IOException {
045                    List<String> locations = new ArrayList<String>();
046                    for (int i = 0; i < files.size(); i++) {
047                            String location = getLocation(baseDir, files.get(i), prefix);
048                            locations.add(location);
049                    }
050                    return locations;
051            }
052    
053            public static String getLocation(File baseDir, File file, String prefix) throws IOException {
054                    String dir = baseDir.getCanonicalPath();
055                    String path = file.getCanonicalPath();
056                    int pos = dir.length() + 1;
057                    return prefix + StringUtils.substring(path, pos);
058            }
059    
060    }