KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > javancss > Javancss


1 /*
2  * Cobertura - http://cobertura.sourceforge.net/
3  *
4  * This file was taken from JavaNCSS
5  * http://www.kclee.com/clemens/java/javancss/
6  * Copyright (C) 2000 Chr. Clemens Lee <clemens a.t kclee d.o.t com>
7  *
8  * Cobertura is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published
10  * by the Free Software Foundation; either version 2 of the License,
11  * or (at your option) any later version.
12  *
13  * Cobertura is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Cobertura; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  * USA
22  */

23
24 package net.sourceforge.cobertura.javancss;
25
26 import java.io.DataInputStream JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Vector JavaDoc;
32
33
34 /**
35  * @author Chr. Clemens Lee, recursive feature by P��k� Hannu,
36  * additional javadoc metrics by Emilio Gongora, <emilio@sms.nl>,
37  * and Guillermo Rodriguez, <guille@sms.nl>.
38  */

39 public class Javancss
40 {
41
42     private JavaParser _pJavaParser = null;
43     private Vector JavaDoc _vJavaSourceFiles = new Vector JavaDoc();
44     private String JavaDoc _sErrorMessage = null;
45     private Vector JavaDoc _vMethodComplexities = new Vector JavaDoc();
46     private Hashtable JavaDoc _htProcessedAtFiles = new Hashtable JavaDoc();
47
48     public Javancss(String JavaDoc sJavaSourceFile_)
49     {
50         //System.out.println("Javancss.<init>(String).sJavaSourceFile_: " + sJavaSourceFile_);
51
_sErrorMessage = null;
52         _vJavaSourceFiles = new Vector JavaDoc();
53         _vJavaSourceFiles.addElement(sJavaSourceFile_);
54         try
55         {
56             _measureFiles(_vJavaSourceFiles);
57         }
58         catch (Exception JavaDoc e)
59         {
60             System.out.println("Javancss.<init>(String).e: " + e);
61         }
62         catch (TokenMgrError pError)
63         {
64             System.out.println("Javancss.<init>(String).pError: " + pError);
65         }
66     }
67
68     private void _measureFiles(Vector JavaDoc vJavaSourceFiles_) throws IOException JavaDoc, ParseException,
69             TokenMgrError
70     {
71         // for each file
72
for (Enumeration JavaDoc e = vJavaSourceFiles_.elements(); e.hasMoreElements();)
73         {
74             String JavaDoc sJavaFileName = (String JavaDoc)e.nextElement();
75
76             // if the file specifies other files...
77
if (sJavaFileName.charAt(0) == '@')
78             {
79                 if (sJavaFileName.length() > 1)
80                 {
81                     String JavaDoc sFileName = sJavaFileName.substring(1);
82                     sFileName = FileUtil.normalizeFileName(sFileName);
83                     if (_htProcessedAtFiles.get(sFileName) != null)
84                     {
85                         continue;
86                     }
87                     _htProcessedAtFiles.put(sFileName, sFileName);
88                     String JavaDoc sJavaSourceFileNames = null;
89                     try
90                     {
91                         sJavaSourceFileNames = FileUtil.readFile(sFileName);
92                     }
93                     catch (IOException JavaDoc pIOException)
94                     {
95                         _sErrorMessage = "File Read Error: " + sFileName;
96
97                         throw pIOException;
98                     }
99                     Vector JavaDoc vTheseJavaSourceFiles = Util.stringToLines(sJavaSourceFileNames);
100                     _measureFiles(vTheseJavaSourceFiles);
101                 }
102             }
103             else
104             {
105                 try
106                 {
107                     _measureSource(sJavaFileName);
108                 }
109                 catch (Throwable JavaDoc pThrowable)
110                 {
111                     // hmm, do nothing? Use getLastError() or so to check for details.
112
}
113             }
114         }
115     }
116
117     private void _measureSource(String JavaDoc sSourceFileName_) throws IOException JavaDoc, ParseException,
118             TokenMgrError
119     {
120         // take user.dir property in account
121
sSourceFileName_ = FileUtil.normalizeFileName(sSourceFileName_);
122
123         DataInputStream JavaDoc disSource = null;
124
125         // opens the file
126
try
127         {
128             disSource = new DataInputStream JavaDoc(new FileInputStream JavaDoc(sSourceFileName_));
129         }
130         catch (IOException JavaDoc pIOException)
131         {
132             if (_sErrorMessage == null)
133             {
134                 _sErrorMessage = "";
135             }
136             else
137             {
138                 _sErrorMessage += "\n";
139             }
140             _sErrorMessage += "File not found: " + sSourceFileName_;
141
142             throw pIOException;
143         }
144
145         String JavaDoc sTempErrorMessage = _sErrorMessage;
146         try
147         {
148             // the same method but with a DataInputSream
149
_measureSource(disSource);
150         }
151         catch (ParseException pParseException)
152         {
153             if (sTempErrorMessage == null)
154             {
155                 sTempErrorMessage = "";
156             }
157             sTempErrorMessage += "ParseException in " + sSourceFileName_
158                     + "\nLast useful checkpoint: \"" + _pJavaParser.getLastFunction() + "\"\n";
159             sTempErrorMessage += pParseException.getMessage() + "\n";
160
161             _sErrorMessage = sTempErrorMessage;
162
163             throw pParseException;
164         }
165         catch (TokenMgrError pTokenMgrError)
166         {
167             if (sTempErrorMessage == null)
168             {
169                 sTempErrorMessage = "";
170             }
171             sTempErrorMessage += "TokenMgrError in " + sSourceFileName_ + "\n"
172                     + pTokenMgrError.getMessage() + "\n";
173             _sErrorMessage = sTempErrorMessage;
174
175             throw pTokenMgrError;
176         }
177     }
178
179     private void _measureSource(DataInputStream JavaDoc disSource_) throws ParseException, TokenMgrError
180     {
181         try
182         {
183             // create a parser object
184
_pJavaParser = new JavaParser(disSource_);
185             // execute the parser
186
_pJavaParser.compilationUnit();
187             //System.out.println("Javancss._measureSource(DataInputStream).SUCCESSFULLY_PARSED");
188
// add new data to global vector
189
_vMethodComplexities.addAll(_pJavaParser.getMethodComplexities());
190         }
191         catch (ParseException pParseException)
192         {
193             if (_sErrorMessage == null)
194             {
195                 _sErrorMessage = "";
196             }
197             _sErrorMessage += "ParseException in STDIN";
198             if (_pJavaParser != null)
199             {
200                 _sErrorMessage += "\nLast useful checkpoint: \"" + _pJavaParser.getLastFunction()
201                         + "\"\n";
202             }
203             _sErrorMessage += pParseException.getMessage() + "\n";
204
205             throw pParseException;
206         }
207         catch (TokenMgrError pTokenMgrError)
208         {
209             if (_sErrorMessage == null)
210             {
211                 _sErrorMessage = "";
212             }
213             _sErrorMessage += "TokenMgrError in STDIN\n";
214             _sErrorMessage += pTokenMgrError.getMessage() + "\n";
215
216             throw pTokenMgrError;
217         }
218     }
219
220     public Vector JavaDoc getMethodComplexities()
221     {
222         return (_vMethodComplexities);
223     }
224
225 }
226
Popular Tags