~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to filters/libmsooxml/MsooXmlReader.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of Office 2007 Filters for KOffice
 
3
 *
 
4
 * Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
 
5
 *
 
6
 * Contact: Suresh Chande suresh.chande@nokia.com
 
7
 *
 
8
 * This library is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU Lesser General Public License
 
10
 * version 2.1 as published by the Free Software Foundation.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
 * 02110-1301 USA
 
21
 *
 
22
 */
 
23
 
 
24
#include "MsooXmlReader.h"
 
25
#include "MsooXmlSchemas.h"
 
26
#include "MsooXmlUtils.h"
 
27
 
 
28
#include <KoXmlWriter.h>
 
29
#include <KoCharacterStyle.h>
 
30
 
 
31
QDebug operator<<(QDebug dbg, const QXmlStreamReader& reader)
 
32
{
 
33
    dbg.nospace() << "QXmlStreamReader(";
 
34
    if (reader.isStartElement()) {
 
35
        dbg.nospace() << "<";
 
36
        dbg.nospace() << reader.qualifiedName().toString().toLocal8Bit().constData();
 
37
        QString attrsString;
 
38
        const QXmlStreamAttributes& attrs = reader.attributes();
 
39
        for (int i = 0; i < attrs.count(); i++) {
 
40
            dbg.nospace() << " " << attrs[i].qualifiedName().toString().toLocal8Bit().constData();
 
41
            dbg.nospace() << "=";
 
42
            dbg.nospace() << attrs[i].value().toString();
 
43
        }
 
44
        if (reader.isEndElement()) {
 
45
            dbg.nospace() << "/>)";
 
46
        } else {
 
47
            dbg.nospace() << ">)";
 
48
        }
 
49
    } else if (reader.isEndElement()) {
 
50
        dbg.nospace() << "</" << reader.qualifiedName().toString().toLocal8Bit().constData() << ">)";
 
51
    } else if (reader.isCharacters()) {
 
52
        dbg.nospace() << "characters:" << reader.text() << ")";
 
53
    } else if (reader.isComment()) {
 
54
        dbg.nospace() << "<!-- " << reader.text().toString().toLocal8Bit().constData() << " -->)";
 
55
    } else if (reader.isCDATA()) {
 
56
        dbg.nospace() << "CDATA:" << reader.text() << ")";
 
57
    } else if (reader.isWhitespace()) {
 
58
        dbg.nospace() << "whitespace:" << reader.text() << ")";
 
59
    } else {
 
60
        dbg.nospace() << reader.tokenString() << reader.text();
 
61
    }
 
62
    return dbg.space();
 
63
}
 
64
 
 
65
using namespace MSOOXML;
 
66
 
 
67
MsooXmlReaderContext::MsooXmlReaderContext(MSOOXML::MsooXmlRelationships* _relationships)
 
68
        : relationships(_relationships)
 
69
{
 
70
}
 
71
 
 
72
MsooXmlReaderContext::~MsooXmlReaderContext()
 
73
{
 
74
}
 
75
 
 
76
enum State {
 
77
    Start,
 
78
    InsideDocument
 
79
};
 
80
 
 
81
MsooXmlReader::MsooXmlReader(KoOdfWriters *writers)
 
82
        : QXmlStreamReader()
 
83
        , KoOdfWriters(*writers)
 
84
{
 
85
    init();
 
86
}
 
87
 
 
88
MsooXmlReader::MsooXmlReader(QIODevice* io, KoOdfWriters *writers)
 
89
        : QXmlStreamReader(io)
 
90
        , KoOdfWriters(*writers)
 
91
{
 
92
    init();
 
93
}
 
94
 
 
95
MsooXmlReader::~MsooXmlReader()
 
96
{
 
97
}
 
98
 
 
99
const char* MsooXmlReader::constOn = "on";
 
100
const char* MsooXmlReader::constOff = "off";
 
101
const char* MsooXmlReader::constTrue = "true";
 
102
const char* MsooXmlReader::constFalse = "false";
 
103
const char* MsooXmlReader::constNone = "none";
 
104
const char* MsooXmlReader::const1 = "1";
 
105
const char* MsooXmlReader::const0 = "0";
 
106
const char* MsooXmlReader::constAuto = "auto";
 
107
const char* MsooXmlReader::constFloat = "float";
 
108
const char* MsooXmlReader::constPercentage = "percentage";
 
109
const char* MsooXmlReader::constCurrency = "currency";
 
110
const char* MsooXmlReader::constDate = "date";
 
111
const char* MsooXmlReader::constTime = "time";
 
112
const char* MsooXmlReader::constBoolean = "boolean";
 
113
const char* MsooXmlReader::constString = "string";
 
114
 
 
115
 
 
116
void MsooXmlReader::init()
 
117
{
 
118
    m_readUndoed = false;
 
119
}
 
120
 
 
121
static const char * tokenNames[] = {
 
122
    "NoToken", "Invalid", "StartDocument", "EndDocument",
 
123
    "StartElement", "EndElement", "Characters", "Comment",
 
124
    "DTD", "EntityReference", "ProcessingInstruction", "??"
 
125
};
 
126
 
 
127
static const char* tokenName(QXmlStreamReader::TokenType t)
 
128
{
 
129
    int i = (int)t;
 
130
    if (i < 0 || i > QXmlStreamReader::ProcessingInstruction)
 
131
        i = QXmlStreamReader::ProcessingInstruction + 1;
 
132
    return tokenNames[i];
 
133
}
 
134
 
 
135
bool MsooXmlReader::readBooleanAttr(const char* attrName, bool defaultValue) const
 
136
{
 
137
    return MSOOXML::Utils::convertBooleanAttr(attributes().value(attrName).toString(), defaultValue);
 
138
}
 
139
 
 
140
void MsooXmlReader::raiseError(const QString & message)
 
141
{
 
142
    QXmlStreamReader::raiseError(
 
143
        m_fileName.isEmpty() ?
 
144
        i18n("%1 (line %2, column %3)", message,
 
145
             QString::number(lineNumber()), QString::number(columnNumber()))
 
146
        : i18n("%1 (%2, line %3, column %4)", message, m_fileName,
 
147
               QString::number(lineNumber()), QString::number(columnNumber()))
 
148
    );
 
149
    kDebug() << errorString();
 
150
}
 
151
 
 
152
QXmlStreamReader::TokenType MsooXmlReader::readNext()
 
153
{
 
154
    if (m_readUndoed) {
 
155
        m_readUndoed = false;
 
156
    } else {
 
157
        m_recentType = QXmlStreamReader::readNext();
 
158
    }
 
159
    kDebug() << tokenName(m_recentType) << *this;
 
160
    return m_recentType;
 
161
}
 
162
 
 
163
void MsooXmlReader::undoReadNext()
 
164
{
 
165
    m_readUndoed = true;
 
166
}
 
167
 
 
168
void MsooXmlReader::raiseElNotFoundError(const char* elementName)
 
169
{
 
170
    raiseError(i18n("Element \"%1\" not found", QLatin1String(elementName)));
 
171
}
 
172
 
 
173
void MsooXmlReader::raiseAttributeNotFoundError(const char* attrName)
 
174
{
 
175
    raiseError(i18n("Attribute \"%1\" not found", QLatin1String(attrName)));
 
176
}
 
177
 
 
178
void MsooXmlReader::raiseNSNotFoundError(const char* nsName)
 
179
{
 
180
    raiseError(i18n("Namespace \"%1\" not found", nsName));
 
181
}
 
182
 
 
183
void MsooXmlReader::raiseUnexpectedAttributeValueError(const QString& value, const char* attrName)
 
184
{
 
185
    raiseError(i18n("Unexpected value \"%1\" of attribute \"%2\"", value, attrName));
 
186
}
 
187
 
 
188
void MsooXmlReader::raiseUnexpectedSecondOccurenceOfElError(const char* elementName)
 
189
{
 
190
    raiseError(i18n("Unexpected second occurrence of \"%1\" element", QLatin1String(elementName)));
 
191
}
 
192
 
 
193
bool MsooXmlReader::expectElName(const char* elementName)
 
194
{
 
195
    kDebug() << elementName << "found:" << name();
 
196
    if (!isStartElement() || name() != QLatin1String(elementName)) {
 
197
        raiseElNotFoundError(elementName);
 
198
        return false;
 
199
    }
 
200
    return true;
 
201
}
 
202
 
 
203
bool MsooXmlReader::expectElNameEnd(const char* elementName)
 
204
{
 
205
    kDebug() << elementName << "found:" << name();
 
206
    if (!isEndElement() || name() != QLatin1String(elementName)) {
 
207
        raiseError(i18n("Expected closing of element \"%1\"", elementName));
 
208
        return false;
 
209
    }
 
210
    return true;
 
211
}
 
212
 
 
213
bool MsooXmlReader::expectEl(const char* qualifiedElementName)
 
214
{
 
215
    kDebug() << qualifiedElementName << "found:" << qualifiedName();
 
216
    if (!isStartElement() || qualifiedName() != QLatin1String(qualifiedElementName)) {
 
217
        raiseElNotFoundError(qualifiedElementName);
 
218
        return false;
 
219
    }
 
220
    return true;
 
221
}
 
222
 
 
223
bool MsooXmlReader::expectEl(const QList<QByteArray>& qualifiedElementNames)
 
224
{
 
225
    if (isStartElement()) {
 
226
        foreach (const QByteArray& qualifiedElementName, qualifiedElementNames) {
 
227
            if (qualifiedName().toString() == qualifiedElementName) {
 
228
                kDebug() << qualifiedElementNames << "found:" << qualifiedName();
 
229
                return true;
 
230
            }
 
231
        }
 
232
    }
 
233
    QString list;
 
234
    foreach (const QByteArray& qualifiedElementName, qualifiedElementNames) {
 
235
        if (!list.isEmpty())
 
236
            list += QLatin1String(", ");
 
237
        list += qualifiedElementName;
 
238
    }
 
239
    raiseError(i18n("None of expected elements found: %1", list));
 
240
    return false;
 
241
}
 
242
 
 
243
bool MsooXmlReader::expectElEnd(const QString& qualifiedElementName)
 
244
{
 
245
    kDebug() << qualifiedElementName << "found:" << qualifiedName();
 
246
//    kDebug() << kBacktrace();
 
247
    if (!isEndElement() || qualifiedName() != qualifiedElementName) {
 
248
        raiseError(i18n("Expected closing of element \"%1\"", qualifiedElementName));
 
249
        return false;
 
250
    }
 
251
    return true;
 
252
}
 
253
 
 
254
bool MsooXmlReader::expectElEnd(const char* qualifiedElementName)
 
255
{
 
256
    return expectElEnd(QLatin1String(qualifiedElementName));
 
257
}
 
258
 
 
259
bool MsooXmlReader::expectNS(const char* nsName)
 
260
{
 
261
    kDebug() << namespaceUri() << (namespaceUri().compare(nsName) == 0);
 
262
    if (0 != namespaceUri().compare(nsName)) {
 
263
        raiseNSNotFoundError(nsName);
 
264
        return false;
 
265
    }
 
266
    return true;
 
267
}
 
268
 
 
269
//----------------------------------------------------------------------------
 
270
 
 
271
KoFilter::ConversionStatus MsooXmlReader::read_sz(const QByteArray& ns, KoCharacterStyle* characterStyle)
 
272
{
 
273
    const QXmlStreamAttributes attrs(attributes());
 
274
    QString val(attrs.value(ns + ":val").toString());
 
275
    kDebug() << (ns + ":val") << val;
 
276
    if (!val.isEmpty()) {
 
277
// CASE #1164
 
278
        bool ok;
 
279
        const qreal pointSize = qreal(val.toUInt(&ok)) / 2.0;   /* half-points */
 
280
        if (ok) {
 
281
            kDebug() << "pointSize:" << pointSize;
 
282
            characterStyle->setFontPointSize(pointSize);
 
283
        }
 
284
    }
 
285
    readNext();
 
286
    return KoFilter::OK;
 
287
}