aboutsummaryrefslogtreecommitdiffstats
path: root/src/de/gultsch/chat/xml/Element.java
blob: af3d8f1ff0a5da4511e16502a8dbf55cbbd516f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package de.gultsch.chat.xml;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import android.util.Log;

public class Element {
	protected String name;
	protected Hashtable<String, String> attributes = new Hashtable<String, String>();
	protected String content;
	protected List<Element> children = new ArrayList<Element>();
	
	public Element(String name) {
		this.name = name;
	}
	
	public Element addChild(Element child) {
		this.content = null;
		children.add(child);
		return this;
	}
	
	public Element setContent(String content) {
		this.content = content;
		this.children.clear();
		return this;
	}
	
	public boolean hasChild(String name) {
		for(Element child : this.children) {
			if (child.getName().equals(name)) {
				return true;
			}
		}
		return false;
	}
	
	public Element setAttribute(String name, String value) {
		this.attributes.put(name, value);
		return this;
	}
	
	public Element setAttributes(Hashtable<String, String> attributes) {
		this.attributes = attributes;
		return this;
	}
	
	public String getAttribute(String name) {
		if (this.attributes.containsKey(name)) {
			return this.attributes.get(name);
		} else {
			return null;
		}
	}
	
	public String toString() {
		StringBuilder elementOutput = new StringBuilder();
		if ((content==null)&&(children.size() == 0)) {
			Tag emptyTag = Tag.empty(name);
			emptyTag.setAtttributes(this.attributes);
			elementOutput.append(emptyTag.toString());
		} else {
			Tag startTag = Tag.start(name);
			startTag.setAtttributes(this.attributes);
			elementOutput.append(startTag);
			if (content!=null) {
				elementOutput.append(content);
			} else {
				for(Element child : children) {
					elementOutput.append(child.toString());
				}
			}
			Tag endTag = Tag.end(name);
			elementOutput.append(endTag);
		}
		return elementOutput.toString();
	}

	public String getName() {
		return name;
	}
}