| ChoiceFactory.java |
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: ChoiceFactory.java 2090 2011-03-07 04:13:05Z dmsmith $
21 */
22 package org.crosswire.common.config;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.ResourceBundle;
27
28 import org.crosswire.common.util.ClassUtil;
29 import org.crosswire.common.util.PluginUtil;
30 import org.jdom.Element;
31
32 /**
33 * Factory for the well known Choices.
34 *
35 * @see gnu.lgpl.License for license details.<br>
36 * The copyright to this program is held by it's authors.
37 * @author Joe Walker [joe at eireneh dot com]
38 */
39 public final class ChoiceFactory {
40 /**
41 * Prevent instantiation
42 */
43 private ChoiceFactory() {
44 }
45
46 /**
47 * Get a ChoiceFactory by element.
48 *
49 * @param option
50 * The element to check
51 * @return One of the ChoiceTypes.
52 */
53 public static Choice getChoice(Element option, ResourceBundle configResources) throws ClassNotFoundException, IllegalAccessException,
54 InstantiationException, StartupException
55 {
56 Class<Choice> clazz = null;
57
58 String type = option.getAttributeValue("type");
59 if ("custom".equals(type)) {
60 String clazzstr = option.getAttributeValue("class");
61 clazz = (Class<Choice>) ClassUtil.forName(clazzstr);
62 } else {
63 clazz = map.get(type);
64 }
65
66 Choice choice = clazz.newInstance();
67 choice.init(option, configResources);
68 return choice;
69 }
70
71 /**
72 * Method getDataMap.
73 */
74 public static Map<String, Object> getDataMap() {
75 return datamap;
76 }
77
78 /**
79 * Storage of various registered objects
80 */
81 private static Map<String, Object> datamap = new HashMap<String, Object>();
82
83 /**
84 * Store of the known ChoiceTypes
85 */
86 private static Map<String, Class<Choice>> map;
87
88 /**
89 * Setup the map of Choices
90 */
91 static {
92 map = PluginUtil.getImplementorsMap(Choice.class);
93 }
94 }
95