1
/***************************************************************************
2
* Copyright (C) 2004-2010 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
***************************************************************************/
23
#include "encoderxml.h"
25
EncoderXML *encoderXML = NULL;
27
static const struct EncoderXMLCharMapping {
32
charmappingdataxml[] = {
33
{""", 0x0022, """}, /** FIXME: is this one required? */
34
{"&", 0x0026, "&"},
35
{"<", 0x003C, "<"},
36
{">", 0x003E, ">"}
38
static const int charmappingdataxmlcount = sizeof(charmappingdataxml) / sizeof(charmappingdataxml[ 0 ]) ;
41
* Private class to store internal variables that should not be visible
42
* in the interface as defined in the header file.
44
class EncoderXML::EncoderXMLPrivate
47
struct CharMappingItem {
53
QList<CharMappingItem> charMapping;
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);
67
EncoderXML::EncoderXML()
68
: Encoder(), d(new EncoderXML::EncoderXMLPrivate)
70
d->buildCharMapping();
73
EncoderXML::~EncoderXML()
78
QString EncoderXML::decode(const QString &text)
80
QString result = text;
82
for (QList<EncoderXMLPrivate::CharMappingItem>::ConstIterator it = d->charMapping.begin(); it != d->charMapping.end(); ++it)
83
result.replace((*it).regExp, (*it).unicode);
86
* Find and replace all characters written as hexadecimal number
89
while ((p = result.indexOf("&#x", p + 1)) >= 0) {
90
int p2 = result.indexOf(";", p + 1);
93
int hex = result.mid(p + 3, p2 - p - 3).toInt(&ok, 16);
95
result.replace(result.mid(p, p2 - p + 1), QChar(hex));
99
* Find and replace all characters written as decimal number
102
while ((p = result.indexOf("&#", p + 1)) >= 0) {
103
int p2 = result.indexOf(";", p + 1);
106
int dec = result.mid(p + 2, p2 - p - 2).toInt(&ok, 10);
108
result.replace(result.mid(p, p2 - p + 1), QChar(dec));
114
QString EncoderXML::encode(const QString &text)
116
QString result = text;
118
for (QList<EncoderXMLPrivate::CharMappingItem>::ConstIterator it = d->charMapping.begin(); it != d->charMapping.end(); ++it)
119
result.replace((*it).unicode, (*it).latex);
124
EncoderXML *EncoderXML::currentEncoderXML()
126
if (encoderXML == NULL)
127
encoderXML = new EncoderXML();