1package de.gultsch.chat.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 String getContent() {
50 return content;
51 }
52
53 public Element setAttribute(String name, String value) {
54 this.attributes.put(name, value);
55 return this;
56 }
57
58 public Element setAttributes(Hashtable<String, String> attributes) {
59 this.attributes = attributes;
60 return this;
61 }
62
63 public String getAttribute(String name) {
64 if (this.attributes.containsKey(name)) {
65 return this.attributes.get(name);
66 } else {
67 return null;
68 }
69 }
70
71 public String toString() {
72 StringBuilder elementOutput = new StringBuilder();
73 if ((content==null)&&(children.size() == 0)) {
74 Tag emptyTag = Tag.empty(name);
75 emptyTag.setAtttributes(this.attributes);
76 elementOutput.append(emptyTag.toString());
77 } else {
78 Tag startTag = Tag.start(name);
79 startTag.setAtttributes(this.attributes);
80 elementOutput.append(startTag);
81 if (content!=null) {
82 elementOutput.append(content);
83 } else {
84 for(Element child : children) {
85 elementOutput.append(child.toString());
86 }
87 }
88 Tag endTag = Tag.end(name);
89 elementOutput.append(endTag);
90 }
91 return elementOutput.toString();
92 }
93
94 public String getName() {
95 return name;
96 }
97}