KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > tools > licensecheck > LicenseCheck


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.tools.licensecheck;
17
18 import java.io.File JavaDoc;
19 import java.io.FileReader JavaDoc;
20 import java.io.BufferedReader JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.HashSet JavaDoc;
23
24 public class LicenseCheck {
25     private static Set JavaDoc extensions = new HashSet JavaDoc();
26     static {
27         extensions.add("java");
28         extensions.add("xsl");
29         extensions.add("js");
30         extensions.add("xml");
31     }
32     private static Set JavaDoc exclusions = new HashSet JavaDoc();
33     static {
34         exclusions.add("target");
35         exclusions.add(".svn");
36     }
37
38     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
39         new LicenseCheck().run();
40     }
41
42     public void run() throws Exception JavaDoc {
43         File JavaDoc file = new File JavaDoc(".");
44         System.out.println("Will print names of files with missing license header, if any.");
45         checkRecursive(file);
46     }
47
48     private void checkRecursive(File JavaDoc directory) throws Exception JavaDoc {
49         File JavaDoc[] files = directory.listFiles();
50         for (int i = 0; i < files.length; i++) {
51             File JavaDoc file = files[i];
52             if (!exclusions.contains(file.getName())) {
53                 if (file.isDirectory()) {
54                     checkRecursive(file);
55                 } else {
56                     String JavaDoc name = file.getName();
57                     int pos = name.lastIndexOf('.');
58                     if (pos != -1) {
59                         String JavaDoc extension = name.substring(pos + 1);
60                         if (extensions.contains(extension)) {
61                             if (!hasLicenseHeader(file)) {
62                                 System.out.println(file.getAbsolutePath());
63                             }
64                         }
65                     }
66                 }
67             }
68         }
69     }
70
71     private boolean hasLicenseHeader(File JavaDoc file) throws Exception JavaDoc {
72         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
73         try {
74             String JavaDoc line = null;
75             int count = 0;
76             while ((line = reader.readLine()) != null) {
77                 if (line.indexOf("Licensed under the Apache License") != -1
78                     || line.indexOf("no Daisy license") != -1)
79                     return true;
80                 count++;
81                 if (count > 15) // read max 15 lines in file
82
break;
83             }
84
85             return false;
86         } finally {
87             reader.close();
88         }
89     }
90 }
91
Popular Tags