| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 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 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 0 | public class MappedOptionsChoice extends AbstractReflectedChoice implements MappedChoice<Object, Object> { |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 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 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
public Map<Object, Object> getOptions() { |
| 70 | 0 | return new TreeMap<Object, Object>(options); |
| 71 | |
} |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public Class<String> getConversionClass() { |
| 79 | 0 | return String.class; |
| 80 | |
} |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
@Override |
| 90 | |
public String convertToString(Object orig) { |
| 91 | 0 | return orig != null ? orig.toString() : ""; |
| 92 | |
} |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 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 | |
|
| 119 | |
|
| 120 | 0 | private static Logger log = LoggerFactory.getLogger(MappedOptionsChoice.class); |
| 121 | |
} |