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.HashMap;
23  import java.util.Map;
24  import java.util.ResourceBundle;
25  
26  import org.crosswire.common.util.ClassUtil;
27  import org.crosswire.common.util.PluginUtil;
28  import org.jdom2.Element;
29  
30  /**
31   * Factory for the well known Choices.
32   * 
33   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
34   * @author Joe Walker
35   */
36  public final class ChoiceFactory {
37      /**
38       * Prevent instantiation
39       */
40      private ChoiceFactory() {
41      }
42  
43      /**
44       * Get a ChoiceFactory by element.
45       * 
46       * @param option
47       *            The element to check
48       * @param configResources the resource bundle holding the option
49       * @return One of the ChoiceTypes.
50       * @throws  InstantiationException
51       *               if this {@code data} represents an abstract class,
52       *               an interface, an array class, a primitive type, or void;
53       *               or if the class has no nullary constructor;
54       *               or if the instantiation fails for some other reason.
55       * @throws ClassNotFoundException if the class is not found
56       * @throws IllegalAccessException  if the class or its nullary
57       *               constructor is not accessible.
58       * @throws StartupException if startup is not possible
59       */
60      public static Choice getChoice(Element option, ResourceBundle configResources) throws ClassNotFoundException, IllegalAccessException,
61              InstantiationException, StartupException
62      {
63          Class<Choice> clazz = null;
64  
65          String type = option.getAttributeValue("type");
66          if ("custom".equals(type)) {
67              String clazzstr = option.getAttributeValue("class");
68              clazz = (Class<Choice>) ClassUtil.forName(clazzstr);
69          } else {
70              clazz = map.get(type);
71          }
72  
73          Choice choice = clazz.newInstance();
74          choice.init(option, configResources);
75          return choice;
76      }
77  
78      /**
79       * Method getDataMap.
80       * 
81       * @return the map data
82       */
83      public static Map<String, Object> getDataMap() {
84          return datamap;
85      }
86  
87      /**
88       * Storage of various registered objects
89       */
90      private static Map<String, Object> datamap = new HashMap<String, Object>();
91  
92      /**
93       * Store of the known ChoiceTypes
94       */
95      private static Map<String, Class<Choice>> map;
96  
97      /**
98       * Setup the map of Choices
99       */
100     static {
101         map = PluginUtil.getImplementorsMap(Choice.class);
102     }
103 }
104