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;
  8
  9public class Element {
 10	protected String name;
 11	protected Hashtable<String, String> attributes = new Hashtable<String, String>();
 12	protected String content;
 13	protected List<Element> children = new ArrayList<Element>();
 14
 15	public Element(String name) {
 16		this.name = name;
 17	}
 18
 19	public Element addChild(Element child) {
 20		this.content = null;
 21		children.add(child);
 22		return child;
 23	}
 24
 25	public Element addChild(String name) {
 26		this.content = null;
 27		Element child = new Element(name);
 28		children.add(child);
 29		return child;
 30	}
 31
 32	public Element addChild(String name, String xmlns) {
 33		this.content = null;
 34		Element child = new Element(name);
 35		child.setAttribute("xmlns", xmlns);
 36		children.add(child);
 37		return child;
 38	}
 39
 40	public Element setContent(String content) {
 41		this.content = content;
 42		this.children.clear();
 43		return this;
 44	}
 45
 46	public Element findChild(String name) {
 47		for (Element child : this.children) {
 48			if (child.getName().equals(name)) {
 49				return child;
 50			}
 51		}
 52		return null;
 53	}
 54
 55	public Element findChild(String name, String xmlns) {
 56		for (Element child : this.children) {
 57			if (child.getName().equals(name)
 58					&& (child.getAttribute("xmlns").equals(xmlns))) {
 59				return child;
 60			}
 61		}
 62		return null;
 63	}
 64
 65	public boolean hasChild(String name) {
 66		return findChild(name) != null;
 67	}
 68
 69	public boolean hasChild(String name, String xmlns) {
 70		return findChild(name, xmlns) != null;
 71	}
 72
 73	public List<Element> getChildren() {
 74		return this.children;
 75	}
 76
 77	public Element setChildren(List<Element> children) {
 78		this.children = children;
 79		return this;
 80	}
 81
 82	public String getContent() {
 83		return content;
 84	}
 85
 86	public Element setAttribute(String name, String value) {
 87		if (name != null && value != null) {
 88			this.attributes.put(name, value);
 89		}
 90		return this;
 91	}
 92
 93	public Element setAttributes(Hashtable<String, String> attributes) {
 94		this.attributes = attributes;
 95		return this;
 96	}
 97
 98	public String getAttribute(String name) {
 99		if (this.attributes.containsKey(name)) {
100			return this.attributes.get(name);
101		} else {
102			return null;
103		}
104	}
105
106	public Hashtable<String, String> getAttributes() {
107		return this.attributes;
108	}
109
110	public String toString() {
111		StringBuilder elementOutput = new StringBuilder();
112		if ((content == null) && (children.size() == 0)) {
113			Tag emptyTag = Tag.empty(name);
114			emptyTag.setAtttributes(this.attributes);
115			elementOutput.append(emptyTag.toString());
116		} else {
117			Tag startTag = Tag.start(name);
118			startTag.setAtttributes(this.attributes);
119			elementOutput.append(startTag);
120			if (content != null) {
121				elementOutput.append(XmlHelper.encodeEntities(content));
122			} else {
123				for (Element child : children) {
124					elementOutput.append(child.toString());
125				}
126			}
127			Tag endTag = Tag.end(name);
128			elementOutput.append(endTag);
129		}
130		return elementOutput.toString();
131	}
132
133	public String getName() {
134		return name;
135	}
136
137	public void clearChildren() {
138		this.children.clear();
139	}
140
141	public void setAttribute(String name, long value) {
142		this.setAttribute(name, Long.toString(value));
143	}
144
145	public void setAttribute(String name, int value) {
146		this.setAttribute(name, Integer.toString(value));
147	}
148}