001    package org.springframework.beans;
002    
003    import java.io.File;
004    import java.io.FileInputStream;
005    import java.io.FileOutputStream;
006    import java.io.IOException;
007    import java.io.InputStream;
008    import java.io.OutputStream;
009    import java.util.ArrayList;
010    import java.util.List;
011    
012    import org.apache.commons.io.IOUtils;
013    
014    public class LicenseHeaderTest {
015    
016        public static void main(String[] args) {
017            new LicenseHeaderTest().exec(args);
018        }
019    
020        protected void exec(String[] args) {
021            try {
022                long beg = System.currentTimeMillis();
023                String dir = "c:/eclipse/sts/2.6.1/ide/ws/rice-trunk";
024                List<File> xmlFiles = getFiles(new File(dir));
025                System.out.println(xmlFiles.size());
026                List<File> problemFiles = getProblemFiles(xmlFiles);
027                String[] invalidHeaders = getInvalidHeaders();
028                System.out.println(problemFiles.size());
029                for (int i = 0; i < problemFiles.size(); i++) {
030                    File problemFile = problemFiles.get(i);
031                    System.out.println(problemFile.getAbsolutePath());
032                    reWrite(problemFile, invalidHeaders);
033                }
034                long end = System.currentTimeMillis();
035                long elapsed = end - beg;
036                double seconds = elapsed / 1000D;
037                System.out.println(seconds);
038            } catch (Throwable t) {
039                t.printStackTrace();
040            }
041        }
042    
043        protected void reWrite(File file, String[] invalidHeaders) throws IOException {
044            InputStream in = new FileInputStream(file);
045            String oldContent = IOUtils.toString(in);
046            in.close();
047            String newContent = getGoodContent(oldContent, invalidHeaders);
048            OutputStream out = new FileOutputStream(file);
049            IOUtils.write(newContent.getBytes(), out);
050            out.close();
051        }
052    
053        protected String getGoodContent(String content, String[] invalidHeaders) {
054            for (String invalidHeader : invalidHeaders) {
055                content = content.replace(invalidHeader, "");
056            }
057            return content;
058    
059        }
060    
061        protected String[] getInvalidHeaders() throws IOException {
062            String baseDir = "C:/eclipse/sts/2.6.1/ide/ws/kuali-spring-util/src/test/resources";
063            String[] filenames = { "invalid1.txt", "invalid2.txt", "invalid3.txt", "invalid4.txt", "invalid5.txt" };
064            String[] invalidHeaders = new String[filenames.length];
065            int count = 0;
066            for (String filename : filenames) {
067                String fullFileName = baseDir + "/" + filename;
068                String content = IOUtils.toString(new FileInputStream(fullFileName));
069                invalidHeaders[count++] = content;
070            }
071            return invalidHeaders;
072        }
073    
074        protected List<File> getProblemFiles(List<File> files) throws IOException {
075            List<File> problemFiles = new ArrayList<File>();
076            for (File file : files) {
077                List<String> lines = IOUtils.readLines(new FileInputStream(file));
078    
079                int prologIndex = getPrologIndex(lines);
080                if (prologIndex != -1 && prologIndex != 0) {
081                    // problemFiles.add(file);
082                    continue;
083                }
084    
085                boolean copyRightIssue = isMultipleCopyRights(lines);
086                if (copyRightIssue) {
087                    problemFiles.add(file);
088                    continue;
089                }
090            }
091            return problemFiles;
092        }
093    
094        protected boolean isMultipleCopyRights(List<String> strings) {
095            int count = 0;
096            for (int i = 0; i < strings.size(); i++) {
097                String s = strings.get(i);
098                String lowerCase = s.toLowerCase();
099                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    }