1package eu.siacs.conversations.xml;
2
3import android.util.Log;
4
5import java.util.ArrayList;
6import java.util.Hashtable;
7import java.util.List;
8
9import eu.siacs.conversations.Config;
10import eu.siacs.conversations.utils.XmlHelper;
11import eu.siacs.conversations.xmpp.jid.InvalidJidException;
12import eu.siacs.conversations.xmpp.jid.Jid;
13
14public class Element {
15 protected String name;
16 protected Hashtable<String, String> attributes = new Hashtable<>();
17 protected String content;
18 protected List<Element> children = new ArrayList<>();
19
20 public Element(String name) {
21 this.name = name;
22 }
23
24 public Element(String name, String xmlns) {
25 this.name = name;
26 this.setAttribute("xmlns", xmlns);
27 }
28
29 public Element addChild(Element child) {
30 this.content = null;
31 children.add(child);
32 return child;
33 }
34
35 public Element addChild(String name) {
36 this.content = null;
37 Element child = new Element(name);
38 children.add(child);
39 return child;
40 }
41
42 public Element addChild(String name, String xmlns) {
43 this.content = null;
44 Element child = new Element(name);
45 child.setAttribute("xmlns", xmlns);
46 children.add(child);
47 return child;
48 }
49
50 public Element setContent(String content) {
51 this.content = content;
52 this.children.clear();
53 return this;
54 }
55
56 public Element findChild(String name) {
57 for (Element child : this.children) {
58 if (child.getName().equals(name)) {
59 return child;
60 }
61 }
62 return null;
63 }
64
65 public String findChildContent(String name) {
66 Element element = findChild(name);
67 return element == null ? null : element.getContent();
68 }
69
70 public Element findChild(String name, String xmlns) {
71 for (Element child : this.children) {
72 if (child.getName().equals(name)
73 && (child.getAttribute("xmlns").equals(xmlns))) {
74 return child;
75 }
76 }
77 return null;
78 }
79
80 public String findChildContent(String name, String xmlns) {
81 Element element = findChild(name,xmlns);
82 return element == null ? null : element.getContent();
83 }
84
85 public boolean hasChild(final String name) {
86 return findChild(name) != null;
87 }
88
89 public boolean hasChild(final String name, final String xmlns) {
90 return findChild(name, xmlns) != null;
91 }
92
93 public List<Element> getChildren() {
94 return this.children;
95 }
96
97 public Element setChildren(List<Element> children) {
98 this.children = children;
99 return this;
100 }
101
102 public String getContent() {
103 return content;
104 }
105
106 public Element setAttribute(String name, String value) {
107 if (name != null && value != null) {
108 this.attributes.put(name, value);
109 }
110 return this;
111 }
112
113 public Element setAttributes(Hashtable<String, String> attributes) {
114 this.attributes = attributes;
115 return this;
116 }
117
118 public String getAttribute(String name) {
119 if (this.attributes.containsKey(name)) {
120 return this.attributes.get(name);
121 } else {
122 return null;
123 }
124 }
125
126 public Jid getAttributeAsJid(String name) {
127 final String jid = this.getAttribute(name);
128 if (jid != null && !jid.isEmpty()) {
129 try {
130 return Jid.fromString(jid);
131 } catch (final InvalidJidException e) {
132 Log.e(Config.LOGTAG, "could not parse jid " + jid);
133 return null;
134 }
135 }
136 return null;
137 }
138
139 public Hashtable<String, String> getAttributes() {
140 return this.attributes;
141 }
142
143 public String toString() {
144 StringBuilder elementOutput = new StringBuilder();
145 if ((content == null) && (children.size() == 0)) {
146 Tag emptyTag = Tag.empty(name);
147 emptyTag.setAtttributes(this.attributes);
148 elementOutput.append(emptyTag.toString());
149 } else {
150 Tag startTag = Tag.start(name);
151 startTag.setAtttributes(this.attributes);
152 elementOutput.append(startTag);
153 if (content != null) {
154 elementOutput.append(XmlHelper.encodeEntities(content));
155 } else {
156 for (Element child : children) {
157 elementOutput.append(child.toString());
158 }
159 }
160 Tag endTag = Tag.end(name);
161 elementOutput.append(endTag);
162 }
163 return elementOutput.toString();
164 }
165
166 public String getName() {
167 return name;
168 }
169
170 public void clearChildren() {
171 this.children.clear();
172 }
173
174 public void setAttribute(String name, long value) {
175 this.setAttribute(name, Long.toString(value));
176 }
177
178 public void setAttribute(String name, int value) {
179 this.setAttribute(name, Integer.toString(value));
180 }
181
182 public boolean getAttributeAsBoolean(String name) {
183 String attr = getAttribute(name);
184 return (attr != null && (attr.equalsIgnoreCase("true") || attr.equalsIgnoreCase("1")));
185 }
186}