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.List;
11
12 import org.apache.commons.io.IOUtils;
13
14 public class LicenseHeaderTest {
15
16 public static void main(String[] args) {
17 new LicenseHeaderTest().exec(args);
18 }
19
20 protected void exec(String[] args) {
21 try {
22 long beg = System.currentTimeMillis();
23 String dir = "c:/eclipse/sts/2.6.1/ide/ws/rice-trunk";
24 List<File> xmlFiles = getFiles(new File(dir));
25 System.out.println(xmlFiles.size());
26 List<File> problemFiles = getProblemFiles(xmlFiles);
27 String[] invalidHeaders = getInvalidHeaders();
28 System.out.println(problemFiles.size());
29 for (int i = 0; i < problemFiles.size(); i++) {
30 File problemFile = problemFiles.get(i);
31 System.out.println(problemFile.getAbsolutePath());
32 reWrite(problemFile, invalidHeaders);
33 }
34 long end = System.currentTimeMillis();
35 long elapsed = end - beg;
36 double seconds = elapsed / 1000D;
37 System.out.println(seconds);
38 } catch (Throwable t) {
39 t.printStackTrace();
40 }
41 }
42
43 protected void reWrite(File file, String[] invalidHeaders) throws IOException {
44 InputStream in = new FileInputStream(file);
45 String oldContent = IOUtils.toString(in);
46 in.close();
47 String newContent = getGoodContent(oldContent, invalidHeaders);
48 OutputStream out = new FileOutputStream(file);
49 IOUtils.write(newContent.getBytes(), out);
50 out.close();
51 }
52
53 protected String getGoodContent(String content, String[] invalidHeaders) {
54 for (String invalidHeader : invalidHeaders) {
55 content = content.replace(invalidHeader, "");
56 }
57 return content;
58
59 }
60
61 protected String[] getInvalidHeaders() throws IOException {
62 String baseDir = "C:/eclipse/sts/2.6.1/ide/ws/kuali-spring-util/src/test/resources";
63 String[] filenames = { "invalid1.txt", "invalid2.txt", "invalid3.txt", "invalid4.txt", "invalid5.txt" };
64 String[] invalidHeaders = new String[filenames.length];
65 int count = 0;
66 for (String filename : filenames) {
67 String fullFileName = baseDir + "/" + filename;
68 String content = IOUtils.toString(new FileInputStream(fullFileName));
69 invalidHeaders[count++] = content;
70 }
71 return invalidHeaders;
72 }
73
74 protected List<File> getProblemFiles(List<File> files) throws IOException {
75 List<File> problemFiles = new ArrayList<File>();
76 for (File file : files) {
77 List<String> lines = IOUtils.readLines(new FileInputStream(file));
78
79 int prologIndex = getPrologIndex(lines);
80 if (prologIndex != -1 && prologIndex != 0) {
81
82 continue;
83 }
84
85 boolean copyRightIssue = isMultipleCopyRights(lines);
86 if (copyRightIssue) {
87 problemFiles.add(file);
88 continue;
89 }
90 }
91 return problemFiles;
92 }
93
94 protected boolean isMultipleCopyRights(List<String> strings) {
95 int count = 0;
96 for (int i = 0; i < strings.size(); i++) {
97 String s = strings.get(i);
98 String lowerCase = s.toLowerCase();
99 int pos = lowerCase.indexOf("copyright");
100 if (pos != -1) {
101 count++;
102 }
103 }
104 return count > 1;
105 }
106
107 protected int getPrologIndex(List<String> strings) {
108 for (int i = 0; i < strings.size(); i++) {
109 String s = strings.get(i);
110 if (isProlog(s)) {
111 return i;
112 }
113 }
114 return -1;
115 }
116
117 protected boolean isProlog(String s) {
118 String lowerCase = s.toLowerCase();
119 int pos1 = lowerCase.indexOf("<?xml");
120 int pos2 = lowerCase.indexOf("?>");
121 boolean flag1 = pos1 != -1 && pos2 != -1;
122 boolean flag2 = pos2 > pos1;
123 return flag1 && flag2;
124 }
125
126 protected boolean isSkip(File file) {
127 String name = file.getAbsolutePath();
128 if (name.contains(".svn")) {
129 return true;
130 }
131 if (name.contains("config\\ide")) {
132 return true;
133 }
134 if (name.contains("db\\impex")) {
135 return true;
136 }
137 return false;
138 }
139
140 protected boolean isAdd(File file) {
141 String name = file.getAbsolutePath();
142 if (name.contains(".x")) {
143 return true;
144 } else {
145 return false;
146 }
147 }
148
149 protected List<File> getFiles(File dir) {
150 File[] contents = dir.listFiles();
151 List<File> files = new ArrayList<File>();
152 for (File file : contents) {
153 if (isSkip(file)) {
154 continue;
155 }
156 if (file.isDirectory()) {
157 files.addAll(getFiles(file));
158 } else {
159 if (isAdd(file)) {
160 files.add(file);
161 }
162 }
163 }
164 return files;
165 }
166 }