andre@1: /**************************************************************************** andre@1: ** Copyright (c) 2013-2014 Debao Zhang andre@1: ** All right reserved. andre@1: ** andre@1: ** Permission is hereby granted, free of charge, to any person obtaining andre@1: ** a copy of this software and associated documentation files (the andre@1: ** "Software"), to deal in the Software without restriction, including andre@1: ** without limitation the rights to use, copy, modify, merge, publish, andre@1: ** distribute, sublicense, and/or sell copies of the Software, and to andre@1: ** permit persons to whom the Software is furnished to do so, subject to andre@1: ** the following conditions: andre@1: ** andre@1: ** The above copyright notice and this permission notice shall be andre@1: ** included in all copies or substantial portions of the Software. andre@1: ** andre@1: ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, andre@1: ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF andre@1: ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND andre@1: ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE andre@1: ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION andre@1: ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION andre@1: ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. andre@1: ** andre@1: ****************************************************************************/ andre@1: #include "xlsxrelationships_p.h" andre@1: #include andre@1: #include andre@1: #include andre@1: #include andre@1: #include andre@1: andre@1: namespace QXlsx { andre@1: andre@1: const QString schema_doc = QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/relationships"); andre@1: const QString schema_msPackage = QStringLiteral("http://schemas.microsoft.com/office/2006/relationships"); andre@1: const QString schema_package = QStringLiteral("http://schemas.openxmlformats.org/package/2006/relationships"); andre@1: //const QString schema_worksheet = QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/relationships"); andre@1: Relationships::Relationships() andre@1: { andre@1: } andre@1: andre@1: QList Relationships::documentRelationships(const QString &relativeType) const andre@1: { andre@1: return relationships(schema_doc + relativeType); andre@1: } andre@1: andre@1: void Relationships::addDocumentRelationship(const QString &relativeType, const QString &target) andre@1: { andre@1: addRelationship(schema_doc + relativeType, target); andre@1: } andre@1: andre@1: QList Relationships::msPackageRelationships(const QString &relativeType) const andre@1: { andre@1: return relationships(schema_msPackage + relativeType); andre@1: } andre@1: andre@1: void Relationships::addMsPackageRelationship(const QString &relativeType, const QString &target) andre@1: { andre@1: addRelationship(schema_msPackage + relativeType, target); andre@1: } andre@1: andre@1: QList Relationships::packageRelationships(const QString &relativeType) const andre@1: { andre@1: return relationships(schema_package + relativeType); andre@1: } andre@1: andre@1: void Relationships::addPackageRelationship(const QString &relativeType, const QString &target) andre@1: { andre@1: addRelationship(schema_package + relativeType, target); andre@1: } andre@1: andre@1: QList Relationships::worksheetRelationships(const QString &relativeType) const andre@1: { andre@1: return relationships(schema_doc + relativeType); andre@1: } andre@1: andre@1: void Relationships::addWorksheetRelationship(const QString &relativeType, const QString &target, const QString &targetMode) andre@1: { andre@1: addRelationship(schema_doc + relativeType, target, targetMode); andre@1: } andre@1: andre@1: QList Relationships::relationships(const QString &type) const andre@1: { andre@1: QList res; andre@1: foreach (XlsxRelationship ship, m_relationships) { andre@1: if (ship.type == type) andre@1: res.append(ship); andre@1: } andre@1: return res; andre@1: } andre@1: andre@1: void Relationships::addRelationship(const QString &type, const QString &target, const QString &targetMode) andre@1: { andre@1: XlsxRelationship relation; andre@1: relation.id = QStringLiteral("rId%1").arg(m_relationships.size()+1); andre@1: relation.type = type; andre@1: relation.target = target; andre@1: relation.targetMode = targetMode; andre@1: andre@1: m_relationships.append(relation); andre@1: } andre@1: andre@1: void Relationships::saveToXmlFile(QIODevice *device) const andre@1: { andre@1: QXmlStreamWriter writer(device); andre@1: andre@1: writer.writeStartDocument(QStringLiteral("1.0"), true); andre@1: writer.writeStartElement(QStringLiteral("Relationships")); andre@1: writer.writeAttribute(QStringLiteral("xmlns"), QStringLiteral("http://schemas.openxmlformats.org/package/2006/relationships")); andre@1: foreach (XlsxRelationship relation, m_relationships) { andre@1: writer.writeStartElement(QStringLiteral("Relationship")); andre@1: writer.writeAttribute(QStringLiteral("Id"), relation.id); andre@1: writer.writeAttribute(QStringLiteral("Type"), relation.type); andre@1: writer.writeAttribute(QStringLiteral("Target"), relation.target); andre@1: if (!relation.targetMode.isNull()) andre@1: writer.writeAttribute(QStringLiteral("TargetMode"), relation.targetMode); andre@1: writer.writeEndElement(); andre@1: } andre@1: writer.writeEndElement();//Relationships andre@1: writer.writeEndDocument(); andre@1: } andre@1: andre@1: QByteArray Relationships::saveToXmlData() const andre@1: { andre@1: QByteArray data; andre@1: QBuffer buffer(&data); andre@1: buffer.open(QIODevice::WriteOnly); andre@1: saveToXmlFile(&buffer); andre@1: andre@1: return data; andre@1: } andre@1: andre@1: bool Relationships::loadFromXmlFile(QIODevice *device) andre@1: { andre@1: clear(); andre@1: QXmlStreamReader reader(device); andre@1: while (!reader.atEnd()) { andre@1: QXmlStreamReader::TokenType token = reader.readNext(); andre@1: if (token == QXmlStreamReader::StartElement) { andre@1: if (reader.name() == QStringLiteral("Relationship")) { andre@1: QXmlStreamAttributes attributes = reader.attributes(); andre@1: XlsxRelationship relationship; andre@1: relationship.id = attributes.value(QLatin1String("Id")).toString(); andre@1: relationship.type = attributes.value(QLatin1String("Type")).toString(); andre@1: relationship.target = attributes.value(QLatin1String("Target")).toString(); andre@1: relationship.targetMode = attributes.value(QLatin1String("TargetMode")).toString(); andre@1: m_relationships.append(relationship); andre@1: } andre@1: } andre@1: andre@1: if (reader.hasError()) andre@1: return false; andre@1: } andre@1: return true; andre@1: } andre@1: andre@1: bool Relationships::loadFromXmlData(const QByteArray &data) andre@1: { andre@1: QBuffer buffer; andre@1: buffer.setData(data); andre@1: buffer.open(QIODevice::ReadOnly); andre@1: return loadFromXmlFile(&buffer); andre@1: } andre@1: andre@1: XlsxRelationship Relationships::getRelationshipById(const QString &id) const andre@1: { andre@1: foreach (XlsxRelationship ship, m_relationships) { andre@1: if (ship.id == id) andre@1: return ship; andre@1: } andre@1: return XlsxRelationship(); andre@1: } andre@1: andre@1: void Relationships::clear() andre@1: { andre@1: m_relationships.clear(); andre@1: } andre@1: andre@1: int Relationships::count() const andre@1: { andre@1: return m_relationships.count(); andre@1: } andre@1: andre@1: bool Relationships::isEmpty() const andre@1: { andre@1: return m_relationships.isEmpty(); andre@1: } andre@1: andre@1: } //namespace