~ubuntu-branches/ubuntu/raring/kbibtex/raring

« back to all changes in this revision

Viewing changes to src/libkbibtexio/fileimporter.cpp

  • Committer: Package Import Robot
  • Author(s): Michael Hanke
  • Date: 2011-07-18 09:29:48 UTC
  • mfrom: (1.1.6) (2.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20110718092948-ksxjmg7kdfamolmg
Tags: 0.3-1
* First upstream release for KDE4 (Closes: #634255). A number of search
  engines are still missing, in comparison to the 0.2 series.
* Bumped Standards-Version to 3.9.2, no changes necessary.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
*   Copyright (C) 2004-2009 by Thomas Fischer                             *
 
3
*   fischer@unix-ag.uni-kl.de                                             *
 
4
*                                                                         *
 
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.                                   *
 
9
*                                                                         *
 
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.                          *
 
14
*                                                                         *
 
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
***************************************************************************/
 
20
#include <QBuffer>
 
21
#include <QTextStream>
 
22
#include <QStringList>
 
23
#include <QRegExp>
 
24
 
 
25
#include <value.h>
 
26
#include "fileimporter.h"
 
27
 
 
28
FileImporter::FileImporter()
 
29
        : QObject()
 
30
{
 
31
    // nothing
 
32
}
 
33
 
 
34
FileImporter::~FileImporter()
 
35
{
 
36
    // nothing
 
37
}
 
38
 
 
39
File* FileImporter::fromString(const QString& text)
 
40
{
 
41
    if (text.isNull() || text.isEmpty())
 
42
        return NULL;
 
43
 
 
44
    QBuffer buffer;
 
45
    buffer.open(QIODevice::WriteOnly);
 
46
    QTextStream stream(&buffer);
 
47
    stream.setCodec("UTF-8");
 
48
    stream << text;
 
49
    buffer.close();
 
50
 
 
51
    buffer.open(QIODevice::ReadOnly);
 
52
    File *result = load(&buffer);
 
53
    buffer.close();
 
54
 
 
55
    return result;
 
56
}
 
57
 
 
58
Person* FileImporter::splitName(const QString& name)
 
59
{
 
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 = "";
 
65
 
 
66
    if (segments.isEmpty())
 
67
        return NULL;
 
68
 
 
69
    if (!containsComma) {
 
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) {
 
76
            --p;
 
77
            ++singleCapitalLettersCounter;
 
78
        }
 
79
 
 
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]);
 
90
        } else {
 
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", ... */
 
94
            while (from > 0) {
 
95
                if (segments[from - 1].compare(segments[from - 1].toLower()) != 0)
 
96
                    break;
 
97
                --from;
 
98
                lastName.prepend(" ");
 
99
                lastName.prepend(segments[from]);
 
100
            }
 
101
 
 
102
            if (from > 0) {
 
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);
 
108
                }
 
109
            }
 
110
        }
 
111
    } else {
 
112
        bool inLastName = TRUE;
 
113
        for (int i = 0; i < segments.count(); ++i) {
 
114
            if (segments[i] == ",")
 
115
                inLastName = FALSE;
 
116
            else if (inLastName) {
 
117
                if (!lastName.isEmpty()) lastName.append(" ");
 
118
                lastName.append(segments[i]);
 
119
            } else {
 
120
                if (!firstName.isEmpty()) firstName.append(" ");
 
121
                firstName.append(segments[i]);
 
122
            }
 
123
        }
 
124
    }
 
125
 
 
126
    return new Person(firstName, lastName);
 
127
}
 
128
 
 
129
// #include "fileimporter.moc"