View Javadoc

1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
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  
17  
18  package org.apache.jetspeed.util;
19  
20  import java.io.File;
21  import java.io.FileReader;
22  import java.io.BufferedReader;
23  import java.io.FileNotFoundException;
24  import java.io.IOException;
25  
26  public class LineCounter
27  {
28      int count = 0;
29  
30      public static void main( String args[] )
31      {
32          LineCounter lc = new LineCounter();
33          int count = 0;
34          int totalCount = 0;
35          for (int ix = 0; ix < args.length; ix++)
36          {
37              count = lc.run(args[ix]);
38              System.out.println( "Count for path [" + args[ix] + "] = " + count );
39              totalCount += count;
40          }
41          System.out.println( "Total Count = " + totalCount );
42      }
43  
44      public int run(String path)
45      {
46          System.out.println("Running LineCounter for " + path );
47          count = 0;
48          return traverse(path);
49      }
50  
51      private int traverse(String path)
52      {
53          File file = new File(path);
54          if (file.isFile()) 
55          {
56              try 
57              {
58                  String name = file.getName();
59                  if (name.endsWith("java"))
60                      count += countFile(file);
61              }
62              catch (Exception e)
63              {
64                  System.err.println("Failed to count file: " + path + " : " + e.toString());
65              }
66          } 
67          else if (file.isDirectory()) 
68          {
69              if (!path.endsWith(File.separator))
70                  path += File.separator;
71  
72      //        System.out.println("Processing directory: " + path);
73              String list[] = file.list();
74  
75              // Process all files recursivly
76              for(int ix = 0; list != null && ix < list.length; ix++)
77                  traverse(path + list[ix]);
78  
79  
80          }
81          return count;
82      }            
83  
84      private int countFile( File file )
85              throws FileNotFoundException, IOException
86      {
87    //      System.out.println("Processing file: " + file.getPath());
88          FileReader reader = new FileReader( file );
89          BufferedReader br = new BufferedReader( reader );
90          String s;
91          int mycount = 0;
92  
93          while ((s = br.readLine()) != null)
94          {
95              if (s.length() > 0)
96                  mycount++;
97          }
98          return mycount;
99      }
100 }
101