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 Element findChild(String name, String xmlns) {
 54		for(Element child : this.children) {
 55			if (child.getName().equals(name)&&(child.getAttribute("xmlns").equals(xmlns))) {
 56				return child;
 57			}
 58		}
 59		return null;
 60	}
 61	
 62	public boolean hasChild(String name) {
 63		return findChild(name) != null;
 64	}
 65	
 66	public boolean hasChild(String name, String xmlns) {
 67		return findChild(name, xmlns) != null;
 68	}
 69	
 70	
 71	
 72	public List<Element> getChildren() {
 73		return this.children;
 74	}
 75	
 76	public Element setChildren(List<Element> children) {
 77		this.children = children;
 78		return this;
 79	}
 80	
 81	public String getContent() {
 82		return content;
 83	}
 84	
 85	public Element setAttribute(String name, String value) {
 86		this.attributes.put(name, value);
 87		return this;
 88	}
 89	
 90	public Element setAttributes(Hashtable<String, String> attributes) {
 91		this.attributes = attributes;
 92		return this;
 93	}
 94	
 95	public String getAttribute(String name) {
 96		if (this.attributes.containsKey(name)) {
 97			return this.attributes.get(name);
 98		} else {
 99			return null;
100		}
101	}
102	
103	public Hashtable<String, String> getAttributes() {
104		return this.attributes;
105	}
106	
107	public String toString() {
108		StringBuilder elementOutput = new StringBuilder();
109		if ((content==null)&&(children.size() == 0)) {
110			Tag emptyTag = Tag.empty(name);
111			emptyTag.setAtttributes(this.attributes);
112			elementOutput.append(emptyTag.toString());
113		} else {
114			Tag startTag = Tag.start(name);
115			startTag.setAtttributes(this.attributes);
116			elementOutput.append(startTag);
117			if (content!=null) {
118				elementOutput.append(encodeEntities(content));
119			} else {
120				for(Element child : children) {
121					elementOutput.append(child.toString());
122				}
123			}
124			Tag endTag = Tag.end(name);
125			elementOutput.append(endTag);
126		}
127		return elementOutput.toString();
128	}
129
130	public String getName() {
131		return name;
132	}
133	
134	private String encodeEntities(String content) {
135		content = content.replace("&","&amp;");
136		content = content.replace("<","&lt;");
137		content = content.replace(">","&gt;");
138		content = content.replace("\"","&quot;");
139		content = content.replace("'","&apos;");
140		return content;
141	}
142
143	public void clearChildren() {
144		this.children.clear();
145	}
146}