xlsxrelationships.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /****************************************************************************
  2. ** Copyright (c) 2013-2014 Debao Zhang <hello@debao.me>
  3. ** All right reserved.
  4. **
  5. ** Permission is hereby granted, free of charge, to any person obtaining
  6. ** a copy of this software and associated documentation files (the
  7. ** "Software"), to deal in the Software without restriction, including
  8. ** without limitation the rights to use, copy, modify, merge, publish,
  9. ** distribute, sublicense, and/or sell copies of the Software, and to
  10. ** permit persons to whom the Software is furnished to do so, subject to
  11. ** the following conditions:
  12. **
  13. ** The above copyright notice and this permission notice shall be
  14. ** included in all copies or substantial portions of the Software.
  15. **
  16. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. **
  24. ****************************************************************************/
  25. #include "xlsxrelationships_p.h"
  26. #include <QXmlStreamWriter>
  27. #include <QXmlStreamReader>
  28. #include <QDir>
  29. #include <QFile>
  30. #include <QBuffer>
  31. namespace QXlsx {
  32. const QString schema_doc = QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/relationships");
  33. const QString schema_msPackage = QStringLiteral("http://schemas.microsoft.com/office/2006/relationships");
  34. const QString schema_package = QStringLiteral("http://schemas.openxmlformats.org/package/2006/relationships");
  35. //const QString schema_worksheet = QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/relationships");
  36. Relationships::Relationships()
  37. {
  38. }
  39. QList<XlsxRelationship> Relationships::documentRelationships(const QString &relativeType) const
  40. {
  41. return relationships(schema_doc + relativeType);
  42. }
  43. void Relationships::addDocumentRelationship(const QString &relativeType, const QString &target)
  44. {
  45. addRelationship(schema_doc + relativeType, target);
  46. }
  47. QList<XlsxRelationship> Relationships::msPackageRelationships(const QString &relativeType) const
  48. {
  49. return relationships(schema_msPackage + relativeType);
  50. }
  51. void Relationships::addMsPackageRelationship(const QString &relativeType, const QString &target)
  52. {
  53. addRelationship(schema_msPackage + relativeType, target);
  54. }
  55. QList<XlsxRelationship> Relationships::packageRelationships(const QString &relativeType) const
  56. {
  57. return relationships(schema_package + relativeType);
  58. }
  59. void Relationships::addPackageRelationship(const QString &relativeType, const QString &target)
  60. {
  61. addRelationship(schema_package + relativeType, target);
  62. }
  63. QList<XlsxRelationship> Relationships::worksheetRelationships(const QString &relativeType) const
  64. {
  65. return relationships(schema_doc + relativeType);
  66. }
  67. void Relationships::addWorksheetRelationship(const QString &relativeType, const QString &target, const QString &targetMode)
  68. {
  69. addRelationship(schema_doc + relativeType, target, targetMode);
  70. }
  71. QList<XlsxRelationship> Relationships::relationships(const QString &type) const
  72. {
  73. QList<XlsxRelationship> res;
  74. foreach (XlsxRelationship ship, m_relationships) {
  75. if (ship.type == type)
  76. res.append(ship);
  77. }
  78. return res;
  79. }
  80. void Relationships::addRelationship(const QString &type, const QString &target, const QString &targetMode)
  81. {
  82. XlsxRelationship relation;
  83. relation.id = QStringLiteral("rId%1").arg(m_relationships.size()+1);
  84. relation.type = type;
  85. relation.target = target;
  86. relation.targetMode = targetMode;
  87. m_relationships.append(relation);
  88. }
  89. void Relationships::saveToXmlFile(QIODevice *device) const
  90. {
  91. QXmlStreamWriter writer(device);
  92. writer.writeStartDocument(QStringLiteral("1.0"), true);
  93. writer.writeStartElement(QStringLiteral("Relationships"));
  94. writer.writeAttribute(QStringLiteral("xmlns"), QStringLiteral("http://schemas.openxmlformats.org/package/2006/relationships"));
  95. foreach (XlsxRelationship relation, m_relationships) {
  96. writer.writeStartElement(QStringLiteral("Relationship"));
  97. writer.writeAttribute(QStringLiteral("Id"), relation.id);
  98. writer.writeAttribute(QStringLiteral("Type"), relation.type);
  99. writer.writeAttribute(QStringLiteral("Target"), relation.target);
  100. if (!relation.targetMode.isNull())
  101. writer.writeAttribute(QStringLiteral("TargetMode"), relation.targetMode);
  102. writer.writeEndElement();
  103. }
  104. writer.writeEndElement();//Relationships
  105. writer.writeEndDocument();
  106. }
  107. QByteArray Relationships::saveToXmlData() const
  108. {
  109. QByteArray data;
  110. QBuffer buffer(&data);
  111. buffer.open(QIODevice::WriteOnly);
  112. saveToXmlFile(&buffer);
  113. return data;
  114. }
  115. bool Relationships::loadFromXmlFile(QIODevice *device)
  116. {
  117. clear();
  118. QXmlStreamReader reader(device);
  119. while (!reader.atEnd()) {
  120. QXmlStreamReader::TokenType token = reader.readNext();
  121. if (token == QXmlStreamReader::StartElement) {
  122. if (reader.name() == QStringLiteral("Relationship")) {
  123. QXmlStreamAttributes attributes = reader.attributes();
  124. XlsxRelationship relationship;
  125. relationship.id = attributes.value(QLatin1String("Id")).toString();
  126. relationship.type = attributes.value(QLatin1String("Type")).toString();
  127. relationship.target = attributes.value(QLatin1String("Target")).toString();
  128. relationship.targetMode = attributes.value(QLatin1String("TargetMode")).toString();
  129. m_relationships.append(relationship);
  130. }
  131. }
  132. if (reader.hasError())
  133. return false;
  134. }
  135. return true;
  136. }
  137. bool Relationships::loadFromXmlData(const QByteArray &data)
  138. {
  139. QBuffer buffer;
  140. buffer.setData(data);
  141. buffer.open(QIODevice::ReadOnly);
  142. return loadFromXmlFile(&buffer);
  143. }
  144. XlsxRelationship Relationships::getRelationshipById(const QString &id) const
  145. {
  146. foreach (XlsxRelationship ship, m_relationships) {
  147. if (ship.id == id)
  148. return ship;
  149. }
  150. return XlsxRelationship();
  151. }
  152. void Relationships::clear()
  153. {
  154. m_relationships.clear();
  155. }
  156. int Relationships::count() const
  157. {
  158. return m_relationships.count();
  159. }
  160. bool Relationships::isEmpty() const
  161. {
  162. return m_relationships.isEmpty();
  163. }
  164. } //namespace