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
|
/*
* Copyright (c) 2010-2012, Isode Limited, London, England.
* All rights reserved.
*/
/*
* Copyright (c) 2010, Remko Tronçon.
* All rights reserved.
*/
package com.isode.stroke.parser;
import com.isode.stroke.base.NotNull;
import java.util.ArrayList;
import java.util.List;
/**
* XML element attributes.
*/
public class AttributeMap {
/**
* Internal class.
*/
public class Entry {
public Entry(Attribute attribute, String value) {
NotNull.exceptIfNull(attribute, "attribute");
NotNull.exceptIfNull(value, "value");
this.attribute = attribute;
this.value = value;
}
public Attribute getAttribute() {
return attribute;
}
public String getValue() {
return value;
}
private final Attribute attribute;
private final String value;
};
public AttributeMap() {
}
/** Not null */
public String getAttribute(String attribute) {
NotNull.exceptIfNull(attribute, "attribute");
return getAttribute(attribute, "");
}
/** Not null*/
public String getAttribute(String attribute, String ns) {
String value = getInternal(attribute, ns);
return value != null ? value : "";
}
/**
*
* @param attribute attribute name.
* @return boolean value, defaulting to false if missing.
*/
public boolean getBoolAttribute(String attribute) {
return getBoolAttribute(attribute, false);
}
public boolean getBoolAttribute(String attribute, boolean defaultValue) {
String value = getInternal(attribute, "");
if (value == null) {
return defaultValue;
}
return "true".equals(value) || "1".equals(value);
}
/** @return Attribute or null if missing.*/
public String getAttributeValue(String attribute) {
return getInternal(attribute, "");
}
/**
*
* @param name Attribute name (non-null).
* @param ns Attribute namespace (non-null).
* @param value Attribute value (non-null).
*/
public void addAttribute(String name, String ns, String value) {
NotNull.exceptIfNull(name, "name");
NotNull.exceptIfNull(ns, "ns");
NotNull.exceptIfNull(value, "value");
attributes.add(new Entry(new Attribute(name, ns), value));
}
/**
* Internal method (used for unit tests).
*/
public List<Entry> getEntries() {
return new ArrayList(attributes);
}
private String getInternal(String name, String ns) {
Attribute attribute = new Attribute(name, ns);
for (Entry entry : attributes) {
if (entry.getAttribute().equals(attribute)) {
return entry.value;
}
}
return null;
}
private final List<Entry> attributes = new ArrayList<Entry>();
}
|