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.jsword.index.search;
21  
22  import java.io.IOException;
23  
24  import org.crosswire.common.util.PluginUtil;
25  import org.crosswire.jsword.book.Book;
26  import org.crosswire.jsword.book.BookException;
27  import org.crosswire.jsword.index.Index;
28  import org.crosswire.jsword.index.IndexManager;
29  import org.crosswire.jsword.index.IndexManagerFactory;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * Factory method for creating a new Searcher.
35   * 
36   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
37   * @author Joe Walker
38   */
39  public final class SearcherFactory {
40      /**
41       * Prevent instantiation
42       */
43      private SearcherFactory() {
44      }
45  
46      /**
47       * Create a new Searcher.
48       * 
49       * @param book the book
50       * @return the searcher
51       * @throws InstantiationException 
52       */
53      public static Searcher createSearcher(Book book) throws InstantiationException {
54          try {
55              IndexManager imanager = IndexManagerFactory.getIndexManager();
56              Index index = imanager.getIndex(book);
57  
58              Searcher parser = PluginUtil.getImplementation(Searcher.class);
59              parser.init(index);
60  
61              return parser;
62          } catch (IOException e) {
63              log.error("createSearcher failed", e);
64              throw new InstantiationException();
65          } catch (BookException e) {
66              log.error("createSearcher failed", e);
67              throw new InstantiationException();
68          } catch (ClassCastException e) {
69              log.error("createSearcher failed", e);
70              throw new InstantiationException();
71          } catch (ClassNotFoundException e) {
72              log.error("createSearcher failed", e);
73              throw new InstantiationException();
74          } catch (IllegalAccessException e) {
75              log.error("createSearcher failed", e);
76              throw new InstantiationException();
77          }
78      }
79  
80      /**
81       * The log stream
82       */
83      private static final Logger log = LoggerFactory.getLogger(SearcherFactory.class);
84  }
85