com.jaeksoft.searchlib.analysis.AnalyzerList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opensearchserver Show documentation
Show all versions of opensearchserver Show documentation
OpenSearchServer is a powerful, enterprise-class, search engine program. Using the web user interface, the crawlers (web, file, database, ...) and the REST/RESTFul API you will be able to integrate quickly and easily advanced full-text search capabilities in your application. OpenSearchServer runs on Windows and Linux/Unix/BSD.
The newest version!
/**
* License Agreement for OpenSearchServer
*
* Copyright (C) 2008-2010 Emmanuel Keller / Jaeksoft
*
* http://www.open-search-server.com
*
* This file is part of OpenSearchServer.
*
* OpenSearchServer is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSearchServer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSearchServer.
* If not, see .
**/
package com.jaeksoft.searchlib.analysis;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.xml.xpath.XPathExpressionException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.jaeksoft.searchlib.SearchLibException;
import com.jaeksoft.searchlib.config.Config;
import com.jaeksoft.searchlib.util.ReadWriteLock;
import com.jaeksoft.searchlib.util.XPathParser;
import com.jaeksoft.searchlib.util.XmlWriter;
public class AnalyzerList {
private Map> nameListMap;
private Map nameLangMap;
private final ReadWriteLock rwl = new ReadWriteLock();
public AnalyzerList() {
nameLangMap = new TreeMap();
nameListMap = new TreeMap>();
}
final private static String getAnalyzerLangKey(String analyzerName, LanguageEnum lang) {
StringBuilder sb = new StringBuilder(analyzerName);
sb.append('_');
sb.append(lang.getCode());
return sb.toString();
}
final private static String getAnalyzerLangKey(Analyzer analyzer) {
return getAnalyzerLangKey(analyzer.getName(), analyzer.getLang());
}
/**
* Update or create an analyzer
*
* @param analyzer
* the analyzer to add
* @return true if it has been created, false if it was an update
* @throws SearchLibException
* inherited error
* @throws ClassNotFoundException
* inherited error
*/
public boolean addOrUpdate(Analyzer analyzer) throws ClassNotFoundException, SearchLibException {
rwl.w.lock();
try {
String langKey = getAnalyzerLangKey(analyzer);
Analyzer oldAnalyzer = nameLangMap.get(langKey);
if (oldAnalyzer != null) {
oldAnalyzer.copyFrom(analyzer);
return false;
}
List alist = nameListMap.get(analyzer.getName());
if (alist == null) {
alist = new ArrayList();
nameListMap.put(analyzer.getName(), alist);
}
alist.add(analyzer);
nameLangMap.put(langKey, analyzer);
return true;
} finally {
rwl.w.unlock();
}
}
public void remove(Analyzer analyzer) {
rwl.w.lock();
try {
List alist = nameListMap.get(analyzer.getName());
if (alist != null) {
alist.remove(analyzer);
if (alist.size() == 0)
nameListMap.remove(analyzer.getName());
}
nameLangMap.remove(getAnalyzerLangKey(analyzer));
} finally {
rwl.w.unlock();
}
}
/**
* Recompile the analyzers
*/
public void recompile() {
rwl.r.lock();
try {
for (List alist : nameListMap.values())
for (Analyzer a : alist)
a.recompile();
} finally {
rwl.r.unlock();
}
}
public List getNameSet() {
return (List) populateNameCollection(null);
}
public Collection populateNameCollection(Collection nameSet) {
rwl.r.lock();
try {
if (nameSet == null)
nameSet = new ArrayList();
nameSet.addAll(nameListMap.keySet());
return nameSet;
} finally {
rwl.r.unlock();
}
}
public List get(String name) {
rwl.r.lock();
try {
return nameListMap.get(name);
} finally {
rwl.r.unlock();
}
}
public Analyzer get(String name, LanguageEnum lang) {
rwl.r.lock();
try {
if (lang == null)
lang = LanguageEnum.UNDEFINED;
return nameLangMap.get(getAnalyzerLangKey(name, lang));
} finally {
rwl.r.unlock();
}
}
public static AnalyzerList fromXmlConfig(Config config, XPathParser xpp, Node parentNode)
throws XPathExpressionException, SearchLibException, DOMException, ClassNotFoundException {
AnalyzerList analyzers = new AnalyzerList();
if (parentNode == null)
return analyzers;
NodeList nodes = xpp.getNodeList(parentNode, "analyzer");
if (nodes == null)
return analyzers;
for (int i = 0; i < nodes.getLength(); i++)
analyzers.addOrUpdate(Analyzer.fromXmlConfig(config, xpp, nodes.item(i)));
return analyzers;
}
public void writeXmlConfig(XmlWriter writer) throws SAXException {
rwl.r.lock();
try {
if (nameListMap.size() == 0)
return;
writer.startElement("analyzers");
for (Analyzer analyzer : nameLangMap.values())
analyzer.writeXmlConfig(writer);
writer.endElement();
} finally {
rwl.r.unlock();
}
}
}