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: 2007
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id:  $
21   */
22  package org.crosswire.jsword.index.lucene.analysis;
23  
24  import java.io.IOException;
25  
26  import org.crosswire.common.util.ClassUtil;
27  import org.crosswire.common.util.Language;
28  import org.crosswire.common.util.Logger;
29  import org.crosswire.common.util.PropertyMap;
30  import org.crosswire.common.util.ResourceUtil;
31  import org.crosswire.jsword.book.Book;
32  
33  /**
34   * A factory creating the appropriate Analyzer for natural language analysis of
35   * text for Lucene Indexing and Query Parsing. Note: [Lang] refers to CommonName
36   * for ISO639 Language Dependency: Analyzer from lucene contrib:
37   * lucene-analyzers-[version].jar, lucene-smartcn-[version].jar,
38   * lucene-snowball-[version].jar
39   * 
40   * Properties used: <Key> : <Value> Default.Analyzer : The default analyzer
41   * class [Lang].Analyzer : Appropriate Analyzer class to be used for the
42   * language of the book
43   * 
44   * @see gnu.lgpl.License for license details.<br>
45   *      The copyright to this program is held by it's authors.
46   * @author Sijo Cherian [sijocherian at yahoo dot com]
47   */
48  public final class AnalyzerFactory {
49      public AbstractBookAnalyzer createAnalyzer(Book book) {
50          AbstractBookAnalyzer newObject = null;
51          Language lang = book == null ? null : book.getLanguage();
52          if (lang != null) {
53              String aClass = getAnalyzerValue(lang);
54  
55              log.debug("Creating analyzer:" + aClass + " BookLang:" + lang);
56  
57              if (aClass != null) {
58                  try {
59                      Class<AbstractBookAnalyzer> impl = (Class<AbstractBookAnalyzer>) ClassUtil.forName(aClass);
60  
61                      newObject = impl.newInstance();
62                  } catch (ClassNotFoundException e) {
63                      log.error("Configuration error in AnalyzerFactory properties", e);
64                  } catch (IllegalAccessException e) {
65                      log.error("Configuration error in AnalyzerFactory properties", e);
66                  } catch (InstantiationException e) {
67                      log.error("Configuration error in AnalyzerFactory properties", e);
68                  }
69              }
70          }
71  
72          if (newObject == null) {
73              newObject = new SimpleLuceneAnalyzer();
74          }
75  
76          // Configure the analyzer
77          newObject.setBook(book);
78          newObject.setDoStemming(getDefaultStemmingProperty());
79          newObject.setDoStopWords(getDefaultStopWordProperty());
80          return newObject;
81      }
82  
83      public static AnalyzerFactory getInstance() {
84          return myInstance;
85      }
86  
87      private AnalyzerFactory() {
88          loadProperties();
89      }
90  
91      public String getAnalyzerValue(Language lang) {
92          String key = lang.getCode() + ".Analyzer";
93          return myProperties.get(key);
94      }
95  
96      public boolean getDefaultStemmingProperty() {
97          String key = DEFAULT_ID + ".Stemming";
98          return Boolean.valueOf(myProperties.get(key)).booleanValue();
99      }
100 
101     public boolean getDefaultStopWordProperty() {
102         String key = DEFAULT_ID + ".StopWord";
103         return Boolean.valueOf(myProperties.get(key)).booleanValue();
104     }
105 
106     private void loadProperties() {
107         try {
108             myProperties = ResourceUtil.getProperties(getClass());
109         } catch (IOException e) {
110             log.error("AnalyzerFactory property load from file failed", e);
111         }
112     }
113 
114     public static final String DEFAULT_ID = "Default";
115     private static final Logger log = Logger.getLogger(AnalyzerFactory.class);
116     private static AnalyzerFactory myInstance = new AnalyzerFactory();
117 
118     private PropertyMap myProperties;
119 
120 }
121