18
18
#include "../field.h"
19
19
#include "../latin1literal.h"
20
20
#include "../progressmanager.h"
21
#include "../filehandler.h"
21
22
#include "../tellico_debug.h"
23
24
#include <kglobal.h> // for KMAX
109
RISImporter::RISImporter(const KURL& url_) : Tellico::Import::TextImporter(url_), m_coll(0), m_cancelled(false) {
110
RISImporter::RISImporter(const KURL::List& urls_) : Tellico::Import::Importer(urls_), m_coll(0), m_cancelled(false) {
144
145
risFields.insert(ris, f);
147
QString str = text();
148
ProgressItem& item = ProgressManager::self()->newProgressItem(this, progressLabel(), true);
149
item.setTotalSteps(urls().count() * 100);
150
connect(&item, SIGNAL(signalCancelled(ProgressItem*)), SLOT(slotCancel()));
151
ProgressItem::Done done(this);
154
KURL::List urls = this->urls();
155
for(KURL::List::ConstIterator it = urls.begin(); it != urls.end() && !m_cancelled; ++it, ++count) {
156
readURL(*it, count, risFields);
165
void RISImporter::readURL(const KURL& url_, int n, const QDict<Data::Field>& risFields_) {
166
QString str = FileHandler::readTextFile(url_);
148
171
QTextIStream t(&str);
150
173
const uint length = str.length();
151
174
const uint stepSize = KMAX(s_stepSize, length/100);
152
175
const bool showProgress = options() & ImportProgress;
154
ProgressItem& item = ProgressManager::self()->newProgressItem(this, progressLabel(), true);
155
item.setTotalSteps(length);
156
connect(&item, SIGNAL(signalCancelled(ProgressItem*)), SLOT(slotCancel()));
157
ProgressItem::Done done(this);
177
bool needToAddFinal = false;
160
180
Data::EntryPtr entry = new Data::Entry(m_coll);
161
181
// technically, the spec requires a space immediately after the hyphen
162
182
// however, at least one website (Springer) outputs RIS with no space after the final "ER -"
163
183
// so just strip the white space later
164
QRegExp rx(QString::fromLatin1("^(\\w\\w)\\s\\s-(.*)$"));
184
// also be gracious and allow only any amount of space before hyphen
185
QRegExp rx(QString::fromLatin1("^(\\w\\w)\\s+-(.*)$"));
165
186
QString currLine, nextLine;
166
187
for(currLine = t.readLine(); !m_cancelled && !currLine.isNull(); currLine = nextLine, j += currLine.length()) {
167
188
nextLine = t.readLine();
171
192
if(tag.isEmpty()) {
174
// kdDebug() << tag << ": " << value << endl;
195
// myDebug() << tag << ": " << value << endl;
175
196
// if the next line is not empty and does not match start regexp, append to value
176
197
while(!nextLine.isEmpty() && nextLine.find(rx) == -1) {
177
198
value += nextLine.stripWhiteSpace();
182
203
if(tag == Latin1Literal("ER")) {
183
204
m_coll->addEntries(entry);
184
205
entry = new Data::Entry(m_coll);
206
needToAddFinal = false;
186
208
} else if(tag == Latin1Literal("TY") && s_typeMap->contains(value)) {
187
209
// for entry-type, switch it to normalized type name
193
215
// the lookup scheme is:
194
216
// 1. any field has an RIS property that matches the tag name
195
217
// 2. default field mapping tag -> field name
196
Data::FieldPtr f = risFields.find(tag);
218
Data::FieldPtr f = risFields_.find(tag);
198
220
// special case for BT
199
221
// primary title for books, secondary for everything else
221
244
entry->setField(f, value);
223
246
if(showProgress && j%stepSize == 0) {
224
ProgressManager::self()->setProgress(this, j);
247
ProgressManager::self()->setProgress(this, n*100 + 100*j/length);
225
248
kapp->processEvents();
252
m_coll->addEntries(entry);
235
256
Tellico::Data::FieldPtr RISImporter::fieldByTag(const QString& tag_) {