1 23 24 package net.sourceforge.cobertura.javancss; 25 26 import java.io.DataInputStream ; 27 import java.io.FileInputStream ; 28 import java.io.IOException ; 29 import java.util.Enumeration ; 30 import java.util.Hashtable ; 31 import java.util.Vector ; 32 33 34 39 public class Javancss 40 { 41 42 private JavaParser _pJavaParser = null; 43 private Vector _vJavaSourceFiles = new Vector (); 44 private String _sErrorMessage = null; 45 private Vector _vMethodComplexities = new Vector (); 46 private Hashtable _htProcessedAtFiles = new Hashtable (); 47 48 public Javancss(String sJavaSourceFile_) 49 { 50 _sErrorMessage = null; 52 _vJavaSourceFiles = new Vector (); 53 _vJavaSourceFiles.addElement(sJavaSourceFile_); 54 try 55 { 56 _measureFiles(_vJavaSourceFiles); 57 } 58 catch (Exception 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 vJavaSourceFiles_) throws IOException , ParseException, 69 TokenMgrError 70 { 71 for (Enumeration e = vJavaSourceFiles_.elements(); e.hasMoreElements();) 73 { 74 String sJavaFileName = (String )e.nextElement(); 75 76 if (sJavaFileName.charAt(0) == '@') 78 { 79 if (sJavaFileName.length() > 1) 80 { 81 String 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 sJavaSourceFileNames = null; 89 try 90 { 91 sJavaSourceFileNames = FileUtil.readFile(sFileName); 92 } 93 catch (IOException pIOException) 94 { 95 _sErrorMessage = "File Read Error: " + sFileName; 96 97 throw pIOException; 98 } 99 Vector vTheseJavaSourceFiles = Util.stringToLines(sJavaSourceFileNames); 100 _measureFiles(vTheseJavaSourceFiles); 101 } 102 } 103 else 104 { 105 try 106 { 107 _measureSource(sJavaFileName); 108 } 109 catch (Throwable pThrowable) 110 { 111 } 113 } 114 } 115 } 116 117 private void _measureSource(String sSourceFileName_) throws IOException , ParseException, 118 TokenMgrError 119 { 120 sSourceFileName_ = FileUtil.normalizeFileName(sSourceFileName_); 122 123 DataInputStream disSource = null; 124 125 try 127 { 128 disSource = new DataInputStream (new FileInputStream (sSourceFileName_)); 129 } 130 catch (IOException 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 sTempErrorMessage = _sErrorMessage; 146 try 147 { 148 _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 disSource_) throws ParseException, TokenMgrError 180 { 181 try 182 { 183 _pJavaParser = new JavaParser(disSource_); 185 _pJavaParser.compilationUnit(); 187 _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 getMethodComplexities() 221 { 222 return (_vMethodComplexities); 223 } 224 225 } 226 | Popular Tags |