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

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 "MsooXmlRelationshipsReader.h"
0025 #include <MsooXmlSchemas.h>
0026 #include <MsooXmlUtils.h>
0027 
0028 //#define MSOOXML_CURRENT_NS
0029 #define MSOOXML_CURRENT_CLASS MsooXmlRelationshipsReader
0030 #define BIND_READ_CLASS MSOOXML_CURRENT_CLASS
0031 
0032 #include <MsooXmlReader_p.h>
0033 
0034 using namespace MSOOXML;
0035 
0036 MsooXmlRelationshipsReaderContext::MsooXmlRelationshipsReaderContext(
0037     const QString& _path, const QString& _file, QMap<QString, QString>& _rels,
0038     QMap<QString, QString>& _targetsForTypes)
0039         : path(_path), file(_file)
0040         , rels(&_rels), targetsForTypes(&_targetsForTypes)
0041 {
0042 }
0043 
0044 class MsooXmlRelationshipsReader::Private
0045 {
0046 public:
0047     Private() {
0048     }
0049     ~Private() {
0050     }
0051     QString pathAndFile;
0052 };
0053 
0054 MsooXmlRelationshipsReader::MsooXmlRelationshipsReader(KoOdfWriters *writers)
0055         : MSOOXML::MsooXmlReader(writers)
0056         , m_context(0)
0057         , d(new Private)
0058 {
0059     init();
0060 }
0061 
0062 MsooXmlRelationshipsReader::~MsooXmlRelationshipsReader()
0063 {
0064     delete d;
0065 }
0066 
0067 void MsooXmlRelationshipsReader::init()
0068 {
0069 }
0070 
0071 KoFilter::ConversionStatus MsooXmlRelationshipsReader::read(MSOOXML::MsooXmlReaderContext* context)
0072 {
0073     m_context = dynamic_cast<MsooXmlRelationshipsReaderContext*>(context);
0074     const KoFilter::ConversionStatus result = readInternal();
0075     m_context = 0;
0076     if (result == KoFilter::OK)
0077         return KoFilter::OK;
0078     return result;
0079 }
0080 
0081 KoFilter::ConversionStatus MsooXmlRelationshipsReader::readInternal()
0082 {
0083     debugMsooXml << "=============================";
0084 
0085     d->pathAndFile = MsooXmlRelationshipsReader::relKey(m_context->path, m_context->file, QString());
0086 
0087     readNext();
0088     if (!isStartDocument()) {
0089         return KoFilter::WrongFormat;
0090     }
0091 
0092     // Relationships
0093     readNext();
0094     debugMsooXml << *this << namespaceUri();
0095 
0096     if (!expectEl("Relationships")) {
0097         return KoFilter::WrongFormat;
0098     }
0099     if (!expectNS(MSOOXML::Schemas::relationships)) {
0100         return KoFilter::WrongFormat;
0101     }
0102     /*
0103         const QXmlStreamAttributes attrs( attributes() );
0104         for (int i=0; i<attrs.count(); i++) {
0105             debugMsooXml << "1 NS prefix:" << attrs[i].name() << "uri:" << attrs[i].namespaceUri();
0106         }*/
0107 
0108     QXmlStreamNamespaceDeclarations namespaces(namespaceDeclarations());
0109     for (int i = 0; i < namespaces.count(); i++) {
0110         debugMsooXml << "NS prefix:" << namespaces[i].prefix() << "uri:" << namespaces[i].namespaceUri();
0111     }
0112 
0113     TRY_READ(Relationships)
0114     debugMsooXml << "===========finished============";
0115     return KoFilter::OK;
0116 }
0117 
0118 #undef CURRENT_EL
0119 #define CURRENT_EL Relationships
0120 //! Relationships handler
0121 /*!
0122  No parent elements.
0123 
0124  Child elements:
0125     - [done] Relationship
0126 */
0127 KoFilter::ConversionStatus MsooXmlRelationshipsReader::read_Relationships()
0128 {
0129     READ_PROLOGUE
0130     while (!atEnd()) {
0131         readNext();
0132         debugMsooXml << *this;
0133         BREAK_IF_END_OF(CURRENT_EL)
0134         if (isStartElement()) {
0135             TRY_READ_IF(Relationship)
0136             ELSE_WRONG_FORMAT
0137         }
0138     }
0139     READ_EPILOGUE
0140 }
0141 
0142 #undef CURRENT_EL
0143 #define CURRENT_EL Relationship
0144 //! Relationship handler
0145 /*!
0146  Parent elements:
0147     - [done] Relationships
0148 
0149  No child elements.
0150 */
0151 KoFilter::ConversionStatus MsooXmlRelationshipsReader::read_Relationship()
0152 {
0153     READ_PROLOGUE
0154     const QXmlStreamAttributes attrs(attributes());
0155     READ_ATTR_WITHOUT_NS(Id)
0156     READ_ATTR_WITHOUT_NS(Type)
0157     READ_ATTR_WITHOUT_NS(Target)
0158     QString fixedPath(m_context->path);
0159     while (Target.startsWith(QLatin1String("../"))) {
0160         //debugMsooXml << "- Target:" << Target << "fixedPath:" << fixedPath;
0161         Target.remove(0, 3);
0162         fixedPath.truncate(fixedPath.lastIndexOf('/'));
0163         //debugMsooXml << "= Target:" << Target << "fixedPath:" << fixedPath;
0164     }
0165     //debugMsooXml << "adding rel:";
0166     //debugMsooXml << d->pathAndFile + Id;
0167     //debugMsooXml << fixedPath + '/' + Target;
0168 
0169     m_context->rels->insert(d->pathAndFile + Id, fixedPath + '/' + Target);
0170     //debugMsooXml << "added target" << Target << "for type" << Type << "path=" << fixedPath << "key=" << targetKey(m_context->path + '/' + m_context->file, Type);
0171     m_context->targetsForTypes->insert(targetKey(m_context->path + '/' + m_context->file, Type), fixedPath + '/' + Target);
0172 
0173     readNext();
0174     READ_EPILOGUE
0175 }