forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonUnmarshallerContext.java
More file actions
321 lines (275 loc) · 10.1 KB
/
JsonUnmarshallerContext.java
File metadata and controls
321 lines (275 loc) · 10.1 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
/*
* Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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.amazonaws.transform;
import static com.fasterxml.jackson.core.JsonToken.END_ARRAY;
import static com.fasterxml.jackson.core.JsonToken.END_OBJECT;
import static com.fasterxml.jackson.core.JsonToken.FIELD_NAME;
import static com.fasterxml.jackson.core.JsonToken.START_ARRAY;
import static com.fasterxml.jackson.core.JsonToken.START_OBJECT;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.amazonaws.http.HttpResponse;
public class JsonUnmarshallerContext {
private final JsonParser jsonParser;
private final Stack<String> stack = new Stack<String>();
private String stackString = "";
private String currentField;
// The string has been deleted from stackString when doing update on the stack
private String lastParsedParentElement;
private Map<String, String> metadata = new HashMap<String, String>();
private List<MetadataExpression> metadataExpressions = new ArrayList<MetadataExpression>();
public JsonToken currentToken;
private JsonToken nextToken;
private final HttpResponse httpResponse;
public JsonUnmarshallerContext(JsonParser jsonParser) {
this(jsonParser, null);
}
public JsonUnmarshallerContext(JsonParser jsonParser, HttpResponse httpResponse) {
this.jsonParser = jsonParser;
this.httpResponse = httpResponse;
}
/**
* Returns the value of the header with the specified name from the
* response, or null if not present.
*
* @param header
* The name of the header to lookup.
*
* @return The value of the header with the specified name from the
* response, or null if not present.
*/
public String getHeader(String header) {
if (httpResponse == null) return null;
return httpResponse.getHeaders().get(header);
}
public HttpResponse getHttpResponse() {
return httpResponse;
}
/**
* Returns the element depth of the parser's current position in the JSON
* document being parsed.
*
* @return The element depth of the parser's current position in the JSON
* document being parsed.
*/
public int getCurrentDepth() {
int depth = 0;
for (String s : stack) {
if (!(s.equals(START_OBJECT.asString()) || s.equals(START_ARRAY.toString()))) {
depth++;
}
}
if (currentField != null) depth++;
return depth;
}
/**
* Returns the text of the current token, or throws an exception if
* the current token does not contain text (ex: '{', '}', etc.).
*
* @return The text of the current token.
*
* @throws IOException
*/
public String readText() throws IOException {
switch (currentToken) {
case VALUE_STRING:
String text = jsonParser.getText();
return text;
case VALUE_FALSE: return "false";
case VALUE_TRUE: return "true";
case VALUE_NULL: return null;
case VALUE_NUMBER_FLOAT:
case VALUE_NUMBER_INT:
return jsonParser.getNumberValue().toString();
case FIELD_NAME:
return jsonParser.getText();
default:
throw new RuntimeException(
"We expected a VALUE token but got: " + currentToken);
}
}
public boolean isStartOfDocument() {
return jsonParser == null || jsonParser.getCurrentToken() == null;
}
/**
* Tests the specified expression against the current position in the JSON
* document being parsed.
*
* @param expression
* The psuedo-xpath expression to test.
* @return True if the expression matches the current document position,
* otherwise false.
*/
public boolean testExpression(String expression) {
if (expression.equals("."))
return true;
return stackString.endsWith(expression);
}
/**
* This will return the last token when doing split by "/" on the
* stackString
*/
public String getCurrentParentElement() {
String[] tokens = stackString.split("/");
if (tokens.length == 0) {
return "";
}
return tokens[tokens.length - 1];
}
/**
* Tests the specified expression against the current position in the JSON
* document being parsed, and restricts the expression to matching at the
* specified stack depth.
*
* @param expression
* The psuedo-xpath expression to test.
* @param stackDepth
* The depth in the stack representing where the expression must
* start matching in order for this method to return true.
*
* @return True if the specified expression matches the current position in
* the JSON document, starting from the specified depth.
*/
public boolean testExpression(String expression, int stackDepth) {
if (expression.equals(".")) return true;
int index = -1;
while ((index = expression.indexOf("/", index + 1)) > -1) {
// Don't consider attributes a new depth level
if (expression.charAt(index + 1) != '@') {
stackDepth++;
}
}
return stackString.endsWith("/" + expression) &&
stackDepth == getCurrentDepth();
}
public JsonToken nextToken() throws IOException {
// Use the value from the nextToken field if
// we've already populated it to peek ahead.
JsonToken token = (nextToken != null) ?
nextToken : jsonParser.nextToken();
this.currentToken = token;
nextToken = null;
updateContext();
return token;
}
public JsonToken peek() throws IOException {
if (nextToken != null) return nextToken;
nextToken = jsonParser.nextToken();
return nextToken;
}
public JsonParser getJsonParser() {
return jsonParser;
}
/**
* Returns any metadata collected through metadata expressions while this
* context was reading the JSON events from the JSON document.
*
* @return A map of any metadata collected through metadata expressions
* while this context was reading the JSON document.
*/
public Map<String, String> getMetadata() {
return metadata;
}
/**
* Registers an expression, which if matched, will cause the data for the
* matching element to be stored in the metadata map under the specified
* key.
*
* @param expression
* The expression an element must match in order for it's data to
* be pulled out and stored in the metadata map.
* @param targetDepth
* The depth in the JSON document where the expression match must
* start.
* @param storageKey
* The key under which to store the matching element's data.
*/
public void registerMetadataExpression(String expression, int targetDepth,
String storageKey) {
metadataExpressions.add(new MetadataExpression(expression, targetDepth,
storageKey));
}
/*
* Private Interface
*/
/**
* Simple container for the details of a metadata expression this
* unmarshaller context is looking for.
*/
private static class MetadataExpression {
public String expression;
public int targetDepth;
public String key;
public MetadataExpression(String expression, int targetDepth, String key) {
this.expression = expression;
this.targetDepth = targetDepth;
this.key = key;
}
}
private void updateContext() throws IOException {
lastParsedParentElement = null;
if (currentToken == null) return;
if (currentToken == START_OBJECT || currentToken == START_ARRAY) {
if (currentField != null) {
stack.push(currentField);
stack.push(currentToken.asString());
currentField = null;
}
} else if (currentToken == END_OBJECT || currentToken == END_ARRAY) {
if (!stack.isEmpty()) {
boolean squareBracketsMatch = currentToken == END_ARRAY && stack.peek().equals(START_ARRAY.asString());
boolean curlyBracketsMatch = currentToken == END_OBJECT && stack.peek().equals(START_OBJECT.asString());
if (squareBracketsMatch || curlyBracketsMatch) {
stack.pop();
lastParsedParentElement = stack.pop();
}
}
currentField = null;
} else if (currentToken == FIELD_NAME) {
String t = jsonParser.getText();
currentField = t;
}
rebuildStackString();
}
@Override
public String toString() {
return stackString;
}
/**
* This will return the deleted string in stackString when doing update on
* the stack
*/
public String getLastParsedParentElement() {
return lastParsedParentElement;
}
private void rebuildStackString() {
stackString = "";
for (String s : stack) {
if (! (s.equals(START_ARRAY.asString()) || s.equals(START_OBJECT.asString()))) {
stackString += "/" + s;
}
}
if (currentField != null) {
stackString += "/" + currentField;
}
if (stackString == "") stackString = "/";
}
}