2
* This file is part of Office 2007 Filters for KOffice
4
* Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
6
* Contact: Suresh Chande suresh.chande@nokia.com
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.
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.
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
24
#include "MsooXmlReader.h"
25
#include "MsooXmlSchemas.h"
26
#include "MsooXmlUtils.h"
28
#include <KoXmlWriter.h>
29
#include <KoCharacterStyle.h>
31
QDebug operator<<(QDebug dbg, const QXmlStreamReader& reader)
33
dbg.nospace() << "QXmlStreamReader(";
34
if (reader.isStartElement()) {
36
dbg.nospace() << reader.qualifiedName().toString().toLocal8Bit().constData();
38
const QXmlStreamAttributes& attrs = reader.attributes();
39
for (int i = 0; i < attrs.count(); i++) {
40
dbg.nospace() << " " << attrs[i].qualifiedName().toString().toLocal8Bit().constData();
42
dbg.nospace() << attrs[i].value().toString();
44
if (reader.isEndElement()) {
45
dbg.nospace() << "/>)";
47
dbg.nospace() << ">)";
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() << ")";
60
dbg.nospace() << reader.tokenString() << reader.text();
65
using namespace MSOOXML;
67
MsooXmlReaderContext::MsooXmlReaderContext(MSOOXML::MsooXmlRelationships* _relationships)
68
: relationships(_relationships)
72
MsooXmlReaderContext::~MsooXmlReaderContext()
81
MsooXmlReader::MsooXmlReader(KoOdfWriters *writers)
83
, KoOdfWriters(*writers)
88
MsooXmlReader::MsooXmlReader(QIODevice* io, KoOdfWriters *writers)
89
: QXmlStreamReader(io)
90
, KoOdfWriters(*writers)
95
MsooXmlReader::~MsooXmlReader()
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";
116
void MsooXmlReader::init()
118
m_readUndoed = false;
121
static const char * tokenNames[] = {
122
"NoToken", "Invalid", "StartDocument", "EndDocument",
123
"StartElement", "EndElement", "Characters", "Comment",
124
"DTD", "EntityReference", "ProcessingInstruction", "??"
127
static const char* tokenName(QXmlStreamReader::TokenType t)
130
if (i < 0 || i > QXmlStreamReader::ProcessingInstruction)
131
i = QXmlStreamReader::ProcessingInstruction + 1;
132
return tokenNames[i];
135
bool MsooXmlReader::readBooleanAttr(const char* attrName, bool defaultValue) const
137
return MSOOXML::Utils::convertBooleanAttr(attributes().value(attrName).toString(), defaultValue);
140
void MsooXmlReader::raiseError(const QString & message)
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()))
149
kDebug() << errorString();
152
QXmlStreamReader::TokenType MsooXmlReader::readNext()
155
m_readUndoed = false;
157
m_recentType = QXmlStreamReader::readNext();
159
kDebug() << tokenName(m_recentType) << *this;
163
void MsooXmlReader::undoReadNext()
168
void MsooXmlReader::raiseElNotFoundError(const char* elementName)
170
raiseError(i18n("Element \"%1\" not found", QLatin1String(elementName)));
173
void MsooXmlReader::raiseAttributeNotFoundError(const char* attrName)
175
raiseError(i18n("Attribute \"%1\" not found", QLatin1String(attrName)));
178
void MsooXmlReader::raiseNSNotFoundError(const char* nsName)
180
raiseError(i18n("Namespace \"%1\" not found", nsName));
183
void MsooXmlReader::raiseUnexpectedAttributeValueError(const QString& value, const char* attrName)
185
raiseError(i18n("Unexpected value \"%1\" of attribute \"%2\"", value, attrName));
188
void MsooXmlReader::raiseUnexpectedSecondOccurenceOfElError(const char* elementName)
190
raiseError(i18n("Unexpected second occurrence of \"%1\" element", QLatin1String(elementName)));
193
bool MsooXmlReader::expectElName(const char* elementName)
195
kDebug() << elementName << "found:" << name();
196
if (!isStartElement() || name() != QLatin1String(elementName)) {
197
raiseElNotFoundError(elementName);
203
bool MsooXmlReader::expectElNameEnd(const char* elementName)
205
kDebug() << elementName << "found:" << name();
206
if (!isEndElement() || name() != QLatin1String(elementName)) {
207
raiseError(i18n("Expected closing of element \"%1\"", elementName));
213
bool MsooXmlReader::expectEl(const char* qualifiedElementName)
215
kDebug() << qualifiedElementName << "found:" << qualifiedName();
216
if (!isStartElement() || qualifiedName() != QLatin1String(qualifiedElementName)) {
217
raiseElNotFoundError(qualifiedElementName);
223
bool MsooXmlReader::expectEl(const QList<QByteArray>& qualifiedElementNames)
225
if (isStartElement()) {
226
foreach (const QByteArray& qualifiedElementName, qualifiedElementNames) {
227
if (qualifiedName().toString() == qualifiedElementName) {
228
kDebug() << qualifiedElementNames << "found:" << qualifiedName();
234
foreach (const QByteArray& qualifiedElementName, qualifiedElementNames) {
236
list += QLatin1String(", ");
237
list += qualifiedElementName;
239
raiseError(i18n("None of expected elements found: %1", list));
243
bool MsooXmlReader::expectElEnd(const QString& qualifiedElementName)
245
kDebug() << qualifiedElementName << "found:" << qualifiedName();
246
// kDebug() << kBacktrace();
247
if (!isEndElement() || qualifiedName() != qualifiedElementName) {
248
raiseError(i18n("Expected closing of element \"%1\"", qualifiedElementName));
254
bool MsooXmlReader::expectElEnd(const char* qualifiedElementName)
256
return expectElEnd(QLatin1String(qualifiedElementName));
259
bool MsooXmlReader::expectNS(const char* nsName)
261
kDebug() << namespaceUri() << (namespaceUri().compare(nsName) == 0);
262
if (0 != namespaceUri().compare(nsName)) {
263
raiseNSNotFoundError(nsName);
269
//----------------------------------------------------------------------------
271
KoFilter::ConversionStatus MsooXmlReader::read_sz(const QByteArray& ns, KoCharacterStyle* characterStyle)
273
const QXmlStreamAttributes attrs(attributes());
274
QString val(attrs.value(ns + ":val").toString());
275
kDebug() << (ns + ":val") << val;
276
if (!val.isEmpty()) {
279
const qreal pointSize = qreal(val.toUInt(&ok)) / 2.0; /* half-points */
281
kDebug() << "pointSize:" << pointSize;
282
characterStyle->setFontPointSize(pointSize);