| ClassChoice.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: ClassChoice.java 2050 2010-12-09 15:31:45Z dmsmith $
21 */
22 package org.crosswire.common.config;
23
24 import org.crosswire.common.util.ClassUtil;
25 import org.crosswire.common.util.Logger;
26
27 /**
28 * A class to convert between strings and objects of a type.
29 *
30 * @see gnu.lgpl.License for license details.<br>
31 * The copyright to this program is held by it's authors.
32 * @author Joe Walker [joe at eireneh dot com]
33 */
34 public class ClassChoice extends AbstractReflectedChoice {
35 /*
36 * (non-Javadoc)
37 *
38 * @see org.crosswire.common.config.Choice#getConvertionClass()
39 */
40 public Class<?> getConversionClass() {
41 return Class.class;
42 }
43
44 /*
45 * (non-Javadoc)
46 *
47 * @see
48 * org.crosswire.common.config.AbstractReflectedChoice#convertToString(java
49 * .lang.Object)
50 */
51 @Override
52 public String convertToString(Object orig) {
53 if (orig == null) {
54 return null;
55 }
56
57 return ((Class<?>) orig).getName();
58 }
59
60 /*
61 * (non-Javadoc)
62 *
63 * @see
64 * org.crosswire.common.config.AbstractReflectedChoice#convertToObject(java
65 * .lang.String)
66 */
67 @Override
68 public Object convertToObject(String orig) {
69 try {
70 return ClassUtil.forName(orig);
71 } catch (ClassNotFoundException ex) {
72 log.warn("Class not found: " + orig, ex);
73 return null;
74 }
75 }
76
77 /**
78 * The log stream
79 */
80 private static final Logger log = Logger.getLogger(ClassChoice.class);
81 }
82