Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[044716]: contrib / brl / b3p / expatpp / expatpp.cpp Maximize Restore History

Download this file

expatpp.cpp    801 lines (647 with data), 19.1 kB

  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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
// expatpp
#ifdef UNDER_CE
#include <string.h>
#include <windows.h>
#include <dbgapi.h>
#define assert ASSERT
#else
#include <cstring> // for std::strlen() & strcmp()
#include <string>
using namespace std;
#include <assert.h>
#endif
#include "expatpp.h"
// may be defined in xmltchar.h or elsewhere
#ifndef tcscmp
#ifdef XML_UNICODE
#define tcscmp wcscmp
#else
#define tcscmp strcmp
#endif // XML_UNICODE
#endif // tcscmp
#ifndef BUFSIZ
#define BUFSIZ 1024
#endif
expatpp::expatpp(bool createParser) :
mParser(0), // in case of exception below
mHaveParsed(false)
{
if (createParser) {
// subclasses may call this ctor after parser created!
mParser = XML_ParserCreate(0);
SetupHandlers();
}
}
void
expatpp::SetupHandlers()
{
::XML_SetUserData(mParser, this);
::XML_SetElementHandler(mParser, startElementCallback, endElementCallback);
::XML_SetCharacterDataHandler(mParser, charDataCallback);
::XML_SetProcessingInstructionHandler(mParser, processingInstructionCallback);
::XML_SetDefaultHandler(mParser, defaultHandlerCallback);
::XML_SetUnparsedEntityDeclHandler(mParser, unParsedEntityDeclCallback);
::XML_SetNotationDeclHandler(mParser, notationDeclCallback);
::XML_SetNotStandaloneHandler(mParser, notStandaloneHandlerCallback);
::XML_SetNamespaceDeclHandler(mParser, startNamespaceCallback, endNamespaceCallback);
#ifndef EXPATPP_COMPATIBLE_EXPAT12
::XML_SetAttlistDeclHandler(mParser, attlistDeclCallback);
::XML_SetCdataSectionHandler(mParser, startCdataSectionCallback, endCdataSectionCallback);
::XML_SetCommentHandler(mParser, commentCallback);
::XML_SetDoctypeDeclHandler(mParser, startDoctypeDeclCallback, endDoctypeDeclCallback);
::XML_SetElementDeclHandler(mParser, elementDeclCallback);
::XML_SetEntityDeclHandler(mParser, entityDeclCallback);
//::XML_SetSkippedEntityHandler(mParser, skippedEntityCallback);
::XML_SetXmlDeclHandler(mParser, xmlDeclCallback);
#endif
}
expatpp::~expatpp()
{
if (mParser) // allows subclasses to avoid finishing parsing
ReleaseParser();
}
/**
Provide single point that will call XML_ParserFree.
Nothing else in this code should call XML_ParserFree!
*/
void
expatpp::ReleaseParser()
{
::XML_ParserFree(mParser);
mParser = 0;
}
/**
Provide single point that will call XML_ParserReset.
Guarded against trivial reset before use in case that breaks
expat or creates overhead.
\todo pass in encoding to XML_ParserReset when we support encodings
*/
/*void
expatpp::ResetParser()
{
#ifdef EXPATPP_COMPATIBLE_EXPAT12
assert(!"Reset not available in earlier than expat 1.95.3");s
#else
if (mHaveParsed) {
::XML_ParserReset(mParser, NULL);
SetupHandlers();
mHaveParsed = false;
}
#endif
}*/
/**
Parse entire file, basically copy of the loop from the elements.c example.
*/
XML_Status
expatpp::parseFile(FILE* inFile)
{
// ResetParser();
// hack here
// Sep, 2012, Yi Dong: hack to not use BUFSIZ for large file
//char buf[BUFSIZ];
long lSize = 1024*1024*1000;
char* buf = new char[lSize];
int done;
if (!inFile)
return XML_STATUS_ERROR;
fseek(inFile, 0, SEEK_SET); // reset for reading
do {
/*size_t len = fread(buf, 1, sizeof(buf), inFile);
done = len < sizeof(buf);*/
size_t len = fread(buf, 1, lSize, inFile);
done = len < lSize;
enum XML_Status parseStatus;
if ((parseStatus = XML_Parse(buf, len, done))!=XML_STATUS_OK) {
return parseStatus;
}
} while (!done);
delete buf;
return XML_STATUS_OK;
}
XML_Status
expatpp::XML_Parse(const char *s, int len, int isFinal)
{
mHaveParsed = true;
const XML_Status retStatus = (const XML_Status) ::XML_Parse(mParser, s, len, isFinal);
if (isFinal)
CheckFinalStatus(retStatus);
return retStatus;
}
XML_Error
expatpp::XML_GetErrorCode()
{
return ::XML_GetErrorCode(mParser);
}
int
expatpp::XML_GetCurrentLineNumber()
{
return ::XML_GetCurrentLineNumber(mParser);
}
int
expatpp::XML_GetCurrentColumnNumber()
{
return ::XML_GetCurrentColumnNumber(mParser);
}
/**
Parse string which is assumed to be entire XML document.
Written to stop stupid errors of being off by one in the string length causing
wasted debugging time, such as:
\verbatim
const char[] kSampleSettings = "<settings/>";
const int sampleSize = sizeof(kSampleSettings)-1; // unless you remember to subtract one here will get invalid token error
if (!parser.XML_Parse(kSampleSettings, sampleSize, 1)) {
\endverbatim
*/
XML_Status
expatpp::parseString(const char* inString)
{
// ResetParser();
const int inLen = strlen(inString);
return XML_Parse(inString, inLen, 1);
}
void
expatpp::startElementCallback(void *userData, const XML_Char* name, const XML_Char** atts)
{
((expatpp*)userData)->startElement(name, atts);
}
void
expatpp::endElementCallback(void *userData, const XML_Char* name)
{
((expatpp*)userData)->endElement(name);
}
void
expatpp::startNamespaceCallback(void *userData, const XML_Char* prefix, const XML_Char* uri)
{
((expatpp*)userData)->startNamespace(prefix, uri);
}
void
expatpp::endNamespaceCallback(void *userData, const XML_Char* prefix)
{
((expatpp*)userData)->endNamespace(prefix);
}
void
expatpp::charDataCallback(void *userData, const XML_Char* s, int len)
{
((expatpp*)userData)->charData(s, len);
}
void
expatpp:: processingInstructionCallback(void *userData, const XML_Char* target, const XML_Char* data)
{
((expatpp*)userData)->processingInstruction(target, data);
}
void
expatpp::defaultHandlerCallback(void* userData, const XML_Char* s, int len)
{
((expatpp*)userData)->defaultHandler(s, len);
}
int
expatpp::notStandaloneHandlerCallback(void* userData)
{
return ((expatpp*)userData)->notStandaloneHandler();
}
void
expatpp::unParsedEntityDeclCallback(void* userData, const XML_Char* entityName, const XML_Char* base, const XML_Char* systemId, const XML_Char* publicId, const XML_Char* notationName)
{
((expatpp*)userData)->unparsedEntityDecl(entityName, base, systemId, publicId, notationName);
}
void
expatpp::notationDeclCallback(void *userData, const XML_Char* notationName, const XML_Char* base, const XML_Char* systemId, const XML_Char* publicId)
{
((expatpp*)userData)->notationDecl(notationName, base, systemId, publicId);
}
void
expatpp::startElement(const XML_Char*, const XML_Char**)
{}
void
expatpp::endElement(const XML_Char*)
{}
void
expatpp::startNamespace(const XML_Char* /* prefix */, const XML_Char* /* uri */)
{}
void
expatpp::endNamespace(const XML_Char*)
{}
void
expatpp::charData(const XML_Char*, int )
{
}
void
expatpp::processingInstruction(const XML_Char*, const XML_Char*)
{
}
void
expatpp::defaultHandler(const XML_Char*, int)
{
}
int
expatpp::notStandaloneHandler()
{
return 0;
}
void
expatpp::unparsedEntityDecl(const XML_Char*, const XML_Char*, const XML_Char*, const XML_Char*, const XML_Char*)
{
}
void
expatpp::notationDecl(const XML_Char*, const XML_Char*, const XML_Char*, const XML_Char*)
{
}
int
expatpp::skipWhiteSpace(const XML_Char* startFrom)
{
// use our own XML definition of white space
// TO DO - confirm this is correct!
const XML_Char* s = startFrom;
XML_Char c = *s;
while ((c==' ') || (c=='\t') || (c=='\n') || (c=='\r')) {
s++;
c = *s;
}
const int numSkipped = s - startFrom;
return numSkipped;
}
/**
Iterate the paired attribute name/value until find a pair with matching name.
\return pointer to the value or null if not found.
*/
const XML_Char*
expatpp::getAttribute(const XML_Char* matchingName, const XML_Char** atts)
{
for (int i=0; atts[i]; i++) {
const XML_Char* attributeName = atts[i++];
assert(attributeName); // shouldn't fail this because of loop test above
if(tcscmp(attributeName, matchingName)==0) {
return atts[i]; // if 2nd item was missing, this returns 0 safely indicating failure
}
}
return 0;
}
/**
\bug will always return 0 for PPC
*/
bool
expatpp::getIntegerAttribute(const XML_Char *matchingName, const XML_Char **atts, int& outAtt)
{
const XML_Char* attStr = getAttribute(matchingName, atts);
if (!attStr)
return false;
int i=0;
#ifdef XML_UNICODE
fail to compile because need this now
#else
sscanf(attStr, "%d", &i);
#endif
outAtt = i;
return true;
}
/**
\bug will always return 0 for PPC
*/
bool
expatpp::getDoubleAttribute(const XML_Char *matchingName, const XML_Char **atts, double& outAtt)
{
const XML_Char* attStr = getAttribute(matchingName, atts);
if (!attStr)
return false;
float f = 0.0; // sscanf doesn't allow point to double
#ifdef XML_UNICODE
fail to compile because need this now
#else
sscanf(attStr, "%f", &f);
#endif
outAtt = f;
return true;
}
bool
expatpp::emptyCharData(const XML_Char *s, int len)
{
// usually call from top of overriden charData methods
if (len==0)
return true; //*** early exit - empty string, may never occur??
// skip newline and empty whitespace
if (
((len==1) && ( (s[0]=='\n') || (s[0]=='\r')) ) || // just CR or just LF
((len==2) && (s[0]=='\r') && (s[1]=='\n')) // DOS-style CRLF
)
return true; //*** early exit - newline
const int lastCharAt = len-1;
if (s[lastCharAt]==' ') { // maybe all whitespace
int i;
for (i=0; i<lastCharAt; i++) {
if (s[i]!=' ')
break;
}
if (i==lastCharAt)
return true; //*** early exit - all spaces
}
return false;
}
//-------- Added for expat 1.95.5---------------
void
expatpp::attlistDeclCallback(void *userData,
const XML_Char *elname,
const XML_Char *attname,
const XML_Char *att_type,
const XML_Char *dflt,
int isrequired)
{
((expatpp*)userData)->attlistDecl(elname, attname, att_type, dflt, isrequired);
}
void
expatpp::commentCallback(void *userData, const XML_Char *data)
{
((expatpp*)userData)->comment(data);
}
void
expatpp::elementDeclCallback(void *userData, const XML_Char *name, XML_Content *model)
{
((expatpp*)userData)->elementDecl(name, model);
}
void
expatpp::endCdataSectionCallback(void *userData)
{
((expatpp*)userData)->endCdataSection();
}
void
expatpp::endDoctypeDeclCallback(void *userData)
{
((expatpp*)userData)->endDoctypeDecl();
}
void
expatpp::entityDeclCallback(void *userData,
const XML_Char *entityName,
int is_parameter_entity,
const XML_Char *value,
int value_length,
const XML_Char *base,
const XML_Char *systemId,
const XML_Char *publicId,
const XML_Char *notationName)
{
((expatpp*)userData)->entityDecl(entityName, is_parameter_entity, value, value_length, base, systemId, publicId, notationName);
}
void
expatpp::skippedEntityCallback(void *userData, const XML_Char *entityName, int is_parameter_entity)
{
((expatpp*)userData)->skippedEntity(entityName, is_parameter_entity);
}
void
expatpp::startCdataSectionCallback(void *userData)
{
((expatpp*)userData)->startCdataSection();
}
void
expatpp::startDoctypeDeclCallback(void *userData,
const XML_Char *doctypeName,
const XML_Char *sysid,
const XML_Char *pubid,
int has_internal_subset)
{
((expatpp*)userData)->startDoctypeDecl(doctypeName, sysid, pubid, has_internal_subset);
}
void
expatpp::xmlDeclCallback(void *userData, const XML_Char *version,
const XML_Char *encoding,
int standalone)
{
((expatpp*)userData)->xmlDecl(version, encoding, standalone);
}
void
expatpp::attlistDecl(
const XML_Char *elname,
const XML_Char *attname,
const XML_Char *att_type,
const XML_Char *dflt,
int isrequired)
{
}
void
expatpp::comment( const XML_Char *data)
{
}
void
expatpp::elementDecl( const XML_Char *name, XML_Content *model)
{
}
void
expatpp::endCdataSection()
{
}
void
expatpp::endDoctypeDecl()
{
}
void
expatpp::entityDecl(
const XML_Char *entityName,
int is_parameter_entity,
const XML_Char *value,
int value_length,
const XML_Char *base,
const XML_Char *systemId,
const XML_Char *publicId,
const XML_Char *notationName)
{
}
void
expatpp::skippedEntity( const XML_Char *entityName, int is_parameter_entity)
{
}
void
expatpp::startCdataSection()
{
}
void
expatpp::startDoctypeDecl(const XML_Char *doctypeName,
const XML_Char *sysid,
const XML_Char *pubid,
int has_internal_subset)
{
}
void
expatpp::xmlDecl( const XML_Char *version,
const XML_Char *encoding,
int standalone)
{
}
// -------------------------------------------------------
// e x p a t p p N e s t i n g
// -------------------------------------------------------
/**
\param parent can be null in which case this is root parser
\note The handlers set in here MUST be also set in SetupHandlers
which is a virtual method invoked by expatpp::ResetParser. Otherwise
you can have subtle bugs with a nested parser not properly returning
after reusing a parser (nasty and found rapidly only via extensive unit
tests and plentiful assertions!).
\WARNING
The assumption that is not obvious here is that if you want to use
nested parsers, then your topmost parser must also be an expatppNesting
subclass, NOT an expatpp subclass, because we need the
nestedStartElementCallback and nestedEndElementCallback
callbacks to override those in the expatpp ctor.
\todo go back over code in detail and confirm above warning still valid
I think if we used expat's functions to invoke the registered callback
might be safer - the explicit function call we have in nestedEndElementCallback
certainly assumes the parent type.
*/
expatppNesting::expatppNesting(expatppNesting* parent) :
expatpp(parent==0), // don't create parser - we're taking over from parent if given
mDepth(0),
mSelfDeleting(true),
mParent(parent),
mOwnedChild(0)
{
if ( parent )
{
RegisterWithParentXMLParser();
parent->AdoptChild(this);
}
else
{
// No parent - the expatpp constructor will have created a new mParser (expat parser)
::XML_SetElementHandler(mParser, nestedStartElementCallback, nestedEndElementCallback);
}
assert(mParser); // either we created above or expatpp
}
expatppNesting::~expatppNesting()
{
assert(!mParent); // if we are a sub-parser, should not delete without calling returnToParent
DeleteChild();
}
/**
Call parent version then override same as in our ctor.
*/
void
expatppNesting::SetupHandlers()
{
expatpp::SetupHandlers();
::XML_SetElementHandler(mParser, nestedStartElementCallback, nestedEndElementCallback);
}
/**
Must use if you have adopted a child parser and want to dispose of it early.
*/
void
expatppNesting::DeleteChild()
{
delete mOwnedChild;
mOwnedChild = 0;
}
/**
Invoked as a callback from a child ctor when we pass in a parent pointer.
OR used from switchToNewSubParser, in which case it may be the 2nd time
we're called for a given child (see scenarios in expatppNesting class comment).
*/
void
expatppNesting::AdoptChild(expatppNesting* adoptingChild)
{
if ( mOwnedChild != adoptingChild )
{
delete mOwnedChild;
mOwnedChild = adoptingChild;
}
}
/**
to use parent's underlying expat parser
*/
void
expatppNesting::RegisterWithParentXMLParser()
{
mParser = mParent->mParser;
::XML_SetUserData(mParser, this);
}
/**
User code (typically the startElement handler of user parsers derived from expatppNesting)
may call
switchToNewSubParser( new UserChildParser() );
to hand off the current document to a child parser that understands the next segment of XML.
Control will be returned to the original (parent) parser when the end of the child element
is reached.
In its lifetime a 'parent' parser may switch control to several child parsers (one at a time
of course) as it moves through the document encoutering various types of child element.
A child to which older code (eg: OOFILE) has just switched control by
new childParser(this) will be self-deleting and will clear our mOwnedChild in its dtor.
*/
void expatppNesting::switchToNewSubParser( expatppNesting* pAdoptedChild )
{
assert(pAdoptedChild);
AdoptChild(pAdoptedChild);
pAdoptedChild->BeAdopted(this);
}
/**
If this is root parser, nestedEndElementCallback won't call returnToParent.
Therefore it is safe to put parsers on the stack.
*/
expatppNesting*
expatppNesting::returnToParent()
{
expatppNesting* ret = mParent;
::XML_SetUserData(mParser, mParent);
mParent=0;
mParser=0; // prevent parser shutdown by expatpp::~expatpp!!
if (mSelfDeleting) {
ret->OwnedChildOrphansItself(this);
delete this; // MUST BE LAST THING CALLED IN NON-VIRTUAL FUNCTION, NO MEMBER ACCESS
}
return ret;
}
void
expatppNesting::nestedStartElementCallback(void *userData, const XML_Char* name, const XML_Char** atts)
{
assert(userData);
expatppNesting* nestedParser = (expatppNesting*)userData;
nestedParser->mDepth++;
nestedParser->startElement(name, atts); // probably user override
}
/**
If this is root parser, will never hit nestedEndElementCallback after closing element,
except for when we call it.
\param userData should be non-nil except for specific case of ending root
*/
void
expatppNesting::nestedEndElementCallback(void *userData, const XML_Char* name)
{
if (!userData)
return; // end tag for root
expatppNesting* nestedParser = (expatppNesting*)userData;
// we don't know until we hit a closing tag 'outside' us that our run is done
if (nestedParser->mDepth==0) {
expatppNesting* parentParser = nestedParser->returnToParent();
nestedEndElementCallback(parentParser, name); // callbacks for expatppNesting stay registered, so safe
//if we don't invoke their callback, they will not balance their mDepth
}
else {
// end of an element this parser has started - normal case
nestedParser->endElement(name); // probably user override
nestedParser->mDepth--;
}
}
/**
Called by switchToNewSubParser to indicate a newly created child parser
is now the currently active child for adoptingParent and the child
isn't expected to be self deleting.
Normal code to create an owned child would be either
switchToNewSubParser( new UserChildParser(this) );
where this is the currently active parser and you want to be deleting it, or
new UserChildParser(this);
to have a child parser self-delete
\par Important Safety Note
Copes with the situation of people forgetting to pass
in the parent parser (and hence creating a new one by default)
if invoked by switchToNewSubParser( new UserChildParser() )
by somewhat wastefully deleting the parser created in expatpp::expatpp
by us being a root parser.
*/
void
expatppNesting::BeAdopted(expatppNesting* adoptingParent)
{
if (mParent) {
assert(mParent==adoptingParent);
}
else { // root parser being adopted, cleanup!
ReleaseParser();
mParent = adoptingParent;
RegisterWithParentXMLParser();
}
mSelfDeleting = false;
}