xlsxrelationships.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // xlsxrelationships.cpp
  2. #include <QtGlobal>
  3. #include <QXmlStreamWriter>
  4. #include <QXmlStreamReader>
  5. #include <QDir>
  6. #include <QFile>
  7. #include <QBuffer>
  8. #include "xlsxrelationships_p.h"
  9. QT_BEGIN_NAMESPACE_XLSX
  10. const QLatin1String schema_doc("http://schemas.openxmlformats.org/officeDocument/2006/relationships");
  11. const QLatin1String schema_msPackage("http://schemas.microsoft.com/office/2006/relationships");
  12. const QLatin1String schema_package("http://schemas.openxmlformats.org/package/2006/relationships");
  13. //const QString schema_worksheet = QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/relationships");
  14. Relationships::Relationships()
  15. {
  16. }
  17. QList<XlsxRelationship> Relationships::documentRelationships(const QString &relativeType) const
  18. {
  19. return relationships(schema_doc + relativeType);
  20. }
  21. void Relationships::addDocumentRelationship(const QString &relativeType, const QString &target)
  22. {
  23. addRelationship(schema_doc + relativeType, target);
  24. }
  25. QList<XlsxRelationship> Relationships::msPackageRelationships(const QString &relativeType) const
  26. {
  27. return relationships(schema_msPackage + relativeType);
  28. }
  29. void Relationships::addMsPackageRelationship(const QString &relativeType, const QString &target)
  30. {
  31. addRelationship(schema_msPackage + relativeType, target);
  32. }
  33. QList<XlsxRelationship> Relationships::packageRelationships(const QString &relativeType) const
  34. {
  35. return relationships(schema_package + relativeType);
  36. }
  37. void Relationships::addPackageRelationship(const QString &relativeType, const QString &target)
  38. {
  39. addRelationship(schema_package + relativeType, target);
  40. }
  41. QList<XlsxRelationship> Relationships::worksheetRelationships(const QString &relativeType) const
  42. {
  43. return relationships(schema_doc + relativeType);
  44. }
  45. void Relationships::addWorksheetRelationship(const QString &relativeType, const QString &target, const QString &targetMode)
  46. {
  47. addRelationship(schema_doc + relativeType, target, targetMode);
  48. }
  49. QList<XlsxRelationship> Relationships::relationships(const QString &type) const
  50. {
  51. QList<XlsxRelationship> res;
  52. for (const XlsxRelationship &ship : m_relationships) {
  53. if (ship.type == type)
  54. res.append(ship);
  55. }
  56. return res;
  57. }
  58. void Relationships::addRelationship(const QString &type, const QString &target, const QString &targetMode)
  59. {
  60. XlsxRelationship relation;
  61. relation.id = QStringLiteral("rId%1").arg(m_relationships.size()+1);
  62. relation.type = type;
  63. relation.target = target;
  64. relation.targetMode = targetMode;
  65. m_relationships.append(relation);
  66. }
  67. void Relationships::saveToXmlFile(QIODevice *device) const
  68. {
  69. QXmlStreamWriter writer(device);
  70. writer.writeStartDocument(QStringLiteral("1.0"), true);
  71. writer.writeStartElement(QStringLiteral("Relationships"));
  72. writer.writeAttribute(QStringLiteral("xmlns"), QStringLiteral("http://schemas.openxmlformats.org/package/2006/relationships"));
  73. for (const XlsxRelationship &relation : m_relationships) {
  74. writer.writeStartElement(QStringLiteral("Relationship"));
  75. writer.writeAttribute(QStringLiteral("Id"), relation.id);
  76. writer.writeAttribute(QStringLiteral("Type"), relation.type);
  77. writer.writeAttribute(QStringLiteral("Target"), relation.target);
  78. if (!relation.targetMode.isNull())
  79. writer.writeAttribute(QStringLiteral("TargetMode"), relation.targetMode);
  80. writer.writeEndElement();
  81. }
  82. writer.writeEndElement();//Relationships
  83. writer.writeEndDocument();
  84. }
  85. QByteArray Relationships::saveToXmlData() const
  86. {
  87. QByteArray data;
  88. QBuffer buffer(&data);
  89. buffer.open(QIODevice::WriteOnly);
  90. saveToXmlFile(&buffer);
  91. return data;
  92. }
  93. bool Relationships::loadFromXmlFile(QIODevice *device)
  94. {
  95. clear();
  96. QXmlStreamReader reader(device);
  97. while (!reader.atEnd()) {
  98. QXmlStreamReader::TokenType token = reader.readNext();
  99. if (token == QXmlStreamReader::StartElement) {
  100. if (reader.name() == QStringLiteral("Relationship")) {
  101. QXmlStreamAttributes attributes = reader.attributes();
  102. XlsxRelationship relationship;
  103. relationship.id = attributes.value(QLatin1String("Id")).toString();
  104. relationship.type = attributes.value(QLatin1String("Type")).toString();
  105. relationship.target = attributes.value(QLatin1String("Target")).toString();
  106. relationship.targetMode = attributes.value(QLatin1String("TargetMode")).toString();
  107. m_relationships.append(relationship);
  108. }
  109. }
  110. if (reader.hasError())
  111. return false;
  112. }
  113. return true;
  114. }
  115. bool Relationships::loadFromXmlData(const QByteArray &data)
  116. {
  117. QBuffer buffer;
  118. buffer.setData(data);
  119. buffer.open(QIODevice::ReadOnly);
  120. return loadFromXmlFile(&buffer);
  121. }
  122. XlsxRelationship Relationships::getRelationshipById(const QString &id) const
  123. {
  124. for (const XlsxRelationship &ship : m_relationships) {
  125. if (ship.id == id)
  126. return ship;
  127. }
  128. return XlsxRelationship();
  129. }
  130. void Relationships::clear()
  131. {
  132. m_relationships.clear();
  133. }
  134. int Relationships::count() const
  135. {
  136. return m_relationships.count();
  137. }
  138. bool Relationships::isEmpty() const
  139. {
  140. return m_relationships.isEmpty();
  141. }
  142. QT_END_NAMESPACE_XLSX