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
14 public static List<Option> forField(Element field) {
15 List<Option> options = new ArrayList<>();
16 for (Element el : field.getChildren()) {
17 if (el.getNamespace() == null || !el.getNamespace().equals("jabber:x:data")) continue;
18 if (!el.getName().equals("option")) continue;
19 options.add(new Option(el));
20 }
21 return options;
22 }
23
24 public Option(final Element option) {
25 this(
26 option.findChildContent("value", "jabber:x:data"),
27 option.getAttribute("label"),
28 parseSVG(option.findChild("svg", "http://www.w3.org/2000/svg"))
29 );
30 }
31
32 public Option(final String value, final String label) {
33 this(value, label, null);
34 }
35
36 public Option(final String value, final String label, final SVG icon) {
37 this.value = value;
38 this.label = label == null ? value : label;
39 this.icon = icon;
40 }
41
42 public boolean equals(Object o) {
43 if (!(o instanceof Option)) return false;
44
45 if (value == ((Option) o).value) return true;
46 if (value == null || ((Option) o).value == null) return false;
47 return value.equals(((Option) o).value);
48 }
49
50 public String toString() { return label; }
51
52 public String getValue() { return value; }
53
54 public SVG getIcon() { return icon; }
55
56 private static SVG parseSVG(final Element svg) {
57 if (svg == null) return null;
58 try {
59 return SVG.getFromString(svg.toString());
60 } catch (final SVGParseException e) {
61 return null;
62 }
63 }
64}