| FilterFactory.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 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: FilterFactory.java 2110 2011-03-08 13:55:32Z dmsmith $
21 */
22 package org.crosswire.jsword.book.filter;
23
24 import java.util.HashMap;
25 import java.util.Locale;
26 import java.util.Map;
27
28 import org.crosswire.common.util.Logger;
29 import org.crosswire.common.util.PluginUtil;
30
31 /**
32 * A simple container for all the known filters.
33 *
34 * @see gnu.lgpl.License for license details.<br>
35 * The copyright to this program is held by it's authors.
36 * @author Joe Walker [joe at eireneh dot com]
37 */
38 public final class FilterFactory {
39 /**
40 * Prevent instantiation
41 */
42 private FilterFactory() {
43 }
44
45 /**
46 * The log stream
47 */
48 private static final Logger log = Logger.getLogger(FilterFactory.class);
49
50 /**
51 * The lookup table of filters
52 */
53 private static Map<String, Filter> filters = new HashMap<String, Filter>();
54
55 /**
56 * The lookup table of filters
57 */
58 private static volatile Filter deft;
59
60 /**
61 * Populate the lookup table of filters and the default from the properties
62 * file.
63 */
64 static {
65 Map<String, Class<Filter>> map = PluginUtil.getImplementorsMap(Filter.class);
66
67 // the default value
68 try {
69 Class<Filter> cdeft = map.remove("default");
70 deft = cdeft.newInstance();
71 } catch (InstantiationException e) {
72 log.fatal("Failed to get default filter, will attempt to use first", e);
73 } catch (IllegalAccessException e) {
74 log.fatal("Failed to get default filter, will attempt to use first", e);
75 }
76
77 // the lookup table
78 Filter instance = null;
79 for (Map.Entry<String, Class<Filter>> entry : map.entrySet()) {
80 try {
81 Class<Filter> clazz = entry.getValue();
82 instance = clazz.newInstance();
83 addFilter(entry.getKey(), instance);
84 } catch (InstantiationException ex) {
85 log.error("Failed to add filter", ex);
86 } catch (IllegalAccessException ex) {
87 log.error("Failed to add filter", ex);
88 }
89 }
90
91 // if the default didn't work then make a stab at an answer
92 if (deft == null) {
93 deft = instance;
94 }
95 }
96
97 /**
98 * Find a filter given a lookup string. If lookup is null or the filter is
99 * not found then the default filter will be used.
100 */
101 public static Filter getFilter(String lookup) {
102 Filter reply = filters.get(lookup.toLowerCase(Locale.ENGLISH));
103
104 if (reply == null) {
105 reply = deft;
106 }
107
108 return reply.clone();
109 }
110
111 /**
112 * Find a filter given a lookup string
113 */
114 public static Filter getDefaultFilter() {
115 return deft.clone();
116 }
117
118 /**
119 * Add to our list of known filters
120 */
121 public static void addFilter(String name, Filter instance) {
122 filters.put(name.toLowerCase(Locale.ENGLISH), instance);
123 }
124 }
125