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