Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MappedOptionsChoice |
|
| 2.6;2.6 |
1 | /** | |
2 | * Distribution License: | |
3 | * JSword is free software; you can redistribute it and/or modify it under | |
4 | * the terms of the GNU Lesser General Public License, version 2.1 or later | |
5 | * as published by the Free Software Foundation. This program is distributed | |
6 | * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | |
7 | * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
8 | * See the GNU Lesser General Public License for more details. | |
9 | * | |
10 | * The License is available on the internet at: | |
11 | * http://www.gnu.org/copyleft/lgpl.html | |
12 | * or by writing to: | |
13 | * Free Software Foundation, Inc. | |
14 | * 59 Temple Place - Suite 330 | |
15 | * Boston, MA 02111-1307, USA | |
16 | * | |
17 | * © CrossWire Bible Society, 2005 - 2016 | |
18 | * | |
19 | */ | |
20 | package org.crosswire.common.config; | |
21 | ||
22 | import java.util.Iterator; | |
23 | import java.util.Map; | |
24 | import java.util.ResourceBundle; | |
25 | import java.util.TreeMap; | |
26 | ||
27 | import org.crosswire.jsword.JSOtherMsg; | |
28 | import org.jdom2.Element; | |
29 | import org.slf4j.Logger; | |
30 | import org.slf4j.LoggerFactory; | |
31 | ||
32 | /** | |
33 | * A class to convert between strings and objects of a type. | |
34 | * | |
35 | * @see gnu.lgpl.License The GNU Lesser General Public License for details. | |
36 | * @author Joe Walker | |
37 | * @author DM Smith | |
38 | */ | |
39 | 0 | public class MappedOptionsChoice extends AbstractReflectedChoice implements MappedChoice<Object, Object> { |
40 | /* | |
41 | * (non-Javadoc) | |
42 | * | |
43 | * @see org.crosswire.common.config.Choice#init(org.jdom2.Element) | |
44 | */ | |
45 | @Override | |
46 | public void init(Element option, ResourceBundle configResources) throws StartupException { | |
47 | 0 | assert configResources != null; |
48 | ||
49 | 0 | super.init(option, configResources); |
50 | 0 | Element mapElement = option.getChild("map"); |
51 | 0 | if (mapElement == null) { |
52 | 0 | throw new StartupException(JSOtherMsg.lookupText("Missing {0} element in config.xml", "map")); |
53 | } | |
54 | ||
55 | 0 | String name = mapElement.getAttributeValue("name"); |
56 | 0 | Object map = ChoiceFactory.getDataMap().get(name); |
57 | 0 | if (map instanceof Map<?, ?>) { |
58 | 0 | options = (Map<?, ?>) map; |
59 | } else { | |
60 | 0 | options = new TreeMap<Object, Object>(); |
61 | } | |
62 | 0 | } |
63 | ||
64 | /* | |
65 | * (non-Javadoc) | |
66 | * | |
67 | * @see org.crosswire.common.config.MappedChoice#getOptions() | |
68 | */ | |
69 | public Map<Object, Object> getOptions() { | |
70 | 0 | return new TreeMap<Object, Object>(options); |
71 | } | |
72 | ||
73 | /* | |
74 | * (non-Javadoc) | |
75 | * | |
76 | * @see org.crosswire.common.config.Choice#getConvertionClass() | |
77 | */ | |
78 | public Class<String> getConversionClass() { | |
79 | 0 | return String.class; |
80 | } | |
81 | ||
82 | /* | |
83 | * (non-Javadoc) | |
84 | * | |
85 | * @see | |
86 | * org.crosswire.common.config.AbstractReflectedChoice#convertToString(java | |
87 | * .lang.Object) | |
88 | */ | |
89 | @Override | |
90 | public String convertToString(Object orig) { | |
91 | 0 | return orig != null ? orig.toString() : ""; |
92 | } | |
93 | ||
94 | /* | |
95 | * (non-Javadoc) | |
96 | * | |
97 | * @see | |
98 | * org.crosswire.common.config.AbstractReflectedChoice#convertToObject(java | |
99 | * .lang.String) | |
100 | */ | |
101 | @Override | |
102 | public Object convertToObject(String orig) { | |
103 | 0 | Iterator<?> iter = options.entrySet().iterator(); |
104 | 0 | Map.Entry<?, ?> mapEntry = null; |
105 | 0 | while (iter.hasNext()) { |
106 | 0 | mapEntry = (Map.Entry<?, ?>) iter.next(); |
107 | 0 | if (mapEntry.getValue().toString().equals(orig) || mapEntry.getKey().toString().equals(orig)) { |
108 | 0 | return mapEntry.getKey().toString(); |
109 | } | |
110 | } | |
111 | 0 | log.warn(JSOtherMsg.lookupText("Ignoring invalid option: {0}", orig)); |
112 | 0 | return ""; |
113 | } | |
114 | ||
115 | private Map<?, ?> options; | |
116 | ||
117 | /** | |
118 | * The log stream | |
119 | */ | |
120 | 0 | private static Logger log = LoggerFactory.getLogger(MappedOptionsChoice.class); |
121 | } |