Element.java

  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 getJid() {
109        final String jid = this.getAttribute("jid");
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 Jid getTo() {
121        final String to = this.getAttribute("to");
122        if (to != null && !to.isEmpty()) {
123            try {
124                return Jid.fromString(to);
125            } catch (final InvalidJidException e) {
126                return null;
127            }
128        }
129        return null;
130    }
131
132    public Jid getFrom() {
133        final String from = this.getAttribute("from");
134        if (from != null && !from.isEmpty()) {
135            try {
136                return Jid.fromString(from);
137            } catch (final InvalidJidException e) {
138                return null;
139            }
140        }
141        return null;
142    }
143
144	public Hashtable<String, String> getAttributes() {
145		return this.attributes;
146	}
147
148	public String toString() {
149		StringBuilder elementOutput = new StringBuilder();
150		if ((content == null) && (children.size() == 0)) {
151			Tag emptyTag = Tag.empty(name);
152			emptyTag.setAtttributes(this.attributes);
153			elementOutput.append(emptyTag.toString());
154		} else {
155			Tag startTag = Tag.start(name);
156			startTag.setAtttributes(this.attributes);
157			elementOutput.append(startTag);
158			if (content != null) {
159				elementOutput.append(XmlHelper.encodeEntities(content));
160			} else {
161				for (Element child : children) {
162					elementOutput.append(child.toString());
163				}
164			}
165			Tag endTag = Tag.end(name);
166			elementOutput.append(endTag);
167		}
168		return elementOutput.toString();
169	}
170
171	public String getName() {
172		return name;
173	}
174
175	public void clearChildren() {
176		this.children.clear();
177	}
178
179	public void setAttribute(String name, long value) {
180		this.setAttribute(name, Long.toString(value));
181	}
182
183	public void setAttribute(String name, int value) {
184		this.setAttribute(name, Integer.toString(value));
185	}
186}