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

0001 /*
0002  * This file is part of Office 2007 Filters for Calligra
0003  *
0004  * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
0005  *
0006  * Contact: Suresh Chande suresh.chande@nokia.com
0007  *
0008  * This library is free software; you can redistribute it and/or
0009  * modify it under the terms of the GNU Lesser General Public License
0010  * version 2.1 as published by the Free Software Foundation.
0011  *
0012  * This library is distributed in the hope that it will be useful, but
0013  * WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library; if not, write to the Free Software
0019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0020  * 02110-1301 USA
0021  *
0022  */
0023 
0024 #include "MsooXmlDocPropertiesReader.h"
0025 #include "MsooXmlSchemas.h"
0026 #include <MsooXmlUtils.h>
0027 #include <KoXmlWriter.h>
0028 
0029 #define MSOOXML_CURRENT_NS "cp"
0030 #define MSOOXML_CURRENT_CLASS MsooXmlDocPropertiesReader
0031 #define BIND_READ_CLASS MSOOXML_CURRENT_CLASS
0032 
0033 #include "MsooXmlReader_p.h"
0034 
0035 using namespace MSOOXML;
0036 
0037 MsooXmlDocPropertiesReader::MsooXmlDocPropertiesReader(KoOdfWriters* writers) : MsooXmlReader(writers)
0038 {
0039     elemMap.insert(QLatin1String("dc:creator"), QLatin1String("meta:initial-creator"));
0040     elemMap.insert(QLatin1String("cp:lastModifiedBy"), QLatin1String("dc:creator"));
0041     elemMap.insert(QLatin1String("dc:description"), QLatin1String("dc:description"));
0042     elemMap.insert(QLatin1String("dc:subject"), QLatin1String("dc:subject"));
0043     elemMap.insert(QLatin1String("dc:title"), QLatin1String("dc:title"));
0044     elemMap.insert(QLatin1String("cp:keywords"), QLatin1String("meta:keyword"));
0045     elemMap.insert(QLatin1String("dcterms:created"), QLatin1String("meta:creation-date"));
0046     elemMap.insert(QLatin1String("dcterms:modified"), QLatin1String("dc:date"));
0047     elemMap.insert(QLatin1String("cp:revision"), QLatin1String("meta:editing-cycles"));
0048 }
0049 
0050 
0051 KoFilter::ConversionStatus MsooXmlDocPropertiesReader::read(MsooXmlReaderContext*)
0052 {
0053     debugMsooXml << "=============================";
0054 
0055     readNext();
0056     if (!isStartDocument()) {
0057         return KoFilter::WrongFormat;
0058     }
0059 
0060     readNext();
0061 
0062     if (!expectEl("cp:coreProperties"))
0063         return KoFilter::WrongFormat;
0064 
0065     if (!expectNS(MSOOXML::Schemas::core_properties))
0066         return KoFilter::WrongFormat;
0067 
0068     QXmlStreamNamespaceDeclarations namespaces(namespaceDeclarations());
0069     for (int i = 0; i < namespaces.count(); i++) {
0070         debugMsooXml << "NS prefix:" << namespaces[i].prefix() << "uri:" << namespaces[i].namespaceUri();
0071     }
0072 //! @todo find out whether the namespace returned by namespaceUri()
0073 //!       is exactly the same ref as the element of namespaceDeclarations()
0074     if (!namespaces.contains(QXmlStreamNamespaceDeclaration("cp", MSOOXML::Schemas::core_properties))) {
0075         raiseError(i18n("Namespace \"%1\" not found", QLatin1String(MSOOXML::Schemas::core_properties)));
0076         return KoFilter::WrongFormat;
0077     }
0078 //! @todo expect other namespaces too...
0079 
0080     debugMsooXml << qualifiedName();
0081     TRY_READ(coreProperties)
0082 
0083     debugMsooXml << "===========finished============";
0084     return KoFilter::OK;
0085 }
0086 
0087 #undef CURRENT_EL
0088 #define CURRENT_EL coreProperties
0089 KoFilter::ConversionStatus MsooXmlDocPropertiesReader::read_coreProperties()
0090 {
0091     READ_PROLOGUE
0092 
0093     while (!atEnd()) {
0094         readNext();
0095         BREAK_IF_END_OF(CURRENT_EL)
0096         if (isStartElement()) {
0097             const QString qn = qualifiedName().toString();
0098             while (!isEndElement() && !isCharacters())
0099                 readNext();
0100 
0101             const QMap<QString,QString>::ConstIterator it = elemMap.constFind(qn);
0102             if (it == elemMap.constEnd()) {
0103                 debugMsooXml << "Unknown metadata ignored:" << qn;
0104                 while (!isEndElement())
0105                     readNext();
0106                 continue;
0107             }
0108             debugMsooXml << "Found:" << it.key() << "Mapped to:" << it.value();
0109             const QString t = text().toString();
0110             //can't use qPrintable() the string has to remain valid until endElement is called
0111             //which we can't do if we call qPrintable, the QByteArray falls out of scope after
0112             //the statement in which it is used
0113             QByteArray elementArray = it.value().toLocal8Bit();
0114             meta->startElement(elementArray.constData());
0115             meta->addTextNode(t.toUtf8());
0116             meta->endElement();
0117             while (!isEndElement())
0118                 readNext();
0119         }
0120     }
0121 
0122     READ_EPILOGUE
0123 }
0124