-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvndump.jj
379 lines (327 loc) · 11.7 KB
/
svndump.jj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
options {
STATIC = false;
JAVA_UNICODE_ESCAPE = false;
USER_CHAR_STREAM = true;
}
PARSER_BEGIN(SvnDumpParser)
package com.github.cstroe.svndumpgui.generated;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.LinkedHashMap;
import java.security.MessageDigest;
import com.github.cstroe.svndumpgui.api.ContentChunk;
import com.github.cstroe.svndumpgui.api.RepositoryConsumer;
import com.github.cstroe.svndumpgui.api.Preamble;
import com.github.cstroe.svndumpgui.api.RepositoryWriter;
import com.github.cstroe.svndumpgui.internal.utility.SvnDumpCharStream;
import com.github.cstroe.svndumpgui.api.Node;
import com.github.cstroe.svndumpgui.api.NodeHeader;
import com.github.cstroe.svndumpgui.api.Property;
import com.github.cstroe.svndumpgui.api.Revision;
import com.github.cstroe.svndumpgui.internal.ContentChunkImpl;
import com.github.cstroe.svndumpgui.internal.PreambleImpl;
import com.github.cstroe.svndumpgui.internal.NodeImpl;
import com.github.cstroe.svndumpgui.internal.RevisionImpl;
import com.github.cstroe.svndumpgui.internal.utility.SvnDumpCharStream;
public class SvnDumpParser {
private int fileContentChunkSize = 1024 * 1024 * 4; // 4 MB buffer for file content chunks by default
public void setFileContentChunkSize(int size) {
this.fileContentChunkSize = size;
}
private String readCharacters(Integer numberOfBytes) {
if(numberOfBytes == null) {
return null;
}
try {
byte[] bytes = ((SvnDumpCharStream) token_source.input_stream).readBytes(numberOfBytes);
return new String(bytes, StandardCharsets.UTF_8);
} catch(IOException ex) {
throw new RuntimeException(ex);
}
}
private void readChunks(Long length, RepositoryConsumer consumer, String md5sum, String sha1sum, String path) throws ParseException {
if(length == null || length == 0) {
return;
}
MessageDigest md5;
MessageDigest sha1;
try {
md5 = MessageDigest.getInstance("MD5");
sha1 = MessageDigest.getInstance("SHA1");
} catch(java.security.NoSuchAlgorithmException ex) {
throw new RuntimeException(ex);
}
SvnDumpCharStream stream = (SvnDumpCharStream) token_source.input_stream;
long lengthLeft = length;
while(lengthLeft > 0) {
int chunkSize = (int) Math.min((long)fileContentChunkSize, lengthLeft);
ContentChunk chunk = null;
try {
byte[] freshBytes = stream.readBytes(chunkSize);
if(md5sum != null) {
md5.update(freshBytes);
}
if(sha1sum != null) {
sha1.update(freshBytes);
}
chunk = new ContentChunkImpl(freshBytes);
lengthLeft -= chunkSize;
} catch (IOException ex) {
throw new RuntimeException(ex);
}
assert chunk != null;
consumer.consume(chunk);
}
consumer.endChunks();
String computedMd5Sum = toHex(md5.digest(), 32);
String computedSha1Sum = toHex(sha1.digest(), 40);
if(md5sum != null && !md5sum.equals(computedMd5Sum)) {
throw new ParseException("MD5 sum is incorrect! Expected: " + md5sum + ", Actual: " + computedMd5Sum);
}
if(sha1sum != null && !sha1sum.equals(computedSha1Sum)) {
throw new ParseException("SHA1 sum is incorrect! Expected: " + sha1sum + ", Actual: " + computedSha1Sum);
}
token.next = null; // we've advanced in the stream, we don't know what the next token is.
}
// swiped from http://stackoverflow.com/questions/415953
private String toHex(byte[] digest, int length) {
BigInteger bitInt = new BigInteger(1, digest);
String hashText = bitInt.toString(16);
while(hashText.length() < length) {
hashText = "0" + hashText;
}
return hashText;
}
public static void consume(InputStream inputStream, RepositoryConsumer consumer) throws ParseException {
new SvnDumpParser(new SvnDumpCharStream(inputStream)).Start(consumer);
}
}
PARSER_END(SvnDumpParser)
// Lexer
SKIP: { " " }
TOKEN: { <EOL: "\n" | "\r" | "\r\n"> }
TOKEN: { <NUMBER: (["0"-"9"])+> }
TOKEN: { < COLON: ":"> }
TOKEN: { <VERSION_KEY: "SVN-fs-dump-format-version"> }
TOKEN: { <UUID_KEY: "UUID"> }
TOKEN: { <UUID_VALUE: (["0"-"9","a"-"z"]){8} "-" (["0"-"9","a"-"z"]){4} "-" (["0"-"9","a"-"z"]){4} "-" (["0"-"9","a"-"z"]){4} "-" (["0"-"9","a"-"z"]){12}> }
TOKEN: { <REVISION_NUMBER_KEY: "Revision-number"> }
TOKEN: { <PROP_CONTENT_LENGTH_KEY: "Prop-content-length"> }
TOKEN: { <CONTENT_LENGTH_KEY: "Content-length"> }
TOKEN: { <KEY: "K"> }
TOKEN: { <VAL: "V"> }
TOKEN: { <PROPS_END: "PROPS-END"> }
TOKEN: { <NODE_PATH_KEY: "Node-path: "> : READ_PATH }
TOKEN: { <NODE_KIND_KEY: "Node-kind"> }
TOKEN: { <NODE_KIND_VALUE: ("file"|"dir")> }
TOKEN: { <NODE_ACTION_KEY: "Node-action"> }
TOKEN: { <NODE_ACTION_VALUE: ("change" | "add" | "delete" | "replace")> }
TOKEN: { <NODE_COPYFROM_REV_KEY: "Node-copyfrom-rev"> }
TOKEN: { <NODE_COPYFROM_PATH_KEY: "Node-copyfrom-path: "> : READ_PATH }
TOKEN: { <TEXT_CONTENT_LENGTH_KEY: "Text-content-length"> }
TOKEN: { <TEXT_CONTENT_MD5_KEY: "Text-content-md5"> }
TOKEN: { <MD5_VALUE: (["0"-"9","a"-"z"]){32}> }
TOKEN: { <TEXT_CONTENT_SHA1_KEY: "Text-content-sha1"> }
TOKEN: { <SHA1_VALUE: (["0"-"9","a"-"z"]){40}> }
TOKEN: { <TEXT_COPYSOURCE_MD5_KEY: "Text-copy-source-md5"> }
TOKEN: { <TEXT_COPYSOURCE_SHA1_KEY: "Text-copy-source-sha1"> }
<READ_PATH> TOKEN: { <NODE_PATH_VALUE: (~["\n","\r"])*> : DEFAULT }
// Parser
public void Start(RepositoryConsumer consumer):
{
Token dumpVersion;
Token uuid;
Revision revision;
Node node = null;
}
{
{
if(!(token_source.input_stream instanceof SvnDumpCharStream)) {
throw new IllegalArgumentException("SvnDumpParser expects only an SvnDumpCharStream as the input stream.");
}
}
<VERSION_KEY> <COLON> dumpVersion = <NUMBER> <EOL>
<EOL>
<UUID_KEY> <COLON> uuid = <UUID_VALUE> <EOL>
<EOL>
{
Preamble preamble = new PreambleImpl(uuid.image);
consumer.consume(preamble);
}
(
revision = Revision()
{ consumer.consume(revision); }
<EOL>
(
node = Node(revision)
{
consumer.consume(node);
String textContentLength = node.get(NodeHeader.TEXT_CONTENT_LENGTH);
String md5sum = node.get(NodeHeader.MD5);
String sha1sum = node.get(NodeHeader.SHA1);
if(textContentLength != null && Long.parseLong(textContentLength) != 0) {
readChunks(Long.parseLong(textContentLength), consumer, md5sum, sha1sum, node.get(NodeHeader.PATH));
}
}
(
<EOL>
{
// really bad hack to know how many EOLs are in the stream
String currentValueRaw = node.getProperties().get(Property.TRAILING_NEWLINE_HINT);
if(currentValueRaw != null) {
int currentValue = Integer.parseInt(currentValueRaw);
currentValue++;
node.getProperties().put(Property.TRAILING_NEWLINE_HINT, Integer.toString(currentValue));
} else {
node.getProperties().put(Property.TRAILING_NEWLINE_HINT, "1");
}
}
)+
{
consumer.endNode(node);
}
)*
{
consumer.endRevision(revision);
revision = null;
}
)*
<EOF>
{ consumer.finish(); }
}
public Revision Revision():
{
RevisionImpl revision;
Token revisionNumber;
Map properties;
}
{
<REVISION_NUMBER_KEY> <COLON> revisionNumber = <NUMBER> <EOL>
( <PROP_CONTENT_LENGTH_KEY> <COLON> <NUMBER> <EOL> )?
<CONTENT_LENGTH_KEY> <COLON> <NUMBER> <EOL>
<EOL>
{ revision = new RevisionImpl(Integer.parseInt(revisionNumber.image)); }
properties = Property()
{ revision.setProperties(properties); }
{ return revision; }
}
public Map Property():
{
Map properties = new LinkedHashMap();
String key, value;
Token keyLength, valueLength;
}
{
(
<KEY> keyLength = <NUMBER> <EOL>
{ key = readCharacters(Integer.parseInt(keyLength.image)); }
<EOL>
<VAL> valueLength = <NUMBER> <EOL>
{ value = readCharacters(Integer.parseInt(valueLength.image)); }
<EOL>
{
properties.put(key, value);
}
)*
<PROPS_END> <EOL>
{ return properties; }
}
public Node Node(Revision revision):
{
NodeImpl svnNode;
Token nodePath = null;
Token nodeKind = null;
Token nodeAction = null;
Token nodeMd5 = null;
Token nodeSha1 = null;
Token contentLength = null;
Token textContentLength = null;
Token propContentLength = null;
Token copiedFromRevision = null;
Token copiedFromPath = null;
Token copiedFromMd5 = null;
Token copiedFromSha1 = null;
Map headers = new LinkedHashMap();
}
{
(
<NODE_PATH_KEY> nodePath = <NODE_PATH_VALUE> <EOL>
{
headers.put(NodeHeader.PATH, nodePath.image);
}
|
<NODE_KIND_KEY> <COLON> nodeKind = <NODE_KIND_VALUE> <EOL>
{
headers.put(NodeHeader.KIND, nodeKind.image);
}
|
<NODE_ACTION_KEY> <COLON> nodeAction = <NODE_ACTION_VALUE> <EOL>
{
headers.put(NodeHeader.ACTION, nodeAction.image);
}
|
<PROP_CONTENT_LENGTH_KEY> <COLON> propContentLength = <NUMBER> <EOL>
{
headers.put(NodeHeader.PROP_CONTENT_LENGTH, propContentLength.image);
}
|
<TEXT_CONTENT_LENGTH_KEY> <COLON> textContentLength = <NUMBER> <EOL>
{
headers.put(NodeHeader.TEXT_CONTENT_LENGTH, textContentLength.image);
}
|
<TEXT_CONTENT_MD5_KEY> <COLON> nodeMd5 = <MD5_VALUE> <EOL>
{
headers.put(NodeHeader.MD5, nodeMd5.image);
}
|
<TEXT_CONTENT_SHA1_KEY> <COLON> nodeSha1 = <SHA1_VALUE> <EOL>
{
headers.put(NodeHeader.SHA1, nodeSha1.image);
}
|
<CONTENT_LENGTH_KEY> <COLON> contentLength = <NUMBER> <EOL>
{
headers.put(NodeHeader.CONTENT_LENGTH, contentLength.image);
}
|
<NODE_COPYFROM_REV_KEY> <COLON> copiedFromRevision = <NUMBER> <EOL>
{
headers.put(NodeHeader.COPY_FROM_REV, copiedFromRevision.image);
}
|
<NODE_COPYFROM_PATH_KEY> copiedFromPath = <NODE_PATH_VALUE> <EOL>
{
headers.put(NodeHeader.COPY_FROM_PATH, copiedFromPath.image);
}
|
<TEXT_COPYSOURCE_MD5_KEY> <COLON> copiedFromMd5 = <MD5_VALUE> <EOL>
{
headers.put(NodeHeader.SOURCE_MD5, copiedFromMd5.image);
}
|
<TEXT_COPYSOURCE_SHA1_KEY> <COLON> copiedFromSha1 = <SHA1_VALUE> <EOL>
{
headers.put(NodeHeader.SOURCE_SHA1, copiedFromSha1.image);
}
)+
{
svnNode = new NodeImpl(revision);
svnNode.setHeaders(headers);
headers = null;
if(propContentLength != null) {
jj_consume_token(EOL);
svnNode.setProperties(Property());
}
}
{ return svnNode; }
}