1
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
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 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