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