KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > tools > code > CheckTextFiles


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.tools.code;
6
7 import java.io.ByteArrayOutputStream JavaDoc;
8 import java.io.File JavaDoc;
9 import java.io.RandomAccessFile JavaDoc;
10
11 import org.h2.util.ByteUtils;
12
13 public class CheckTextFiles {
14     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
15         new CheckTextFiles().run();
16     }
17
18     String JavaDoc[] suffixCheck = new String JavaDoc[]{"html", "jsp", "js", "css", "bat", "nsi", "java", "txt", "properties", "cpp", "def", "h", "rc", "dev", "sql", "xml", "csv", "Driver"};
19     String JavaDoc[] suffixIgnore = new String JavaDoc[]{"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     // must contain "+" otherwise this here counts as well
26
String JavaDoc copyrightLicense = "Copyright 2004-2006 H2 Group. "+"Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).";
27     String JavaDoc[] suffixIgnoreLicense = new String JavaDoc[]{"bat", "nsi", "txt", "properties", "def", "rc", "dev", "xml", "_private.h", "java.sql.Driver", "task"};
28     boolean hasError;
29
30     void run() throws Exception JavaDoc {
31         String JavaDoc baseDir = "src";
32         check(new File JavaDoc(baseDir));
33         if(hasError) {
34             throw new Exception JavaDoc("Errors found");
35         }
36     }
37
38     private void check(File JavaDoc file) throws Exception JavaDoc {
39         String JavaDoc name = file.getName();
40         if(file.isDirectory()) {
41             if(name.equals("CVS") || name.equals(".svn")) {
42                 return;
43             }
44             File JavaDoc[] list = file.listFiles();
45             for(int i=0; i<list.length; i++) {
46                 check(list[i]);
47             }
48         } else {
49             String JavaDoc 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 JavaDoc 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 JavaDoc("Unknown suffix: " + suffix + " for file: " + name);
75             }
76             if(check) {
77                 checkOrFixFile(file, autoFix, checkLicense);
78             }
79         }
80     }
81
82     void checkOrFixFile(File JavaDoc file, boolean fix, boolean checkLicense) throws Exception JavaDoc {
83         RandomAccessFile JavaDoc in = new RandomAccessFile JavaDoc(file, "r");
84         byte[] data = new byte[(int)file.length()];
85         ByteArrayOutputStream JavaDoc out = fix ? new ByteArrayOutputStream JavaDoc() : null;
86         in.readFully(data);
87         in.close();
88         if(checkLicense) {
89             if(data.length > copyrightLicense.length()) {
90                 // don't check tiny files
91
String JavaDoc text = new String JavaDoc(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                     // ok
129
} 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                     // ok
142
} 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 JavaDoc f = new RandomAccessFile JavaDoc(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 JavaDoc file, String JavaDoc 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 JavaDoc("FAIL");
174         }
175     }
176
177 }
178
Popular Tags