OSDN Git Service

プリプロセッサ定義変更
[nlite/nlite.git] / expat / ExpatImpl.h
1 #ifndef DSSI_EXPATIMPL_H
2 #define DSSI_EXPATIMPL_H
3
4 //-----------------------------------------------------------------------------
5 // 
6 // @doc
7 //
8 // @module      ExpatImpl.h - Expat class container |
9 //
10 // This module contains the definition of the expat class container.
11 //
12 // Copyright (c) 1994-2002 - Descartes Systems Sciences, Inc.
13 //
14 // @end
15 //
16 // $History: ExpatImpl.h $
17 //      
18 //      *****************  Version 1  *****************
19 //      User: Tim Smith    Date: 1/29/02    Time: 1:57p
20 //      Created in $/Omni_V2/_ToolLib
21 //      1. String.h now replaced with StringCode.h.
22 //      2. StringRsrc.h modified to use new string class.
23 //      3. Added tons of new classes from the wedge work.
24 //      
25 //-----------------------------------------------------------------------------
26
27 //-----------------------------------------------------------------------------
28 //
29 // Required include files
30 //
31 //-----------------------------------------------------------------------------
32
33 #include <assert.h>
34 #include "expat.h"
35
36 //-----------------------------------------------------------------------------
37 //
38 // Forward definitions
39 //
40 //-----------------------------------------------------------------------------
41
42 //-----------------------------------------------------------------------------
43 //
44 // Template class definition
45 //
46 //-----------------------------------------------------------------------------
47
48 template <class _T>
49 class CExpatImpl
50 {
51
52 // @access Constructors and destructors
53 public:
54         
55         // @cmember General constructor
56
57         CExpatImpl ()
58         {
59                 m_p = NULL;
60         }
61
62         // @cmember Destructor
63
64         ~CExpatImpl ()
65         {
66                 Destroy ();
67         }
68
69 // @access Parser creation and deletion methods
70 public:
71
72         // @cmember Create a parser
73
74         bool Create (const XML_Char *pszEncoding = NULL, 
75                 const XML_Char *pszSep = NULL)
76         {
77
78                 //
79                 // Destroy the old parser
80                 //
81
82                 Destroy ();
83
84                 //
85                 // If the encoding or seperator are empty, then NULL
86                 //
87
88                 if (pszEncoding != NULL && pszEncoding [0] == 0)
89                         pszEncoding = NULL;
90                 if (pszSep != NULL && pszSep [0] == 0)
91                         pszSep = NULL;
92
93                 //
94                 // Create the new one
95                 //
96
97                 m_p = XML_ParserCreate_MM (pszEncoding, NULL, pszSep);
98                 if (m_p == NULL)
99                         return false;
100
101                 //
102                 // Invoke the post create routine
103                 //
104
105                 _T *pThis = static_cast <_T *> (this);
106                 pThis ->OnPostCreate ();
107
108                 //
109                 // Set the user data used in callbacks
110                 //
111
112                 XML_SetUserData (m_p, (void *) this);
113                 return true;
114         }
115
116         // @cmember Destroy the parser
117
118         void Destroy ()
119         {
120                 if (m_p != NULL)
121                         XML_ParserFree (m_p);
122                 m_p = NULL;
123         }
124
125 // @access Parser parse methods
126 public:
127
128         // @cmember Parse a block of data
129
130         bool Parse (const char *pszBuffer, int nLength = -1, bool fIsFinal = true)
131         {
132
133                 //
134                 // Validate
135                 //
136
137                 assert (m_p != NULL);
138
139                 //
140                 // Get the length if not specified
141                 //
142
143                 if (nLength < 0)
144                         nLength = strlen (pszBuffer);
145
146                 //
147                 // Invoke the parser
148                 //
149
150                 return XML_Parse (m_p, pszBuffer, nLength, fIsFinal) != 0;
151         }
152
153         // @cmember Parse a block of data
154
155 #ifdef WCHAR
156         bool Parse (const WCHAR *pszBuffer, int nLength = -1, bool fIsFinal = true)
157         {
158
159                 //
160                 // Validate
161                 //
162
163                 assert (m_p != NULL);
164
165                 //
166                 // Get the length if not specified
167                 //
168
169                 if (nLength < 0)
170                         nLength = wcslen (pszBuffer) * 2;
171
172                 //
173                 // Invoke the parser
174                 //
175
176                 return XML_Parse (m_p, pszBuffer, nLength, fIsFinal) != 0;
177         }
178 #endif
179
180         // @cmember Parse internal buffer
181
182         bool ParseBuffer (int nLength, bool fIsFinal = true)
183         {
184                 assert (m_p != NULL);
185                 return XML_ParseBuffer (m_p, nLength, fIsFinal) != 0;
186         }
187
188         // @cmember Get the internal buffer
189
190         void *GetBuffer (int nLength)
191         {
192                 assert (m_p != NULL);
193                 return XML_GetBuffer (m_p, nLength);
194         }
195
196 // @access Parser callback enable/disable methods
197 public:
198
199         // @cmember Enable/Disable the start element handler
200
201         void EnableStartElementHandler (bool fEnable = true)
202         {
203                 assert (m_p != NULL);
204                 XML_SetStartElementHandler (m_p, fEnable ? StartElementHandler : NULL);
205         }
206
207         // @cmember Enable/Disable the end element handler
208
209         void EnableEndElementHandler (bool fEnable = true)
210         {
211                 assert (m_p != NULL);
212                 XML_SetEndElementHandler (m_p, fEnable ? EndElementHandler : NULL);
213         }
214
215         // @cmember Enable/Disable the element handlers
216
217         void EnableElementHandler (bool fEnable = true)
218         {
219                 assert (m_p != NULL);
220                 EnableStartElementHandler (fEnable);
221                 EnableEndElementHandler (fEnable);
222         }
223
224         // @cmember Enable/Disable the character data handler
225
226         void EnableCharacterDataHandler (bool fEnable = true)
227         {
228                 assert (m_p != NULL);
229                 XML_SetCharacterDataHandler (m_p,
230                         fEnable ? CharacterDataHandler : NULL);
231         }
232
233         // @cmember Enable/Disable the processing instruction handler
234
235         void EnableProcessingInstructionHandler (bool fEnable = true)
236         {
237                 assert (m_p != NULL);
238                 XML_SetProcessingInstructionHandler (m_p, 
239                         fEnable ? ProcessingInstructionHandler : NULL);
240         }
241
242         // @cmember Enable/Disable the comment handler
243
244         void EnableCommentHandler (bool fEnable = true)
245         {
246                 assert (m_p != NULL);
247                 XML_SetCommentHandler (m_p, fEnable ? CommentHandler : NULL);
248         }
249
250         // @cmember Enable/Disable the start CDATA section handler
251
252         void EnableStartCdataSectionHandler (bool fEnable = true)
253         {
254                 assert (m_p != NULL);
255                 XML_SetStartCdataSectionHandler (m_p, 
256                         fEnable ? StartCdataSectionHandler : NULL);
257         }
258
259         // @cmember Enable/Disable the end CDATA section handler
260
261         void EnableEndCdataSectionHandler (bool fEnable = true)
262         {
263                 assert (m_p != NULL);
264                 XML_SetEndCdataSectionHandler (m_p, 
265                         fEnable ? EndCdataSectionHandler : NULL);
266         }
267
268         // @cmember Enable/Disable the CDATA section handlers
269
270         void EnableCdataSectionHandler (bool fEnable = true)
271         {
272                 assert (m_p != NULL);
273                 EnableStartCdataSectionHandler (fEnable);
274                 EnableEndCdataSectionHandler (fEnable);
275         }
276
277         // @cmember Enable/Disable default handler
278         
279         void EnableDefaultHandler (bool fEnable = true, bool fExpand = true)
280         {
281                 assert (m_p != NULL);
282                 if (fExpand)
283                 {
284                         XML_SetDefaultHandlerExpand (m_p, 
285                                 fEnable ? DefaultHandler : NULL);
286                 }
287                 else
288                         XML_SetDefaultHandler (m_p, fEnable ? DefaultHandler : NULL);
289         }
290         
291         // @cmember Enable/Disable external entity ref handler
292         
293         void EnableExternalEntityRefHandler (bool fEnable = true)
294         {
295                 assert (m_p != NULL);
296                 XML_SetExternalEntityRefHandler (m_p, 
297                         fEnable ? ExternalEntityRefHandler : NULL);
298         }
299         
300         // @cmember Enable/Disable unknown encoding handler
301         
302         void EnableUnknownEncodingHandler (bool fEnable = true)
303         {
304                 assert (m_p != NULL);
305                 XML_SetUnknownEncodingHandler (m_p, 
306                         fEnable ? UnknownEncodingHandler : NULL);
307         }
308         
309         // @cmember Enable/Disable start namespace handler
310         
311         void EnableStartNamespaceDeclHandler (bool fEnable = true)
312         {
313                 assert (m_p != NULL);
314                 XML_SetStartNamespaceDeclHandler (m_p, 
315                         fEnable ? StartNamespaceDeclHandler : NULL);
316         }
317         
318         // @cmember Enable/Disable end namespace handler
319         
320         void EnableEndNamespaceDeclHandler (bool fEnable = true)
321         {
322                 assert (m_p != NULL);
323                 XML_SetEndNamespaceDeclHandler (m_p, 
324                         fEnable ? EndNamespaceDeclHandler : NULL);
325         }
326
327         // @cmember Enable/Disable namespace handlers
328
329         void EnableNamespaceDeclHandler (bool fEnable = true)
330         {
331                 EnableStartNamespaceDeclHandler (fEnable);
332                 EnableEndNamespaceDeclHandler (fEnable);
333         }
334         
335         // @cmember Enable/Disable the XML declaration handler
336
337         void EnableXmlDeclHandler (bool fEnable = true)
338         {
339                 assert (m_p != NULL);
340                 XML_SetXmlDeclHandler (m_p, fEnable ? XmlDeclHandler : NULL);
341         }
342
343         // @cmember Enable/Disable the start DOCTYPE declaration handler
344
345         void EnableStartDoctypeDeclHandler (bool fEnable = true)
346         {
347                 assert (m_p != NULL);
348                 XML_SetStartDoctypeDeclHandler (m_p, 
349                         fEnable ? StartDoctypeDeclHandler : NULL);
350         }
351
352         // @cmember Enable/Disable the end DOCTYPE declaration handler
353
354         void EnableEndDoctypeDeclHandler (bool fEnable = true)
355         {
356                 assert (m_p != NULL);
357                 XML_SetEndDoctypeDeclHandler (m_p, 
358                         fEnable ? EndDoctypeDeclHandler : NULL);
359         }
360
361         // @cmember Enable/Disable the DOCTYPE declaration handler
362
363         void EnableDoctypeDeclHandler (bool fEnable = true)
364         {
365                 assert (m_p != NULL);
366                 EnableStartDoctypeDeclHandler (fEnable);
367                 EnableEndDoctypeDeclHandler (fEnable);
368         }
369
370 // @access Parser error reporting methods
371 public:
372
373         // @cmember Get last error
374
375         enum XML_Error GetErrorCode () 
376         {
377                 assert (m_p != NULL);
378                 return XML_GetErrorCode (m_p);
379         }
380
381         // @cmember Get the current byte index
382
383         long GetCurrentByteIndex () 
384         {
385                 assert (m_p != NULL);
386                 return XML_GetCurrentByteIndex (m_p);
387         }
388
389         // @cmember Get the current line number
390
391         int GetCurrentLineNumber () 
392         {
393                 assert (m_p != NULL);
394                 return XML_GetCurrentLineNumber (m_p);
395         }
396
397         // @cmember Get the current column number
398
399         int GetCurrentColumnNumber () 
400         {
401                 assert (m_p != NULL);
402                 return XML_GetCurrentColumnNumber (m_p);
403         }
404
405         // @cmember Get the current byte count
406
407         int GetCurrentByteCount () 
408         {
409                 assert (m_p != NULL);
410                 return XML_GetCurrentByteCount (m_p);
411         }
412
413         // @cmember Get the input context
414
415         const char *GetInputContext (int *pnOffset, int *pnSize)
416         {
417                 assert (m_p != NULL);
418                 return XML_GetInputContext (m_p, pnOffset, pnSize);
419         }
420
421         // @cmember Get last error string
422
423         const XML_LChar *GetErrorString () 
424         {
425                 return XML_ErrorString (GetErrorCode ());
426         }
427
428 // @access Parser other methods
429 public:
430
431         // @cmember Return the version string
432
433         static const XML_LChar *GetExpatVersion ()
434         {
435                 return XML_ExpatVersion ();
436         }
437
438         // @cmember Get the version information
439
440         static void GetExpatVersion (int *pnMajor, int *pnMinor, int *pnMicro)
441         {
442                 XML_expat_version v = XML_ExpatVersionInfo ();
443                 if (pnMajor)
444                         *pnMajor = v .major;
445                 if (pnMinor)
446                         *pnMinor = v .minor;
447                 if (pnMicro)
448                         *pnMicro = v .micro;
449         }
450
451         // @cmember Get last error string
452
453         static const XML_LChar *GetErrorString (enum XML_Error nError) 
454         {
455                 return XML_ErrorString (nError);
456         }
457
458 // @access Public handler methods
459 public:
460
461         // @cmember Start element handler
462
463         void OnStartElement (const XML_Char *pszName, const XML_Char **papszAttrs)
464         {
465                 return;
466         }
467
468         // @cmember End element handler
469
470         void OnEndElement (const XML_Char *pszName)
471         {
472                 return;
473         }
474
475         // @cmember Character data handler
476
477         void OnCharacterData (const XML_Char *pszData, int nLength)
478         {
479                 return;
480         }
481
482         // @cmember Processing instruction handler
483
484         void OnProcessingInstruction (const XML_Char *pszTarget, 
485                 const XML_Char *pszData)
486         {
487                 return;
488         }
489
490         // @cmember Comment handler
491
492         void OnComment (const XML_Char *pszData)
493         {
494                 return;
495         }
496
497         // @cmember Start CDATA section handler
498
499         void OnStartCdataSection ()
500         {
501                 return;
502         }
503
504         // @cmember End CDATA section handler
505
506         void OnEndCdataSection ()
507         {
508                 return;
509         }
510
511         // @cmember Default handler
512         
513         void OnDefault (const XML_Char *pszData, int nLength)
514         {
515                 return;
516         }
517         
518         // @cmember External entity ref handler
519         
520         bool OnExternalEntityRef (const XML_Char *pszContext,
521                 const XML_Char *pszBase, const XML_Char *pszSystemID,
522                 const XML_Char *pszPublicID)
523         {
524                 return false;
525         }
526         
527         // @cmember Unknown encoding handler
528         
529         bool OnUnknownEncoding (const XML_Char *pszName, XML_Encoding *pInfo)
530         {
531                 return false;
532         }
533         
534         // @cmember Start namespace declaration handler
535         
536         void OnStartNamespaceDecl (const XML_Char *pszPrefix, 
537                 const XML_Char *pszURI)
538         {
539                 return;
540         }
541         
542         // @cmember End namespace declaration handler
543         
544         void OnEndNamespaceDecl (const XML_Char *pszPrefix)
545         {
546                 return;
547         }
548         
549         // @cmember XML declaration handler
550
551         void OnXmlDecl (const XML_Char *pszVersion, const XML_Char *pszEncoding,
552                 bool fStandalone)
553         {
554                 return;
555         }
556
557         // @cmember Start DOCTYPE declaration handler
558
559         void OnStartDoctypeDecl (const XML_Char *pszDoctypeName, 
560                 const XML_Char *pszSysID, const XML_Char *pszPubID,
561                 bool fHasInternalSubset)
562         {
563                 return;
564         }
565
566         // @cmember End DOCTYPE declaration handler
567
568         void OnEndDoctypeDecl ()
569         {
570                 return;
571         }
572
573 // @access Protected methods
574 protected:
575
576         // @cmember Handle any post creation
577
578         void OnPostCreate ()
579         {
580         }
581
582 // @access Protected static methods
583 protected:
584
585         // @cmember Start element handler wrapper
586
587         static void __cdecl StartElementHandler (void *pUserData,
588                 const XML_Char *pszName, const XML_Char **papszAttrs)
589         {
590                 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
591                 pThis ->OnStartElement (pszName, papszAttrs);
592         }
593
594         // @cmember End element handler wrapper
595
596         static void __cdecl EndElementHandler (void *pUserData,
597                 const XML_Char *pszName)
598         {
599                 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
600                 pThis ->OnEndElement (pszName);
601         }
602
603         // @cmember Character data handler wrapper
604
605         static void __cdecl CharacterDataHandler (void *pUserData,
606                 const XML_Char *pszData, int nLength)
607         {
608                 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
609                 pThis ->OnCharacterData (pszData, nLength);
610         }
611
612         // @cmember Processing instruction handler wrapper
613
614         static void __cdecl ProcessingInstructionHandler (void *pUserData,
615                 const XML_Char *pszTarget, const XML_Char *pszData)
616         {
617                 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
618                 pThis ->OnProcessingInstruction (pszTarget, pszData);
619         }
620
621         // @cmember Comment handler wrapper
622
623         static void __cdecl CommentHandler (void *pUserData,
624                 const XML_Char *pszData)
625         {
626                 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
627                 pThis ->OnComment (pszData);
628         }
629
630         // @cmember Start CDATA section wrapper
631
632         static void __cdecl StartCdataSectionHandler (void *pUserData)
633         {
634                 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
635                 pThis ->OnStartCdataSection ();
636         }
637
638         // @cmember End CDATA section wrapper
639
640         static void __cdecl EndCdataSectionHandler (void *pUserData)
641         {
642                 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
643                 pThis ->OnEndCdataSection ();
644         }
645
646         // @cmember Default wrapper
647         
648         static void __cdecl DefaultHandler (void *pUserData, 
649                 const XML_Char *pszData, int nLength)
650         {
651                 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
652                 pThis ->OnDefault (pszData, nLength);
653         }
654         
655         // @cmember External entity ref wrapper
656         
657         static int __cdecl ExternalEntityRefHandler (void *pUserData, 
658                 const XML_Char *pszContext, const XML_Char *pszBase, 
659                 const XML_Char *pszSystemID, const XML_Char *pszPublicID)
660         {
661                 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
662                 return pThis ->OnExternalEntityRef (pszContext, 
663                         pszBase, pszSystemID, pszPublicID) ? 1 : 0;
664         }
665         
666         // @cmember Unknown encoding wrapper
667         
668         static int __cdecl UnknownEncodingHandler (void *pUserData, 
669                 const XML_Char *pszName, XML_Encoding *pInfo)
670         {
671                 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
672                 return pThis ->OnUnknownEncoding (pszName, pInfo) ? 1 : 0;
673         }
674         
675         // @cmember Start namespace decl wrapper
676         
677         static void __cdecl StartNamespaceDeclHandler (void *pUserData, 
678                 const XML_Char *pszPrefix, const XML_Char *pszURI)
679         {
680                 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
681                 pThis ->OnStartNamespaceDecl (pszPrefix, pszURI);
682         }
683         
684         // @cmember End namespace decl wrapper
685         
686         static void __cdecl EndNamespaceDeclHandler (void *pUserData, 
687                 const XML_Char *pszPrefix)
688         {
689                 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
690                 pThis ->OnEndNamespaceDecl (pszPrefix);
691         }
692         
693         // @cmember XML declaration wrapper
694
695         static void __cdecl XmlDeclHandler (void *pUserData,
696                 const XML_Char *pszVersion, const XML_Char *pszEncoding,
697                 int nStandalone)
698         {
699                 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
700                 pThis ->OnXmlDecl (pszVersion, pszEncoding, nStandalone != 0);
701         }
702
703         // @cmember Start Doctype declaration wrapper
704
705         static void __cdecl StartDoctypeDeclHandler (void *pUserData,
706                 const XML_Char *pszDoctypeName, const XML_Char *pszSysID, 
707                 const XML_Char *pszPubID, int nHasInternalSubset)
708         {
709                 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
710                 pThis ->OnStartDoctypeDecl (pszDoctypeName, pszSysID, 
711                         pszPubID, nHasInternalSubset != 0);
712         }
713
714         // @cmember End Doctype declaration wrapper
715
716         static void __cdecl EndDoctypeDeclHandler (void *pUserData)
717         {
718                 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
719                 pThis ->OnEndDoctypeDecl ();
720         }
721
722 // @access Protected members
723 protected:
724
725         XML_Parser              m_p;
726 };
727
728 //-----------------------------------------------------------------------------
729 //
730 // Virtual method class definition
731 //
732 //-----------------------------------------------------------------------------
733
734 class CExpat : public CExpatImpl <CExpat>
735 {
736 // @access Constructors and destructors
737 public:
738         
739         CExpat ()
740         {
741         }
742
743 // @access Public handler methods
744 public:
745
746         // @cmember Start element handler
747
748         virtual void OnStartElement (const XML_Char *pszName, 
749                 const XML_Char **papszAttrs)
750         {
751                 return;
752         }
753
754         // @cmember End element handler
755
756         virtual void OnEndElement (const XML_Char *pszName)
757         {
758                 return;
759         }
760
761         // @cmember Character data handler
762
763         virtual void OnCharacterData (const XML_Char *pszData, int nLength)
764         {
765                 return;
766         }
767
768         // @cmember Processing instruction handler
769
770         virtual void OnProcessingInstruction (const XML_Char *pszTarget, 
771                 const XML_Char *pszData)
772         {
773                 return;
774         }
775
776         // @cmember Comment handler
777
778         virtual void OnComment (const XML_Char *pszData)
779         {
780                 return;
781         }
782
783         // @cmember Start CDATA section handler
784
785         virtual void OnStartCdataSection ()
786         {
787                 return;
788         }
789
790         // @cmember End CDATA section handler
791
792         virtual void OnEndCdataSection ()
793         {
794                 return;
795         }
796
797         // @cmember Default handler
798         
799         virtual void OnDefault (const XML_Char *pszData, int nLength)
800         {
801                 return;
802         }
803         
804         // @cmember External entity ref handler
805         
806         virtual bool OnExternalEntityRef (const XML_Char *pszContext,
807                 const XML_Char *pszBase, const XML_Char *pszSystemID,
808                 const XML_Char *pszPublicID)
809         {
810                 return false;
811         }
812         
813         // @cmember Unknown encoding handler
814         
815         virtual bool OnUnknownEncoding (const XML_Char *pszName, XML_Encoding *pInfo)
816         {
817                 return false;
818         }
819         
820         // @cmember Start namespace declaration handler
821         
822         virtual void OnStartNamespaceDecl (const XML_Char *pszPrefix, 
823                 const XML_Char *pszURI)
824         {
825                 return;
826         }
827         
828         // @cmember End namespace declaration handler
829         
830         virtual void OnEndNamespaceDecl (const XML_Char *pszPrefix)
831         {
832                 return;
833         }
834         
835         // @cmember XML declaration handler
836
837         virtual void OnXmlDecl (const XML_Char *pszVersion, 
838                 const XML_Char *pszEncoding, bool fStandalone)
839         {
840                 return;
841         }
842
843         // @cmember Start DOCTYPE declaration handler
844
845         virtual void OnStartDoctypeDecl (const XML_Char *pszDoctypeName, 
846                 const XML_Char *pszSysID, const XML_Char *pszPubID,
847                 bool fHasInternalSubset)
848         {
849                 return;
850         }
851
852         // @cmember End DOCTYPE declaration handler
853
854         virtual void OnEndDoctypeDecl ()
855         {
856                 return;
857         }
858 };
859
860 #endif // DSSI_EXPATIMPL_H