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