1   /**
2    * Distribution License:
3    * This 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 as published
5    * by the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * 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/llgpl.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   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: IntOptionsChoice.java 1575 2007-07-28 16:18:14Z dmsmith $
21   */
22  package org.crosswire.common.config;
23  
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.ResourceBundle;
27  import java.util.TreeMap;
28  
29  import org.crosswire.common.util.Logger;
30  import org.crosswire.jsword.JSOtherMsg;
31  import org.jdom.Element;
32  
33  /**
34   * A class to convert between strings and objects of a type.
35   * 
36   * @see gnu.lgpl.License for license details.<br>
37   *      The copyright to this program is held by it's authors.
38   * @author Joe Walker [joe at eireneh dot com]
39   * @author DM Smith [dmsmith555 at yahoo dot com]
40   */
41  public class MappedOptionsChoice extends AbstractReflectedChoice implements MappedChoice<Object, Object> {
42      /*
43       * (non-Javadoc)
44       * 
45       * @see org.crosswire.common.config.Choice#init(org.jdom.Element)
46       */
47      @Override
48      public void init(Element option, ResourceBundle configResources) throws StartupException {
49          assert configResources != null;
50  
51          super.init(option, configResources);
52          Element mapElement = option.getChild("map");
53          if (mapElement == null) {
54              throw new StartupException(JSOtherMsg.lookupText("Missing {0} element in config.xml", "map"));
55          }
56  
57          String name = mapElement.getAttributeValue("name");
58          Object map = ChoiceFactory.getDataMap().get(name);
59          if (map instanceof Map<?, ?>) {
60              options = (Map<?, ?>) map;
61          } else {
62              options = new TreeMap<Object, Object>();
63          }
64      }
65  
66      /*
67       * (non-Javadoc)
68       * 
69       * @see org.crosswire.common.config.MappedChoice#getOptions()
70       */
71      public Map<Object, Object> getOptions() {
72          return new TreeMap<Object, Object>(options);
73      }
74  
75      /*
76       * (non-Javadoc)
77       * 
78       * @see org.crosswire.common.config.Choice#getConvertionClass()
79       */
80      public Class<String> getConversionClass() {
81          return String.class;
82      }
83  
84      /*
85       * (non-Javadoc)
86       * 
87       * @see
88       * org.crosswire.common.config.AbstractReflectedChoice#convertToString(java
89       * .lang.Object)
90       */
91      @Override
92      public String convertToString(Object orig) {
93          return orig != null ? orig.toString() : "";
94      }
95  
96      /*
97       * (non-Javadoc)
98       * 
99       * @see
100      * org.crosswire.common.config.AbstractReflectedChoice#convertToObject(java
101      * .lang.String)
102      */
103     @Override
104     public Object convertToObject(String orig) {
105         Iterator<?> iter = options.entrySet().iterator();
106         Map.Entry<?, ?> mapEntry = null;
107         while (iter.hasNext()) {
108             mapEntry = (Map.Entry<?, ?>) iter.next();
109             if (mapEntry.getValue().toString().equals(orig) || mapEntry.getKey().toString().equals(orig)) {
110                 return mapEntry.getKey().toString();
111             }
112         }
113         logger.warn(JSOtherMsg.lookupText("Ignoring invalid option: {0}", orig));
114         return "";
115     }
116 
117     private static Logger logger = Logger.getLogger(MappedOptionsChoice.class);
118     private Map<?, ?> options;
119 }
120