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