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