Warning, file /office/calligra/libs/odf/KoOdfReadStore.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2005 David Faure <faure@kde.org>
0003    Copyright (C) 2007 Thorsten Zach3n <zachmann@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "KoOdfReadStore.h"
0022 
0023 #include <OdfDebug.h>
0024 #include <klocalizedstring.h>
0025 
0026 #include <KoStore.h>
0027 #include <KoXmlReader.h>
0028 
0029 #include "KoOdfStylesReader.h"
0030 
0031 #include <QXmlStreamReader>
0032 
0033 class Q_DECL_HIDDEN KoOdfReadStore::Private
0034 {
0035 public:
0036     Private(KoStore *s)
0037             : store(s)
0038     {
0039     }
0040 
0041     KoStore * store;
0042     KoOdfStylesReader stylesReader;
0043     // it is needed to keep the stylesDoc around so that you can access the styles
0044     KoXmlDocument stylesDoc;
0045     KoXmlDocument contentDoc;
0046     KoXmlDocument settingsDoc;
0047 };
0048 
0049 KoOdfReadStore::KoOdfReadStore(KoStore *store)
0050         : d(new Private(store))
0051 {
0052 }
0053 
0054 KoOdfReadStore::~KoOdfReadStore()
0055 {
0056     delete d;
0057 }
0058 
0059 KoStore * KoOdfReadStore::store() const
0060 {
0061     return d->store;
0062 }
0063 
0064 KoOdfStylesReader &KoOdfReadStore::styles()
0065 {
0066     return d->stylesReader;
0067 }
0068 
0069 KoXmlDocument KoOdfReadStore::contentDoc() const
0070 {
0071     return d->contentDoc;
0072 }
0073 
0074 KoXmlDocument KoOdfReadStore::settingsDoc() const
0075 {
0076     return d->settingsDoc;
0077 }
0078 
0079 bool KoOdfReadStore::loadAndParse(QString &errorMessage)
0080 {
0081     if (!loadAndParse("content.xml", d->contentDoc, errorMessage)) {
0082         return false;
0083     }
0084 
0085     if (d->store->hasFile("styles.xml")) {
0086         if (!loadAndParse("styles.xml", d->stylesDoc, errorMessage)) {
0087             return false;
0088         }
0089     }
0090     // Load styles from style.xml
0091     d->stylesReader.createStyleMap(d->stylesDoc, true);
0092     // Also load styles from content.xml
0093     d->stylesReader.createStyleMap(d->contentDoc, false);
0094 
0095     if (d->store->hasFile("settings.xml")) {
0096         loadAndParse("settings.xml", d->settingsDoc, errorMessage);
0097     }
0098     return true;
0099 }
0100 
0101 bool KoOdfReadStore::loadAndParse(const QString &fileName, KoXmlDocument &doc, QString &errorMessage)
0102 {
0103     if (!d->store) {
0104         errorMessage = i18n("No store backend");
0105         return false;
0106     }
0107 
0108     if (!d->store->isOpen()) {
0109         if (!d->store->open(fileName)) {
0110             debugOdf << "Entry " << fileName << " not found!"; // not a warning as embedded stores don't have to have all files
0111             errorMessage = i18n("Could not find %1", fileName);
0112             return false;
0113         }
0114     }
0115 
0116     bool ok = loadAndParse(d->store->device(), doc, errorMessage, fileName);
0117     d->store->close();
0118     return ok;
0119 }
0120 
0121 bool KoOdfReadStore::loadAndParse(QIODevice *fileDevice, KoXmlDocument &doc, QString &errorMessage, const QString &fileName)
0122 {
0123     // Error variables for QDomDocument::setContent
0124     QString errorMsg;
0125     int errorLine, errorColumn;
0126 
0127     if (!fileDevice->isOpen()) {
0128         fileDevice->open(QIODevice::ReadOnly);
0129     }
0130 
0131     QXmlStreamReader reader(fileDevice);
0132     reader.setNamespaceProcessing(true);
0133 
0134     bool ok = doc.setContent(&reader, &errorMsg, &errorLine, &errorColumn);
0135     if (!ok) {
0136         errorOdf << "Parsing error in " << fileName << "! Aborting!" << endl
0137         << " In line: " << errorLine << ", column: " << errorColumn << endl
0138         << " Error message: " << errorMsg << endl;
0139         errorMessage = i18n("Parsing error in the main document at line %1, column %2\nError message: %3"
0140                             , errorLine , errorColumn , errorMsg);
0141     } else {
0142         debugOdf << "File" << fileName << " loaded and parsed";
0143     }
0144     return ok;
0145 }