2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.xml;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2018-03-05 12:02:00 +00:00
|
|
|
import java.util.HashMap;
|
2014-10-22 16:38:44 +00:00
|
|
|
import java.util.Hashtable;
|
|
|
|
import java.util.List;
|
2018-03-05 12:02:00 +00:00
|
|
|
import java.util.Locale;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
|
|
|
import eu.siacs.conversations.utils.XmlHelper;
|
2018-04-28 14:26:40 +00:00
|
|
|
import eu.siacs.conversations.xmpp.InvalidJid;
|
2018-04-28 19:30:30 +00:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
|
2018-03-05 17:30:40 +00:00
|
|
|
import rocks.xmpp.addr.Jid;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
|
|
|
public class Element {
|
2015-09-15 20:52:35 +00:00
|
|
|
private final String name;
|
|
|
|
private Hashtable<String, String> attributes = new Hashtable<>();
|
|
|
|
private String content;
|
2014-11-06 19:10:03 +00:00
|
|
|
protected List<Element> children = new ArrayList<>();
|
2014-10-22 16:38:44 +00:00
|
|
|
|
|
|
|
public Element(String name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
2015-06-25 14:58:24 +00:00
|
|
|
public Element(String name, String xmlns) {
|
|
|
|
this.name = name;
|
|
|
|
this.setAttribute("xmlns", xmlns);
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public Element addChild(Element child) {
|
|
|
|
this.content = null;
|
|
|
|
children.add(child);
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Element addChild(String name) {
|
|
|
|
this.content = null;
|
|
|
|
Element child = new Element(name);
|
|
|
|
children.add(child);
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Element addChild(String name, String xmlns) {
|
|
|
|
this.content = null;
|
|
|
|
Element child = new Element(name);
|
|
|
|
child.setAttribute("xmlns", xmlns);
|
|
|
|
children.add(child);
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Element setContent(String content) {
|
|
|
|
this.content = content;
|
|
|
|
this.children.clear();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Element findChild(String name) {
|
|
|
|
for (Element child : this.children) {
|
|
|
|
if (child.getName().equals(name)) {
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-05-14 12:42:21 +00:00
|
|
|
public String findChildContent(String name) {
|
|
|
|
Element element = findChild(name);
|
|
|
|
return element == null ? null : element.getContent();
|
|
|
|
}
|
|
|
|
|
2019-09-12 08:12:47 +00:00
|
|
|
public LocalizedContent findInternationalizedChildContentInDefaultNamespace(String name) {
|
|
|
|
return LocalizedContent.get(this, name);
|
2018-03-05 12:02:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public Element findChild(String name, String xmlns) {
|
|
|
|
for (Element child : this.children) {
|
2015-08-23 06:01:47 +00:00
|
|
|
if (name.equals(child.getName()) && xmlns.equals(child.getAttribute("xmlns"))) {
|
2014-10-22 16:38:44 +00:00
|
|
|
return child;
|
2015-08-23 06:01:47 +00:00
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-05-14 12:42:21 +00:00
|
|
|
public String findChildContent(String name, String xmlns) {
|
|
|
|
Element element = findChild(name,xmlns);
|
|
|
|
return element == null ? null : element.getContent();
|
|
|
|
}
|
|
|
|
|
2014-12-21 20:43:58 +00:00
|
|
|
public boolean hasChild(final String name) {
|
2014-10-22 16:38:44 +00:00
|
|
|
return findChild(name) != null;
|
|
|
|
}
|
|
|
|
|
2014-12-21 20:43:58 +00:00
|
|
|
public boolean hasChild(final String name, final String xmlns) {
|
2014-10-22 16:38:44 +00:00
|
|
|
return findChild(name, xmlns) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Element> getChildren() {
|
|
|
|
return this.children;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Element setChildren(List<Element> children) {
|
|
|
|
this.children = children;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-09-15 20:52:35 +00:00
|
|
|
public final String getContent() {
|
2014-10-22 16:38:44 +00:00
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Element setAttribute(String name, String value) {
|
|
|
|
if (name != null && value != null) {
|
|
|
|
this.attributes.put(name, value);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-06-24 14:33:15 +00:00
|
|
|
public Element removeAttribute(String name) {
|
|
|
|
this.attributes.remove(name);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-10 00:24:35 +00:00
|
|
|
public Jid getAttributeAsJid(String name) {
|
|
|
|
final String jid = this.getAttribute(name);
|
2014-12-21 20:43:58 +00:00
|
|
|
if (jid != null && !jid.isEmpty()) {
|
|
|
|
try {
|
2018-03-11 17:32:16 +00:00
|
|
|
return Jid.ofEscaped(jid);
|
2018-03-05 17:30:40 +00:00
|
|
|
} catch (final IllegalArgumentException e) {
|
2018-04-28 19:30:30 +00:00
|
|
|
return InvalidJid.of(jid, this instanceof MessagePacket);
|
2014-12-21 20:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2014-11-10 00:24:35 +00:00
|
|
|
}
|
2014-11-06 19:10:03 +00:00
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public Hashtable<String, String> getAttributes() {
|
|
|
|
return this.attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
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(XmlHelper.encodeEntities(content));
|
|
|
|
} else {
|
|
|
|
for (Element child : children) {
|
|
|
|
elementOutput.append(child.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Tag endTag = Tag.end(name);
|
|
|
|
elementOutput.append(endTag);
|
|
|
|
}
|
|
|
|
return elementOutput.toString();
|
|
|
|
}
|
|
|
|
|
2015-09-15 20:52:35 +00:00
|
|
|
public final String getName() {
|
2014-10-22 16:38:44 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearChildren() {
|
|
|
|
this.children.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAttribute(String name, long value) {
|
|
|
|
this.setAttribute(name, Long.toString(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAttribute(String name, int value) {
|
|
|
|
this.setAttribute(name, Integer.toString(value));
|
|
|
|
}
|
2014-12-05 00:54:16 +00:00
|
|
|
|
|
|
|
public boolean getAttributeAsBoolean(String name) {
|
|
|
|
String attr = getAttribute(name);
|
|
|
|
return (attr != null && (attr.equalsIgnoreCase("true") || attr.equalsIgnoreCase("1")));
|
|
|
|
}
|
2015-12-04 21:03:46 +00:00
|
|
|
|
|
|
|
public String getNamespace() {
|
|
|
|
return getAttribute("xmlns");
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|