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