comparison src/org/tmatesoft/hg/internal/ChangelogEntryBuilder.java @ 667:fba85bc1dfb8

Refactoring: move all encoding/decoding operations into single place, EncodingHelper
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 11 Jul 2013 17:54:08 +0200
parents 4e6179bde4fc
children
comparison
equal deleted inserted replaced
666:27a3ddedd6cc 667:fba85bc1dfb8
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
16 */ 16 */
17 package org.tmatesoft.hg.internal; 17 package org.tmatesoft.hg.internal;
18 18
19 import java.io.ByteArrayOutputStream; 19 import java.io.ByteArrayOutputStream;
20 import java.io.UnsupportedEncodingException;
21 import java.util.ArrayList; 20 import java.util.ArrayList;
22 import java.util.Collection; 21 import java.util.Collection;
23 import java.util.Collections; 22 import java.util.Collections;
24 import java.util.Iterator; 23 import java.util.Iterator;
25 import java.util.LinkedHashMap; 24 import java.util.LinkedHashMap;
29 import java.util.TimeZone; 28 import java.util.TimeZone;
30 29
31 import org.tmatesoft.hg.core.HgIOException; 30 import org.tmatesoft.hg.core.HgIOException;
32 import org.tmatesoft.hg.core.Nodeid; 31 import org.tmatesoft.hg.core.Nodeid;
33 import org.tmatesoft.hg.internal.DataSerializer.DataSource; 32 import org.tmatesoft.hg.internal.DataSerializer.DataSource;
34 import org.tmatesoft.hg.repo.HgInvalidStateException;
35 import org.tmatesoft.hg.util.Path; 33 import org.tmatesoft.hg.util.Path;
36 34
37 /** 35 /**
38 * Builds changelog entry 36 * Builds changelog entry
39 * @author Artem Tikhomirov 37 * @author Artem Tikhomirov
40 * @author TMate Software Ltd. 38 * @author TMate Software Ltd.
41 */ 39 */
42 public class ChangelogEntryBuilder implements DataSource { 40 public class ChangelogEntryBuilder implements DataSource {
43 41
42 private final EncodingHelper encHelper;
44 private String user; 43 private String user;
45 private List<Path> modifiedFiles; 44 private List<Path> modifiedFiles;
46 private final Map<String, String> extrasMap = new LinkedHashMap<String, String>(); 45 private final Map<String, String> extrasMap = new LinkedHashMap<String, String>();
47 private Integer tzOffset; 46 private Integer tzOffset;
48 private Long csetTime; 47 private Long csetTime;
49 private Nodeid manifestRev; 48 private Nodeid manifestRev;
50 private CharSequence comment; 49 private CharSequence comment;
50
51 ChangelogEntryBuilder(EncodingHelper encodingHelper) {
52 encHelper = encodingHelper;
53 }
51 54
52 public ChangelogEntryBuilder user(String username) { 55 public ChangelogEntryBuilder user(String username) {
53 user = username; 56 user = username;
54 return this; 57 return this;
55 } 58 }
114 public int serializeLength() { 117 public int serializeLength() {
115 return -1; 118 return -1;
116 } 119 }
117 120
118 public byte[] build() { 121 public byte[] build() {
119 try { 122 ByteArrayOutputStream out = new ByteArrayOutputStream();
120 ByteArrayOutputStream out = new ByteArrayOutputStream(); 123 final int LF = '\n';
121 final int LF = '\n'; 124 CharSequence extras = buildExtras();
122 CharSequence extras = buildExtras(); 125 CharSequence files = buildFiles();
123 CharSequence files = buildFiles(); 126 byte[] manifestRevision = manifestRev.toString().getBytes();
124 byte[] manifestRevision = manifestRev.toString().getBytes(); 127 byte[] username = encHelper.userToChangeset(user());
125 byte[] username = user().getBytes(EncodingHelper.getUTF8().name()); // XXX Java 1.5 128 out.write(manifestRevision, 0, manifestRevision.length);
126 out.write(manifestRevision, 0, manifestRevision.length); 129 out.write(LF);
127 out.write(LF); 130 out.write(username, 0, username.length);
128 out.write(username, 0, username.length); 131 out.write(LF);
129 out.write(LF); 132 final long csetDate = csetTime();
130 final long csetDate = csetTime(); 133 byte[] date = String.format("%d %d", csetDate, csetTimezone(csetDate)).getBytes();
131 byte[] date = String.format("%d %d", csetDate, csetTimezone(csetDate)).getBytes(); 134 out.write(date, 0, date.length);
132 out.write(date, 0, date.length); 135 if (extras.length() > 0) {
133 if (extras.length() > 0) { 136 out.write(' ');
134 out.write(' '); 137 byte[] b = extras.toString().getBytes();
135 byte[] b = extras.toString().getBytes();
136 out.write(b, 0, b.length);
137 }
138 out.write(LF);
139 byte[] b = files.toString().getBytes();
140 out.write(b, 0, b.length); 138 out.write(b, 0, b.length);
141 out.write(LF);
142 out.write(LF);
143 byte[] cmt = comment.toString().getBytes(EncodingHelper.getUTF8().name()); // XXX Java 1.5
144 out.write(cmt, 0, cmt.length);
145 return out.toByteArray();
146 } catch (UnsupportedEncodingException ex) {
147 throw new HgInvalidStateException(ex.getMessage()); // Can't happen, UTF8 is always there
148 } 139 }
140 out.write(LF);
141 byte[] b = encHelper.fileToChangeset(files);
142 out.write(b, 0, b.length);
143 out.write(LF);
144 out.write(LF);
145 byte[] cmt = encHelper.commentToChangeset(comment);
146 out.write(cmt, 0, cmt.length);
147 return out.toByteArray();
149 } 148 }
150 149
151 private CharSequence buildExtras() { 150 private CharSequence buildExtras() {
152 StringBuilder extras = new StringBuilder(); 151 StringBuilder extras = new StringBuilder();
153 for (Iterator<Entry<String, String>> it = extrasMap.entrySet().iterator(); it.hasNext();) { 152 for (Iterator<Entry<String, String>> it = extrasMap.entrySet().iterator(); it.hasNext();) {