1package eu.siacs.conversations.xmpp.forms;
2
3import com.caverock.androidsvg.SVG;
4import com.caverock.androidsvg.SVGParseException;
5import java.util.ArrayList;
6import java.util.List;
7import eu.siacs.conversations.xml.Element;
8
9public class Option {
10 protected final String value;
11 protected final String label;
12 protected final SVG icon;
13 protected final Element iconEl;
14
15 public static List<Option> forField(Element field) {
16 List<Option> options = new ArrayList<>();
17 for (Element el : field.getChildren()) {
18 if (el.getNamespace() == null || !el.getNamespace().equals("jabber:x:data")) continue;
19 if (!el.getName().equals("option")) continue;
20 options.add(new Option(el));
21 }
22 return options;
23 }
24
25 public Option(final Element option) {
26 this(
27 option.findChildContent("value", "jabber:x:data"),
28 option.getAttribute("label"),
29 parseSVG(option.findChild("svg", "http://www.w3.org/2000/svg")),
30 option.findChild("svg", "http://www.w3.org/2000/svg")
31 );
32 }
33
34 public Option(final String value, final String label) {
35 this(value, label, null, null);
36 }
37
38 public Option(final String value, final String label, final SVG icon, final Element iconEl) {
39 this.value = value;
40 this.label = label == null ? value : label;
41 this.icon = icon;
42 this.iconEl = iconEl;
43 }
44
45 public boolean equals(Object o) {
46 if (!(o instanceof Option)) return false;
47
48 if (value == ((Option) o).value) return true;
49 if (value == null || ((Option) o).value == null) return false;
50 return value.equals(((Option) o).value);
51 }
52
53 public String toString() { return label; }
54
55 public String getValue() { return value; }
56
57 public SVG getIcon() { return icon; }
58
59 public Element getIconEl() { return iconEl; }
60
61 private static SVG parseSVG(final Element svg) {
62 if (svg == null) return null;
63 try {
64 return SVG.getFromString(svg.toString());
65 } catch (final SVGParseException e) {
66 return null;
67 }
68 }
69}