This repository was archived by the owner on Jul 20, 2024. It is now read-only.
forked from splunk/splunk-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtomEntry.java
More file actions
249 lines (200 loc) · 7.16 KB
/
AtomEntry.java
File metadata and controls
249 lines (200 loc) · 7.16 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
/*
* Copyright 2012 Splunk, 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.splunk;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
/**
* The {@code AtomEntry} class represents an Atom {@code <entry>} element.
*/
public class AtomEntry extends AtomObject {
/** The value of the Atom entry's {@code <published>} element. */
public String published;
/** The value of the Atom entry's {@code <content>} element. */
public Record content;
/**
* Creates a new {@code AtomEntry} instance.
*
* @return A new {@code AtomEntry} instance.
*/
static AtomEntry create() {
return new AtomEntry();
}
/**
* Creates a new {@code AtomEntry} instance based on a given stream.
* A few endpoints, such as {@code search/jobs/{sid}},
* return an Atom {@code <entry>} element as the root of the response.
*
* @param input The input stream.
* @return An {@code AtomEntry} instance representing the parsed stream.
*/
public static AtomEntry parseStream(InputStream input) {
XMLStreamReader reader = createReader(input);
AtomEntry result = AtomEntry.parse(reader);
try {
reader.close();
}
catch (XMLStreamException e) {
throw new RuntimeException(e.getMessage(), e);
}
return result;
}
/**
* Creates a new {@code AtomEntry} instance based on a given XML reader.
*
* @param reader The XML reader.
* @return An {@code AtomEntry} instance representing the parsed XML.
*/
static AtomEntry parse(XMLStreamReader reader) {
AtomEntry entry = AtomEntry.create();
entry.load(reader, "entry");
return entry;
}
/**
* Initializes the current instance using the given XML reader.
*
* @param reader The XML reader.
*/
@Override void init(XMLStreamReader reader) {
assert reader.isStartElement();
String name = reader.getLocalName();
if (name.equals("published")) {
this.published = parseText(reader);
}
else if (name.equals("content")) {
this.content = parseContent(reader);
}
else {
super.init(reader);
}
}
/**
* Parses the {@code <content>} element of an Atom entry.
*
* @param reader The XML reader.
* @return A {@code Record} object containing the parsed values.
*/
private Record parseContent(XMLStreamReader reader) {
assert isStartElement(reader, "content");
scan(reader);
// The content element should contain a single <dict> element
if (!isStartElement(reader, "dict"))
syntaxError(reader);
content = parseDict(reader);
if (!isEndElement(reader, "content"))
syntaxError(reader);
scan(reader); // Consume </content>
return content;
}
/**
* Parses a {@code <dict>} content element and returns a {@code Record}
* object containing the parsed values.
*
* @param reader The {@code <dict>} element to parse.
* @return A {@code Record} object containing the parsed values.
*/
private Record parseDict(XMLStreamReader reader) {
assert isStartElement(reader, "dict");
Record result = new Record();
scan(reader);
while (isStartElement(reader, "key")) {
String key = reader.getAttributeValue(null, "name");
Object value = parseValue(reader);
// Null values, the result of empty elements, are parsed as though
// they don't exist, making it easier for the client framework to
// supply more meaningful default values.
if (value != null) result.put(key, value);
}
if (!isEndElement(reader, "dict"))
syntaxError(reader);
scan(reader); // Consume </dict>
return result;
}
/**
* Parses a {@code <list>} element and returns a {@code List} object
* containing the parsed values.
*
* @param reader The XML reader.
* @return A {@code List} object containing the parsed values.
*/
private List parseList(XMLStreamReader reader) {
assert isStartElement(reader, "list");
List result = new ArrayList();
scan(reader);
while (isStartElement(reader, "item")) {
Object value = parseValue(reader);
result.add(value);
}
if (!isEndElement(reader, "list"))
syntaxError(reader);
scan(reader); // Consume </list>
return result;
}
// Parses either a dict or list structure.
private Object parseStructure(XMLStreamReader reader) {
String name = reader.getLocalName();
if (name.equals("dict"))
return parseDict(reader);
if (name.equals("list"))
return parseList(reader);
syntaxError(reader);
return null; // Unreached
}
/**
* Parses the value contained by the element at the current cursor position
* of the given reader.
* <p>
* <b>Note:</b> This function takes the parent element as its starting point
* so that it can correctly match the end element. The function takes the
* start element and its corresponding end element, then returns the
* contained value. The cursor is then located at the next element to be
* parsed.
*
* @param reader The XML reader to parse.
* @return An object containing the parsed values. If the source was a text
* value, the object is a {@code String}. If the source was a {@code <dict>}
* element, the object is a {@code Record}. If the source was a
* {@code <list>} element, the object is a {@code List} object.
*/
Object parseValue(XMLStreamReader reader) {
assert reader.isStartElement();
String name = reader.getLocalName();
scan(reader);
Object value;
switch (reader.getEventType()) {
case XMLStreamConstants.CHARACTERS:
value = reader.getText();
scan(reader); // Advance cursor
break;
case XMLStreamConstants.START_ELEMENT:
value = parseStructure(reader);
break;
case XMLStreamConstants.END_ELEMENT:
value = null; // Empty element
break;
default:
value = null;
syntaxError(reader);
}
if (!isEndElement(reader, name))
syntaxError(reader);
scan(reader); // Consume end element
return value;
}
}