Element.java

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