#include "Xpclucene.h" #include "nsString.h" #include "nsCOMPtr.h" #include "nsLuceneFactory.h" #include "nsLuceneIndexReader.h" #include "nsLuceneIndexWriter.h" #include "nsLuceneIndexSearcher.h" #include "nsLuceneAnalyzer.h" #include "nsLuceneQuery.h" #include "nsLuceneStringConverter.h" #include "nsLuceneField.h" #include "nsLuceneDocument.h" #include "nsLuceneHighlighter.h" #include "nsILuceneHits.h" #include "nsLuceneHits.h" #include "nsIFile.h" #include "nsILocalFile.h" #include "nsNetUtil.h" #include "CLucene/CLConfig.h" #include "CLucene.h" NS_IMPL_ISUPPORTS1(nsLuceneFactory, nsILuceneFactory) nsLuceneFactory::nsLuceneFactory() { /* member initializers and constructor code */ } nsLuceneFactory::~nsLuceneFactory() { /* destructor code */ } NS_IMETHODIMP nsLuceneFactory::GetIndexReader(nsIFile* index, nsILuceneIndexReader** _retval) { LUCENETRYCATCH_START nsresult rv; nsCAutoString path; rv = index->GetNativePath(path); NS_ENSURE_SUCCESS(rv, rv); lucene::index::IndexReader* r = lucene::index::IndexReader::open(path.get()); nsCOMPtr indexReader = new nsLuceneIndexReader(r); NS_ENSURE_TRUE(indexReader, NS_ERROR_OUT_OF_MEMORY); *_retval = indexReader; NS_ADDREF(*_retval); return NS_OK; LUCENETRYCATCH_END } NS_IMETHODIMP nsLuceneFactory::GetAnalyzer(PRUint32 analyzerType, nsILuceneAnalyzer** _retval) { LUCENETRYCATCH_START // TODO: better way to choose which analyzer? lucene::analysis::standard::StandardAnalyzer* a = new lucene::analysis::standard::StandardAnalyzer(); nsCOMPtr analyzer = new nsLuceneAnalyzer(a); NS_ENSURE_TRUE(analyzer, NS_ERROR_OUT_OF_MEMORY); *_retval = analyzer; NS_ADDREF(*_retval); return NS_OK; LUCENETRYCATCH_END } NS_IMETHODIMP nsLuceneFactory::ParseQuery(const nsAString& query, const nsAString& field, nsILuceneAnalyzer* analyzer, nsILuceneQuery** _retval) { LUCENETRYCATCH_START LOG(("ParseQuery called with %s %s\n", NS_ConvertUTF16toUTF8(query).get(), NS_ConvertUTF16toUTF8(field).get())); lucene::analysis::Analyzer* a = ((nsLuceneAnalyzer *) analyzer)->getAnalyzer(); nsLuceneStringConverter query32(query); nsLuceneStringConverter field32(field); lucene::search::Query* q = lucene::queryParser::QueryParser::parse((wchar_t *) query32.get(), (wchar_t *) field32.get(), a); nsCOMPtr wquery = new nsLuceneQuery(q); NS_ENSURE_TRUE(wquery, NS_ERROR_OUT_OF_MEMORY); *_retval = wquery; NS_ADDREF(*_retval); return NS_OK; LUCENETRYCATCH_END } NS_IMETHODIMP nsLuceneFactory::GetIndexSearcher(nsILuceneIndexReader* indexReader, nsILuceneIndexSearcher** _retval) { LUCENETRYCATCH_START lucene::index::IndexReader *ir = ((nsLuceneIndexReader*) indexReader)->getIndexReader(); lucene::search::IndexSearcher *is = new lucene::search::IndexSearcher(ir); nsCOMPtr indexSearcher = new nsLuceneIndexSearcher(is, indexReader); NS_ENSURE_TRUE(indexSearcher, NS_ERROR_OUT_OF_MEMORY); *_retval = indexSearcher; NS_ADDREF(*_retval); return NS_OK; LUCENETRYCATCH_END } NS_IMETHODIMP nsLuceneFactory::GetIndexWriter(nsIFile* index, nsILuceneAnalyzer* analyzer, PRBool create, PRBool closeDir, nsILuceneIndexWriter** _retval) { LUCENETRYCATCH_START nsresult rv; nsCAutoString path; rv = index->GetNativePath(path); NS_ENSURE_SUCCESS(rv, rv); lucene::analysis::Analyzer* a = ((nsLuceneAnalyzer *) analyzer)->getAnalyzer(); lucene::index::IndexWriter *iw = new lucene::index::IndexWriter(path.get(), a, create, closeDir); nsCOMPtr liw = new nsLuceneIndexWriter(iw); NS_ENSURE_TRUE(liw, NS_ERROR_OUT_OF_MEMORY); *_retval = liw; NS_ADDREF(*_retval); return NS_OK; LUCENETRYCATCH_END } NS_IMETHODIMP nsLuceneFactory::CreateTextStringField(const nsAString& name, const nsAString& value, PRBool storeTermVector, nsILuceneField **_retval) { LUCENETRYCATCH_START nsLuceneStringConverter name32(name); nsLuceneStringConverter value32(value); lucene::document::Field *f = lucene::document::Field::Text((wchar_t *) name32.get(), (wchar_t *) value32.get(), storeTermVector); nsCOMPtr lf = new nsLuceneField(f); NS_ENSURE_TRUE(lf, NS_ERROR_OUT_OF_MEMORY); *_retval = lf; NS_ADDREF(*_retval); return NS_OK; LUCENETRYCATCH_END } NS_IMETHODIMP nsLuceneFactory::CreateTextKeywordField(const nsAString& name, const nsAString& value, nsILuceneField **_retval) { LUCENETRYCATCH_START nsLuceneStringConverter name32(name); nsLuceneStringConverter value32(value); lucene::document::Field *f = lucene::document::Field::Keyword((wchar_t *) name32.get(), (wchar_t *) value32.get()); nsCOMPtr lf = new nsLuceneField(f); NS_ENSURE_TRUE(lf, NS_ERROR_OUT_OF_MEMORY); *_retval = lf; NS_ADDREF(*_retval); return NS_OK; LUCENETRYCATCH_END } NS_IMETHODIMP nsLuceneFactory::CreateDocument(nsILuceneDocument **_retval) { LUCENETRYCATCH_START lucene::document::Document *d = new lucene::document::Document(); nsCOMPtr ld = new nsLuceneDocument(d, true); NS_ENSURE_TRUE(ld, NS_ERROR_OUT_OF_MEMORY); *_retval = ld; NS_ADDREF(*_retval); return NS_OK; LUCENETRYCATCH_END } NS_IMETHODIMP nsLuceneFactory::CreateHighlighter(nsILuceneQuery* query, const nsAString& fragmentSeparator, PRInt16 maxNumFragmentsRequired, const nsAString& preTag, const nsAString& postTag, nsILuceneHighlighter **_retval) { LUCENETRYCATCH_START nsCOMPtr highlighter = new nsLuceneHighlighter(query, fragmentSeparator, maxNumFragmentsRequired, preTag, postTag); NS_ENSURE_TRUE(highlighter, NS_ERROR_OUT_OF_MEMORY); *_retval = highlighter; NS_ADDREF(*_retval); return NS_OK; LUCENETRYCATCH_END } NS_IMETHODIMP nsLuceneFactory::CreateSingleTermQuery(const nsAString& name, const nsAString& value, nsILuceneQuery** _retval) { LUCENETRYCATCH_START nsLuceneStringConverter name32(name); nsLuceneStringConverter value32(value); lucene::index::Term* t = new lucene::index::Term((wchar_t *) name32.get(), (wchar_t *) value32.get()); lucene::search::Query* q = new lucene::search::TermQuery(t); nsCOMPtr query = new nsLuceneQuery(q); NS_ENSURE_TRUE(query, NS_ERROR_OUT_OF_MEMORY); *_retval = query; NS_ADDREF(*_retval); return NS_OK; LUCENETRYCATCH_END } nsresult nsLuceneFactory::TranslateError(CLuceneError* e) { LOG(("CLuceneError %d: %s", e->number(), e->what())); if(e->number() == CL_ERR_UNKNOWN) { return NS_ERROR_UNEXPECTED; } else { return NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_GENERAL, e->number()); } }