Element.java

  1package eu.siacs.conversations.xml;
  2
  3import java.util.ArrayList;
  4import java.util.Hashtable;
  5import java.util.List;
  6
  7import android.util.Log;
  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 this;
 23	}
 24	
 25	public Element setContent(String content) {
 26		this.content = content;
 27		this.children.clear();
 28		return this;
 29	}
 30	
 31	public Element findChild(String name) {
 32		for(Element child : this.children) {
 33			if (child.getName().equals(name)) {
 34				return child;
 35			}
 36		}
 37		return null;
 38	}
 39	
 40	public boolean hasChild(String name) {
 41		for(Element child : this.children) {
 42			if (child.getName().equals(name)) {
 43				return true;
 44			}
 45		}
 46		return false;
 47	}
 48	
 49	public List<Element> getChildren() {
 50		return this.children;
 51	}
 52	
 53	public String getContent() {
 54		return content;
 55	}
 56	
 57	public Element setAttribute(String name, String value) {
 58		this.attributes.put(name, value);
 59		return this;
 60	}
 61	
 62	public Element setAttributes(Hashtable<String, String> attributes) {
 63		this.attributes = attributes;
 64		return this;
 65	}
 66	
 67	public String getAttribute(String name) {
 68		if (this.attributes.containsKey(name)) {
 69			return this.attributes.get(name);
 70		} else {
 71			return null;
 72		}
 73	}
 74	
 75	public String toString() {
 76		StringBuilder elementOutput = new StringBuilder();
 77		if ((content==null)&&(children.size() == 0)) {
 78			Tag emptyTag = Tag.empty(name);
 79			emptyTag.setAtttributes(this.attributes);
 80			elementOutput.append(emptyTag.toString());
 81		} else {
 82			Tag startTag = Tag.start(name);
 83			startTag.setAtttributes(this.attributes);
 84			elementOutput.append(startTag);
 85			if (content!=null) {
 86				elementOutput.append(content);
 87			} else {
 88				for(Element child : children) {
 89					elementOutput.append(child.toString());
 90				}
 91			}
 92			Tag endTag = Tag.end(name);
 93			elementOutput.append(endTag);
 94		}
 95		return elementOutput.toString();
 96	}
 97
 98	public String getName() {
 99		return name;
100	}
101}