-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathManifestXML_Handler.cpp
372 lines (314 loc) · 9.88 KB
/
ManifestXML_Handler.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// ManifestXML_Handler.cpp: implementation of the ManifestXML_Handler class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ManifestXML_Handler.h"
#include "utils.h"
#include <xercesc/sax2/Attributes.hpp>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
/**
* Constructor for Manifest Handler.
*
* Initialised the data mambers that will be used to collect
* manifest information.
*
* Initialises the state machines that are used by the event
* handlers that retrieve XML data.
*
* @param debugEdit a pointer to an edit box used for debug
* this can be NULL but only if #define DEBUG_TO_EDIT is
* commented out in teh Debug function for this class
*
* @param impressionList a pointer to a list to place all manifest data that
* is found during parsing
*
*/
ManifestXML_Handler::ManifestXML_Handler(CEdit* debugEdit,
CList <ManifestData, ManifestData>* ManifestList)
{
m_OutputEdit = debugEdit;
pManifestList = ManifestList;
ManifestXML_State = FINDING_FILE;
XML_CharState = MANIFEST_INVALID;
InitCurrentManifest();
}
void ManifestXML_Handler::Reset_Handler(CEdit* debugEdit,
CList <ManifestData, ManifestData>* ManifestList)
{
m_OutputEdit = debugEdit;
pManifestList = ManifestList;
ManifestXML_State = FINDING_FILE;
XML_CharState = MANIFEST_INVALID;
InitCurrentManifest();
}
ManifestXML_Handler::~ManifestXML_Handler()
{
OutDebugs( "ManifestXML_Handler::~ManifestXML_Handler()" );
}
/**
* Event handler that is invoked when text is found between tags.
*
* A State machine that populates the current manifest member
* with string text found between start and end tags
* i.e <tag>String Text</tag>
*
* @params parametes are as specified by the SAX2 interface
* of a DefaultHandler class
*/
void ManifestXML_Handler::characters(const XMLCh* const chars,
const unsigned int length)
{
char* inText = XMLString::transcode(chars);
switch (XML_CharState)
{
case MANIFEST_IMAGE :
currentManifest.manifImageFile = inText;
Debug("Got the IMAGE");
break;
case MANIFEST_MULTIMEDIA :
currentManifest.manifMultimediaFile = inText;
Debug("Got the MULTIMEDIA");
break;
case MANIFEST_PRINT :
currentManifest.manifPrintFile = inText;
Debug("Got the PRINT");
break;
}
}
/**
* Event handler that is invoked when an start tag is found
*
* A State machine that populates the current manifest menber
* when an open tag is found and also controls the state of
* the charactes stage machine
*
* @params parametes are as specified by the SAX2 interface
* of a DefaultHandler class
*/
void ManifestXML_Handler::startElement(const XMLCh* const uri,
const XMLCh* const localname,
const XMLCh* const qname,
const Attributes& attrs)
{
char* element = XMLString::transcode(qname);
CString elementString = element;
XMLString::release(&element);
/* variables used to extact attibute strings */
unsigned int len = attrs.getLength();
char* QName;
char* Value;
Debug(elementString);
switch (ManifestXML_State)
{
case FINDING_FILE :
if (elementString.Compare("file") == 0)
{
ManifestXML_State = FOUND_FILE;
Debug("FOUND file");
for (unsigned int attrCount = 0; attrCount < len; attrCount++)
{
QName = XMLString::transcode(attrs.getQName((short) attrCount));
CString QNameString = QName;
XMLString::release(&QName);
Value = XMLString::transcode(attrs.getValue((short) attrCount));
if (QNameString.Compare("method") == 0)
{
currentManifest.manifMethod = Value;
gotFileMethod = true;
}
if (QNameString.Compare("date_required") == 0)
{
currentManifest.manifDateRequired = Value;
gotDateRequired = true;
}
QNameString.Empty();
}
}
break;
case FOUND_FILE :
if (elementString.Compare("image_file") == 0)
{
Debug("FOUND image_file");
XML_CharState = MANIFEST_IMAGE;
gotImageFile = true;
for (unsigned int attrCount = 0; attrCount < len; attrCount++)
{
QName = XMLString::transcode(attrs.getQName((short) attrCount));
CString QNameString = QName;
XMLString::release(&QName);
Value = XMLString::transcode(attrs.getValue((short) attrCount));
if (QNameString.Compare("type") == 0)
{
currentManifest.manifImageFileType = Value;
gotImageFileType = true;
}
QNameString.Empty();
}
}
if (elementString.Compare("multimedia_file") == 0)
{
Debug("FOUND multimedia_file");
XML_CharState = MANIFEST_MULTIMEDIA;
gotMultimediaFile = true;
for (unsigned int attrCount = 0; attrCount < len; attrCount++)
{
QName = XMLString::transcode(attrs.getQName((short) attrCount));
CString QNameString = QName;
XMLString::release(&QName);
Value = XMLString::transcode(attrs.getValue((short) attrCount));
if (QNameString.Compare("type") == 0)
{
currentManifest.manifMultimediaFileType = Value;
gotMultimediaFileType = true;
}
QNameString.Empty();
}
}
if (elementString.Compare("print_file") == 0)
{
Debug("FOUND print_file");
XML_CharState = MANIFEST_PRINT;
gotPrintFile = true;
for (unsigned int attrCount = 0; attrCount < len; attrCount++)
{
QName = XMLString::transcode(attrs.getQName((short) attrCount));
CString QNameString = QName;
XMLString::release(&QName);
Value = XMLString::transcode(attrs.getValue((short) attrCount));
if (QNameString.Compare("type") == 0)
{
currentManifest.manifPrintFileType = Value;
gotPrintFileType = true;
}
QNameString.Empty();
}
}
break;
}
elementString.Empty();
}
/**
* Event handler that is invoked when an end tag is found
*
* A State machine controls the state of the charactes stage machine
*
*
* @params parametes are as specified by the SAX2 interface
* of a DefaultHandler class
*/
void ManifestXML_Handler::endElement(const XMLCh* const uri,
const XMLCh* const localname,
const XMLCh* const qname)
{
char* element = XMLString::transcode(qname);
CString elementString = element;
XMLString::release(&element);
Debug(elementString);
/* Make sure we are not processing the character input*/
XML_CharState = MANIFEST_INVALID;
switch (ManifestXML_State)
{
case FOUND_FILE :
if (elementString.Compare("file") == 0)
{
Debug("FOUND /file");
if ( (gotImageFile == true)
&& (gotMultimediaFile == true)
&& (gotPrintFile == true) )
{
Debug(" Found COMPLETE file manifest");
pManifestList->AddTail(currentManifest);
}
else
{
/* didn't find all the relevant data log the error and purge the data */
Debug(" INCOMPLETE file Manifest found");
}
InitCurrentManifest();
ManifestXML_State = FINDING_FILE;
}
break;
case FINDING_FILE :
InitCurrentManifest();
break;
}
}
/**
* If the XML file is misformed then this event handler gets called
*
* Just does debug for now
*
* TODO: Check what happens if Validation is switched off and there
* is an error in the XML.
*
*/
void ManifestXML_Handler::fatalError(const SAXParseException& exception)
{
char* message = XMLString::transcode(exception.getMessage());
CString errorMessage;
long i = exception.getLineNumber();
errorMessage.Format("Fatal Error: %s at line: %i", message, i);
Debug(errorMessage);
}
/**
* Initialises all the local variables of the Current manifest
* and the flags that signify if a certain element has been found
*
*/
void ManifestXML_Handler::InitCurrentManifest()
{
gotFileMethod = false;
gotDateRequired = false;
gotImageFile = false;
gotImageFileType = false;
gotMultimediaFile = false;
gotMultimediaFileType = false;
gotPrintFile = false;
gotPrintFileType = false;
currentManifest.manifMethod = "";
currentManifest.manifDateRequired = "";
currentManifest.manifImageFile = "";
currentManifest.manifImageFileType = "";
currentManifest.manifMultimediaFile = "";
currentManifest.manifMultimediaFileType = "";
currentManifest.manifPrintFile = "";
currentManifest.manifPrintFileType = "";
}
/**
* Writes a debug string to a file or an Edit box
*
* For speed purposes this code is #define out.
*
* Uncomment the appropriate #define section when debug
* is required
*/
void ManifestXML_Handler::Debug(CString debugMessage)
{
//#define DEBUG_TO_EDIT
#ifdef DEBUG_TO_EDIT
if (m_OutputEdit->m_hWnd != 0)
{
CString tempString;
this->m_OutputEdit->GetWindowText(tempString);
this->m_OutputEdit->SetWindowText(tempString + debugMessage + "\r\n");
}
#endif
//#define DEBUG_TO_FILE
#ifdef DUBUG_TO_FILE
CStdioFile* errorFile = new CStdioFile("C:\\debug.txt", CFile::modeCreate
| CFile::modeNoTruncate
| CFile::modeWrite );
LPCTSTR errorMessage = (LPCTSTR)debugMessage;
errorFile->Seek(0, CFile::end);
errorFile->WriteString(errorMessage);
errorFile->WriteString("\n");
errorFile->Close();
#endif
}