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.HashSet; 011 import java.util.Iterator; 012 import java.util.List; 013 import java.util.Properties; 014 import java.util.Set; 015 016 import org.apache.commons.io.IOUtils; 017 import org.springframework.core.io.DefaultResourceLoader; 018 import org.springframework.core.io.Resource; 019 import org.springframework.core.io.ResourceLoader; 020 021 public class CopyrightHandler { 022 023 public static final String ECL_CR = "${ecl.cr}"; 024 public static final String ECL_LF = "${ecl.lf}"; 025 public static final String CR = "\r"; 026 public static final String LF = "\n"; 027 028 public static void main(String[] args) { 029 new CopyrightHandler().exec(args); 030 } 031 032 protected void exec(String[] args) { 033 try { 034 if (args == null || args.length != 1) { 035 throw new RuntimeException("Pass in basedir as the first (and only argument)"); 036 } 037 String basedir = args[0]; 038 ProblemFileContext context = new MultipleCopyrightContext(basedir); 039 ProblemFileDetector detector = new ProblemFileDetector(); 040 List<File> files = detector.getProblemFiles(context); 041 System.out 042 .println("Located " + files.size() + " files with multiple lines containing the text 'copyright'"); 043 Properties invalidEcl = getProperties("invalid-ecl.properties"); 044 Set<String> contentsToRemove = getValues(invalidEcl); 045 ContentRemover remover = new ContentRemover(); 046 Iterator<File> itr = files.iterator(); 047 List<File> updatedFiles = new ArrayList<File>(); 048 List<File> nonUpdatedFiles = new ArrayList<File>(); 049 while (itr.hasNext()) { 050 File file = itr.next(); 051 boolean updated = remover.removeContent(file, contentsToRemove); 052 if (!updated) { 053 copy(file); 054 nonUpdatedFiles.add(file); 055 } else { 056 updatedFiles.add(file); 057 } 058 } 059 System.out.println("Updated files"); 060 System.out.println("---------------------"); 061 for (File file : updatedFiles) { 062 System.out.println(file.getAbsolutePath()); 063 } 064 System.out.println(); 065 System.out.println("Multi-copyright files"); 066 System.out.println("---------------------"); 067 for (File file : nonUpdatedFiles) { 068 System.out.println(file.getAbsolutePath()); 069 } 070 } catch (Throwable t) { 071 t.printStackTrace(); 072 } 073 } 074 075 protected Set<String> getValues(Properties properties) { 076 Set<String> values = new HashSet<String>(); 077 Set<String> keys = properties.stringPropertyNames(); 078 for (String key : keys) { 079 String value = properties.getProperty(key); 080 if (values.contains(value)) { 081 throw new RuntimeException("key " + key + " is a duplicate"); 082 } 083 values.add(value); 084 } 085 return values; 086 } 087 088 protected Properties getProperties(String location) { 089 ResourceLoader loader = new DefaultResourceLoader(); 090 Resource resource = loader.getResource(location); 091 Properties properties = new Properties(); 092 InputStream in = null; 093 try { 094 in = resource.getInputStream(); 095 properties.load(in); 096 return properties; 097 } catch (IOException e) { 098 throw new RuntimeException(e); 099 } 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 }