1 5 package org.h2.tools.code; 6 7 import java.io.ByteArrayOutputStream ; 8 import java.io.File ; 9 import java.io.RandomAccessFile ; 10 11 import org.h2.util.ByteUtils; 12 13 public class CheckTextFiles { 14 public static void main(String [] args) throws Exception { 15 new CheckTextFiles().run(); 16 } 17 18 String [] suffixCheck = new String []{"html", "jsp", "js", "css", "bat", "nsi", "java", "txt", "properties", "cpp", "def", "h", "rc", "dev", "sql", "xml", "csv", "Driver"}; 19 String [] suffixIgnore = new String []{"gif", "png", "odg", "ico", "sxd", "layout", "res", "win", "dll", "jar", "task"}; 20 boolean failOnError; 21 boolean allowTab, allowCR = true, allowTrailingSpaces = true; 22 int spacesPerTab = 4; 23 boolean autoFix = true; 24 boolean useCRLF = true; 25 String copyrightLicense = "Copyright 2004-2006 H2 Group. "+"Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html)."; 27 String [] suffixIgnoreLicense = new String []{"bat", "nsi", "txt", "properties", "def", "rc", "dev", "xml", "_private.h", "java.sql.Driver", "task"}; 28 boolean hasError; 29 30 void run() throws Exception { 31 String baseDir = "src"; 32 check(new File (baseDir)); 33 if(hasError) { 34 throw new Exception ("Errors found"); 35 } 36 } 37 38 private void check(File file) throws Exception { 39 String name = file.getName(); 40 if(file.isDirectory()) { 41 if(name.equals("CVS") || name.equals(".svn")) { 42 return; 43 } 44 File [] list = file.listFiles(); 45 for(int i=0; i<list.length; i++) { 46 check(list[i]); 47 } 48 } else { 49 String suffix = ""; 50 int lastDot = name.lastIndexOf('.'); 51 if(lastDot >= 0) { 52 suffix = name.substring(lastDot+1); 53 } 54 boolean check = false, ignore = false; 55 for(int i=0; i<suffixCheck.length; i++) { 56 if(suffix.equals(suffixCheck[i])) { 57 check = true; 58 } 59 } 60 for(int i=0; i<suffixIgnore.length; i++) { 61 if(suffix.equals(suffixIgnore[i])) { 62 ignore = true; 63 } 64 } 65 boolean checkLicense = true; 66 for(int i=0; i<suffixIgnoreLicense.length; i++) { 67 String ig = suffixIgnoreLicense[i]; 68 if(suffix.equals(ig) || name.endsWith(ig)) { 69 checkLicense = false; 70 break; 71 } 72 } 73 if(ignore == check) { 74 throw new Error ("Unknown suffix: " + suffix + " for file: " + name); 75 } 76 if(check) { 77 checkOrFixFile(file, autoFix, checkLicense); 78 } 79 } 80 } 81 82 void checkOrFixFile(File file, boolean fix, boolean checkLicense) throws Exception { 83 RandomAccessFile in = new RandomAccessFile (file, "r"); 84 byte[] data = new byte[(int)file.length()]; 85 ByteArrayOutputStream out = fix ? new ByteArrayOutputStream () : null; 86 in.readFully(data); 87 in.close(); 88 if(checkLicense) { 89 if(data.length > copyrightLicense.length()) { 90 String text = new String (data); 92 if(text.indexOf(copyrightLicense) < 0) { 93 fail(file, "license is missing", 0); 94 } 95 } 96 } 97 int line = 1; 98 boolean lastWasWhitespace = false; 99 for(int i=0; i<data.length; i++) { 100 char ch = (char) (data[i] & 0xff); 101 if(ch > 127) { 102 fail(file, "contains character "+ch, line); 103 return; 104 } else if(ch < 32) { 105 if(ch == '\n') { 106 if(lastWasWhitespace && !allowTrailingSpaces) { 107 fail(file, "contains trailing white space", line); 108 return; 109 } 110 if(fix) { 111 if(useCRLF) { 112 out.write('\r'); 113 } 114 out.write(ch); 115 } 116 lastWasWhitespace = false; 117 line++; 118 } else if(ch == '\r') { 119 if(!allowCR) { 120 fail(file, "contains CR", line); 121 return; 122 } 123 if(lastWasWhitespace && !allowTrailingSpaces) { 124 fail(file, "contains trailing white space", line); 125 return; 126 } 127 lastWasWhitespace = false; 128 } else if(ch == '\t') { 130 if(fix) { 131 for(int j=0; j<spacesPerTab; j++) { 132 out.write(' '); 133 } 134 } else { 135 if(!allowTab) { 136 fail(file, "contains TAB", line); 137 return; 138 } 139 } 140 lastWasWhitespace = true; 141 } else { 143 fail(file, "contains character "+(int)ch, line); 144 return; 145 } 146 } else { 147 if(fix) { 148 out.write(ch); 149 } 150 lastWasWhitespace = Character.isWhitespace(ch); 151 } 152 } 153 if(lastWasWhitespace && !allowTrailingSpaces) { 154 fail(file, "contains trailing white space at the very end", line); 155 return; 156 } 157 if(fix) { 158 byte[] changed = out.toByteArray(); 159 if(ByteUtils.compareNotNull(data, changed) != 0) { 160 RandomAccessFile f = new RandomAccessFile (file, "rw"); 161 f.write(changed); 162 f.setLength(changed.length); 163 f.close(); 164 System.out.println("CHANGED: File " + file.getName()); 165 } 166 } 167 } 168 169 private void fail(File file, String error, int line) { 170 System.out.println("FAIL: File " + file.getAbsolutePath() + " " + error + " at line " + line); 171 hasError = true; 172 if(failOnError) { 173 throw new Error ("FAIL"); 174 } 175 } 176 177 } 178 | Popular Tags |