forked from igniterealtime/Smack
/
DefaultExtensionElement.java
136 lines (124 loc) · 4.24 KB
/
DefaultExtensionElement.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
/**
*
* Copyright 2003-2007 Jive Software.
*
* 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 org.jivesoftware.smack.packet;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* Default implementation of the ExtensionElement interface. Unless a ExtensionElementProvider
* is registered with {@link org.jivesoftware.smack.provider.ProviderManager ProviderManager},
* instances of this class will be returned when getting stanza(/packet) extensions.<p>
*
* This class provides a very simple representation of an XML sub-document. Each element
* is a key in a Map with its CDATA being the value. For example, given the following
* XML sub-document:
*
* <pre>
* <foo xmlns="http://bar.com">
* <color>blue</color>
* <food>pizza</food>
* </foo></pre>
*
* In this case, getValue("color") would return "blue", and getValue("food") would
* return "pizza". This parsing mechanism mechanism is very simplistic and will not work
* as desired in all cases (for example, if some of the elements have attributes. In those
* cases, a custom ExtensionElementProvider should be used.
*
* @author Matt Tucker
* @deprecated use {@link org.jivesoftware.smack.packet.StandardExtensionElement} instead.
*/
@Deprecated
public class DefaultExtensionElement implements ExtensionElement {
private String elementName;
private String namespace;
private Map<String,String> map;
/**
* Creates a new generic stanza(/packet) extension.
*
* @param elementName the name of the element of the XML sub-document.
* @param namespace the namespace of the element.
*/
public DefaultExtensionElement(String elementName, String namespace) {
this.elementName = elementName;
this.namespace = namespace;
}
/**
* Returns the XML element name of the extension sub-packet root element.
*
* @return the XML element name of the stanza(/packet) extension.
*/
public String getElementName() {
return elementName;
}
/**
* Returns the XML namespace of the extension sub-packet root element.
*
* @return the XML namespace of the stanza(/packet) extension.
*/
public String getNamespace() {
return namespace;
}
@Override
public CharSequence toXML() {
XmlStringBuilder buf = new XmlStringBuilder();
buf.halfOpenElement(elementName).xmlnsAttribute(namespace).rightAngleBracket();
for (String name : getNames()) {
String value = getValue(name);
buf.element(name, value);
}
buf.closeElement(elementName);
return buf;
}
/**
* Returns an unmodifiable collection of the names that can be used to get
* values of the stanza(/packet) extension.
*
* @return the names.
*/
public synchronized Collection<String> getNames() {
if (map == null) {
return Collections.emptySet();
}
return Collections.unmodifiableSet(new HashMap<String,String>(map).keySet());
}
/**
* Returns a stanza(/packet) extension value given a name.
*
* @param name the name.
* @return the value.
*/
public synchronized String getValue(String name) {
if (map == null) {
return null;
}
return map.get(name);
}
/**
* Sets a stanza(/packet) extension value using the given name.
*
* @param name the name.
* @param value the value.
*/
public synchronized void setValue(String name, String value) {
if (map == null) {
map = new HashMap<String,String>();
}
map.put(name, value);
}
}