KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > codeMetrics > LineCounter


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Somik Raha
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/codeMetrics/LineCounter.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/01/02 16:24:55 $
10
// $Revision: 1.12 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

27 package org.htmlparser.tests.codeMetrics;
28
29 import java.io.BufferedReader JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileFilter JavaDoc;
32 import java.io.FileReader JavaDoc;
33
34 public class LineCounter {
35
36     public int count(File JavaDoc file) {
37         System.out.println("Handling "+file.getName());
38         int count = 0;
39         // Get all files in current directory
40
if (file.isDirectory()) {
41             // Get the listing in this directory
42
count = recurseDirectory(file, count);
43         } else {
44             // It is a file
45
count = countLinesIn(file);
46         }
47         return count;
48     }
49
50     /**
51      * Counts code excluding comments and blank lines in the given file
52      * @param file
53      * @return int
54      */

55     public int countLinesIn(File JavaDoc file) {
56         int count = 0;
57         System.out.println("Counting "+file.getName());
58         try {
59             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(file.getAbsolutePath()));
60             String JavaDoc line = null;
61             do {
62                 line = reader.readLine();
63                 if (line!=null &&
64                     line.indexOf("*")==-1 &&
65                     line.indexOf("//")==-1 &&
66                     line.length()>0
67                 ) count++;
68             }
69             while (line!=null);
70         }
71         catch (Exception JavaDoc e) {
72             e.printStackTrace();
73         }
74         return count;
75     }
76
77     public int recurseDirectory(File JavaDoc file, int count) {
78         File JavaDoc [] files = file.listFiles(new FileFilter JavaDoc() {
79             public boolean accept(File JavaDoc file) {
80                 if (file.getName().indexOf(".java")!=-1 || file.isDirectory()) {
81                     return true;
82                 } else {
83                     return false;
84                 }
85             }
86         });
87         for (int i=0;i<files.length;i++) {
88             count += count(files[i]);
89         }
90         return count;
91     }
92
93     public static void main(String JavaDoc [] args) {
94         LineCounter lc = new LineCounter();
95         System.out.println("Line Count = "+lc.count(new File JavaDoc(args[0])));
96     }
97 }
98
Popular Tags