1 17 package org.alfresco.repo.search.impl.lucene; 18 19 import java.io.Reader ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import org.alfresco.repo.search.impl.lucene.analysis.PathAnalyser; 24 import org.alfresco.service.cmr.dictionary.DictionaryService; 25 import org.alfresco.service.cmr.dictionary.PropertyDefinition; 26 import org.alfresco.service.cmr.dictionary.DataTypeDefinition; 27 import org.alfresco.service.namespace.QName; 28 import org.apache.lucene.analysis.Analyzer; 29 import org.apache.lucene.analysis.TokenStream; 30 import org.apache.lucene.analysis.WhitespaceAnalyzer; 31 import org.apache.lucene.analysis.standard.StandardAnalyzer; 32 33 43 44 public class LuceneAnalyser extends Analyzer 45 { 46 47 private DictionaryService dictionaryService; 48 49 private Analyzer defaultAnalyser; 50 51 private Map <String , Analyzer> analysers = new HashMap <String , Analyzer>(); 52 53 60 public LuceneAnalyser(DictionaryService dictionaryService) 61 { 62 this(new StandardAnalyzer()); 63 this.dictionaryService = dictionaryService; 64 } 65 66 73 public LuceneAnalyser(Analyzer defaultAnalyser) 74 { 75 this.defaultAnalyser = defaultAnalyser; 76 } 77 78 public TokenStream tokenStream(String fieldName, Reader reader) 79 { 80 Analyzer analyser = (Analyzer) analysers.get(fieldName); 81 if (analyser == null) 82 { 83 analyser = findAnalyser(fieldName); 84 } 85 return analyser.tokenStream(fieldName, reader); 86 } 87 88 private Analyzer findAnalyser(String fieldName) 89 { 90 Analyzer analyser; 91 if (fieldName.equals("PATH")) 92 { 93 analyser = new PathAnalyser(); 94 } 95 else if (fieldName.equals("QNAME")) 96 { 97 analyser = new PathAnalyser(); 98 } 99 else if (fieldName.equals("TYPE")) 100 { 101 throw new UnsupportedOperationException ("TYPE must not be tokenised"); 102 } 103 else if (fieldName.equals("ASPECT")) 104 { 105 throw new UnsupportedOperationException ("ASPECT must not be tokenised"); 106 } 107 else if (fieldName.equals("ANCESTOR")) 108 { 109 analyser = new WhitespaceAnalyzer(); 110 } 111 else if (fieldName.equals("TEXT")) 112 { 113 DataTypeDefinition dataType = dictionaryService.getDataType(DataTypeDefinition.TEXT); 114 analyser = loadAnalyzer(dataType); 115 } 116 else if (fieldName.startsWith("@")) 117 { 118 QName propertyQName = QName.createQName(fieldName.substring(1)); 119 PropertyDefinition propertyDef = dictionaryService.getProperty(propertyQName); 120 DataTypeDefinition dataType = (propertyDef == null) ? dictionaryService.getDataType(DataTypeDefinition.TEXT) : propertyDef.getDataType(); 121 analyser = loadAnalyzer(dataType); 122 } 123 else 124 { 125 analyser = defaultAnalyser; 126 } 127 analysers.put(fieldName, analyser); 128 return analyser; 129 } 130 131 private Analyzer loadAnalyzer(DataTypeDefinition dataType) 132 { 133 String analyserClassName = dataType.getAnalyserClassName(); 134 try 135 { 136 Class <?> clazz = Class.forName(analyserClassName); 137 Analyzer analyser = (Analyzer)clazz.newInstance(); 138 return analyser; 139 } 140 catch (ClassNotFoundException e) 141 { 142 throw new RuntimeException ("Unable to load analyser for property of type " + dataType.getName() + " using " + analyserClassName); 143 } 144 catch (InstantiationException e) 145 { 146 throw new RuntimeException ("Unable to load analyser for property of type " + dataType.getName() + " using " + analyserClassName); 147 } 148 catch (IllegalAccessException e) 149 { 150 throw new RuntimeException ("Unable to load analyser for property of type " + dataType.getName() + " using " + analyserClassName); 151 } 152 } 153 154 } 155 | Popular Tags |