File indexing completed on 2024-05-12 16:29:19

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2013 Inge Wallin <inge@lysator.liu.se>
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 
0022 // Own
0023 #include "OdfParser.h"
0024 
0025 // Calligra
0026 #include <KoStore.h>
0027 #include <KoXmlReader.h>
0028 #include <KoXmlNS.h>
0029 
0030 #include "OdfReaderDebug.h"
0031 
0032 OdfParser::OdfParser()
0033 {
0034 }
0035 
0036 OdfParser::~OdfParser()
0037 {
0038 }
0039 
0040 
0041 KoFilter::ConversionStatus OdfParser::parseMetadata(KoStore &odfStore,
0042                                                     // out parameters:
0043                                                     QHash<QString, QString> *metadata)
0044 {
0045     if (!odfStore.open("meta.xml")) {
0046         debugOdfReader << "Cannot open meta.xml";
0047         return KoFilter::FileNotFound;
0048     }
0049 
0050     // FIXME: Convert this to the KoXmlStreamReader.
0051     KoXmlDocument doc;
0052     QString errorMsg;
0053     int errorLine;
0054     int errorColumn;
0055     if (!doc.setContent(odfStore.device(), true, &errorMsg, &errorLine, &errorColumn)) {
0056         debugOdfReader << "Error occurred while parsing meta.xml "
0057                  << errorMsg << " in Line: " << errorLine
0058                  << " Column: " << errorColumn;
0059         odfStore.close();
0060         return KoFilter::ParsingError;
0061     }
0062 
0063     KoXmlNode childNode = doc.documentElement();
0064     childNode = KoXml::namedItemNS(childNode, KoXmlNS::office, "meta");
0065     KoXmlElement element;
0066     forEachElement (element, childNode) {
0067         metadata->insert(element.tagName(), element.text());
0068     }
0069 
0070     odfStore.close();
0071     return KoFilter::OK;
0072 }
0073 
0074 
0075 KoFilter::ConversionStatus OdfParser::parseManifest(KoStore &odfStore,
0076                                                     // out parameters:
0077                                                     QHash<QString, QString> *manifest)
0078 {
0079     if (!odfStore.open("META-INF/manifest.xml")) {
0080         debugOdfReader << "Cannot to open manifest.xml.";
0081         return KoFilter::FileNotFound;
0082     }
0083 
0084     // FIXME: Convert this to the KoXmlStreamReader.
0085     KoXmlDocument doc;
0086     QString errorMsg;
0087     int errorLine, errorColumn;
0088     if (!doc.setContent(odfStore.device(), true, &errorMsg, &errorLine, &errorColumn)) {
0089         debugOdfReader << "Error occurred while parsing meta.xml "
0090                  << errorMsg << " in Line: " << errorLine
0091                  << " Column: " << errorColumn;
0092         return KoFilter::ParsingError;
0093     }
0094 
0095     KoXmlNode childNode = doc.documentElement();
0096     KoXmlElement nodeElement;
0097     forEachElement (nodeElement, childNode) {
0098         // Normalize the file name, i.e. remove trailing slashes.
0099         QString path = nodeElement.attribute("full-path");
0100         if (path.endsWith(QLatin1Char('/')))
0101             path.chop(1);
0102         QString type = nodeElement.attribute("media-type");
0103 
0104         manifest->insert(path, type);
0105     }
0106 
0107     odfStore.close();
0108     return KoFilter::OK;
0109 }