Element.java

  1package eu.siacs.conversations.xml;
  2
  3import java.util.ArrayList;
  4import java.util.Hashtable;
  5import java.util.List;
  6
  7public class Element {
  8	protected String name;
  9	protected Hashtable<String, String> attributes = new Hashtable<String, String>();
 10	protected String content;
 11	protected List<Element> children = new ArrayList<Element>();
 12	
 13	public Element(String name) {
 14		this.name = name;
 15	}
 16	
 17	public Element addChild(Element child) {
 18		this.content = null;
 19		children.add(child);
 20		return child;
 21	}
 22	
 23	public Element addChild(String name) {
 24		this.content = null;
 25		Element child = new Element(name);
 26		children.add(child);
 27		return child;
 28	}
 29	
 30	public Element addChild(String name, String xmlns) {
 31		this.content = null;
 32		Element child = new Element(name);
 33		child.setAttribute("xmlns", xmlns);
 34		children.add(child);
 35		return child;
 36	}
 37	
 38	public Element setContent(String content) {
 39		this.content = content;
 40		this.children.clear();
 41		return this;
 42	}
 43	
 44	public Element findChild(String name) {
 45		for(Element child : this.children) {
 46			if (child.getName().equals(name)) {
 47				return child;
 48			}
 49		}
 50		return null;
 51	}
 52	
 53	public boolean hasChild(String name) {
 54		for(Element child : this.children) {
 55			if (child.getName().equals(name)) {
 56				return true;
 57			}
 58		}
 59		return false;
 60	}
 61	
 62	public List<Element> getChildren() {
 63		return this.children;
 64	}
 65	
 66	public String getContent() {
 67		return content;
 68	}
 69	
 70	public Element setAttribute(String name, String value) {
 71		this.attributes.put(name, value);
 72		return this;
 73	}
 74	
 75	public Element setAttributes(Hashtable<String, String> attributes) {
 76		this.attributes = attributes;
 77		return this;
 78	}
 79	
 80	public String getAttribute(String name) {
 81		if (this.attributes.containsKey(name)) {
 82			return this.attributes.get(name);
 83		} else {
 84			return null;
 85		}
 86	}
 87	
 88	public String toString() {
 89		StringBuilder elementOutput = new StringBuilder();
 90		if ((content==null)&&(children.size() == 0)) {
 91			Tag emptyTag = Tag.empty(name);
 92			emptyTag.setAtttributes(this.attributes);
 93			elementOutput.append(emptyTag.toString());
 94		} else {
 95			Tag startTag = Tag.start(name);
 96			startTag.setAtttributes(this.attributes);
 97			elementOutput.append(startTag);
 98			if (content!=null) {
 99				elementOutput.append(encodeEntities(content));
100			} else {
101				for(Element child : children) {
102					elementOutput.append(child.toString());
103				}
104			}
105			Tag endTag = Tag.end(name);
106			elementOutput.append(endTag);
107		}
108		return elementOutput.toString();
109	}
110
111	public String getName() {
112		return name;
113	}
114	
115	private String encodeEntities(String content) {
116		content = content.replace("&","&amp;");
117		content = content.replace("<","&lt;");
118		content = content.replace(">","&gt;");
119		content = content.replace("\"","&quot;");
120		content = content.replace("'","&apos;");
121		return content;
122	}
123}