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 as published by
5    * 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/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   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: ConverterFactory.java 2099 2011-03-07 17:13:00Z dmsmith $
21   */
22  package org.crosswire.jsword.util;
23  
24  import java.util.Map;
25  
26  import org.crosswire.common.util.PluginUtil;
27  import org.crosswire.common.xml.Converter;
28  import org.crosswire.jsword.JSOtherMsg;
29  
30  /**
31   * A factory for Converters.
32   * 
33   * @see gnu.lgpl.License for license details.<br>
34   *      The copyright to this program is held by it's authors.
35   * @author Joe Walker [joe at eireneh dot com]
36   * @see org.crosswire.common.xml.Converter
37   */
38  public final class ConverterFactory {
39      /**
40       * Prevent instantiation
41       */
42      private ConverterFactory() {
43      }
44  
45      /**
46       * Generate a converter for the current converter name
47       */
48      public static Converter getConverter() {
49          try {
50              Class<Converter> clazz = PluginUtil.getImplementorsMap(Converter.class).get(name);
51              assert clazz != null : JSOtherMsg.lookupText("No converter called: {0}", name);
52              return clazz.newInstance();
53          } catch (InstantiationException e) {
54              assert false : e;
55          } catch (IllegalAccessException e) {
56              assert false : e;
57          }
58          return null;
59      }
60  
61      /**
62       * Get a map of the known converters, by looking up the answers in Project
63       */
64      public static Map<String, Class<Converter>> getKnownConverters() {
65          return PluginUtil.getImplementorsMap(Converter.class);
66      }
67  
68      /**
69       * For config to set the currently preferred converter implementation
70       */
71      public static void setCurrentConverterName(String name) {
72          ConverterFactory.name = name;
73      }
74  
75      /**
76       * For config to read the currently preferred converter implementation
77       */
78      public static String getCurrentConverterName() {
79          return name;
80      }
81  
82      /**
83       * Current default converter implementation
84       */
85      private static String name = "Configurable";
86  }
87