comparison src/org/tmatesoft/hg/repo/HgChangelog.java @ 427:31a89587eb04

FIXMEs: consistent names, throws for commands and their handlers. Use of checked exceptions in hi-level api
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 29 Mar 2012 17:14:35 +0200
parents 063b0663495a
children 1ee452f31187
comparison
equal deleted inserted replaced
426:063b0663495a 427:31a89587eb04
28 import java.util.List; 28 import java.util.List;
29 import java.util.Locale; 29 import java.util.Locale;
30 import java.util.Map; 30 import java.util.Map;
31 import java.util.TimeZone; 31 import java.util.TimeZone;
32 32
33 import org.tmatesoft.hg.core.HgBadArgumentException;
34 import org.tmatesoft.hg.core.Nodeid; 33 import org.tmatesoft.hg.core.Nodeid;
35 import org.tmatesoft.hg.internal.Callback; 34 import org.tmatesoft.hg.internal.Callback;
36 import org.tmatesoft.hg.internal.DataAccess; 35 import org.tmatesoft.hg.internal.DataAccess;
37 import org.tmatesoft.hg.internal.IterateControlMediator; 36 import org.tmatesoft.hg.internal.IterateControlMediator;
38 import org.tmatesoft.hg.internal.Lifecycle; 37 import org.tmatesoft.hg.internal.Lifecycle;
225 } catch (CloneNotSupportedException ex) { 224 } catch (CloneNotSupportedException ex) {
226 throw new InternalError(ex.toString()); 225 throw new InternalError(ex.toString());
227 } 226 }
228 } 227 }
229 228
230 /*package*/ static RawChangeset parse(DataAccess da) throws IOException, HgBadArgumentException { 229 /*package*/ static RawChangeset parse(DataAccess da) throws IOException, HgInvalidDataFormatException {
231 byte[] data = da.byteArray(); 230 byte[] data = da.byteArray();
232 RawChangeset rv = new RawChangeset(); 231 RawChangeset rv = new RawChangeset();
233 rv.init(data, 0, data.length, null); 232 rv.init(data, 0, data.length, null);
234 return rv; 233 return rv;
235 } 234 }
236 235
237 // @param usersPool - it's likely user names get repeated again and again throughout repository. can be null 236 // @param usersPool - it's likely user names get repeated again and again throughout repository. can be null
238 // FIXME replace HgBadArgumentException with HgInvalidDataFormatException or HgInvalidControlFileException 237 /* package-local */void init(byte[] data, int offset, int length, Pool<String> usersPool) throws HgInvalidDataFormatException {
239 /* package-local */void init(byte[] data, int offset, int length, Pool<String> usersPool) throws HgBadArgumentException {
240 final int bufferEndIndex = offset + length; 238 final int bufferEndIndex = offset + length;
241 final byte lineBreak = (byte) '\n'; 239 final byte lineBreak = (byte) '\n';
242 int breakIndex1 = indexOf(data, lineBreak, offset, bufferEndIndex); 240 int breakIndex1 = indexOf(data, lineBreak, offset, bufferEndIndex);
243 if (breakIndex1 == -1) { 241 if (breakIndex1 == -1) {
244 throw new HgBadArgumentException("Bad Changeset data", null); 242 throw new HgInvalidDataFormatException("Bad Changeset data");
245 } 243 }
246 Nodeid _nodeid = Nodeid.fromAscii(data, 0, breakIndex1); 244 Nodeid _nodeid = Nodeid.fromAscii(data, 0, breakIndex1);
247 int breakIndex2 = indexOf(data, lineBreak, breakIndex1 + 1, bufferEndIndex); 245 int breakIndex2 = indexOf(data, lineBreak, breakIndex1 + 1, bufferEndIndex);
248 if (breakIndex2 == -1) { 246 if (breakIndex2 == -1) {
249 throw new HgBadArgumentException("Bad Changeset data", null); 247 throw new HgInvalidDataFormatException("Bad Changeset data");
250 } 248 }
251 String _user = new String(data, breakIndex1 + 1, breakIndex2 - breakIndex1 - 1); 249 String _user = new String(data, breakIndex1 + 1, breakIndex2 - breakIndex1 - 1);
252 if (usersPool != null) { 250 if (usersPool != null) {
253 _user = usersPool.unify(_user); 251 _user = usersPool.unify(_user);
254 } 252 }
255 int breakIndex3 = indexOf(data, lineBreak, breakIndex2 + 1, bufferEndIndex); 253 int breakIndex3 = indexOf(data, lineBreak, breakIndex2 + 1, bufferEndIndex);
256 if (breakIndex3 == -1) { 254 if (breakIndex3 == -1) {
257 throw new HgBadArgumentException("Bad Changeset data", null); 255 throw new HgInvalidDataFormatException("Bad Changeset data");
258 } 256 }
259 String _timeString = new String(data, breakIndex2 + 1, breakIndex3 - breakIndex2 - 1); 257 String _timeString = new String(data, breakIndex2 + 1, breakIndex3 - breakIndex2 - 1);
260 int space1 = _timeString.indexOf(' '); 258 int space1 = _timeString.indexOf(' ');
261 if (space1 == -1) { 259 if (space1 == -1) {
262 throw new HgBadArgumentException(String.format("Bad Changeset data: %s in [%d..%d]", "time string", breakIndex2+1, breakIndex3), null); 260 throw new HgInvalidDataFormatException(String.format("Bad Changeset data: %s in [%d..%d]", "time string", breakIndex2+1, breakIndex3));
263 } 261 }
264 int space2 = _timeString.indexOf(' ', space1 + 1); 262 int space2 = _timeString.indexOf(' ', space1 + 1);
265 if (space2 == -1) { 263 if (space2 == -1) {
266 space2 = _timeString.length(); 264 space2 = _timeString.length();
267 } 265 }
303 } else { 301 } else {
304 breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex); 302 breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex);
305 } 303 }
306 } 304 }
307 if (breakIndex4 == -1 || breakIndex4 >= bufferEndIndex) { 305 if (breakIndex4 == -1 || breakIndex4 >= bufferEndIndex) {
308 throw new HgBadArgumentException("Bad Changeset data", null); 306 throw new HgInvalidDataFormatException("Bad Changeset data");
309 } 307 }
310 } else { 308 } else {
311 breakIndex4--; 309 breakIndex4--;
312 } 310 }
313 String _comment; 311 String _comment;
315 _comment = new String(data, breakIndex4 + 2, bufferEndIndex - breakIndex4 - 2, "UTF-8"); 313 _comment = new String(data, breakIndex4 + 2, bufferEndIndex - breakIndex4 - 2, "UTF-8");
316 // TODO post-1.0 respect ui.fallbackencoding and try to decode if set; use EncodingHelper 314 // TODO post-1.0 respect ui.fallbackencoding and try to decode if set; use EncodingHelper
317 } catch (UnsupportedEncodingException ex) { 315 } catch (UnsupportedEncodingException ex) {
318 _comment = ""; 316 _comment = "";
319 // Could hardly happen 317 // Could hardly happen
320 throw new HgBadArgumentException("Bad Changeset data", ex); 318 throw new HgInvalidDataFormatException("Bad Changeset data", ex);
321 } 319 }
322 // change this instance at once, don't leave it partially changes in case of error 320 // change this instance at once, don't leave it partially changes in case of error
323 this.manifest = _nodeid; 321 this.manifest = _nodeid;
324 this.user = _user; 322 this.user = _user;
325 this.time = _time; 323 this.time = _time;
379 byte[] data = da.byteArray(); 377 byte[] data = da.byteArray();
380 cset.init(data, 0, data.length, usersPool); 378 cset.init(data, 0, data.length, usersPool);
381 // XXX there's no guarantee for Changeset.Callback that distinct instance comes each time, consider instance reuse 379 // XXX there's no guarantee for Changeset.Callback that distinct instance comes each time, consider instance reuse
382 inspector.next(revisionNumber, Nodeid.fromBinary(nodeid, 0), cset); 380 inspector.next(revisionNumber, Nodeid.fromBinary(nodeid, 0), cset);
383 progressHelper.worked(1); 381 progressHelper.worked(1);
384 } catch (HgBadArgumentException ex) { 382 } catch (HgInvalidDataFormatException ex) {
385 // see below about better exception 383 throw ex.setRevisionIndex(revisionNumber);
386 throw new HgInvalidControlFileException("Failed reading changelog", ex, null).setRevisionIndex(revisionNumber);
387 } catch (IOException ex) { 384 } catch (IOException ex) {
388 // XXX need better exception, perhaps smth like HgChangelogException (extends HgInvalidControlFileException) 385 // XXX need better exception, perhaps smth like HgChangelogException (extends HgInvalidControlFileException)
389 throw new HgInvalidControlFileException("Failed reading changelog", ex, null).setRevisionIndex(revisionNumber); 386 throw new HgInvalidControlFileException("Failed reading changelog", ex, null).setRevisionIndex(revisionNumber);
390 } 387 }
391 if (iterateControl != null) { 388 if (iterateControl != null) {