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 org.crosswire.common.util.ClassUtil;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  /**
27   * A class to convert between strings and objects of a type.
28   * 
29   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
30   * @author Joe Walker
31   */
32  public class ClassChoice extends AbstractReflectedChoice {
33      /*
34       * (non-Javadoc)
35       * 
36       * @see org.crosswire.common.config.Choice#getConvertionClass()
37       */
38      public Class<?> getConversionClass() {
39          return Class.class;
40      }
41  
42      /*
43       * (non-Javadoc)
44       * 
45       * @see
46       * org.crosswire.common.config.AbstractReflectedChoice#convertToString(java
47       * .lang.Object)
48       */
49      @Override
50      public String convertToString(Object orig) {
51          if (orig == null) {
52              return null;
53          }
54  
55          return ((Class<?>) orig).getName();
56      }
57  
58      /*
59       * (non-Javadoc)
60       * 
61       * @see
62       * org.crosswire.common.config.AbstractReflectedChoice#convertToObject(java
63       * .lang.String)
64       */
65      @Override
66      public Object convertToObject(String orig) {
67          try {
68              return ClassUtil.forName(orig);
69          } catch (ClassNotFoundException ex) {
70              log.warn("Class not found: {}", orig, ex);
71              return null;
72          }
73      }
74  
75      /**
76       * The log stream
77       */
78      private static final Logger log = LoggerFactory.getLogger(ClassChoice.class);
79  }
80