Element.java

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