Mercurial > clickerconvert
comparison src/xlsx/xlsxrelationships.cpp @ 1:93d3106bb9a4
Add qt xlsx library
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Tue, 22 Mar 2016 10:38:08 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:49cd5cc0b072 | 1:93d3106bb9a4 |
---|---|
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 | |
32 namespace QXlsx { | |
33 | |
34 const QString schema_doc = QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/relationships"); | |
35 const QString schema_msPackage = QStringLiteral("http://schemas.microsoft.com/office/2006/relationships"); | |
36 const QString schema_package = QStringLiteral("http://schemas.openxmlformats.org/package/2006/relationships"); | |
37 //const QString schema_worksheet = QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/relationships"); | |
38 Relationships::Relationships() | |
39 { | |
40 } | |
41 | |
42 QList<XlsxRelationship> Relationships::documentRelationships(const QString &relativeType) const | |
43 { | |
44 return relationships(schema_doc + relativeType); | |
45 } | |
46 | |
47 void Relationships::addDocumentRelationship(const QString &relativeType, const QString &target) | |
48 { | |
49 addRelationship(schema_doc + relativeType, target); | |
50 } | |
51 | |
52 QList<XlsxRelationship> Relationships::msPackageRelationships(const QString &relativeType) const | |
53 { | |
54 return relationships(schema_msPackage + relativeType); | |
55 } | |
56 | |
57 void Relationships::addMsPackageRelationship(const QString &relativeType, const QString &target) | |
58 { | |
59 addRelationship(schema_msPackage + relativeType, target); | |
60 } | |
61 | |
62 QList<XlsxRelationship> Relationships::packageRelationships(const QString &relativeType) const | |
63 { | |
64 return relationships(schema_package + relativeType); | |
65 } | |
66 | |
67 void Relationships::addPackageRelationship(const QString &relativeType, const QString &target) | |
68 { | |
69 addRelationship(schema_package + relativeType, target); | |
70 } | |
71 | |
72 QList<XlsxRelationship> Relationships::worksheetRelationships(const QString &relativeType) const | |
73 { | |
74 return relationships(schema_doc + relativeType); | |
75 } | |
76 | |
77 void Relationships::addWorksheetRelationship(const QString &relativeType, const QString &target, const QString &targetMode) | |
78 { | |
79 addRelationship(schema_doc + relativeType, target, targetMode); | |
80 } | |
81 | |
82 QList<XlsxRelationship> Relationships::relationships(const QString &type) const | |
83 { | |
84 QList<XlsxRelationship> res; | |
85 foreach (XlsxRelationship ship, m_relationships) { | |
86 if (ship.type == type) | |
87 res.append(ship); | |
88 } | |
89 return res; | |
90 } | |
91 | |
92 void Relationships::addRelationship(const QString &type, const QString &target, const QString &targetMode) | |
93 { | |
94 XlsxRelationship relation; | |
95 relation.id = QStringLiteral("rId%1").arg(m_relationships.size()+1); | |
96 relation.type = type; | |
97 relation.target = target; | |
98 relation.targetMode = targetMode; | |
99 | |
100 m_relationships.append(relation); | |
101 } | |
102 | |
103 void Relationships::saveToXmlFile(QIODevice *device) const | |
104 { | |
105 QXmlStreamWriter writer(device); | |
106 | |
107 writer.writeStartDocument(QStringLiteral("1.0"), true); | |
108 writer.writeStartElement(QStringLiteral("Relationships")); | |
109 writer.writeAttribute(QStringLiteral("xmlns"), QStringLiteral("http://schemas.openxmlformats.org/package/2006/relationships")); | |
110 foreach (XlsxRelationship relation, m_relationships) { | |
111 writer.writeStartElement(QStringLiteral("Relationship")); | |
112 writer.writeAttribute(QStringLiteral("Id"), relation.id); | |
113 writer.writeAttribute(QStringLiteral("Type"), relation.type); | |
114 writer.writeAttribute(QStringLiteral("Target"), relation.target); | |
115 if (!relation.targetMode.isNull()) | |
116 writer.writeAttribute(QStringLiteral("TargetMode"), relation.targetMode); | |
117 writer.writeEndElement(); | |
118 } | |
119 writer.writeEndElement();//Relationships | |
120 writer.writeEndDocument(); | |
121 } | |
122 | |
123 QByteArray Relationships::saveToXmlData() const | |
124 { | |
125 QByteArray data; | |
126 QBuffer buffer(&data); | |
127 buffer.open(QIODevice::WriteOnly); | |
128 saveToXmlFile(&buffer); | |
129 | |
130 return data; | |
131 } | |
132 | |
133 bool Relationships::loadFromXmlFile(QIODevice *device) | |
134 { | |
135 clear(); | |
136 QXmlStreamReader reader(device); | |
137 while (!reader.atEnd()) { | |
138 QXmlStreamReader::TokenType token = reader.readNext(); | |
139 if (token == QXmlStreamReader::StartElement) { | |
140 if (reader.name() == QStringLiteral("Relationship")) { | |
141 QXmlStreamAttributes attributes = reader.attributes(); | |
142 XlsxRelationship relationship; | |
143 relationship.id = attributes.value(QLatin1String("Id")).toString(); | |
144 relationship.type = attributes.value(QLatin1String("Type")).toString(); | |
145 relationship.target = attributes.value(QLatin1String("Target")).toString(); | |
146 relationship.targetMode = attributes.value(QLatin1String("TargetMode")).toString(); | |
147 m_relationships.append(relationship); | |
148 } | |
149 } | |
150 | |
151 if (reader.hasError()) | |
152 return false; | |
153 } | |
154 return true; | |
155 } | |
156 | |
157 bool Relationships::loadFromXmlData(const QByteArray &data) | |
158 { | |
159 QBuffer buffer; | |
160 buffer.setData(data); | |
161 buffer.open(QIODevice::ReadOnly); | |
162 return loadFromXmlFile(&buffer); | |
163 } | |
164 | |
165 XlsxRelationship Relationships::getRelationshipById(const QString &id) const | |
166 { | |
167 foreach (XlsxRelationship ship, m_relationships) { | |
168 if (ship.id == id) | |
169 return ship; | |
170 } | |
171 return XlsxRelationship(); | |
172 } | |
173 | |
174 void Relationships::clear() | |
175 { | |
176 m_relationships.clear(); | |
177 } | |
178 | |
179 int Relationships::count() const | |
180 { | |
181 return m_relationships.count(); | |
182 } | |
183 | |
184 bool Relationships::isEmpty() const | |
185 { | |
186 return m_relationships.isEmpty(); | |
187 } | |
188 | |
189 } //namespace |