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