1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Properties;
25
26 import org.apache.commons.io.FileUtils;
27 import org.codehaus.plexus.util.StringUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class MetaInfUtils {
32
33 private static final Logger logger = LoggerFactory.getLogger(MetaInfUtils.class);
34
35 public static void scanAndCreateFiles(List<MetaInfContext> contexts) throws IOException {
36 for (MetaInfContext context : contexts) {
37 List<File> files = getFiles(context);
38 List<MetaInfResource> resources = getResources(context, files);
39 doLocations(context, resources);
40 if (context.isAddPropertiesFile()) {
41 doProperties(context, resources);
42 }
43 }
44 }
45
46 public static List<File> getFiles(MetaInfContext context) throws IOException {
47 Assert.notNull(context.getBaseDir(), "baseDir is null");
48 Assert.notNull(context.getOutputFile(), "outputFile is null");
49 List<String> includes = context.getIncludes();
50 List<String> excludes = context.getExcludes();
51 logger.info("Examining - " + context.getBaseDir().getCanonicalPath());
52 logger.info("Include - " + CollectionUtils.getSpaceSeparatedString(includes));
53 logger.info("Exclude - " + CollectionUtils.getSpaceSeparatedString(excludes));
54 SimpleScanner scanner = new SimpleScanner(context.getBaseDir(), includes, excludes);
55 List<File> files = scanner.getFiles();
56 logger.info("Located " + files.size() + " files");
57 return files;
58 }
59
60 public static void doLocations(MetaInfContext context, List<MetaInfResource> resources) throws IOException {
61 List<String> locations = getLocations(resources);
62 if (context.isSort()) {
63 Collections.sort(locations);
64 }
65 logger.info("Creating " + context.getOutputFile().getCanonicalPath());
66 FileUtils.writeLines(context.getOutputFile(), locations);
67 }
68
69 public static void doProperties(MetaInfContext context, List<MetaInfResource> resources) {
70 logger.debug("doProperties()");
71 Properties properties = getProperties(context, resources);
72 File propertiesFile = new File(LocationUtils.getCanonicalPath(context.getOutputFile()) + ".properties");
73 PropertyUtils.store(properties, propertiesFile, "UTF-8");
74 }
75
76 public static Properties getProperties(MetaInfContext context, List<MetaInfResource> resources) {
77 Properties properties = new Properties();
78 for (MetaInfResource resource : resources) {
79 String sizeKey = resource.getKey() + ".size";
80 properties.setProperty(sizeKey, resource.getSize() + "");
81 if (context.isAddLineCount()) {
82 String linesKey = resource.getKey() + ".lines";
83 properties.setProperty(linesKey, resource.getLines() + "");
84 }
85 }
86 return properties;
87 }
88
89 public static String getPropertyKey(String location) {
90 String key = StringUtils.replace(location, ":", ".");
91 return StringUtils.replace(key, "/", ".");
92 }
93
94 public static void scanAndCreateFile(MetaInfContext context) throws IOException {
95 scanAndCreateFiles(Arrays.asList(context));
96 }
97
98 public static List<MetaInfResource> getResources(MetaInfContext context, List<File> files) throws IOException {
99 List<MetaInfResource> resources = new ArrayList<MetaInfResource>();
100 for (int i = 0; i < files.size(); i++) {
101 MetaInfResource resource = getResource(context, files.get(i));
102 resources.add(resource);
103 }
104 return resources;
105 }
106
107 public static List<String> getLocations(List<MetaInfResource> resources) {
108 List<String> locations = new ArrayList<String>();
109 for (MetaInfResource resource : resources) {
110 locations.add(resource.getLocation());
111 }
112 return locations;
113 }
114
115 public static List<String> getLocations(File baseDir, List<File> files, String prefix) throws IOException {
116 List<String> locations = new ArrayList<String>();
117 for (int i = 0; i < files.size(); i++) {
118 String location = getLocation(baseDir, files.get(i), prefix);
119 locations.add(location);
120 }
121 return locations;
122 }
123
124 public static MetaInfResource getResource(MetaInfContext context, File file) throws IOException {
125 String location = getLocation(context.getBaseDir(), file, context.getPrefix());
126 long size = file.length();
127 long lines = -1;
128 if (context.isAddLineCount()) {
129 lines = LocationUtils.getLineCount(file);
130 }
131 String key = getPropertyKey(location);
132
133 MetaInfResource resource = new MetaInfResource();
134 resource.setLocation(location);
135 resource.setSize(size);
136 resource.setKey(key);
137 resource.setLines(lines);
138 return resource;
139 }
140
141 public static String getLocation(File baseDir, File file, String prefix) throws IOException {
142 String dir = baseDir.getCanonicalPath();
143 String path = file.getCanonicalPath();
144 int pos = dir.length() + 1;
145 return prefix + StringUtils.substring(path, pos);
146 }
147
148 }