1 26 27 package net.sourceforge.groboutils.codecoverage.v2.util; 28 29 import net.sourceforge.groboutils.codecoverage.v2.IChannelLogger; 30 import java.io.Reader ; 31 import java.io.BufferedReader ; 32 import java.io.IOException ; 33 34 35 45 public class ConvertSingleLog 46 { 47 private IChannelLogger channels[]; 48 49 58 public ConvertSingleLog( IChannelLogger ch[] ) 59 { 60 if (ch == null) 61 { 62 throw new IllegalArgumentException ("no null args"); 63 } 64 65 int len = ch.length; 66 if (len <= 0) 67 { 68 throw new IllegalArgumentException ("must pass at least one channel"); 69 } 70 this.channels = new IChannelLogger[ len ]; 71 for (int i = 0; i < len; ++i) 72 { 73 if (ch[i] == null) 74 { 75 throw new IllegalArgumentException ("channel "+i+" is null"); 76 } 77 this.channels[i] = ch[i]; 78 } 79 } 80 81 82 89 public void read( Reader r, boolean ignoreFormatErrors ) 90 throws IOException 91 { 92 if (r == null) 93 { 94 throw new IllegalArgumentException ("no null args"); 95 } 96 97 BufferedReader br = new BufferedReader ( r ); 98 try 99 { 100 String line; 101 while ((line = br.readLine()) != null) 102 { 103 processLine( line, ignoreFormatErrors ); 105 } 106 } 107 finally 108 { 109 br.close(); 110 r.close(); 111 } 112 } 113 114 115 124 public void processLine( String line, boolean ignoreFormatErrors ) 125 throws IOException 126 { 127 if (line == null) 128 { 129 throw new IOException ("End of stream: line is null"); 130 } 131 line = line.trim(); 132 if (line.length() <= 0) 133 { 134 return; 136 } 137 138 try 139 { 140 int pos[] = { 0, 0 }; 141 int channel = (int)nextShort( pos, line ); 142 143 if (channel < 0) 145 { 146 throw new IOException ( "Invalid channel: requested "+ 147 channel+", but we're restricted to "+this.channels.length+ 148 " channels." ); 149 } 150 if (channel >= this.channels.length) 151 { 152 if (ignoreFormatErrors) 153 { 154 return; 155 } 156 throw new IOException ( "Invalid channel: requested "+ 158 channel+", but we're restricted to "+this.channels.length+ 159 " channels." ); 160 } 161 162 String classID = nextElement( pos, line ); 164 String s = lastElement( pos, line ); 165 HexUtil.TwoShorts ts = new HexUtil.TwoShorts(); 166 167 if (!HexUtil.getInstance().parseTwoHex( s, ts, ' ', 0 )) 169 { 170 throw new IOException ( "invalid format for hexidecimal ["+ 171 line+"]" ); 172 } 173 174 this.channels[ channel ].cover( classID, ts.a, ts.b ); 177 } 178 catch (IOException ioe) 179 { 180 if (!ignoreFormatErrors) 181 { 182 throw ioe; 183 } 184 } 186 } 187 188 189 protected String nextElement( int pos[], String line ) 190 throws IOException 191 { 192 pos[1] = line.indexOf( ',', pos[0] ); 193 if (pos[1] <= pos[0]) 194 { 195 throw new IOException ( "invalid format ["+line+"]" ); 196 } 197 String ret = line.substring( pos[0], pos[1] ).trim(); 198 pos[0] = pos[1] + 1; 199 return ret; 200 } 201 202 203 protected short nextShort( int pos[], String line ) 204 throws IOException 205 { 206 String s = nextElement( pos, line ); 207 return parseShort( s, line ); 208 } 209 210 211 protected String lastElement( int pos[], String line ) 212 throws IOException 213 { 214 if (pos[0] >= line.length()) 215 { 216 throw new IOException ( "invalid format ["+line+"]" ); 217 } 218 String s = line.substring( pos[0] ); 219 return s; 220 } 221 222 223 protected short parseShort( String txt, String line ) 224 throws IOException 225 { 226 try 227 { 228 return Short.parseShort( txt ); 229 } 230 catch (NumberFormatException e) 231 { 232 throw new IOException ( "invalid format for short ["+line+"]" ); 233 } 234 } 235 } 236 237 | Popular Tags |