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