forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicBSONCallback.java
More file actions
216 lines (172 loc) · 5.65 KB
/
Copy pathBasicBSONCallback.java
File metadata and controls
216 lines (172 loc) · 5.65 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
/*
* 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.
*/
// BasicBSONCallback.java
package org.bson;
import org.bson.types.BSONTimestamp;
import org.bson.types.BasicBSONList;
import org.bson.types.Binary;
import org.bson.types.Code;
import org.bson.types.CodeWScope;
import org.bson.types.MaxKey;
import org.bson.types.MinKey;
import org.bson.types.ObjectId;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;
public class BasicBSONCallback implements BSONCallback {
public BasicBSONCallback(){
reset();
}
public BSONObject create(){
return new BasicBSONObject();
}
protected BSONObject createList() {
return new BasicBSONList();
}
public BSONCallback createBSONCallback(){
return new BasicBSONCallback();
}
public BSONObject create( boolean array , List<String> path ){
if ( array )
return createList();
return create();
}
public void objectStart(){
if ( _stack.size() > 0 )
throw new IllegalStateException( "something is wrong" );
objectStart(false);
}
public void objectStart(boolean array){
_root = create(array, null);
_stack.add( (BSONObject)_root );
}
public void objectStart(String name){
objectStart( false , name );
}
public void objectStart(boolean array, String name){
_nameStack.addLast( name );
final BSONObject o = create( array , _nameStack );
_stack.getLast().put( name , o);
_stack.addLast( o );
}
public Object objectDone(){
final BSONObject o =_stack.removeLast();
if ( _nameStack.size() > 0 )
_nameStack.removeLast();
else if ( _stack.size() > 0 )
throw new IllegalStateException( "something is wrong" );
return !BSON.hasDecodeHooks() ? o : (BSONObject)BSON.applyDecodingHooks(o);
}
public void arrayStart(){
objectStart( true );
}
public void arrayStart(String name){
objectStart( true , name );
}
public Object arrayDone(){
return objectDone();
}
public void gotNull( String name ){
cur().put( name , null );
}
public void gotUndefined( String name ) { }
public void gotMinKey( String name ){
cur().put( name , new MinKey() );
}
public void gotMaxKey( String name ){
cur().put( name , new MaxKey() );
}
public void gotBoolean( String name , boolean v ){
_put( name , v );
}
public void gotDouble( final String name , final double v ){
_put( name , v );
}
public void gotInt( final String name , final int v ){
_put( name , v );
}
public void gotLong( final String name , final long v ){
_put( name , v );
}
public void gotDate( String name , long millis ){
_put( name , new Date( millis ) );
}
public void gotRegex( String name , String pattern , String flags ){
_put( name , Pattern.compile( pattern , BSON.regexFlags( flags ) ) );
}
public void gotString( final String name , final String v ){
_put( name , v );
}
public void gotSymbol( String name , String v ){
_put( name , v );
}
public void gotTimestamp( String name , int time , int inc ){
_put( name , new BSONTimestamp( time , inc ) );
}
public void gotObjectId( String name , ObjectId id ){
_put( name , id );
}
public void gotDBRef( String name , String ns , ObjectId id ){
_put( name , new BasicBSONObject( "$ns" , ns ).append( "$id" , id ) );
}
@Deprecated
public void gotBinaryArray( String name , byte[] data ){
gotBinary( name, BSON.B_GENERAL, data );
}
public void gotBinary( String name , byte type , byte[] data ){
if( type == BSON.B_GENERAL || type == BSON.B_BINARY )
_put( name , data );
else
_put( name , new Binary( type , data ) );
}
public void gotUUID( String name , long part1, long part2){
_put( name , new UUID(part1, part2) );
}
public void gotCode( String name , String code ){
_put( name , new Code( code ) );
}
public void gotCodeWScope( String name , String code , Object scope ){
_put( name , new CodeWScope( code, (BSONObject)scope ) );
}
protected void _put( final String name , final Object o ){
cur().put( name , !BSON.hasDecodeHooks() ? o : BSON.applyDecodingHooks( o ) );
}
protected BSONObject cur(){
return _stack.getLast();
}
protected String curName(){
return (!_nameStack.isEmpty()) ? _nameStack.getLast() : null;
}
public Object get(){
return _root;
}
protected void setRoot(Object o) {
_root = o;
}
protected boolean isStackEmpty() {
return _stack.size() < 1;
}
public void reset(){
_root = null;
_stack.clear();
_nameStack.clear();
}
private Object _root;
private final LinkedList<BSONObject> _stack = new LinkedList<BSONObject>();
private final LinkedList<String> _nameStack = new LinkedList<String>();
}