Element.java

  1package eu.siacs.conversations.xml;
  2
  3import java.util.ArrayList;
  4import java.util.HashMap;
  5import java.util.Hashtable;
  6import java.util.List;
  7import java.util.Locale;
  8
  9import eu.siacs.conversations.utils.XmlHelper;
 10import eu.siacs.conversations.xmpp.InvalidJid;
 11import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
 12import rocks.xmpp.addr.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 LocalizedContent findInternationalizedChildContentInDefaultNamespace(String name) {
 71		return LocalizedContent.get(this, name);
 72	}
 73
 74	public Element findChild(String name, String xmlns) {
 75		for (Element child : this.children) {
 76			if (name.equals(child.getName()) && xmlns.equals(child.getAttribute("xmlns"))) {
 77				return child;
 78			}
 79		}
 80		return null;
 81	}
 82
 83	public String findChildContent(String name, String xmlns) {
 84		Element element = findChild(name,xmlns);
 85		return element == null ? null : element.getContent();
 86	}
 87
 88	public boolean hasChild(final String name) {
 89		return findChild(name) != null;
 90	}
 91
 92	public boolean hasChild(final String name, final String xmlns) {
 93		return findChild(name, xmlns) != null;
 94	}
 95
 96	public List<Element> getChildren() {
 97		return this.children;
 98	}
 99
100	public Element setChildren(List<Element> children) {
101		this.children = children;
102		return this;
103	}
104
105	public final String getContent() {
106		return content;
107	}
108
109	public Element setAttribute(String name, String value) {
110		if (name != null && value != null) {
111			this.attributes.put(name, value);
112		}
113		return this;
114	}
115
116	public Element removeAttribute(String name) {
117		this.attributes.remove(name);
118		return this;
119	}
120
121	public Element setAttributes(Hashtable<String, String> attributes) {
122		this.attributes = attributes;
123		return this;
124	}
125
126	public String getAttribute(String name) {
127		if (this.attributes.containsKey(name)) {
128			return this.attributes.get(name);
129		} else {
130			return null;
131		}
132	}
133
134	public Jid getAttributeAsJid(String name) {
135		final String jid = this.getAttribute(name);
136		if (jid != null && !jid.isEmpty()) {
137			try {
138				return Jid.ofEscaped(jid);
139			} catch (final IllegalArgumentException e) {
140				return InvalidJid.of(jid, this instanceof MessagePacket);
141			}
142		}
143		return null;
144	}
145
146	public Hashtable<String, String> getAttributes() {
147		return this.attributes;
148	}
149
150	public String toString() {
151		StringBuilder elementOutput = new StringBuilder();
152		if ((content == null) && (children.size() == 0)) {
153			Tag emptyTag = Tag.empty(name);
154			emptyTag.setAtttributes(this.attributes);
155			elementOutput.append(emptyTag.toString());
156		} else {
157			Tag startTag = Tag.start(name);
158			startTag.setAtttributes(this.attributes);
159			elementOutput.append(startTag);
160			if (content != null) {
161				elementOutput.append(XmlHelper.encodeEntities(content));
162			} else {
163				for (Element child : children) {
164					elementOutput.append(child.toString());
165				}
166			}
167			Tag endTag = Tag.end(name);
168			elementOutput.append(endTag);
169		}
170		return elementOutput.toString();
171	}
172
173	public final String getName() {
174		return name;
175	}
176
177	public void clearChildren() {
178		this.children.clear();
179	}
180
181	public void setAttribute(String name, long value) {
182		this.setAttribute(name, Long.toString(value));
183	}
184
185	public void setAttribute(String name, int value) {
186		this.setAttribute(name, Integer.toString(value));
187	}
188
189	public boolean getAttributeAsBoolean(String name) {
190		String attr = getAttribute(name);
191		return (attr != null && (attr.equalsIgnoreCase("true") || attr.equalsIgnoreCase("1")));
192	}
193
194	public String getNamespace() {
195		return getAttribute("xmlns");
196	}
197}