/
ParseUtils.java
433 lines (395 loc) · 17.2 KB
/
ParseUtils.java
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
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.controller.parsing;
import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
import static org.jboss.as.controller.ControllerMessages.MESSAGES;
import java.lang.reflect.Array;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.xml.XMLConstants;
import javax.xml.stream.Location;
import javax.xml.stream.XMLStreamException;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType;
import org.jboss.dmr.Property;
import org.jboss.staxmapper.XMLExtendedStreamReader;
/**
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
public final class ParseUtils {
private ParseUtils() {
}
public static Element nextElement(XMLExtendedStreamReader reader) throws XMLStreamException {
if (reader.nextTag() == END_ELEMENT) {
return null;
}
return Element.forName(reader.getLocalName());
}
/**
* A variation of nextElement that verifies the nextElement is not in a different namespace.
*
* @param reader the XmlExtendedReader to read from.
* @param expectedNamespace the namespace expected.
* @return the element or null if the end is reached
* @throws XMLStreamException if the namespace is wrong or there is a problem accessing the reader
*/
public static Element nextElement(XMLExtendedStreamReader reader, Namespace expectedNamespace) throws XMLStreamException {
Element element = nextElement(reader);
if (element == null) {
return element;
} else if (expectedNamespace.equals(Namespace.forUri(reader.getNamespaceURI()))) {
return element;
}
throw unexpectedElement(reader);
}
/**
* Get an exception reporting an unexpected XML element.
* @param reader the stream reader
* @return the exception
*/
public static XMLStreamException unexpectedElement(final XMLExtendedStreamReader reader) {
return MESSAGES.unexpectedElement(reader.getName(), reader.getLocation());
}
/**
* Get an exception reporting an unexpected end tag for an XML element.
* @param reader the stream reader
* @return the exception
*/
public static XMLStreamException unexpectedEndElement(final XMLExtendedStreamReader reader) {
return MESSAGES.unexpectedEndElement(reader.getName(), reader.getLocation());
}
/**
* Get an exception reporting an unexpected XML attribute.
* @param reader the stream reader
* @param index the attribute index
* @return the exception
*/
public static XMLStreamException unexpectedAttribute(final XMLExtendedStreamReader reader, final int index) {
return MESSAGES.unexpectedAttribute(reader.getAttributeName(index), reader.getLocation());
}
/**
* Get an exception reporting an invalid XML attribute value.
* @param reader the stream reader
* @param index the attribute index
* @return the exception
*/
public static XMLStreamException invalidAttributeValue(final XMLExtendedStreamReader reader, final int index) {
return MESSAGES.invalidAttributeValue(reader.getAttributeValue(index), reader.getAttributeName(index), reader.getLocation());
}
/**
* Get an exception reporting a missing, required XML attribute.
* @param reader the stream reader
* @param required a set of enums whose toString method returns the
* attribute name
* @return the exception
*/
public static XMLStreamException missingRequired(final XMLExtendedStreamReader reader, final Set<?> required) {
final StringBuilder b = new StringBuilder();
Iterator<?> iterator = required.iterator();
while (iterator.hasNext()) {
final Object o = iterator.next();
b.append(o.toString());
if (iterator.hasNext()) {
b.append(", ");
}
}
return MESSAGES.missingRequiredAttributes(b, reader.getLocation());
}
/**
* Get an exception reporting a missing, required XML child element.
* @param reader the stream reader
* @param required a set of enums whose toString method returns the
* attribute name
* @return the exception
*/
public static XMLStreamException missingRequiredElement(final XMLExtendedStreamReader reader, final Set<?> required) {
final StringBuilder b = new StringBuilder();
Iterator<?> iterator = required.iterator();
while (iterator.hasNext()) {
final Object o = iterator.next();
b.append(o.toString());
if (iterator.hasNext()) {
b.append(", ");
}
}
return MESSAGES.missingRequiredElements(b, reader.getLocation());
}
/**
* Get an exception reporting a missing, required XML child element.
* @param reader the stream reader
* @param required a set of enums whose toString method returns the
* attribute name
* @return the exception
*/
public static XMLStreamException missingOneOf(final XMLExtendedStreamReader reader, final Set<?> required) {
final StringBuilder b = new StringBuilder();
Iterator<?> iterator = required.iterator();
while (iterator.hasNext()) {
final Object o = iterator.next();
b.append(o.toString());
if (iterator.hasNext()) {
b.append(", ");
}
}
return MESSAGES.missingOneOf(b, reader.getLocation());
}
/**
* Checks that the current element has no attributes, throwing an
* {@link javax.xml.stream.XMLStreamException} if one is found.
* @param reader the reader
* @throws javax.xml.stream.XMLStreamException if an error occurs
*/
public static void requireNoAttributes(final XMLExtendedStreamReader reader) throws XMLStreamException {
if (reader.getAttributeCount() > 0) {
throw unexpectedAttribute(reader, 0);
}
}
/**
* Consumes the remainder of the current element, throwing an
* {@link javax.xml.stream.XMLStreamException} if it contains any child
* elements.
* @param reader the reader
* @throws javax.xml.stream.XMLStreamException if an error occurs
*/
public static void requireNoContent(final XMLExtendedStreamReader reader) throws XMLStreamException {
if (reader.hasNext() && reader.nextTag() != END_ELEMENT) {
throw unexpectedElement(reader);
}
}
/**
* Require that the namespace of the current element matches the required namespace.
*
* @param reader the reader
* @param requiredNs the namespace required
* @throws XMLStreamException if the current namespace does not match the required namespace
*/
public static void requireNamespace(final XMLExtendedStreamReader reader, final Namespace requiredNs) throws XMLStreamException {
Namespace actualNs = Namespace.forUri(reader.getNamespaceURI());
if (actualNs != requiredNs) {
throw unexpectedElement(reader);
}
}
/**
* Get an exception reporting that an attribute of a given name has already
* been declared in this scope.
* @param reader the stream reader
* @param name the name that was redeclared
* @return the exception
*/
public static XMLStreamException duplicateAttribute(final XMLExtendedStreamReader reader, final String name) {
return MESSAGES.duplicateAttribute(name, reader.getLocation());
}
/**
* Get an exception reporting that an element of a given type and name has
* already been declared in this scope.
* @param reader the stream reader
* @param name the name that was redeclared
* @return the exception
*/
public static XMLStreamException duplicateNamedElement(final XMLExtendedStreamReader reader, final String name) {
return MESSAGES.duplicateNamedElement(name, reader.getLocation());
}
/**
* Read an element which contains only a single boolean attribute.
* @param reader the reader
* @param attributeName the attribute name, usually "value"
* @return the boolean value
* @throws javax.xml.stream.XMLStreamException if an error occurs or if the
* element does not contain the specified attribute, contains other
* attributes, or contains child elements.
*/
public static boolean readBooleanAttributeElement(final XMLExtendedStreamReader reader, final String attributeName)
throws XMLStreamException {
requireSingleAttribute(reader, attributeName);
final boolean value = Boolean.parseBoolean(reader.getAttributeValue(0));
requireNoContent(reader);
return value;
}
/**
* Read an element which contains only a single string attribute.
* @param reader the reader
* @param attributeName the attribute name, usually "value" or "name"
* @return the string value
* @throws javax.xml.stream.XMLStreamException if an error occurs or if the
* element does not contain the specified attribute, contains other
* attributes, or contains child elements.
*/
public static String readStringAttributeElement(final XMLExtendedStreamReader reader, final String attributeName)
throws XMLStreamException {
requireSingleAttribute(reader, attributeName);
final String value = reader.getAttributeValue(0);
requireNoContent(reader);
return value;
}
/**
* Read an element which contains only a single list attribute of a given
* type.
* @param reader the reader
* @param attributeName the attribute name, usually "value"
* @param type the value type class
* @param <T> the value type
* @return the value list
* @throws javax.xml.stream.XMLStreamException if an error occurs or if the
* element does not contain the specified attribute, contains other
* attributes, or contains child elements.
*/
@SuppressWarnings({ "unchecked" })
public static <T> List<T> readListAttributeElement(final XMLExtendedStreamReader reader, final String attributeName,
final Class<T> type) throws XMLStreamException {
requireSingleAttribute(reader, attributeName);
// todo: fix this when this method signature is corrected
final List<T> value = (List<T>) reader.getListAttributeValue(0, type);
requireNoContent(reader);
return value;
}
public static Property readProperty(final XMLExtendedStreamReader reader) throws XMLStreamException {
final int cnt = reader.getAttributeCount();
String name = null;
String value = null;
for (int i = 0; i < cnt; i++) {
if (reader.getAttributeNamespace(i) != null) {
throw unexpectedAttribute(reader, i);
}
final String localName = reader.getAttributeLocalName(i);
if (localName.equals("name")) {
name = reader.getAttributeValue(i);
} else if (localName.equals("value")) {
value = reader.getAttributeValue(i);
} else {
throw unexpectedAttribute(reader, i);
}
}
if (name == null) {
throw missingRequired(reader, Collections.singleton("name"));
}
if (reader.next() != END_ELEMENT) {
throw unexpectedElement(reader);
}
return new Property(name, new ModelNode().set(value == null ? "" : value));
}
/**
* Read an element which contains only a single list attribute of a given
* type, returning it as an array.
* @param reader the reader
* @param attributeName the attribute name, usually "value"
* @param type the value type class
* @param <T> the value type
* @return the value list as an array
* @throws javax.xml.stream.XMLStreamException if an error occurs or if the
* element does not contain the specified attribute, contains other
* attributes, or contains child elements.
*/
@SuppressWarnings({ "unchecked" })
public static <T> T[] readArrayAttributeElement(final XMLExtendedStreamReader reader, final String attributeName,
final Class<T> type) throws XMLStreamException {
final List<T> list = readListAttributeElement(reader, attributeName, type);
return list.toArray((T[]) Array.newInstance(type, list.size()));
}
/**
* Require that the current element have only a single attribute with the
* given name.
* @param reader the reader
* @param attributeName the attribute name
* @throws javax.xml.stream.XMLStreamException if an error occurs
*/
public static void requireSingleAttribute(final XMLExtendedStreamReader reader, final String attributeName)
throws XMLStreamException {
final int count = reader.getAttributeCount();
if (count == 0) {
throw missingRequired(reader, Collections.singleton(attributeName));
}
requireNoNamespaceAttribute(reader, 0);
if (!attributeName.equals(reader.getAttributeLocalName(0))) {
throw unexpectedAttribute(reader, 0);
}
if (count > 1) {
throw unexpectedAttribute(reader, 1);
}
}
/**
* Require all the named attributes, returning their values in order.
* @param reader the reader
* @param attributeNames the attribute names
* @return the attribute values in order
* @throws javax.xml.stream.XMLStreamException if an error occurs
*/
public static String[] requireAttributes(final XMLExtendedStreamReader reader, final String... attributeNames)
throws XMLStreamException {
final int length = attributeNames.length;
final String[] result = new String[length];
for (int i = 0; i < length; i++) {
final String name = attributeNames[i];
final String value = reader.getAttributeValue(null, name);
if (value == null) {
throw missingRequired(reader, Collections.singleton(name));
}
result[i] = value;
}
return result;
}
public static boolean isNoNamespaceAttribute(final XMLExtendedStreamReader reader, final int index) {
String namespace = reader.getAttributeNamespace(index);
// FIXME when STXM-8 is done, remove the null check
return namespace == null || XMLConstants.NULL_NS_URI.equals(namespace);
}
public static void requireNoNamespaceAttribute(final XMLExtendedStreamReader reader, final int index)
throws XMLStreamException {
if (!isNoNamespaceAttribute(reader, index)) {
throw unexpectedAttribute(reader, index);
}
}
public static ModelNode parseBoundedIntegerAttribute(final XMLExtendedStreamReader reader, final int index,
final int minInclusive, final int maxInclusive, boolean allowExpression) throws XMLStreamException {
final String stringValue = reader.getAttributeValue(index);
if (allowExpression) {
ModelNode expression = parsePossibleExpression(stringValue);
if (expression.getType() == ModelType.EXPRESSION) {
return expression;
}
}
try {
final int value = Integer.parseInt(stringValue);
if (value < minInclusive || value > maxInclusive) {
throw MESSAGES.invalidAttributeValue(value, reader.getAttributeName(index), minInclusive, maxInclusive, reader.getLocation());
}
return new ModelNode().set(value);
} catch (NumberFormatException nfe) {
throw MESSAGES.invalidAttributeValueInt(nfe, stringValue, reader.getAttributeName(index), reader.getLocation());
}
}
public static ModelNode parsePossibleExpression(String value) {
ModelNode result = new ModelNode();
int openIdx = value.indexOf("${");
if (openIdx > -1 && value.lastIndexOf('}') > openIdx) {
result.setExpression(value);
}
else {
result.set(value);
}
return result;
}
public static String getWarningMessage(final String msg, final Location location) {
return MESSAGES.parsingProblem(location.getLineNumber(), location.getColumnNumber(), msg);
}
}