| PathChoice.java |
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 java.io.File;
23
24 import org.crosswire.common.util.StringUtil;
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 PathChoice extends AbstractReflectedChoice {
33 /*
34 * (non-Javadoc)
35 *
36 * @see org.crosswire.common.config.Choice#getConvertionClass()
37 */
38 public Class<File[]> getConversionClass() {
39 return File[].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 File[] paths = (File[]) orig;
52 String[] names = new String[paths.length];
53 for (int i = 0; i < paths.length; i++) {
54 names[i] = paths[i].getAbsolutePath();
55 }
56
57 return StringUtil.join(names, File.pathSeparator);
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 String[] names = StringUtil.split(orig, File.pathSeparator);
70 File[] paths = new File[names.length];
71 for (int i = 0; i < names.length; i++) {
72 paths[i] = new File(names[i]);
73 }
74
75 return paths;
76 }
77 }
78