-
Notifications
You must be signed in to change notification settings - Fork 0
/
lowriter.cpp
352 lines (290 loc) · 15.8 KB
/
lowriter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <QMessageBox>
#include <QString>
#include <QStringList>
#include <QFileInfo>
#include <quazip.h>
#include <quazipfile.h>
#include <quazipnewinfo.h>
#include <QFlags>
#include <QModelIndex>
#include <QXmlStreamWriter>
#include "lowriter.h"
#include "enums.h"
loWriter::loWriter(const QFileInfo *file, const loModel *tree, QObject *parent) :
QObject(parent)
{
QuaZip *loFile = new QuaZip(file->absoluteFilePath()) ;
if(!loFile->open(QuaZip::mdCreate)) {
QMessageBox::critical(0,tr("Save Error"),tr("Cannot create ZIP file.")) ;
delete loFile ;
this->deleteLater() ;
return ;
}
QFile tmpFile ;
QuaZipFile writeFile ;
writeFile.setZip(loFile);
// We write the common (XML stuff) files to the archive
QStringList commonFiles;
commonFiles << "ejmd_v1.xsd" << "imscp_v1p1.xsd" << "imsmd_v1p2p4.xsd" ;
while(!commonFiles.isEmpty()) {
QString tmpFileName = commonFiles.takeFirst() ;
QuaZipNewInfo tmpFileInfo(tmpFileName) ;
writeFile.open(QIODevice::WriteOnly,tmpFileInfo) ;
tmpFile.setFileName(":/xml/" + tmpFileName);
tmpFile.open(QIODevice::ReadOnly) ;
writeFile.write(tmpFile.readAll()) ;
tmpFile.close() ;
writeFile.close() ;
}
// Now we write all the resources created in the data tree
for(int i=0;i<tree->rowCount();i++) {
QFlags<ItemType::ItemTypes> itemType(tree->index(i,0).data(Qt::UserRole+1).toInt()) ;
if(itemType.testFlag(ItemType::FoldResources)) {
for(int j=0;j<tree->rowCount(tree->index(i,0));j++) {
for(int k=0;k<tree->rowCount(tree->index(j,0,tree->index(i,0)));k++) {
QFlags<ItemType::ItemTypes> resType(tree->index(k,0,tree->index(j,0,tree->index(i,0))).data(Qt::UserRole+1).toInt()) ;
if(resType.testFlag(ItemType::File)) {
QuaZipNewInfo tmpFileInfo(tree->index(k,0,tree->index(j,0,tree->index(i,0))).data(Qt::DisplayRole).toString()) ;
writeFile.open(QIODevice::WriteOnly,tmpFileInfo) ;
writeFile.write(tree->index(k,0,tree->index(j,0,tree->index(i,0))).data(Qt::UserRole).toByteArray()) ;
writeFile.close();
}
}
}
}
}
// And finally let's compose and write the lo manifest
QuaZipNewInfo manifestInfo("imsmanifest.xml") ;
writeFile.open(QIODevice::WriteOnly,manifestInfo) ;
QXmlStreamWriter xmlOutput(&writeFile) ;
ImsmdMetadata *imsmdData = &qVariantValue<ImsmdMetadata>(tree->index(0,0).data(Qt::UserRole)) ;
xmlOutput.setAutoFormatting(TRUE);
xmlOutput.writeStartDocument();
xmlOutput.writeStartElement("manifest");
xmlOutput.writeAttribute("xmlns","http://www.imsglobal.org/xsd/imscp_v1p1");
xmlOutput.writeAttribute("xmlns:imsmd","http://www.imsglobal.org/xsd/imsmd_v1p2");
xmlOutput.writeAttribute("xmlns:ejmd","http://www.edujudge.eu/ejmd_v2");
xmlOutput.writeAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
xmlOutput.writeAttribute("identifier","TMPIDENTIFIER");
xmlOutput.writeAttribute("xsi:schemaLocation","http://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p4.xsd http://www.edujudge.eu/ejmd_v2 ejmd_v2.xsd");
xmlOutput.writeStartElement("metadata");
xmlOutput.writeTextElement("schema","IMS Content");
xmlOutput.writeTextElement("schemaversion","1.1");
xmlOutput.writeStartElement("imsmd:lom");
xmlOutput.writeStartElement("imsmd:general");
xmlOutput.writeTextElement("imsmd:identifier",imsmdData->general->identifier);
xmlOutput.writeTextElement("imsmd:language",imsmdData->general->language);
xmlOutput.writeStartElement("imsmd:title");
writeLangString(&xmlOutput,imsmdData->general->title) ;
xmlOutput.writeEndElement() ; // imsmd:title
xmlOutput.writeStartElement("imsmd:description");
writeLangString(&xmlOutput,imsmdData->general->description) ;
xmlOutput.writeEndElement(); // imsmd:description
xmlOutput.writeEndElement() ; // imsmd:general
xmlOutput.writeStartElement("imsmd:lifecycle") ;
xmlOutput.writeStartElement("imsmd:version");
writeLangString(&xmlOutput,imsmdData->lifecycle->version) ;
xmlOutput.writeEndElement(); // imsmd:version
xmlOutput.writeStartElement("imsmd:status");
writeImsmdItem(&xmlOutput,imsmdData->lifecycle->status) ;
xmlOutput.writeEndElement(); // imsmd:status
writeContributeItem(&xmlOutput,imsmdData->lifecycle->contribute) ;
xmlOutput.writeEndElement() ; // imsmd:lifecycle
xmlOutput.writeStartElement("imsmd:technical") ;
writeFormatData(&xmlOutput,imsmdData->technical->format) ;
writeRequirementData(&xmlOutput,imsmdData->technical->requirement) ;
xmlOutput.writeEndElement() ; // imsmd:technical
xmlOutput.writeStartElement("imsmd:educational") ;
xmlOutput.writeStartElement("imsmd:interactivitytype");
writeImsmdItem(&xmlOutput,imsmdData->educational->interactivitytype) ;
xmlOutput.writeEndElement(); // imsmd:interactivitytype
xmlOutput.writeStartElement("imsmd:learningresourcetype");
writeImsmdItem(&xmlOutput,imsmdData->educational->learningresourcetype) ;
xmlOutput.writeEndElement(); // imsmd:learningresourcetype
xmlOutput.writeStartElement("imsmd:interactivitylevel");
writeImsmdItem(&xmlOutput,imsmdData->educational->interactivitylevel) ;
xmlOutput.writeEndElement(); // imsmd:interactivitylevel
xmlOutput.writeStartElement("imsmd:intendedenduserrole");
writeImsmdItem(&xmlOutput,imsmdData->educational->intendedenduserrole) ;
xmlOutput.writeEndElement(); // imsmd:intendedenduserrole
xmlOutput.writeStartElement("imsmd:context");
writeImsmdItem(&xmlOutput,imsmdData->educational->context) ;
xmlOutput.writeEndElement(); // imsmd:context
xmlOutput.writeStartElement("imsmd:typicalagerange");
writeLangString(&xmlOutput,imsmdData->educational->typicalagerange) ;
xmlOutput.writeEndElement(); // imsmd:typicalagerange
xmlOutput.writeStartElement("imsmd:typicallearningtime");
xmlOutput.writeTextElement("imsmd:datetime",imsmdData->educational->typicallearningtime);
xmlOutput.writeEndElement(); // imsmd:typicallearningtime
xmlOutput.writeStartElement("imsmd:description");
writeLangString(&xmlOutput,imsmdData->educational->description) ;
xmlOutput.writeEndElement(); // imsmd:description
xmlOutput.writeTextElement("imsmd:language",imsmdData->educational->language);
xmlOutput.writeEndElement() ; // imsmd:educational
xmlOutput.writeStartElement("imsmd:rights") ;
xmlOutput.writeStartElement("imsmd:cost");
writeImsmdItem(&xmlOutput,imsmdData->rights->cost) ;
xmlOutput.writeEndElement(); // imsmd:cost
xmlOutput.writeStartElement("imsmd:copyrightandotherrestrictions");
writeImsmdItem(&xmlOutput,imsmdData->rights->copyrightandotherrestrictions) ;
xmlOutput.writeEndElement(); // imsmd:copyrightandotherrestrictions
xmlOutput.writeStartElement("imsmd:description");
writeLangString(&xmlOutput,imsmdData->rights->description) ;
xmlOutput.writeEndElement();
xmlOutput.writeEndElement() ; // imsmd:rights
xmlOutput.writeStartElement("imsmd:classification") ;
xmlOutput.writeStartElement("imsmd:purpose");
writeImsmdItem(&xmlOutput,imsmdData->classification->purpose) ;
xmlOutput.writeEndElement(); // imsmd:purpose
xmlOutput.writeStartElement("imsmd:keyword");
writeLangString(&xmlOutput,imsmdData->classification->keyword) ;
xmlOutput.writeEndElement(); // imsmd:keyword
xmlOutput.writeEndElement() ; // imsmd:classification
xmlOutput.writeEndElement() ; // imsmd:lom
EjmdMetadata *ejmdData = &qVariantValue<EjmdMetadata>(tree->index(1,0).data(Qt::UserRole)) ;
xmlOutput.writeStartElement("ejmd:metadata");
xmlOutput.writeStartElement("ejmd:general");
xmlOutput.writeStartElement("ejmd:hints");
xmlOutput.writeStartElement("ejmd:submission");
xmlOutput.writeAttribute("ejmd:time-solve",ejmdData->general->hints->submission->timesolve);
xmlOutput.writeAttribute("ejmd:time-submit",ejmdData->general->hints->submission->timesubmit);
xmlOutput.writeAttribute("ejmd:attempts",QString::number(ejmdData->general->hints->submission->attempts));
xmlOutput.writeAttribute("ejmd:code-lines",QString::number(ejmdData->general->hints->submission->codelines));
xmlOutput.writeAttribute("ejmd:length",QString::number(ejmdData->general->hints->submission->length));
xmlOutput.writeEndElement(); // ejmd:submission
xmlOutput.writeStartElement("ejmd:compilation");
xmlOutput.writeAttribute("ejmd:time",ejmdData->general->hints->compilation->time);
xmlOutput.writeAttribute("ejmd:size",QString::number(ejmdData->general->hints->compilation->size));
xmlOutput.writeEndElement(); // ejmd:compilation
xmlOutput.writeStartElement("ejmd:execution");
xmlOutput.writeAttribute("ejmd:time",ejmdData->general->hints->execution->time);
xmlOutput.writeEndElement(); // ejmd:execution
xmlOutput.writeEndElement(); // ejmd:hints
xmlOutput.writeEndElement(); // ejmd:general
xmlOutput.writeStartElement("ejmd:presentation");
xmlOutput.writeStartElement("ejmd:description");
xmlOutput.writeAttribute("ejmd:resource",ejmdData->presentation->description->resource);
xmlOutput.writeEndElement(); // ejmd:description
xmlOutput.writeEndElement(); // ejmd:presentation
xmlOutput.writeStartElement("ejmd:solution") ;
xmlOutput.writeAttribute("ejmd:resource",ejmdData->solution->resource) ;
xmlOutput.writeAttribute("ejmd:compilationLine",ejmdData->solution->compilationLine);
xmlOutput.writeAttribute("ejmd:executionLine",ejmdData->solution->executionLine);
xmlOutput.writeAttribute("ejmd:language",ejmdData->solution->language);
xmlOutput.writeAttribute("ejmd:languageVersion",ejmdData->solution->languageVersion);
xmlOutput.writeEndElement(); // ejmd:solution
EjmdEvaluation *tmpEvaluation = ejmdData->evaluation ;
while(tmpEvaluation != NULL) {
xmlOutput.writeStartElement("ejmd:evaluation");
xmlOutput.writeAttribute("ejmd:evaluationModel",tmpEvaluation->evaluationModel);
xmlOutput.writeAttribute("ejmd:evaluationModelVersion",QString::number(tmpEvaluation->evaluationModelVersion));
xmlOutput.writeStartElement("ejmd:tests");
EjmdEvaluationTestsTestFiles *tmpTestFiles = tmpEvaluation->tests->testFiles ;
while(tmpTestFiles != NULL) {
xmlOutput.writeStartElement("ejmd:testFiles");
xmlOutput.writeAttribute("ejmd:arguments",tmpTestFiles->arguments);
xmlOutput.writeAttribute("ejmd:valorization",QString::number(tmpTestFiles->valorization));
xmlOutput.writeStartElement("ejmd:input");
xmlOutput.writeAttribute("ejmd:resource",tmpTestFiles->inputResource);
xmlOutput.writeEndElement(); // ejmd:input
xmlOutput.writeStartElement("ejmd:output");
xmlOutput.writeAttribute("ejmd:resource",tmpTestFiles->outputResource);
xmlOutput.writeEndElement(); // ejmd:output
xmlOutput.writeEndElement(); // ejmd:testFiles
tmpTestFiles = tmpTestFiles->next ;
}
xmlOutput.writeEndElement(); // ejmd:tests
xmlOutput.writeEndElement(); // ejmd:evaluation
tmpEvaluation = tmpEvaluation->next ;
}
xmlOutput.writeEndElement(); // ejmd:metadata
xmlOutput.writeEndElement() ; // metadata
xmlOutput.writeStartElement("organizations");
xmlOutput.writeEndElement() ; // organizations
xmlOutput.writeStartElement("resources");
for(int i=0;i<tree->rowCount();i++) {
QFlags<ItemType::ItemTypes> itemType(tree->index(i,0).data(Qt::UserRole+1).toInt()) ;
if(itemType.testFlag(ItemType::FoldResources)) {
for(int j=0;j<tree->rowCount(tree->index(i,0));j++) {
xmlOutput.writeStartElement("resource");
xmlOutput.writeAttribute("identifier",tree->index(j,0,tree->index(i,0)).data(Qt::DisplayRole).toString());
QString tmpResType ;
QFlags<ItemType::ItemTypes> itemType(tree->index(j,0,tree->index(i,0)).data(Qt::UserRole+1).toInt()) ;
if(itemType.testFlag(ItemType::ResInput))
tmpResType = "input" ;
else if(itemType.testFlag(ItemType::ResOutput))
tmpResType = "output" ;
else if(itemType.testFlag(ItemType::ResSolution))
tmpResType = "solution" ;
else if(itemType.testFlag(ItemType::ResWebcontent))
tmpResType = "webcontent" ;
xmlOutput.writeAttribute("type",tmpResType);
if(itemType.testFlag(ItemType::ResWebcontent)) {
xmlOutput.writeAttribute("href",tree->index(j,0,tree->index(i,0)).data(Qt::UserRole).toString());
}
for(int k=0;k<tree->rowCount(tree->index(j,0,tree->index(i,0)));k++) {
xmlOutput.writeStartElement("file");
xmlOutput.writeAttribute("href",tree->index(k,0,tree->index(j,0,tree->index(i,0))).data(Qt::DisplayRole).toString());
xmlOutput.writeEndElement(); // file
}
xmlOutput.writeEndElement(); // resource
}
}
}
xmlOutput.writeEndElement() ; // resources
xmlOutput.writeEndElement() ; // manifest
xmlOutput.writeEndDocument();
writeFile.close();
loFile->close() ;
}
void loWriter::writeLangString(QXmlStreamWriter *streamWriter, LangString *langString) {
while(langString != NULL) {
streamWriter->writeStartElement("imsmd:langstring");
streamWriter->writeAttribute("xml:lang",langString->language);
streamWriter->writeCharacters(langString->content);
streamWriter->writeEndElement();
langString = langString->next ;
}
}
void loWriter::writeImsmdItem(QXmlStreamWriter *streamWriter, ImsmdItem *imsmdItem) {
streamWriter->writeStartElement("imsmd:source");
writeLangString(streamWriter,imsmdItem->source) ;
streamWriter->writeEndElement(); // imsmd:source
streamWriter->writeStartElement("imsmd:value");
writeLangString(streamWriter,imsmdItem->value) ;
streamWriter->writeEndElement(); // imsmd:value
}
void loWriter::writeContributeItem(QXmlStreamWriter *streamWriter, ContributeItem *contItem) {
while(contItem != NULL) {
streamWriter->writeStartElement("imsmd:contribute");
streamWriter->writeStartElement("imsmd:role");
writeImsmdItem(streamWriter,contItem->role) ;
streamWriter->writeEndElement(); // imsmd:role
streamWriter->writeStartElement("imsmd:centity");
streamWriter->writeTextElement("imsmd:vcard",contItem->vcard);
streamWriter->writeEndElement(); // imsmd:centity
streamWriter->writeEndElement(); // imsmd:contribute
contItem = contItem->next ;
}
}
void loWriter::writeFormatData(QXmlStreamWriter *streamWriter, FormatData *formatData) {
while(formatData != NULL) {
streamWriter->writeTextElement("imsmd:format",formatData->format);
formatData = formatData->next ;
}
}
void loWriter::writeRequirementData(QXmlStreamWriter *streamWriter, RequirementData *reqData) {
while(reqData != NULL) {
streamWriter->writeStartElement("imsmd:requirement");
streamWriter->writeStartElement("imsmd:type");
writeImsmdItem(streamWriter,reqData->type) ;
streamWriter->writeEndElement(); // imsmd:type
streamWriter->writeStartElement("imsmd:name");
writeImsmdItem(streamWriter,reqData->name) ;
streamWriter->writeEndElement(); // imsmd:name
streamWriter->writeTextElement("imsmd:minimumversion",reqData->minimumversion);
streamWriter->writeTextElement("imsmd:maximumversion",reqData->maximumversion);
streamWriter->writeEndElement(); // imsmd:requirement
reqData = reqData->next ;
}
}