1 package org.springframework.beans;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9 import java.util.ArrayList;
10 import java.util.HashSet;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Properties;
14 import java.util.Set;
15
16 import org.apache.commons.io.IOUtils;
17 import org.springframework.core.io.DefaultResourceLoader;
18 import org.springframework.core.io.Resource;
19 import org.springframework.core.io.ResourceLoader;
20
21 public class CopyrightHandler {
22
23 public static final String ECL_CR = "${ecl.cr}";
24 public static final String ECL_LF = "${ecl.lf}";
25 public static final String CR = "\r";
26 public static final String LF = "\n";
27
28 public static void main(String[] args) {
29 new CopyrightHandler().exec(args);
30 }
31
32 protected void exec(String[] args) {
33 try {
34 if (args == null || args.length != 1) {
35 throw new RuntimeException("Pass in basedir as the first (and only argument)");
36 }
37 String basedir = args[0];
38 ProblemFileContext context = new MultipleCopyrightContext(basedir);
39 ProblemFileDetector detector = new ProblemFileDetector();
40 List<File> files = detector.getProblemFiles(context);
41 System.out
42 .println("Located " + files.size() + " files with multiple lines containing the text 'copyright'");
43 Properties invalidEcl = getProperties("invalid-ecl.properties");
44 Set<String> contentsToRemove = getValues(invalidEcl);
45 ContentRemover remover = new ContentRemover();
46 Iterator<File> itr = files.iterator();
47 List<File> updatedFiles = new ArrayList<File>();
48 List<File> nonUpdatedFiles = new ArrayList<File>();
49 while (itr.hasNext()) {
50 File file = itr.next();
51 boolean updated = remover.removeContent(file, contentsToRemove);
52 if (!updated) {
53 copy(file);
54 nonUpdatedFiles.add(file);
55 } else {
56 updatedFiles.add(file);
57 }
58 }
59 System.out.println("Updated files");
60 System.out.println("---------------------");
61 for (File file : updatedFiles) {
62 System.out.println(file.getAbsolutePath());
63 }
64 System.out.println();
65 System.out.println("Multi-copyright files");
66 System.out.println("---------------------");
67 for (File file : nonUpdatedFiles) {
68 System.out.println(file.getAbsolutePath());
69 }
70 } catch (Throwable t) {
71 t.printStackTrace();
72 }
73 }
74
75 protected Set<String> getValues(Properties properties) {
76 Set<String> values = new HashSet<String>();
77 Set<String> keys = properties.stringPropertyNames();
78 for (String key : keys) {
79 String value = properties.getProperty(key);
80 if (values.contains(value)) {
81 throw new RuntimeException("key " + key + " is a duplicate");
82 }
83 values.add(value);
84 }
85 return values;
86 }
87
88 protected Properties getProperties(String location) {
89 ResourceLoader loader = new DefaultResourceLoader();
90 Resource resource = loader.getResource(location);
91 Properties properties = new Properties();
92 InputStream in = null;
93 try {
94 in = resource.getInputStream();
95 properties.load(in);
96 return properties;
97 } catch (IOException e) {
98 throw new RuntimeException(e);
99 } finally {
100 IOUtils.closeQuietly(in);
101 }
102 }
103
104 protected Properties getXMLProperties(String location) {
105 ResourceLoader loader = new DefaultResourceLoader();
106 Resource resource = loader.getResource(location);
107 Properties properties = new Properties();
108 InputStream in = null;
109 try {
110 in = resource.getInputStream();
111 properties.loadFromXML(in);
112 return properties;
113 } catch (IOException e) {
114 throw new RuntimeException(e);
115 } finally {
116 IOUtils.closeQuietly(in);
117 }
118 }
119
120 public static final void write(File file, String content) {
121 OutputStream out = null;
122 try {
123 out = new FileOutputStream(file);
124 IOUtils.write(content.getBytes(), out);
125 } catch (IOException e) {
126 throw new RuntimeException(e);
127 } finally {
128 IOUtils.closeQuietly(out);
129 }
130 }
131
132 public static final String read(File file) {
133 InputStream in = null;
134 try {
135 in = new FileInputStream(file);
136 return IOUtils.toString(in);
137 } catch (IOException e) {
138 throw new RuntimeException(e);
139 } finally {
140 IOUtils.closeQuietly(in);
141 }
142 }
143
144 public static final String expand(String s) {
145 s = s.replace(ECL_CR, CR);
146 s = s.replace(ECL_LF, LF);
147 return s;
148 }
149
150 public static final String flatten(String s) {
151 s = s.replace(CR, ECL_CR);
152 s = s.replace(LF, ECL_LF);
153 return s;
154 }
155
156 protected void copy(File file) {
157 String content = read(file);
158 String flat = flatten(content);
159 String filename = file.getName();
160 File flatFile = new File("C:/temp/ecl/" + filename);
161 write(flatFile, flat);
162 }
163
164 }