Element.java

  1package eu.siacs.conversations.xml;
  2
  3import org.jetbrains.annotations.NotNull;
  4
  5import java.util.ArrayList;
  6import java.util.Hashtable;
  7import java.util.List;
  8
  9import eu.siacs.conversations.utils.XmlHelper;
 10import eu.siacs.conversations.xmpp.InvalidJid;
 11import eu.siacs.conversations.xmpp.Jid;
 12import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
 13
 14public class Element {
 15    private final String name;
 16    private Hashtable<String, String> attributes = new Hashtable<>();
 17    private String content;
 18    protected List<Element> children = new ArrayList<>();
 19
 20    public Element(String name) {
 21        this.name = name;
 22    }
 23
 24    public Element(String name, String xmlns) {
 25        this.name = name;
 26        this.setAttribute("xmlns", xmlns);
 27    }
 28
 29    public Element addChild(Element child) {
 30        this.content = null;
 31        children.add(child);
 32        return child;
 33    }
 34
 35    public Element addChild(String name) {
 36        this.content = null;
 37        Element child = new Element(name);
 38        children.add(child);
 39        return child;
 40    }
 41
 42    public Element addChild(String name, String xmlns) {
 43        this.content = null;
 44        Element child = new Element(name);
 45        child.setAttribute("xmlns", xmlns);
 46        children.add(child);
 47        return child;
 48    }
 49
 50    public Element setContent(String content) {
 51        this.content = content;
 52        this.children.clear();
 53        return this;
 54    }
 55
 56    public Element findChild(String name) {
 57        for (Element child : this.children) {
 58            if (child.getName().equals(name)) {
 59                return child;
 60            }
 61        }
 62        return null;
 63    }
 64
 65    public String findChildContent(String name) {
 66        Element element = findChild(name);
 67        return element == null ? null : element.getContent();
 68    }
 69
 70    public LocalizedContent findInternationalizedChildContentInDefaultNamespace(String name) {
 71        return LocalizedContent.get(this, name);
 72    }
 73
 74    public Element findChild(String name, String xmlns) {
 75        for (Element child : this.children) {
 76            if (name.equals(child.getName()) && xmlns.equals(child.getAttribute("xmlns"))) {
 77                return child;
 78            }
 79        }
 80        return null;
 81    }
 82
 83    public Element findChildEnsureSingle(String name, String xmlns) {
 84        final List<Element> results = new ArrayList<>();
 85        for (Element child : this.children) {
 86            if (name.equals(child.getName()) && xmlns.equals(child.getAttribute("xmlns"))) {
 87                results.add(child);
 88            }
 89        }
 90        if (results.size() == 1) {
 91            return results.get(0);
 92        }
 93        return null;
 94    }
 95
 96    public String findChildContent(String name, String xmlns) {
 97        Element element = findChild(name, xmlns);
 98        return element == null ? null : element.getContent();
 99    }
100
101    public boolean hasChild(final String name) {
102        return findChild(name) != null;
103    }
104
105    public boolean hasChild(final String name, final String xmlns) {
106        return findChild(name, xmlns) != null;
107    }
108
109    public List<Element> getChildren() {
110        return this.children;
111    }
112
113    public Element setChildren(List<Element> children) {
114        this.children = children;
115        return this;
116    }
117
118    public final String getContent() {
119        return content;
120    }
121
122    public Element setAttribute(String name, String value) {
123        if (name != null && value != null) {
124            this.attributes.put(name, value);
125        }
126        return this;
127    }
128
129    public Element setAttribute(String name, Jid value) {
130        if (name != null && value != null) {
131            this.attributes.put(name, value.toEscapedString());
132        }
133        return this;
134    }
135
136    public void removeAttribute(final String name) {
137        this.attributes.remove(name);
138    }
139
140    public Element setAttributes(Hashtable<String, String> attributes) {
141        this.attributes = attributes;
142        return this;
143    }
144
145    public String getAttribute(String name) {
146        if (this.attributes.containsKey(name)) {
147            return this.attributes.get(name);
148        } else {
149            return null;
150        }
151    }
152
153    public Jid getAttributeAsJid(String name) {
154        final String jid = this.getAttribute(name);
155        if (jid != null && !jid.isEmpty()) {
156            try {
157                return Jid.ofEscaped(jid);
158            } catch (final IllegalArgumentException e) {
159                return InvalidJid.of(jid, this instanceof MessagePacket);
160            }
161        }
162        return null;
163    }
164
165    public Hashtable<String, String> getAttributes() {
166        return this.attributes;
167    }
168
169    @NotNull
170    public String toString() {
171        final StringBuilder elementOutput = new StringBuilder();
172        if ((content == null) && (children.size() == 0)) {
173            final Tag emptyTag = Tag.empty(name);
174            emptyTag.setAttributes(this.attributes);
175            elementOutput.append(emptyTag);
176        } else {
177            final Tag startTag = Tag.start(name);
178            startTag.setAttributes(this.attributes);
179            elementOutput.append(startTag);
180            if (content != null) {
181                elementOutput.append(XmlHelper.encodeEntities(content));
182            } else {
183                for (final Element child : children) {
184                    elementOutput.append(child.toString());
185                }
186            }
187            final Tag endTag = Tag.end(name);
188            elementOutput.append(endTag);
189        }
190        return elementOutput.toString();
191    }
192
193    public final String getName() {
194        return name;
195    }
196
197    public void clearChildren() {
198        this.children.clear();
199    }
200
201    public void setAttribute(String name, long value) {
202        this.setAttribute(name, Long.toString(value));
203    }
204
205    public void setAttribute(String name, int value) {
206        this.setAttribute(name, Integer.toString(value));
207    }
208
209    public boolean getAttributeAsBoolean(String name) {
210        String attr = getAttribute(name);
211        return (attr != null && (attr.equalsIgnoreCase("true") || attr.equalsIgnoreCase("1")));
212    }
213
214    public String getNamespace() {
215        return getAttribute("xmlns");
216    }
217}