~ubuntu-branches/ubuntu/precise/kbibtex/precise

« back to all changes in this revision

Viewing changes to src/libkbibtexio/encoderxml.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-2010 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 <QRegExp>
 
21
#include <QList>
 
22
 
 
23
#include "encoderxml.h"
 
24
 
 
25
EncoderXML *encoderXML = NULL;
 
26
 
 
27
static const struct EncoderXMLCharMapping {
 
28
    const char *regexp;
 
29
    unsigned int unicode;
 
30
    const char *latex;
 
31
}
 
32
charmappingdataxml[] = {
 
33
    {"&quot;", 0x0022, "&quot;"}, /** FIXME: is this one required? */
 
34
    {"&amp;", 0x0026, "&amp;"},
 
35
    {"&lt;", 0x003C, "&lt;"},
 
36
    {"&gt;", 0x003E, "&gt;"}
 
37
};
 
38
static const int charmappingdataxmlcount = sizeof(charmappingdataxml) / sizeof(charmappingdataxml[ 0 ]) ;
 
39
 
 
40
/**
 
41
 * Private class to store internal variables that should not be visible
 
42
 * in the interface as defined in the header file.
 
43
 */
 
44
class EncoderXML::EncoderXMLPrivate
 
45
{
 
46
public:
 
47
    struct CharMappingItem {
 
48
        QRegExp regExp;
 
49
        QChar unicode;
 
50
        QString latex;
 
51
    };
 
52
 
 
53
    QList<CharMappingItem> charMapping;
 
54
 
 
55
    void buildCharMapping() {
 
56
        for (int i = 0; i < charmappingdataxmlcount; i++) {
 
57
            CharMappingItem charMappingItem;
 
58
            charMappingItem.regExp = QRegExp(charmappingdataxml[ i ].regexp);
 
59
            charMappingItem.unicode = QChar(charmappingdataxml[ i ].unicode);
 
60
            charMappingItem.latex = QString(charmappingdataxml[ i ].latex);
 
61
            charMapping.append(charMappingItem);
 
62
        }
 
63
    }
 
64
 
 
65
};
 
66
 
 
67
EncoderXML::EncoderXML()
 
68
        : Encoder(), d(new EncoderXML::EncoderXMLPrivate)
 
69
{
 
70
    d->buildCharMapping();
 
71
}
 
72
 
 
73
EncoderXML::~EncoderXML()
 
74
{
 
75
    // nothing
 
76
}
 
77
 
 
78
QString EncoderXML::decode(const QString &text)
 
79
{
 
80
    QString result = text;
 
81
 
 
82
    for (QList<EncoderXMLPrivate::CharMappingItem>::ConstIterator it = d->charMapping.begin(); it != d->charMapping.end(); ++it)
 
83
        result.replace((*it).regExp, (*it).unicode);
 
84
 
 
85
    /**
 
86
      * Find and replace all characters written as hexadecimal number
 
87
      */
 
88
    int p = -1;
 
89
    while ((p = result.indexOf("&#x", p + 1)) >= 0) {
 
90
        int p2 = result.indexOf(";", p + 1);
 
91
        if (p2 < 0) break;
 
92
        bool ok = FALSE;
 
93
        int hex = result.mid(p + 3, p2 - p - 3).toInt(&ok, 16);
 
94
        if (ok && hex > 0)
 
95
            result.replace(result.mid(p, p2 - p + 1), QChar(hex));
 
96
    }
 
97
 
 
98
    /**
 
99
      * Find and replace all characters written as decimal number
 
100
      */
 
101
    p = -1;
 
102
    while ((p = result.indexOf("&#", p + 1)) >= 0) {
 
103
        int p2 = result.indexOf(";", p + 1);
 
104
        if (p2 < 0) break;
 
105
        bool ok = FALSE;
 
106
        int dec = result.mid(p + 2, p2 - p - 2).toInt(&ok, 10);
 
107
        if (ok && dec > 0)
 
108
            result.replace(result.mid(p, p2 - p + 1), QChar(dec));
 
109
    }
 
110
 
 
111
    return result;
 
112
}
 
113
 
 
114
QString EncoderXML::encode(const QString &text)
 
115
{
 
116
    QString result = text;
 
117
 
 
118
    for (QList<EncoderXMLPrivate::CharMappingItem>::ConstIterator it = d->charMapping.begin(); it != d->charMapping.end(); ++it)
 
119
        result.replace((*it).unicode, (*it).latex);
 
120
 
 
121
    return result;
 
122
}
 
123
 
 
124
EncoderXML *EncoderXML::currentEncoderXML()
 
125
{
 
126
    if (encoderXML == NULL)
 
127
        encoderXML = new EncoderXML();
 
128
 
 
129
    return encoderXML;
 
130
}