forked from google/gdata-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttributeHelper.java
More file actions
703 lines (659 loc) · 24.2 KB
/
Copy pathAttributeHelper.java
File metadata and controls
703 lines (659 loc) · 24.2 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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/* Copyright (c) 2008 Google 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.google.gdata.data;
import com.google.gdata.client.CoreErrorDomain;
import com.google.gdata.util.ParseException;
import org.xml.sax.Attributes;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Helps accessing tag attributes.
*
* The helper only checks attributes in the default namespace ("") and
* rejects unknown attributes.
*
* The idea is to remove (consume) attributes as they are read
* from the list and at the end make sure that all attributes have
* been read, to detect whether unknown attributes have been
* specified. This is done by the method {@link #assertAllConsumed()} usually
* called from
* {@link com.google.gdata.util.XmlParser.ElementHandler#processEndElement()}.
*
*
*/
public class AttributeHelper {
/** Maps attribute local name to string value. */
protected final Map<String, String> attrs = new HashMap<String, String>();
/** set of attributes that are duplicated */
private Set<String> dups = new HashSet<String>();
/** if the content has been consumed */
private boolean contentConsumed = false;
/** element's text content or {@code null} for no text content */
private String content = null;
/**
* Creates a helper tied to a specific set of SAX attributes.
*
* @param attrs the SAX attributes to be processed
*/
public AttributeHelper(Attributes attrs) {
// attributes
for (int i = 0; i < attrs.getLength(); i++) {
if (attrs.getURI(i).length() != 0) {
String attrLocalName = attrs.getLocalName(i);
if (this.attrs.put(attrLocalName, attrs.getValue(i)) != null) {
dups.add(attrLocalName);
}
} else {
this.attrs.put(attrs.getQName(i), attrs.getValue(i));
}
}
}
/**
* Gets the element's text content and removes it from the list.
*
* @param required indicates attributes is required
* @return element's text content or {@code null} for no text content
* @exception ParseException if required is set and the text content
* is not defined
*/
public String consumeContent(boolean required) throws ParseException {
return consume(null, required);
}
/**
* Gets the value of an attribute and remove it from the list.
*
* @param name attribute name or <code>null</code> for text content
* @param required indicates attributes is required
* @return attribute value or null if not available
* @exception ParseException if required is set and the attribute
* is not defined
*/
public String consume(String name, boolean required) throws ParseException {
if (name == null) {
if (content == null && required) {
throw new ParseException(
CoreErrorDomain.ERR.missingRequiredContent);
}
contentConsumed = true;
return content;
}
String value = attrs.get(name);
if (value == null) {
if (required) {
ParseException pe = new ParseException(
CoreErrorDomain.ERR.missingAttribute);
pe.setInternalReason("Missing attribute: '" + name + "'");
throw pe;
}
return null;
}
attrs.remove(name);
return value;
}
/**
* Gets the value of a byte attribute and remove it from the list.
*
* @param name attribute name or <code>null</code> for text content
* @param required indicates attribute is required
* @param defaultValue the default value for an optional attribute (used if
* not present)
* @return the byte value of this attribute
* @exception ParseException if required is set and the attribute
* is not defined, or if the attribute value is not a valid byte
*/
public byte consumeByte(String name, boolean required, byte defaultValue)
throws ParseException {
String value = consume(name, required);
if (value == null) {
return defaultValue;
}
try {
return Byte.parseByte(value);
} catch (NumberFormatException e) {
ParseException pe = new ParseException(
CoreErrorDomain.ERR.invalidByteAttribute);
pe.setInternalReason("Invalid byte value for attribute: '" + name + "'");
throw pe;
}
}
/**
* Gets the value of a byte attribute and remove it from the list.
*
* @param name attribute name
* @param required indicates attribute is required
* @return the byte value of this attribute, 0 by default
* @exception ParseException if required is set and the attribute is not
* defined, or if the attribute value is not a valid byte
*/
public byte consumeByte(String name, boolean required) throws ParseException {
return consumeByte(name, required, (byte) 0);
}
/**
* Gets the value of a short attribute and remove it from the list.
*
* @param name attribute name or <code>null</code> for text content
* @param required indicates attribute is required
* @param defaultValue the default value for an optional attribute (used if
* not present)
* @return the short value of this attribute
* @exception ParseException if required is set and the attribute
* is not defined, or if the attribute value is not a valid short
*/
public short consumeShort(String name, boolean required, short defaultValue)
throws ParseException {
String value = consume(name, required);
if (value == null) {
return defaultValue;
}
try {
return Short.parseShort(value);
} catch (NumberFormatException e) {
ParseException pe = new ParseException(
CoreErrorDomain.ERR.invalidShortAttribute);
pe.setInternalReason("Invalid short value for attribute: '" + name + "'");
throw pe;
}
}
/**
* Gets the value of a short attribute and remove it from the list.
*
* @param name attribute name
* @param required indicates attribute is required
* @return the short value of this attribute, 0 by default
* @exception ParseException if required is set and the attribute
* is not defined, or if the attribute value is not a valid short
*/
public short consumeShort(String name, boolean required)
throws ParseException {
return consumeShort(name, required, (short) 0);
}
/**
* Gets the value of an integer attribute and remove it from the list.
*
* @param name attribute name or <code>null</code> for text content
* @param required indicates attribute is required
* @param defaultValue the default value for an optional attribute (used
* if not present)
* @return the integer value of this attribute
* @exception ParseException if required is set and the attribute
* is not defined, or if the attribute value is not a valid integer
*/
public int consumeInteger(String name, boolean required, int defaultValue)
throws ParseException {
String value = consume(name, required);
if (value == null) {
return defaultValue;
}
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
ParseException pe = new ParseException(
CoreErrorDomain.ERR.invalidIntegerAttribute);
pe.setInternalReason("Invalid integer value for attribute: '" +
name + "'");
throw pe;
}
}
/**
* Gets the value of an integer attribute and remove it from the list.
*
* @param name attribute name
* @param required indicates attribute is required
* @return the integer value of this attribute, 0 by default
* @exception ParseException if required is set and the attribute
* is not defined, or if the attribute value is not a valid integer
*/
public int consumeInteger(String name, boolean required)
throws ParseException {
return consumeInteger(name, required, 0);
}
/**
* Gets the value of a long attribute and remove it from the list.
*
* @param name attribute name or <code>null</code> for text content
* @param required indicates attribute is required
* @param defaultValue the default value for an optional attribute (used
* if not present)
* @return the long value of this attribute
* @exception ParseException if required is set and the attribute
* is not defined, or if the attribute value is not a valid long
*/
public long consumeLong(String name, boolean required, long defaultValue)
throws ParseException {
String value = consume(name, required);
if (value == null) {
return defaultValue;
}
try {
return Long.parseLong(value);
} catch (NumberFormatException e) {
ParseException pe = new ParseException(
CoreErrorDomain.ERR.invalidLongAttribute, e);
pe.setInternalReason("Invalid long value for attribute: '" +
name + "'");
throw pe;
}
}
/**
* Gets the value of a long attribute and remove it from the list.
*
* @param name attribute name
* @param required indicates attribute is required
* @return the long value of this attribute, 0 by default
* @exception ParseException if required is set and the attribute
* is not defined, or if the attribute value is not a valid long
*/
public long consumeLong(String name, boolean required)
throws ParseException {
return consumeLong(name, required, 0);
}
/**
* Gets the value of a big integer attribute and remove it from the list.
*
* @param name attribute name or <code>null</code> for text content
* @param required indicates attribute is required
* @param defaultValue the default value for an optional attribute (used if
* not present)
* @return the big integer value of this attribute
* @exception ParseException if required is set and the attribute
* is not defined, or if the attribute value is not a valid big integer
*/
public BigInteger consumeBigInteger(String name, boolean required,
BigInteger defaultValue) throws ParseException {
String value = consume(name, required);
if (value == null) {
return defaultValue;
}
try {
return new BigInteger(value);
} catch (NumberFormatException e) {
ParseException pe = new ParseException(
CoreErrorDomain.ERR.invalidBigIntegerAttribute);
pe.setInternalReason("Invalid big integer value for attribute: '" + name
+ "'");
throw pe;
}
}
/**
* Gets the value of a big integer attribute and remove it from the list.
*
* @param name attribute name
* @param required indicates attribute is required
* @return the big integer value of this attribute, 0 by default
* @exception ParseException if required is set and the attribute
* is not defined, or if the attribute value is not a valid big integer
*/
public BigInteger consumeBigInteger(String name, boolean required)
throws ParseException {
return consumeBigInteger(name, required, BigInteger.ZERO);
}
/**
* Gets the value of a big decimal attribute and remove it from the list.
*
* @param name attribute name or <code>null</code> for text content
* @param required indicates attribute is required
* @param defaultValue the default value for an optional attribute (used if
* not present)
* @return the big decimal value of this attribute
* @exception ParseException if required is set and the attribute
* is not defined, or if the attribute value is not a valid big decimal
*/
public BigDecimal consumeBigDecimal(String name, boolean required,
BigDecimal defaultValue) throws ParseException {
String value = consume(name, required);
if (value == null) {
return defaultValue;
}
try {
return new BigDecimal(value);
} catch (NumberFormatException e) {
ParseException pe = new ParseException(
CoreErrorDomain.ERR.invalidBigDecimalAttribute);
pe.setInternalReason("Invalid big decimal value for attribute: '" + name
+ "'");
throw pe;
}
}
/**
* Gets the value of a big decimal attribute and remove it from the list.
*
* @param name attribute name
* @param required indicates attribute is required
* @return the big decimal value of this attribute, 0 by default
* @exception ParseException if required is set and the attribute
* is not defined, or if the attribute value is not a valid big decimal
*/
public BigDecimal consumeBigDecimal(String name, boolean required)
throws ParseException {
return consumeBigDecimal(name, required, BigDecimal.ZERO);
}
/**
* Gets the value of a double attribute and remove it from the list.
*
* @param name attribute name or <code>null</code> for text content
* @param required indicates attribute is required
* @param defaultValue the default value for an optional attribute (used if
* not present)
* @return the double value of this attribute
* @throws ParseException if required is set and the attribute is not defined,
* or if the attribute value is not a valid double
*/
public double consumeDouble(
String name, boolean required, double defaultValue)
throws ParseException {
String value = consume(name, required);
if (value == null) {
return defaultValue;
}
if ("INF".equals(value)) {
return Double.POSITIVE_INFINITY;
} else if ("-INF".equals(value)) {
return Double.NEGATIVE_INFINITY;
}
try {
return Double.parseDouble(value);
} catch (NumberFormatException e) {
ParseException pe = new ParseException(
CoreErrorDomain.ERR.invalidDoubleAttribute, e);
pe.setInternalReason("Invalid double value for attribute: '" +
name + "'");
throw pe;
}
}
/**
* Gets the value of a double attribute and remove it from the list.
*
* @param name attribute name
* @param required indicates attribute is required
* @return the double value of this attribute, 0 by default
* @throws ParseException if required is set and the attribute is not defined,
* or if the attribute value is not a valid double
*/
public double consumeDouble(String name, boolean required)
throws ParseException {
return consumeDouble(name, required, 0);
}
/**
* Gets the value of a float attribute and remove it from the list.
*
* @param name attribute name or <code>null</code> for text content
* @param required indicates attribute is required
* @param defaultValue the default value for an optional attribute (used if
* not present)
* @return the float value of this attribute
* @throws ParseException if required is set and the attribute is not defined,
* or if the attribute value is not a valid float
*/
public float consumeFloat(
String name, boolean required, float defaultValue)
throws ParseException {
String value = consume(name, required);
if (value == null) {
return defaultValue;
}
if ("INF".equals(value)) {
return Float.POSITIVE_INFINITY;
} else if ("-INF".equals(value)) {
return Float.NEGATIVE_INFINITY;
}
try {
return Float.parseFloat(value);
} catch (NumberFormatException e) {
ParseException pe = new ParseException(
CoreErrorDomain.ERR.invalidFloatAttribute, e);
pe.setInternalReason("Invalid float value for attribute: '" +
name + "'");
throw pe;
}
}
/**
* Gets the value of a float attribute and remove it from the list.
*
* @param name attribute name
* @param required indicates attribute is required
* @return the float value of this attribute, 0 by default
* @throws ParseException if required is set and the attribute is not defined,
* or if the attribute value is not a valid float
*/
public float consumeFloat(String name, boolean required)
throws ParseException {
return consumeFloat(name, required, 0);
}
/**
* Gets the value of a boolean attribute and remove it from the list. The
* accepted values are based upon xsd:boolean syntax (true, false, 1, 0).
*
* @param name attribute name or <code>null</code> for text content
* @param required indicates attribute is required
* @param defaultValue the default value for an optional attribute (used
* if not present)
* @return the boolean value of this attribute
* @exception ParseException if required is set and the attribute
* is not defined, or if the attribute value is neither {@code true}
* nor {@code false}.
*/
public boolean consumeBoolean(String name, boolean required,
boolean defaultValue)
throws ParseException {
String value = consume(name, required);
if (value == null) {
return defaultValue;
}
if ("true".equals(value) || "1".equals(value)) {
return true;
} else if ("false".equals(value) || "0".equals(value)) {
return false;
} else {
ParseException pe = new ParseException(
CoreErrorDomain.ERR.invalidBooleanAttribute);
pe.setInternalReason("Invalid boolean value for attribute: '" +
name + "'");
throw pe;
}
}
/**
* Gets the value of a boolean attribute and remove it from the list. The
* accepted values are based upon xsd:boolean syntax (true, false, 1, 0).
*
* @param name attribute name
* @param required indicates attribute is required
* @return the boolean value of this attribute, false by default
* @exception ParseException if required is set and the attribute
* is not defined, or if the attribute value is neither {@code true}
* nor {@code false}.
*/
public boolean consumeBoolean(String name, boolean required)
throws ParseException {
return consumeBoolean(name, required, false);
}
/**
* Gets the value of a {@link DateTime} attribute and remove it from the list.
*
* @param name attribute name or <code>null</code> for text content
* @param required indicates attribute is required
* @return the date-time value of this attribute, {@code null} by default
* @exception ParseException if required is set and the attribute
* is not defined, or if the date-time attribute cannot be parsed
*/
public DateTime consumeDateTime(String name, boolean required)
throws ParseException {
String value = consume(name, required);
if (value == null) {
return null;
}
try {
return DateTime.parseDateTimeChoice(value);
} catch (NumberFormatException e) {
ParseException pe = new ParseException(
CoreErrorDomain.ERR.invalidDatetime, e);
pe.setInternalReason("Badly formatted datetime in attribute: " + name);
throw pe;
}
}
/**
* Defines a custom mapping of an enum value to an attribute value (similar to
* a closure).
*/
public static interface EnumToAttributeValue<T extends Enum<T>> {
String getAttributeValue(T enumValue);
}
/**
* Implements the most common custom mapping of an enum value to an attribute
* value using the lower-case form of the enum name.
*/
public static class LowerCaseEnumToAttributeValue<T extends Enum<T>>
implements EnumToAttributeValue<T> {
public String getAttributeValue(T enumValue) {
return enumValue.name().toLowerCase();
}
}
/**
* Gets the value of an enumerated attribute and remove it from the list,
* using a custom mapping of enum to attribute value.
*
* @param name attribute name or <code>null</code> for text content
* @param required indicates attribute is required
* @param enumClass enumeration class
* @param defaultValue the default value for an optional attribute
* (used if not present)
* @param enumToAttributeValue custom mapping of enum to attribute value
* @return an enumerated value
* @throws ParseException if required is set and the attribute is not defined,
* or if the attribute value is not a valid enumerated
* value
*/
public <T extends Enum<T>> T consumeEnum(String name, boolean required,
Class<T> enumClass, T defaultValue,
EnumToAttributeValue<T> enumToAttributeValue)
throws ParseException {
String value = consume(name, required);
if (value == null) {
return defaultValue;
}
for (T enumValue : enumClass.getEnumConstants()) {
if (enumToAttributeValue.getAttributeValue(enumValue).equals(value)) {
return enumValue;
}
}
ParseException pe = new ParseException(
CoreErrorDomain.ERR.invalidAttributeValue);
pe.setInternalReason("Invalid value for attribute : '" + name + "'");
throw pe;
}
/**
* Gets the value of an enumerated attribute and remove it from the list.
*
* Enumerated values are case-insensitive.
*
* @param name attribute name or <code>null</code> for text content
* @param required indicates attribute is required
* @param enumClass enumeration class
* @param defaultValue the default value for an optional attribute (used
* if not present)
* @return an enumerated value
* @exception ParseException if required is set and the attribute
* is not defined, or if the attribute value is not a valid
* enumerated value
*/
public <T extends Enum<T>> T consumeEnum(String name, boolean required,
Class<T> enumClass, T defaultValue)
throws ParseException {
String value = consume(name, required);
if (value == null) {
return defaultValue;
}
try {
return Enum.valueOf(enumClass, value.toUpperCase());
} catch (IllegalArgumentException e) {
ParseException pe = new ParseException(
CoreErrorDomain.ERR.invalidAttributeValue, e);
pe.setInternalReason("Invalid value for attribute : '" + name + "'");
throw pe;
}
}
/**
* Gets the value of an enumerated attribute and remove it from the list.
*
* Enumerated values are case-insensitive.
*
* @param name attribute name
* @param required indicates attribute is required
* @param enumClass enumeration class
* @return an enumerated value or null if not present
* @exception ParseException if required is set and the attribute
* is not defined, or if the attribute value is not a valid
* enumerated value
*/
public <T extends Enum<T>> T consumeEnum(String name, boolean required,
Class<T> enumClass)
throws ParseException {
return consumeEnum(name, required, enumClass, null);
}
/**
* Makes sure all attributes have been removed from the list.
*
* To all attribute in the default namespace must correspond exactly
* one call to consume*().
*
* @exception ParseException if an attribute in the default namespace
* hasn't been removed
*/
public void assertAllConsumed() throws ParseException {
StringBuffer message = new StringBuffer();
if (!attrs.isEmpty()) {
message.append("Unknown attribute");
if (attrs.size() > 1) {
message.append('s');
}
message.append(':');
for (String name : attrs.keySet()) {
message.append(" '");
message.append(name);
message.append("' ");
}
}
if (!dups.isEmpty()) {
message.append("Duplicate attribute");
if (dups.size() > 1) {
message.append('s');
}
message.append(':');
for (String dup : dups) {
message.append(" '");
message.append(dup);
message.append("' ");
}
}
if (!contentConsumed && content != null && content.length() != 0) {
message.append("Unexpected text content ");
}
if (message.length() != 0) {
throw new ParseException(message.toString());
}
}
/**
* Sets the content.
*
* @param content element's text content
*/
void setContent(String content) {
// text content
this.content = content == null ? null : content.trim();
}
}