1
/***************************************************************************
2
* Copyright (C) 2004-2009 by Thomas Fischer *
3
* fischer@unix-ag.uni-kl.de *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19
***************************************************************************/
21
#include <QTextStream>
22
#include <QStringList>
26
#include "fileimporter.h"
28
FileImporter::FileImporter()
34
FileImporter::~FileImporter()
39
File* FileImporter::fromString(const QString& text)
41
if (text.isNull() || text.isEmpty())
45
buffer.open(QIODevice::WriteOnly);
46
QTextStream stream(&buffer);
47
stream.setCodec("UTF-8");
51
buffer.open(QIODevice::ReadOnly);
52
File *result = load(&buffer);
58
Person* FileImporter::splitName(const QString& name)
60
// FIXME: This is a rather ugly code
61
QStringList segments = name.split(QRegExp("[ ,]+"));
62
bool containsComma = name.contains(',');
63
QString firstName = "";
64
QString lastName = "";
66
if (segments.isEmpty())
70
/** PubMed uses a special writing style for names, where the last name is followed by single capital letter,
71
* each being the first letter of each first name
72
* So, check how many single capital letters are at the end of the given segment list */
73
int singleCapitalLettersCounter = 0;
74
int p = segments.count() - 1;
75
while (segments[p].length() == 1 && segments[p].compare(segments[p].toUpper()) == 0) {
77
++singleCapitalLettersCounter;
80
if (singleCapitalLettersCounter > 0) {
81
/** this is a special case for names from PubMed, which are formatted like "Fischer T"
82
* all segment values until the first single letter segment are last name parts */
83
for (int i = 0; i < p; ++i)
84
lastName.append(segments[i]).append(" ");
85
lastName.append(segments[p]);
86
/** single letter segments are first name parts */
87
for (int i = p + 1; i < segments.count() - 1; ++i)
88
firstName.append(segments[i]).append(" ");
89
firstName.append(segments[segments.count() - 1]);
91
int from = segments.count() - 1;
92
lastName = segments[from];
93
/** check for lower case parts of the last name such as "van", "von", "de", ... */
95
if (segments[from - 1].compare(segments[from - 1].toLower()) != 0)
98
lastName.prepend(" ");
99
lastName.prepend(segments[from]);
103
/** there are segments left for the first name */
104
firstName = *segments.begin();
105
for (QStringList::Iterator it = ++segments.begin(); from > 1; ++it, --from) {
106
firstName.append(" ");
107
firstName.append(*it);
112
bool inLastName = TRUE;
113
for (int i = 0; i < segments.count(); ++i) {
114
if (segments[i] == ",")
116
else if (inLastName) {
117
if (!lastName.isEmpty()) lastName.append(" ");
118
lastName.append(segments[i]);
120
if (!firstName.isEmpty()) firstName.append(" ");
121
firstName.append(segments[i]);
126
return new Person(firstName, lastName);
129
// #include "fileimporter.moc"