forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaxUnmarshallerContext.java
More file actions
292 lines (258 loc) · 10.1 KB
/
StaxUnmarshallerContext.java
File metadata and controls
292 lines (258 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
/*
* 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 java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.XMLEvent;
/**
* Contains the unmarshalling state for the parsing of an XML response. The
* unmarshallers are stateless so that they can be reused, so this class holds
* the state while different unmarshallers work together to parse an XML
* response. It also tracks the current position and element depth of the
* document being parsed and provides utilties for accessing the next XML event
* from the parser, reading element text, handling attribute XML events, etc.
*/
public class StaxUnmarshallerContext {
private XMLEvent currentEvent;
private final XMLEventReader eventReader;
public final Stack<String> stack = new Stack<String>();
private String stackString = "";
private Map<String, String> metadata = new HashMap<String, String>();
private List<MetadataExpression> metadataExpressions = new ArrayList<MetadataExpression>();
private Iterator<?> attributeIterator;
private final Map<String, String> headers;
/**
* Constructs a new unmarshaller context using the specified source of XML events.
*
* @param eventReader
* The source of XML events for this unmarshalling context.
*/
public StaxUnmarshallerContext(XMLEventReader eventReader) {
this(eventReader, null);
}
/**
* Constructs a new unmarshaller context using the specified source of XML
* events, and a set of response headers.
*
* @param eventReader
* The source of XML events for this unmarshalling context.
* @param headers
* The set of response headers associated with this unmarshaller
* context.
*/
public StaxUnmarshallerContext(XMLEventReader eventReader, Map<String, String> headers) {
this.eventReader = eventReader;
this.headers = headers;
}
/**
* 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 (headers == null) return null;
return headers.get(header);
}
/**
* Returns the text contents of the current element being parsed.
*
* @return The text contents of the current element being parsed.
* @throws XMLStreamException
*/
public String readText() throws XMLStreamException {
if (currentEvent.isAttribute()) {
Attribute attribute = (Attribute)currentEvent;
return attribute.getValue();
}
StringBuilder sb = new StringBuilder();
while (true) {
XMLEvent event = eventReader.peek();
if (event.getEventType() == XMLStreamConstants.CHARACTERS) {
eventReader.nextEvent();
sb.append(event.asCharacters().getData());
} else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) {
return sb.toString();
} else {
throw new RuntimeException("Encountered unexpected event: " + event.toString());
}
}
}
/**
* Returns the element depth of the parser's current position in the XML
* document being parsed.
*
* @return The element depth of the parser's current position in the XML
* document being parsed.
*/
public int getCurrentDepth() {
return stack.size();
}
/**
* Tests the specified expression against the current position in the XML
* 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);
}
/**
* Tests the specified expression against the current position in the XML
* document being parsed, and restricts the expression to matching at the
* specified stack depth.
*
* @param expression
* The psuedo-xpath expression to test.
* @param startingStackDepth
* 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 XML document, starting from the specified depth.
*/
public boolean testExpression(String expression, int startingStackDepth) {
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) != '@') {
startingStackDepth++;
}
}
return (startingStackDepth == getCurrentDepth()
&& stackString.endsWith("/" + expression));
}
/**
* Returns true if this unmarshaller context is at the very beginning of a
* source document (i.e. no data has been parsed from the document yet).
*
* @return true if this unmarshaller context is at the very beginning of a
* source document (i.e. no data has been parsed from the document
* yet).
*/
public boolean isStartOfDocument() throws XMLStreamException {
return eventReader.peek().isStartDocument();
}
/**
* Returns the next XML event for the document being parsed.
*
* @return The next XML event for the document being parsed.
*
* @throws XMLStreamException
*/
public XMLEvent nextEvent() throws XMLStreamException {
if (attributeIterator != null && attributeIterator.hasNext()) {
currentEvent = (XMLEvent)attributeIterator.next();
} else {
currentEvent = eventReader.nextEvent();
}
if (currentEvent.isStartElement()) {
attributeIterator = currentEvent.asStartElement().getAttributes();
}
updateContext(currentEvent);
if (eventReader.hasNext()) {
XMLEvent nextEvent = eventReader.peek();
if (nextEvent != null && nextEvent.isCharacters()) {
for (MetadataExpression metadataExpression : metadataExpressions) {
if (testExpression(metadataExpression.expression, metadataExpression.targetDepth)) {
metadata.put(metadataExpression.key, nextEvent.asCharacters().getData());
}
}
}
}
return currentEvent;
}
/**
* Returns any metadata collected through metadata expressions while this
* context was reading the XML events from the XML document.
*
* @return A map of any metadata collected through metadata expressions
* while this context was reading the XML 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 XML 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(XMLEvent event) {
if (event == null) return;
if (event.isEndElement()) {
stack.pop();
stackString = "";
for (String s : stack) {
stackString += "/" + s;
}
} else if (event.isStartElement()) {
stack.push(event.asStartElement().getName().getLocalPart());
stackString += "/" + event.asStartElement().getName().getLocalPart();
} else if (event.isAttribute()) {
Attribute attribute = (Attribute)event;
stackString = "";
for (String s : stack) {
stackString += "/" + s;
}
stackString += "/@" + attribute.getName().getLocalPart();
}
}
}