aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/xml/Element.java
blob: 0b8c20358585e706b438bf4159bc7e9539be0fad (plain)
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
package de.thedevstack.conversationsplus.xml;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import de.thedevstack.android.logcat.Logging;
import de.thedevstack.conversationsplus.Config;
import de.thedevstack.conversationsplus.xmpp.jid.InvalidJidException;
import de.thedevstack.conversationsplus.xmpp.jid.Jid;

public class Element {
	private final String name;
	private Hashtable<String, String> attributes = new Hashtable<>();
	private String content;
	protected List<Element> children = new ArrayList<>();

	public Element(String name) {
		this.name = name;
	}

	public Element(String name, String xmlns) {
		this.name = name;
		this.setAttribute("xmlns", xmlns);
	}

	public Element addChild(Element child) {
		this.content = null;
		children.add(child);
		return child;
	}

	public Element addChild(String name) {
		this.content = null;
		Element child = new Element(name);
		children.add(child);
		return child;
	}

	public Element addChild(String name, String xmlns) {
		this.content = null;
		Element child = new Element(name);
		child.setAttribute("xmlns", xmlns);
		children.add(child);
		return child;
	}

	public Element setContent(String content) {
		this.content = content;
		this.children.clear();
		return this;
	}

	public Element findChild(String name) {
		for (Element child : this.children) {
			if (child.getName().equals(name)) {
				return child;
			}
		}
		return null;
	}

	public String findChildContent(String name) {
		Element element = findChild(name);
		return element == null ? null : element.getContent();
	}

	public Element findChild(String name, String xmlns) {
		for (Element child : this.children) {
			if (name.equals(child.getName()) && xmlns.equals(child.getAttribute("xmlns"))) {
				return child;
			}
		}
		return null;
	}

	public Element findChildRecursive(String name, String xmlns) {
	    Element foundChild = null;
        for (Element child : this.children) {
            if (name.equals(child.getName()) && xmlns.equals(child.getAttribute("xmlns"))) {
                foundChild = child;
                break;
            } else {
                foundChild = findChildRecursive(name, xmlns);
                if (null != foundChild) {
                    break;
                }
            }
        }
        return foundChild;
    }

    public Element findChild(XmlElementContainer container) {
	    Element element = container.getXmlElement();
        return findChild(element.getName(), element.getNamespace());
    }

	public String findChildContent(String name, String xmlns) {
		Element element = findChild(name,xmlns);
		return element == null ? null : element.getContent();
	}

	public boolean hasChild(final String name) {
		return findChild(name) != null;
	}

	public boolean hasChild(final String name, final String xmlns) {
		return findChild(name, xmlns) != null;
	}

	public boolean hasChildRecursive(String name, String xmlns) {
	    return null != findChildRecursive(name, xmlns);
    }

    public boolean hasChildRecursive(XmlElementContainer container) {
        Element element = container.getXmlElement();
        return hasChildRecursive(element.getName(), element.getNamespace());
    }

    public boolean hasChild(XmlElementContainer container) {
        Element element = container.getXmlElement();
        return hasChild(element.getName(), element.getNamespace());
    }

	public List<Element> getChildren() {
		return this.children;
	}

	public Element setChildren(List<Element> children) {
		this.children = children;
		return this;
	}

	public final String getContent() {
		return content;
	}

	public Element setAttribute(String name, String value) {
		if (name != null && value != null) {
			this.attributes.put(name, value);
		}
		return this;
	}

	public Element setAttributes(Hashtable<String, String> attributes) {
		this.attributes = attributes;
		return this;
	}

	public String getAttribute(String name) {
		if (this.attributes.containsKey(name)) {
			return this.attributes.get(name);
		} else {
			return null;
		}
	}

	public Jid getAttributeAsJid(String name) {
		final String jid = this.getAttribute(name);
		if (jid != null && !jid.isEmpty()) {
			try {
				return Jid.fromString(jid);
			} catch (final InvalidJidException e) {
				Logging.e(Config.LOGTAG, "could not parse jid " + jid);
				return null;
			}
		}
		return null;
	}

	public Hashtable<String, String> getAttributes() {
		return this.attributes;
	}

	public String toString() {
		StringBuilder elementOutput = new StringBuilder();
		if ((content == null) && (children.size() == 0)) {
			Tag emptyTag = Tag.empty(name);
			emptyTag.setAtttributes(this.attributes);
			elementOutput.append(emptyTag.toString());
		} else {
			Tag startTag = Tag.start(name);
			startTag.setAtttributes(this.attributes);
			elementOutput.append(startTag);
			if (content != null) {
				elementOutput.append(XmlHelper.encodeEntities(content));
			} else {
				for (Element child : children) {
					elementOutput.append(child.toString());
				}
			}
			Tag endTag = Tag.end(name);
			elementOutput.append(endTag);
		}
		return elementOutput.toString();
	}

	public final String getName() {
		return name;
	}

	public void clearChildren() {
		this.children.clear();
	}

	public void setAttribute(String name, long value) {
		this.setAttribute(name, Long.toString(value));
	}

	public void setAttribute(String name, int value) {
		this.setAttribute(name, Integer.toString(value));
	}

	public boolean getAttributeAsBoolean(String name) {
		String attr = getAttribute(name);
		return (attr != null && (attr.equalsIgnoreCase("true") || attr.equalsIgnoreCase("1")));
	}

	public String getNamespace() {
		return getAttribute("xmlns");
	}
}