Add read xml function for Relationships
This commit is contained in:
@@ -24,38 +24,68 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
#include "xlsxrelationships_p.h"
|
#include "xlsxrelationships_p.h"
|
||||||
#include "xlsxxmlwriter_p.h"
|
#include "xlsxxmlwriter_p.h"
|
||||||
|
#include "xlsxxmlreader_p.h"
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
|
||||||
namespace QXlsx {
|
namespace QXlsx {
|
||||||
|
|
||||||
Relationships::Relationships(QObject *parent) :
|
const QString schema_doc = QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/relationships");
|
||||||
QObject(parent)
|
const QString schema_msPackage = QStringLiteral("http://schemas.microsoft.com/office/2006/relationships");
|
||||||
|
const QString schema_package = QStringLiteral("http://schemas.openxmlformats.org/package/2006/relationships");
|
||||||
|
//const QString schema_worksheet = QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/relationships");
|
||||||
|
Relationships::Relationships()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QList<XlsxRelationship> Relationships::documentRelationships(const QString &relativeType) const
|
||||||
|
{
|
||||||
|
return relationships(schema_doc + relativeType);
|
||||||
|
}
|
||||||
|
|
||||||
void Relationships::addDocumentRelationship(const QString &relativeType, const QString &target)
|
void Relationships::addDocumentRelationship(const QString &relativeType, const QString &target)
|
||||||
{
|
{
|
||||||
QString type = QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/relationships") + relativeType;
|
addRelationship(schema_doc + relativeType, target);
|
||||||
addRelationship(type, target);
|
}
|
||||||
|
|
||||||
|
QList<XlsxRelationship> Relationships::msPackageRelationships(const QString &relativeType) const
|
||||||
|
{
|
||||||
|
return relationships(schema_msPackage + relativeType);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Relationships::addMsPackageRelationship(const QString &relativeType, const QString &target)
|
void Relationships::addMsPackageRelationship(const QString &relativeType, const QString &target)
|
||||||
{
|
{
|
||||||
QString type = QStringLiteral("http://schemas.microsoft.com/office/2006/relationships") + relativeType;
|
addRelationship(schema_msPackage + relativeType, target);
|
||||||
addRelationship(type, target);
|
}
|
||||||
|
|
||||||
|
QList<XlsxRelationship> Relationships::packageRelationships(const QString &relativeType) const
|
||||||
|
{
|
||||||
|
return relationships(schema_package + relativeType);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Relationships::addPackageRelationship(const QString &relativeType, const QString &target)
|
void Relationships::addPackageRelationship(const QString &relativeType, const QString &target)
|
||||||
{
|
{
|
||||||
QString type = QStringLiteral("http://schemas.openxmlformats.org/package/2006/relationships") + relativeType;
|
addRelationship(schema_package + relativeType, target);
|
||||||
addRelationship(type, target);
|
}
|
||||||
|
|
||||||
|
QList<XlsxRelationship> Relationships::worksheetRelationships(const QString &relativeType) const
|
||||||
|
{
|
||||||
|
return relationships(schema_doc + relativeType);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Relationships::addWorksheetRelationship(const QString &relativeType, const QString &target, const QString &targetMode)
|
void Relationships::addWorksheetRelationship(const QString &relativeType, const QString &target, const QString &targetMode)
|
||||||
{
|
{
|
||||||
QString type = QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/relationships") + relativeType;
|
addRelationship(schema_doc + relativeType, target, targetMode);
|
||||||
addRelationship(type, target, targetMode);
|
}
|
||||||
|
|
||||||
|
QList<XlsxRelationship> Relationships::relationships(const QString &type) const
|
||||||
|
{
|
||||||
|
QList<XlsxRelationship> res;
|
||||||
|
foreach (XlsxRelationship ship, m_relationships) {
|
||||||
|
if (ship.type == type)
|
||||||
|
res.append(ship);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Relationships::addRelationship(const QString &type, const QString &target, const QString &targetMode)
|
void Relationships::addRelationship(const QString &type, const QString &target, const QString &targetMode)
|
||||||
@@ -89,4 +119,28 @@ void Relationships::saveToXmlFile(QIODevice *device)
|
|||||||
writer.writeEndDocument();
|
writer.writeEndDocument();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Relationships::loadFromXmlFile(QIODevice *device)
|
||||||
|
{
|
||||||
|
m_relationships.clear();
|
||||||
|
XmlStreamReader reader(device);
|
||||||
|
while(!reader.atEnd()) {
|
||||||
|
QXmlStreamReader::TokenType token = reader.readNext();
|
||||||
|
if (token == QXmlStreamReader::StartElement) {
|
||||||
|
if (reader.name() == QStringLiteral("Relationship")) {
|
||||||
|
QXmlStreamAttributes attributes = reader.attributes();
|
||||||
|
XlsxRelationship relationship;
|
||||||
|
relationship.id = attributes.value(QLatin1String("Id")).toString();
|
||||||
|
relationship.type = attributes.value(QLatin1String("Type")).toString();
|
||||||
|
relationship.target = attributes.value(QLatin1String("Target")).toString();
|
||||||
|
relationship.targetMode = attributes.value(QLatin1String("TargetMode")).toString();
|
||||||
|
m_relationships.append(relationship);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (reader.hasError()) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} //namespace
|
} //namespace
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
#ifndef XLSXRELATIONSHIPS_H
|
#ifndef XLSXRELATIONSHIPS_H
|
||||||
#define XLSXRELATIONSHIPS_H
|
#define XLSXRELATIONSHIPS_H
|
||||||
|
|
||||||
|
#include "xlsxglobal.h"
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
class QIODevice;
|
class QIODevice;
|
||||||
@@ -39,23 +40,28 @@ struct XlsxRelationship
|
|||||||
QString targetMode;
|
QString targetMode;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Relationships : public QObject
|
class XLSX_AUTOTEST_EXPORT Relationships
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
|
||||||
public:
|
public:
|
||||||
explicit Relationships(QObject *parent = 0);
|
Relationships();
|
||||||
|
|
||||||
signals:
|
QList<XlsxRelationship> documentRelationships(const QString &relativeType) const;
|
||||||
|
QList<XlsxRelationship> packageRelationships(const QString &relativeType) const;
|
||||||
|
QList<XlsxRelationship> msPackageRelationships(const QString &relativeType) const;
|
||||||
|
QList<XlsxRelationship> worksheetRelationships(const QString &relativeType) const;
|
||||||
|
|
||||||
public slots:
|
|
||||||
void addDocumentRelationship(const QString &relativeType, const QString &target);
|
void addDocumentRelationship(const QString &relativeType, const QString &target);
|
||||||
void addPackageRelationship(const QString &relativeType, const QString &target);
|
void addPackageRelationship(const QString &relativeType, const QString &target);
|
||||||
void addMsPackageRelationship(const QString &relativeType, const QString &target);
|
void addMsPackageRelationship(const QString &relativeType, const QString &target);
|
||||||
void addWorksheetRelationship(const QString &relativeType, const QString &target, const QString &targetMode=QString());
|
void addWorksheetRelationship(const QString &relativeType, const QString &target, const QString &targetMode=QString());
|
||||||
|
|
||||||
void saveToXmlFile(QIODevice *device);
|
void saveToXmlFile(QIODevice *device);
|
||||||
|
void loadFromXmlFile(QIODevice *device);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
QList<XlsxRelationship> relationships(const QString &type) const;
|
||||||
void addRelationship(const QString &type, const QString &target, const QString &targetMode=QString());
|
void addRelationship(const QString &type, const QString &target, const QString &targetMode=QString());
|
||||||
|
|
||||||
QList<XlsxRelationship> m_relationships;
|
QList<XlsxRelationship> m_relationships;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -2,4 +2,5 @@ TEMPLATE=subdirs
|
|||||||
SUBDIRS=\
|
SUBDIRS=\
|
||||||
utility \
|
utility \
|
||||||
mergecell \
|
mergecell \
|
||||||
zipreader
|
zipreader \
|
||||||
|
relationships
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#-------------------------------------------------
|
||||||
|
#
|
||||||
|
# Project created by QtCreator 2013-09-10T13:27:11
|
||||||
|
#
|
||||||
|
#-------------------------------------------------
|
||||||
|
|
||||||
|
QT += testlib xlsx xlsx-private
|
||||||
|
CONFIG += testcase
|
||||||
|
DEFINES += XLSX_TEST
|
||||||
|
|
||||||
|
TARGET = tst_relationshipstest
|
||||||
|
CONFIG += console
|
||||||
|
CONFIG -= app_bundle
|
||||||
|
|
||||||
|
TEMPLATE = app
|
||||||
|
|
||||||
|
|
||||||
|
SOURCES += tst_relationshipstest.cpp
|
||||||
|
DEFINES += SRCDIR=\\\"$$PWD/\\\"
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
#include "private/xlsxrelationships_p.h"
|
||||||
|
#include <QString>
|
||||||
|
#include <QtTest>
|
||||||
|
|
||||||
|
class RelationshipsTest : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
RelationshipsTest();
|
||||||
|
|
||||||
|
private Q_SLOTS:
|
||||||
|
void testSaveXml();
|
||||||
|
void testLoadXml();
|
||||||
|
};
|
||||||
|
|
||||||
|
RelationshipsTest::RelationshipsTest()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void RelationshipsTest::testSaveXml()
|
||||||
|
{
|
||||||
|
QXlsx::Relationships rels;
|
||||||
|
rels.addDocumentRelationship("/officeDocument", "xl/workbook.xml");
|
||||||
|
|
||||||
|
QByteArray xmldata;
|
||||||
|
QBuffer buffer(&xmldata);
|
||||||
|
buffer.open(QIODevice::WriteOnly);
|
||||||
|
rels.saveToXmlFile(&buffer);
|
||||||
|
|
||||||
|
QVERIFY2(xmldata.contains("<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\" Target=\"xl/workbook.xml\"/>"), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
void RelationshipsTest::testLoadXml()
|
||||||
|
{
|
||||||
|
QByteArray xmldata("<\?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"\?>"
|
||||||
|
"<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">"
|
||||||
|
"<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\" Target=\"xl/workbook.xml\"/>"
|
||||||
|
"</Relationships>");
|
||||||
|
QBuffer buffer(&xmldata);
|
||||||
|
buffer.open(QIODevice::ReadOnly);
|
||||||
|
|
||||||
|
QXlsx::Relationships rels;
|
||||||
|
rels.loadFromXmlFile(&buffer);
|
||||||
|
|
||||||
|
QCOMPARE(rels.documentRelationships("/officeDocument").size(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
QTEST_APPLESS_MAIN(RelationshipsTest)
|
||||||
|
|
||||||
|
#include "tst_relationshipstest.moc"
|
||||||
Reference in New Issue
Block a user