forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSONWriter.java
More file actions
601 lines (526 loc) · 16.4 KB
/
Copy pathBSONWriter.java
File metadata and controls
601 lines (526 loc) · 16.4 KB
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*
* Copyright (c) 2008-2014 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb;
import org.bson.BSONException;
import org.bson.types.BSONTimestamp;
import org.bson.types.Binary;
import org.bson.types.ObjectId;
import java.io.Closeable;
import java.util.Arrays;
import static java.lang.String.format;
abstract class BSONWriter implements Closeable {
private final BSONWriterSettings settings;
private State state;
private Context context;
private String currentName;
private int serializationDepth;
private boolean closed;
/**
* Initializes a new instance of the BSONWriter class.
*
* @param settings The writer settings.
*/
protected BSONWriter(final BSONWriterSettings settings) {
this.settings = settings;
state = State.INITIAL;
}
protected String getName() {
return currentName;
}
protected boolean isClosed() {
return closed;
}
protected void setState(final State state) {
this.state = state;
}
protected State getState() {
return state;
}
protected Context getContext() {
return context;
}
protected void setContext(final Context context) {
this.context = context;
}
/**
* Flushes any pending data to the output destination.
*/
public abstract void flush();
/**
* Writes a BSON Binary data element to the writer.
*
* @param binary The Binary data.
*/
public abstract void writeBinaryData(Binary binary);
/**
* Writes a BSON Binary data element to the writer.
*
* @param name The name of the element.
* @param binary The Binary data value.
*/
public void writeBinaryData(final String name, final Binary binary) {
writeName(name);
writeBinaryData(binary);
}
/**
* Writes a BSON Boolean to the writer.
*
* @param value The Boolean value.
*/
public abstract void writeBoolean(boolean value);
/**
* Writes a BSON Boolean element to the writer.
*
* @param name The name of the element.
* @param value The Boolean value.
*/
public void writeBoolean(final String name, final boolean value) {
writeName(name);
writeBoolean(value);
}
/**
* Writes a BSON DateTime to the writer.
*
* @param value The number of milliseconds since the Unix epoch.
*/
public abstract void writeDateTime(long value);
/**
* Writes a BSON DateTime element to the writer.
*
* @param name The name of the element.
* @param value The number of milliseconds since the Unix epoch.
*/
public void writeDateTime(final String name, final long value) {
writeName(name);
writeDateTime(value);
}
/**
* Writes a BSON Double to the writer.
*
* @param value The Double value.
*/
public abstract void writeDouble(double value);
/**
* Writes a BSON Double element to the writer.
*
* @param name The name of the element.
* @param value The Double value.
*/
public void writeDouble(final String name, final double value) {
writeName(name);
writeDouble(value);
}
/**
* Writes the end of a BSON array to the writer.
*/
public void writeEndArray() {
serializationDepth--;
}
/**
* Writes the end of a BSON document to the writer.
*/
public void writeEndDocument() {
serializationDepth--;
}
/**
* Writes a BSON Int32 to the writer.
*
* @param value The Int32 value.
*/
public abstract void writeInt32(int value);
/**
* Writes a BSON Int32 element to the writer.
*
* @param name The name of the element.
* @param value The Int32 value.
*/
public void writeInt32(final String name, final int value) {
writeName(name);
writeInt32(value);
}
/**
* Writes a BSON Int64 to the writer.
*
* @param value The Int64 value.
*/
public abstract void writeInt64(long value);
/**
* Writes a BSON Int64 element to the writer.
*
* @param name The name of the element.
* @param value The Int64 value.
*/
public void writeInt64(final String name, final long value) {
writeName(name);
writeInt64(value);
}
/**
* Writes a BSON JavaScript to the writer.
*
* @param code The JavaScript code.
*/
public abstract void writeJavaScript(String code);
/**
* Writes a BSON JavaScript element to the writer.
*
* @param name The name of the element.
* @param code The JavaScript code.
*/
public void writeJavaScript(final String name, final String code) {
writeName(name);
writeJavaScript(code);
}
/**
* Writes a BSON JavaScript to the writer (call WriteStartDocument to start writing the scope).
*
* @param code The JavaScript code.
*/
public abstract void writeJavaScriptWithScope(String code);
/**
* Writes a BSON JavaScript element to the writer (call WriteStartDocument to start writing the scope).
*
* @param name The name of the element.
* @param code The JavaScript code.
*/
public void writeJavaScriptWithScope(final String name, final String code) {
writeName(name);
writeJavaScriptWithScope(code);
}
/**
* Writes a BSON MaxKey to the writer.
*/
public abstract void writeMaxKey();
/**
* Writes a BSON MaxKey element to the writer.
*
* @param name The name of the element.
*/
public void writeMaxKey(final String name) {
writeName(name);
writeMaxKey();
}
/**
* Writes a BSON MinKey to the writer.
*/
public abstract void writeMinKey();
/**
* Writes a BSON MinKey element to the writer.
*
* @param name The name of the element.
*/
public void writeMinKey(final String name) {
writeName(name);
writeMinKey();
}
/**
* Writes the name of an element to the writer.
*
* @param name The name of the element.
*/
public void writeName(final String name) {
if (state != State.NAME) {
throwInvalidState("WriteName", State.NAME);
}
this.currentName = name;
state = State.VALUE;
}
/**
* Writes a BSON null to the writer.
*/
public abstract void writeNull();
/**
* Writes a BSON null element to the writer.
*
* @param name The name of the element.
*/
public void writeNull(final String name) {
writeName(name);
writeNull();
}
/**
* Writes a BSON ObjectId to the writer.
*
* @param objectId The ObjectId value.
*/
public abstract void writeObjectId(ObjectId objectId);
/**
* Writes a BSON ObjectId element to the writer.
*
* @param name The name of the element.
* @param objectId The ObjectId value.
*/
public void writeObjectId(final String name, final ObjectId objectId) {
writeName(name);
writeObjectId(objectId);
}
// /**
// * Writes a BSON regular expression to the writer.
// */
// public abstract void writeRegularExpression(RegularExpression regularExpression);
//
// /**
// * Writes a BSON regular expression element to the writer.
// *
// * @param name The name of the element.
// * @param regularExpression The RegularExpression value.
// */
// public void writeRegularExpression(final String name, final RegularExpression regularExpression) {
// writeName(name);
// writeRegularExpression(regularExpression);
// }
//
/**
* Writes the start of a BSON array to the writer.
*
* @throws BSONException if maximum serialization depth exceeded.
*/
public void writeStartArray() {
serializationDepth++;
if (serializationDepth > settings.getMaxSerializationDepth()) {
throw new BSONException("Maximum serialization depth exceeded (does the object being serialized have a circular reference?).");
}
}
/**
* Writes the start of a BSON array element to the writer.
*
* @param name The name of the element.
*/
public void writeStartArray(final String name) {
writeName(name);
writeStartArray();
}
/**
* Writes the start of a BSON document to the writer.
*
* @throws BSONException if maximum serialization depth exceeded.
*/
public void writeStartDocument() {
serializationDepth++;
if (serializationDepth > settings.getMaxSerializationDepth()) {
throw new BSONException("Maximum serialization depth exceeded (does the object being serialized have a circular reference?).");
}
}
/**
* Writes the start of a BSON document element to the writer.
*
* @param name The name of the element.
*/
public void writeStartDocument(final String name) {
writeName(name);
writeStartDocument();
}
/**
* Writes a BSON String to the writer.
*
* @param value The String value.
*/
public abstract void writeString(String value);
/**
* Writes a BSON String element to the writer.
*
* @param name The name of the element.
* @param value The String value.
*/
public void writeString(final String name, final String value) {
writeName(name);
writeString(value);
}
/**
* Writes a BSON Symbol to the writer.
*
* @param value The symbol.
*/
public abstract void writeSymbol(String value);
/**
* Writes a BSON Symbol element to the writer.
*
* @param name The name of the element.
* @param value The symbol.
*/
public void writeSymbol(final String name, final String value) {
writeName(name);
writeSymbol(value);
}
/**
* Writes a BSON Timestamp to the writer.
*
* @param value The combined timestamp/increment value.
*/
public abstract void writeTimestamp(BSONTimestamp value);
/**
* Writes a BSON Timestamp element to the writer.
*
* @param name The name of the element.
* @param value The combined timestamp/increment value.
*/
public void writeTimestamp(final String name, final BSONTimestamp value) {
writeName(name);
writeTimestamp(value);
}
/**
* Writes a BSON undefined to the writer.
*/
public abstract void writeUndefined();
/**
* Writes a BSON undefined element to the writer.
*
* @param name The name of the element.
*/
public void writeUndefined(final String name) {
writeName(name);
writeUndefined();
}
protected State getNextState() {
if (getContext().getContextType() == BSONContextType.ARRAY) {
return State.VALUE;
} else {
return State.NAME;
}
}
protected boolean checkState(final State[] validStates) {
for (final State cur : validStates) {
if (cur == getState()) {
return true;
}
}
return false;
}
protected void checkPreconditions(final String methodName, final State... validStates) {
if (isClosed()) {
throw new IllegalStateException("BSONWriter");
}
if (!checkState(validStates)) {
throwInvalidState(methodName, validStates);
}
}
/**
* Throws an InvalidOperationException when the method called is not valid for the current ContextType.
*
* @param methodName The name of the method.
* @param actualContextType The actual ContextType.
* @param validContextTypes The valid ContextTypes.
* @throws BSONException
*/
protected void throwInvalidContextType(final String methodName, final BSONContextType actualContextType,
final BSONContextType... validContextTypes) {
final String validContextTypesString = StringUtils.join(" or ", Arrays.asList(validContextTypes));
final String message = format("%s can only be called when ContextType is %s, "
+ "not when ContextType is %s.", methodName, validContextTypesString,
actualContextType);
throw new BSONException(message);
}
/**
* Throws an InvalidOperationException when the method called is not valid for the current state.
*
* @param methodName The name of the method.
* @param validStates The valid states.
* @throws BSONException
*/
protected void throwInvalidState(final String methodName, final State... validStates) {
final String message;
if (state == State.INITIAL || state == State.SCOPE_DOCUMENT || state == State.DONE) {
if (!methodName.startsWith("end") && !methodName.equals("writeName")) { // NOPMD
//NOPMD collapsing these if statements will not aid readability
String typeName = methodName.substring(5);
if (typeName.startsWith("start")) {
typeName = typeName.substring(5);
}
String article = "A";
if (Arrays.asList('A', 'E', 'I', 'O', 'U').contains(typeName.charAt(0))) {
article = "An";
}
message = format("%s %s value cannot be written to the root level of a BSON document.", article,
typeName);
throw new BSONException(message);
}
}
final String validStatesString = StringUtils.join(" or ", Arrays.asList(validStates));
message = format("%s can only be called when State is %s, not when State is %s", methodName,
validStatesString, state);
throw new BSONException(message);
}
/**
* Closes the writer.
*/
public void close() {
closed = true;
}
enum State {
/**
* The initial state.
*/
INITIAL,
/**
* The writer is positioned to write a name.
*/
NAME,
/**
* The writer is positioned to write a value.
*/
VALUE,
/**
* The writer is positioned to write a scope document (call WriteStartDocument to start writing the scope document).
*/
SCOPE_DOCUMENT,
/**
* The writer is done.
*/
DONE,
/**
* The writer is closed.
*/
CLOSED
}
class Context {
private final Context parentContext;
private final BSONContextType contextType;
public Context(final Context from) {
parentContext = from.parentContext;
contextType = from.contextType;
}
public Context(final Context parentContext, final BSONContextType contextType) {
this.parentContext = parentContext;
this.contextType = contextType;
}
public Context getParentContext() {
return parentContext;
}
public BSONContextType getContextType() {
return contextType;
}
public Context copy() {
return new Context(this);
}
}
class Mark {
private final Context markedContext;
private final State markedState;
private final String currentName;
private final int serializationDepth;
protected Mark() {
this.markedContext = BSONWriter.this.context.copy();
this.markedState = BSONWriter.this.state;
this.currentName = BSONWriter.this.currentName;
this.serializationDepth = BSONWriter.this.serializationDepth;
}
protected void reset() {
setContext(markedContext);
setState(markedState);
BSONWriter.this.currentName = currentName;
BSONWriter.this.serializationDepth = serializationDepth;
}
}
}