2014-02-28 17:46:01 +00:00
|
|
|
package eu.siacs.conversations.xml;
|
2014-01-30 15:42:35 +00:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Hashtable;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
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>();
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-01-30 15:42:35 +00:00
|
|
|
public Element(String name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-01-30 15:42:35 +00:00
|
|
|
public Element addChild(Element child) {
|
|
|
|
this.content = null;
|
|
|
|
children.add(child);
|
2014-03-20 14:49:53 +00:00
|
|
|
return child;
|
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-03-20 14:49:53 +00:00
|
|
|
public Element addChild(String name) {
|
|
|
|
this.content = null;
|
|
|
|
Element child = new Element(name);
|
|
|
|
children.add(child);
|
|
|
|
return child;
|
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-03-20 14:49:53 +00:00
|
|
|
public Element addChild(String name, String xmlns) {
|
|
|
|
this.content = null;
|
|
|
|
Element child = new Element(name);
|
|
|
|
child.setAttribute("xmlns", xmlns);
|
|
|
|
children.add(child);
|
|
|
|
return child;
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-01-30 15:42:35 +00:00
|
|
|
public Element setContent(String content) {
|
|
|
|
this.content = content;
|
|
|
|
this.children.clear();
|
|
|
|
return this;
|
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-02-01 14:07:20 +00:00
|
|
|
public Element findChild(String name) {
|
2014-06-13 09:16:52 +00:00
|
|
|
for (Element child : this.children) {
|
2014-02-01 14:07:20 +00:00
|
|
|
if (child.getName().equals(name)) {
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-03-27 01:02:59 +00:00
|
|
|
public Element findChild(String name, String xmlns) {
|
2014-06-13 09:16:52 +00:00
|
|
|
for (Element child : this.children) {
|
|
|
|
if (child.getName().equals(name)
|
|
|
|
&& (child.getAttribute("xmlns").equals(xmlns))) {
|
2014-03-27 01:02:59 +00:00
|
|
|
return child;
|
2014-02-01 00:25:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-27 01:02:59 +00:00
|
|
|
return null;
|
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-03-27 01:02:59 +00:00
|
|
|
public boolean hasChild(String name) {
|
|
|
|
return findChild(name) != null;
|
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-03-27 01:02:59 +00:00
|
|
|
public boolean hasChild(String name, String xmlns) {
|
|
|
|
return findChild(name, xmlns) != null;
|
2014-02-01 00:25:56 +00:00
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-02-02 15:05:15 +00:00
|
|
|
public List<Element> getChildren() {
|
|
|
|
return this.children;
|
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-03-27 01:02:59 +00:00
|
|
|
public Element setChildren(List<Element> children) {
|
|
|
|
this.children = children;
|
|
|
|
return this;
|
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-02-01 14:07:20 +00:00
|
|
|
public String getContent() {
|
|
|
|
return content;
|
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-01-30 15:42:35 +00:00
|
|
|
public Element setAttribute(String name, String value) {
|
2014-06-13 09:16:52 +00:00
|
|
|
if (name != null && value != null) {
|
|
|
|
this.attributes.put(name, value);
|
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
return this;
|
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-01-30 15:42:35 +00:00
|
|
|
public Element setAttributes(Hashtable<String, String> attributes) {
|
|
|
|
this.attributes = attributes;
|
|
|
|
return this;
|
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-02-01 00:25:56 +00:00
|
|
|
public String getAttribute(String name) {
|
|
|
|
if (this.attributes.containsKey(name)) {
|
|
|
|
return this.attributes.get(name);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-03-27 01:02:59 +00:00
|
|
|
public Hashtable<String, String> getAttributes() {
|
|
|
|
return this.attributes;
|
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-01-30 15:42:35 +00:00
|
|
|
public String toString() {
|
|
|
|
StringBuilder elementOutput = new StringBuilder();
|
2014-06-13 09:16:52 +00:00
|
|
|
if ((content == null) && (children.size() == 0)) {
|
2014-01-30 15:42:35 +00:00
|
|
|
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);
|
2014-06-13 09:16:52 +00:00
|
|
|
if (content != null) {
|
2014-03-16 13:12:30 +00:00
|
|
|
elementOutput.append(encodeEntities(content));
|
2014-01-30 15:42:35 +00:00
|
|
|
} else {
|
2014-06-13 09:16:52 +00:00
|
|
|
for (Element child : children) {
|
2014-01-30 15:42:35 +00:00
|
|
|
elementOutput.append(child.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Tag endTag = Tag.end(name);
|
|
|
|
elementOutput.append(endTag);
|
|
|
|
}
|
|
|
|
return elementOutput.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
2014-06-13 09:16:52 +00:00
|
|
|
|
2014-03-16 13:12:30 +00:00
|
|
|
private String encodeEntities(String content) {
|
2014-06-13 09:16:52 +00:00
|
|
|
content = content.replace("&", "&");
|
|
|
|
content = content.replace("<", "<");
|
|
|
|
content = content.replace(">", ">");
|
|
|
|
content = content.replace("\"", """);
|
|
|
|
content = content.replace("'", "'");
|
2014-03-16 13:12:30 +00:00
|
|
|
return content;
|
|
|
|
}
|
2014-04-07 18:05:45 +00:00
|
|
|
|
|
|
|
public void clearChildren() {
|
|
|
|
this.children.clear();
|
|
|
|
}
|
2014-08-04 23:36:17 +00:00
|
|
|
|
|
|
|
public void setAttribute(String name, long value) {
|
|
|
|
this.setAttribute(name, ""+value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAttribute(String name, int value) {
|
|
|
|
this.setAttribute(name, ""+value);
|
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|