tools/assistant/lib/fulltextsearch/qdocument.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team.
       
     4 ** All rights reserved.
       
     5 **
       
     6 ** Portion Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     7 ** All rights reserved.
       
     8 **
       
     9 ** This file may be used under the terms of the GNU Lesser General Public
       
    10 ** License version 2.1 as published by the Free Software Foundation and
       
    11 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    12 ** Please review the following information to ensure the GNU Lesser General
       
    13 ** Public License version 2.1 requirements will be met:
       
    14 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    15 **
       
    16 ****************************************************************************/
       
    17 
       
    18 #include "qdocument_p.h"
       
    19 #include "qreader_p.h"
       
    20 #include "qclucene_global_p.h"
       
    21 
       
    22 #include <CLucene.h>
       
    23 #include <CLucene/util/Reader.h>
       
    24 #include <CLucene/document/Document.h>
       
    25 
       
    26 QT_BEGIN_NAMESPACE
       
    27 
       
    28 QCLuceneDocumentPrivate::QCLuceneDocumentPrivate()
       
    29     : QSharedData()
       
    30 {
       
    31     document = 0;
       
    32     deleteCLuceneDocument = true;
       
    33 }
       
    34 
       
    35 QCLuceneDocumentPrivate::QCLuceneDocumentPrivate(const QCLuceneDocumentPrivate &other)
       
    36     : QSharedData()
       
    37 {
       
    38     document = _CL_POINTER(other.document);
       
    39     deleteCLuceneDocument = other.deleteCLuceneDocument;
       
    40 }
       
    41 
       
    42 QCLuceneDocumentPrivate::~QCLuceneDocumentPrivate()
       
    43 {
       
    44     if (deleteCLuceneDocument)
       
    45         _CLDECDELETE(document);
       
    46 }
       
    47 
       
    48 
       
    49 QCLuceneDocument::QCLuceneDocument()
       
    50     : d(new QCLuceneDocumentPrivate())
       
    51 {
       
    52     // nothing todo
       
    53     d->document = new lucene::document::Document();
       
    54 }
       
    55 
       
    56 QCLuceneDocument::~QCLuceneDocument()
       
    57 {
       
    58     qDeleteAll(fieldList);
       
    59     fieldList.clear();
       
    60 }
       
    61 
       
    62 void QCLuceneDocument::add(QCLuceneField *field)
       
    63 {
       
    64     field->d->deleteCLuceneField = false;
       
    65     d->document->add(*field->d->field);
       
    66     fieldList.append(field);
       
    67 }
       
    68 
       
    69 QCLuceneField* QCLuceneDocument::getField(const QString &name) const
       
    70 {
       
    71     QCLuceneField* field = 0;
       
    72     foreach (field, fieldList) {
       
    73         if (field->name() == name && field->d->field != 0)
       
    74             return field;
       
    75     }
       
    76 
       
    77     field = 0;
       
    78     TCHAR *fieldName = QStringToTChar(name);
       
    79     lucene::document::Field *f = d->document->getField(fieldName);
       
    80     if (f) {
       
    81         field = new QCLuceneField();
       
    82         field->d->field = f;
       
    83         fieldList.append(field);
       
    84         field->d->deleteCLuceneField = false;
       
    85 
       
    86         lucene::util::Reader *r = f->readerValue();
       
    87         if (r) {
       
    88             field->reader->d->reader = r;
       
    89             field->reader->d->deleteCLuceneReader = false;
       
    90         }
       
    91     }
       
    92     delete [] fieldName;
       
    93     
       
    94     return field;
       
    95 }
       
    96 
       
    97 QString QCLuceneDocument::get(const QString &name) const
       
    98 {
       
    99     QCLuceneField* field = getField(name);
       
   100     if (field)
       
   101         return field->stringValue();
       
   102     
       
   103     return QString();
       
   104 }
       
   105 
       
   106 QString QCLuceneDocument::toString() const
       
   107 {
       
   108     return TCharToQString(d->document->toString());
       
   109 }
       
   110 
       
   111 void QCLuceneDocument::setBoost(qreal boost)
       
   112 {
       
   113     d->document->setBoost(qreal(boost));
       
   114 }
       
   115 
       
   116 qreal QCLuceneDocument::getBoost() const
       
   117 {
       
   118     return qreal(d->document->getBoost());
       
   119 }
       
   120 
       
   121 void QCLuceneDocument::removeField(const QString &name)
       
   122 {
       
   123     TCHAR *fieldName = QStringToTChar(name);
       
   124     d->document->removeField(fieldName);
       
   125     delete [] fieldName;
       
   126 
       
   127     QList<QCLuceneField*> tmp;
       
   128     lucene::document::DocumentFieldEnumeration *dfe = d->document->fields();
       
   129     while (dfe->hasMoreElements()) {
       
   130         const lucene::document::Field* f = dfe->nextElement();
       
   131         foreach (QCLuceneField* field, fieldList) {
       
   132             if (f == field->d->field) {
       
   133                 tmp.append(field);
       
   134                 break;
       
   135             }
       
   136         }
       
   137     }
       
   138     _CLDELETE(dfe);
       
   139     fieldList = tmp;
       
   140 }
       
   141 
       
   142 void QCLuceneDocument::removeFields(const QString &name)
       
   143 {
       
   144     for (qint32 i = fieldList.count() -1; i >= 0; --i) {
       
   145         QCLuceneField* field = fieldList.at(i);
       
   146         if (field->name() == name)
       
   147             delete fieldList.takeAt(i);
       
   148     }
       
   149 
       
   150     TCHAR *fieldName = QStringToTChar(name);
       
   151     d->document->removeFields(fieldName);
       
   152     delete [] fieldName;
       
   153 }
       
   154 
       
   155 QStringList QCLuceneDocument::getValues(const QString &name) const
       
   156 {
       
   157     TCHAR *fieldName = QStringToTChar(name);
       
   158     TCHAR **values = d->document->getValues(fieldName);
       
   159 
       
   160     QStringList retValue;
       
   161     if (values) {
       
   162         for (qint32 i = 0; 0 != values[i]; ++i) {
       
   163             retValue.append(TCharToQString((const TCHAR*)values[i]));
       
   164             delete [] values[i]; values[i] = 0;
       
   165         }
       
   166         delete values;
       
   167     }
       
   168 
       
   169     delete [] fieldName;
       
   170     return retValue;
       
   171 }
       
   172 
       
   173 void QCLuceneDocument::clear()
       
   174 {
       
   175     d->document->clear();
       
   176     qDeleteAll(fieldList);
       
   177     fieldList.clear();
       
   178 }
       
   179 
       
   180 QT_END_NAMESPACE