source: trunk/tools/assistant/lib/fulltextsearch/qdocument.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 4.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QCLucene library and is distributable under
7** the terms of the LGPL license as specified in the license.txt file.
8**
9****************************************************************************/
10
11#include "qdocument_p.h"
12#include "qreader_p.h"
13#include "qclucene_global_p.h"
14
15#include <CLucene.h>
16#include <CLucene/util/Reader.h>
17#include <CLucene/document/Document.h>
18
19QT_BEGIN_NAMESPACE
20
21QCLuceneDocumentPrivate::QCLuceneDocumentPrivate()
22 : QSharedData()
23{
24 document = 0;
25 deleteCLuceneDocument = true;
26}
27
28QCLuceneDocumentPrivate::QCLuceneDocumentPrivate(const QCLuceneDocumentPrivate &other)
29 : QSharedData()
30{
31 document = _CL_POINTER(other.document);
32}
33
34QCLuceneDocumentPrivate::~QCLuceneDocumentPrivate()
35{
36 if (deleteCLuceneDocument)
37 _CLDECDELETE(document);
38}
39
40
41QCLuceneDocument::QCLuceneDocument()
42 : d(new QCLuceneDocumentPrivate())
43{
44 // nothing todo
45 d->document = new lucene::document::Document();
46}
47
48QCLuceneDocument::~QCLuceneDocument()
49{
50 qDeleteAll(fieldList);
51 fieldList.clear();
52}
53
54void QCLuceneDocument::add(QCLuceneField *field)
55{
56 field->d->deleteCLuceneField = false;
57 d->document->add(*field->d->field);
58 fieldList.append(field);
59}
60
61QCLuceneField* QCLuceneDocument::getField(const QString &name) const
62{
63 QCLuceneField* field = 0;
64 foreach (field, fieldList) {
65 if (field->name() == name && field->d->field != 0)
66 return field;
67 }
68
69 field = 0;
70 TCHAR *fieldName = QStringToTChar(name);
71 lucene::document::Field *f = d->document->getField(fieldName);
72 if (f) {
73 field = new QCLuceneField();
74 field->d->field = f;
75 fieldList.append(field);
76 field->d->deleteCLuceneField = false;
77
78 lucene::util::Reader *r = f->readerValue();
79 if (r) {
80 field->reader->d->reader = r;
81 field->reader->d->deleteCLuceneReader = false;
82 }
83 }
84 delete [] fieldName;
85
86 return field;
87}
88
89QString QCLuceneDocument::get(const QString &name) const
90{
91 QCLuceneField* field = getField(name);
92 if (field)
93 return field->stringValue();
94
95 return QString();
96}
97
98QString QCLuceneDocument::toString() const
99{
100 return TCharToQString(d->document->toString());
101}
102
103void QCLuceneDocument::setBoost(qreal boost)
104{
105 d->document->setBoost(qreal(boost));
106}
107
108qreal QCLuceneDocument::getBoost() const
109{
110 return qreal(d->document->getBoost());
111}
112
113void QCLuceneDocument::removeField(const QString &name)
114{
115 TCHAR *fieldName = QStringToTChar(name);
116 d->document->removeField(fieldName);
117 delete [] fieldName;
118
119 QList<QCLuceneField*> tmp;
120 lucene::document::DocumentFieldEnumeration *dfe = d->document->fields();
121 while (dfe->hasMoreElements()) {
122 const lucene::document::Field* f = dfe->nextElement();
123 foreach (QCLuceneField* field, fieldList) {
124 if (f == field->d->field) {
125 tmp.append(field);
126 break;
127 }
128 }
129 }
130 _CLDELETE(dfe);
131 fieldList = tmp;
132}
133
134void QCLuceneDocument::removeFields(const QString &name)
135{
136 for (qint32 i = fieldList.count() -1; i >= 0; --i) {
137 QCLuceneField* field = fieldList.at(i);
138 if (field->name() == name)
139 delete fieldList.takeAt(i);
140 }
141
142 TCHAR *fieldName = QStringToTChar(name);
143 d->document->removeFields(fieldName);
144 delete [] fieldName;
145}
146
147QStringList QCLuceneDocument::getValues(const QString &name) const
148{
149 TCHAR *fieldName = QStringToTChar(name);
150 TCHAR **values = d->document->getValues(fieldName);
151
152 QStringList retValue;
153 if (values) {
154 for (qint32 i = 0; 0 != values[i]; ++i) {
155 retValue.append(TCharToQString((const TCHAR*)values[i]));
156 delete [] values[i]; values[i] = 0;
157 }
158 delete values;
159 }
160
161 delete [] fieldName;
162 return retValue;
163}
164
165void QCLuceneDocument::clear()
166{
167 d->document->clear();
168 qDeleteAll(fieldList);
169 fieldList.clear();
170}
171
172QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.