1 #ifndef DSSI_EXPATIMPL_H
2 #define DSSI_EXPATIMPL_H
4 //-----------------------------------------------------------------------------
8 // @module ExpatImpl.h - Expat class container |
10 // This module contains the definition of the expat class container.
12 // Copyright (c) 1994-2002 - Descartes Systems Sciences, Inc.
16 // $History: ExpatImpl.h $
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.
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 // Required include files
31 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 // Forward definitions
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 // Template class definition
46 //-----------------------------------------------------------------------------
52 // @access Constructors and destructors
55 // @cmember General constructor
62 // @cmember Destructor
69 // @access Parser creation and deletion methods
72 // @cmember Create a parser
74 bool Create (const XML_Char *pszEncoding = NULL,
75 const XML_Char *pszSep = NULL)
79 // Destroy the old parser
85 // If the encoding or seperator are empty, then NULL
88 if (pszEncoding != NULL && pszEncoding [0] == 0)
90 if (pszSep != NULL && pszSep [0] == 0)
97 m_p = XML_ParserCreate_MM (pszEncoding, NULL, pszSep);
102 // Invoke the post create routine
105 _T *pThis = static_cast <_T *> (this);
106 pThis ->OnPostCreate ();
109 // Set the user data used in callbacks
112 XML_SetUserData (m_p, (void *) this);
116 // @cmember Destroy the parser
121 XML_ParserFree (m_p);
125 // @access Parser parse methods
128 // @cmember Parse a block of data
130 bool Parse (const char *pszBuffer, int nLength = -1, bool fIsFinal = true)
137 assert (m_p != NULL);
140 // Get the length if not specified
144 nLength = strlen (pszBuffer);
150 return XML_Parse (m_p, pszBuffer, nLength, fIsFinal) != 0;
153 // @cmember Parse a block of data
156 bool Parse (const WCHAR *pszBuffer, int nLength = -1, bool fIsFinal = true)
163 assert (m_p != NULL);
166 // Get the length if not specified
170 nLength = wcslen (pszBuffer) * 2;
176 return XML_Parse (m_p, pszBuffer, nLength, fIsFinal) != 0;
180 // @cmember Parse internal buffer
182 bool ParseBuffer (int nLength, bool fIsFinal = true)
184 assert (m_p != NULL);
185 return XML_ParseBuffer (m_p, nLength, fIsFinal) != 0;
188 // @cmember Get the internal buffer
190 void *GetBuffer (int nLength)
192 assert (m_p != NULL);
193 return XML_GetBuffer (m_p, nLength);
196 // @access Parser callback enable/disable methods
199 // @cmember Enable/Disable the start element handler
201 void EnableStartElementHandler (bool fEnable = true)
203 assert (m_p != NULL);
204 XML_SetStartElementHandler (m_p, fEnable ? StartElementHandler : NULL);
207 // @cmember Enable/Disable the end element handler
209 void EnableEndElementHandler (bool fEnable = true)
211 assert (m_p != NULL);
212 XML_SetEndElementHandler (m_p, fEnable ? EndElementHandler : NULL);
215 // @cmember Enable/Disable the element handlers
217 void EnableElementHandler (bool fEnable = true)
219 assert (m_p != NULL);
220 EnableStartElementHandler (fEnable);
221 EnableEndElementHandler (fEnable);
224 // @cmember Enable/Disable the character data handler
226 void EnableCharacterDataHandler (bool fEnable = true)
228 assert (m_p != NULL);
229 XML_SetCharacterDataHandler (m_p,
230 fEnable ? CharacterDataHandler : NULL);
233 // @cmember Enable/Disable the processing instruction handler
235 void EnableProcessingInstructionHandler (bool fEnable = true)
237 assert (m_p != NULL);
238 XML_SetProcessingInstructionHandler (m_p,
239 fEnable ? ProcessingInstructionHandler : NULL);
242 // @cmember Enable/Disable the comment handler
244 void EnableCommentHandler (bool fEnable = true)
246 assert (m_p != NULL);
247 XML_SetCommentHandler (m_p, fEnable ? CommentHandler : NULL);
250 // @cmember Enable/Disable the start CDATA section handler
252 void EnableStartCdataSectionHandler (bool fEnable = true)
254 assert (m_p != NULL);
255 XML_SetStartCdataSectionHandler (m_p,
256 fEnable ? StartCdataSectionHandler : NULL);
259 // @cmember Enable/Disable the end CDATA section handler
261 void EnableEndCdataSectionHandler (bool fEnable = true)
263 assert (m_p != NULL);
264 XML_SetEndCdataSectionHandler (m_p,
265 fEnable ? EndCdataSectionHandler : NULL);
268 // @cmember Enable/Disable the CDATA section handlers
270 void EnableCdataSectionHandler (bool fEnable = true)
272 assert (m_p != NULL);
273 EnableStartCdataSectionHandler (fEnable);
274 EnableEndCdataSectionHandler (fEnable);
277 // @cmember Enable/Disable default handler
279 void EnableDefaultHandler (bool fEnable = true, bool fExpand = true)
281 assert (m_p != NULL);
284 XML_SetDefaultHandlerExpand (m_p,
285 fEnable ? DefaultHandler : NULL);
288 XML_SetDefaultHandler (m_p, fEnable ? DefaultHandler : NULL);
291 // @cmember Enable/Disable external entity ref handler
293 void EnableExternalEntityRefHandler (bool fEnable = true)
295 assert (m_p != NULL);
296 XML_SetExternalEntityRefHandler (m_p,
297 fEnable ? ExternalEntityRefHandler : NULL);
300 // @cmember Enable/Disable unknown encoding handler
302 void EnableUnknownEncodingHandler (bool fEnable = true)
304 assert (m_p != NULL);
305 XML_SetUnknownEncodingHandler (m_p,
306 fEnable ? UnknownEncodingHandler : NULL);
309 // @cmember Enable/Disable start namespace handler
311 void EnableStartNamespaceDeclHandler (bool fEnable = true)
313 assert (m_p != NULL);
314 XML_SetStartNamespaceDeclHandler (m_p,
315 fEnable ? StartNamespaceDeclHandler : NULL);
318 // @cmember Enable/Disable end namespace handler
320 void EnableEndNamespaceDeclHandler (bool fEnable = true)
322 assert (m_p != NULL);
323 XML_SetEndNamespaceDeclHandler (m_p,
324 fEnable ? EndNamespaceDeclHandler : NULL);
327 // @cmember Enable/Disable namespace handlers
329 void EnableNamespaceDeclHandler (bool fEnable = true)
331 EnableStartNamespaceDeclHandler (fEnable);
332 EnableEndNamespaceDeclHandler (fEnable);
335 // @cmember Enable/Disable the XML declaration handler
337 void EnableXmlDeclHandler (bool fEnable = true)
339 assert (m_p != NULL);
340 XML_SetXmlDeclHandler (m_p, fEnable ? XmlDeclHandler : NULL);
343 // @cmember Enable/Disable the start DOCTYPE declaration handler
345 void EnableStartDoctypeDeclHandler (bool fEnable = true)
347 assert (m_p != NULL);
348 XML_SetStartDoctypeDeclHandler (m_p,
349 fEnable ? StartDoctypeDeclHandler : NULL);
352 // @cmember Enable/Disable the end DOCTYPE declaration handler
354 void EnableEndDoctypeDeclHandler (bool fEnable = true)
356 assert (m_p != NULL);
357 XML_SetEndDoctypeDeclHandler (m_p,
358 fEnable ? EndDoctypeDeclHandler : NULL);
361 // @cmember Enable/Disable the DOCTYPE declaration handler
363 void EnableDoctypeDeclHandler (bool fEnable = true)
365 assert (m_p != NULL);
366 EnableStartDoctypeDeclHandler (fEnable);
367 EnableEndDoctypeDeclHandler (fEnable);
370 // @access Parser error reporting methods
373 // @cmember Get last error
375 enum XML_Error GetErrorCode ()
377 assert (m_p != NULL);
378 return XML_GetErrorCode (m_p);
381 // @cmember Get the current byte index
383 long GetCurrentByteIndex ()
385 assert (m_p != NULL);
386 return XML_GetCurrentByteIndex (m_p);
389 // @cmember Get the current line number
391 int GetCurrentLineNumber ()
393 assert (m_p != NULL);
394 return XML_GetCurrentLineNumber (m_p);
397 // @cmember Get the current column number
399 int GetCurrentColumnNumber ()
401 assert (m_p != NULL);
402 return XML_GetCurrentColumnNumber (m_p);
405 // @cmember Get the current byte count
407 int GetCurrentByteCount ()
409 assert (m_p != NULL);
410 return XML_GetCurrentByteCount (m_p);
413 // @cmember Get the input context
415 const char *GetInputContext (int *pnOffset, int *pnSize)
417 assert (m_p != NULL);
418 return XML_GetInputContext (m_p, pnOffset, pnSize);
421 // @cmember Get last error string
423 const XML_LChar *GetErrorString ()
425 return XML_ErrorString (GetErrorCode ());
428 // @access Parser other methods
431 // @cmember Return the version string
433 static const XML_LChar *GetExpatVersion ()
435 return XML_ExpatVersion ();
438 // @cmember Get the version information
440 static void GetExpatVersion (int *pnMajor, int *pnMinor, int *pnMicro)
442 XML_expat_version v = XML_ExpatVersionInfo ();
451 // @cmember Get last error string
453 static const XML_LChar *GetErrorString (enum XML_Error nError)
455 return XML_ErrorString (nError);
458 // @access Public handler methods
461 // @cmember Start element handler
463 void OnStartElement (const XML_Char *pszName, const XML_Char **papszAttrs)
468 // @cmember End element handler
470 void OnEndElement (const XML_Char *pszName)
475 // @cmember Character data handler
477 void OnCharacterData (const XML_Char *pszData, int nLength)
482 // @cmember Processing instruction handler
484 void OnProcessingInstruction (const XML_Char *pszTarget,
485 const XML_Char *pszData)
490 // @cmember Comment handler
492 void OnComment (const XML_Char *pszData)
497 // @cmember Start CDATA section handler
499 void OnStartCdataSection ()
504 // @cmember End CDATA section handler
506 void OnEndCdataSection ()
511 // @cmember Default handler
513 void OnDefault (const XML_Char *pszData, int nLength)
518 // @cmember External entity ref handler
520 bool OnExternalEntityRef (const XML_Char *pszContext,
521 const XML_Char *pszBase, const XML_Char *pszSystemID,
522 const XML_Char *pszPublicID)
527 // @cmember Unknown encoding handler
529 bool OnUnknownEncoding (const XML_Char *pszName, XML_Encoding *pInfo)
534 // @cmember Start namespace declaration handler
536 void OnStartNamespaceDecl (const XML_Char *pszPrefix,
537 const XML_Char *pszURI)
542 // @cmember End namespace declaration handler
544 void OnEndNamespaceDecl (const XML_Char *pszPrefix)
549 // @cmember XML declaration handler
551 void OnXmlDecl (const XML_Char *pszVersion, const XML_Char *pszEncoding,
557 // @cmember Start DOCTYPE declaration handler
559 void OnStartDoctypeDecl (const XML_Char *pszDoctypeName,
560 const XML_Char *pszSysID, const XML_Char *pszPubID,
561 bool fHasInternalSubset)
566 // @cmember End DOCTYPE declaration handler
568 void OnEndDoctypeDecl ()
573 // @access Protected methods
576 // @cmember Handle any post creation
582 // @access Protected static methods
585 // @cmember Start element handler wrapper
587 static void __cdecl StartElementHandler (void *pUserData,
588 const XML_Char *pszName, const XML_Char **papszAttrs)
590 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
591 pThis ->OnStartElement (pszName, papszAttrs);
594 // @cmember End element handler wrapper
596 static void __cdecl EndElementHandler (void *pUserData,
597 const XML_Char *pszName)
599 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
600 pThis ->OnEndElement (pszName);
603 // @cmember Character data handler wrapper
605 static void __cdecl CharacterDataHandler (void *pUserData,
606 const XML_Char *pszData, int nLength)
608 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
609 pThis ->OnCharacterData (pszData, nLength);
612 // @cmember Processing instruction handler wrapper
614 static void __cdecl ProcessingInstructionHandler (void *pUserData,
615 const XML_Char *pszTarget, const XML_Char *pszData)
617 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
618 pThis ->OnProcessingInstruction (pszTarget, pszData);
621 // @cmember Comment handler wrapper
623 static void __cdecl CommentHandler (void *pUserData,
624 const XML_Char *pszData)
626 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
627 pThis ->OnComment (pszData);
630 // @cmember Start CDATA section wrapper
632 static void __cdecl StartCdataSectionHandler (void *pUserData)
634 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
635 pThis ->OnStartCdataSection ();
638 // @cmember End CDATA section wrapper
640 static void __cdecl EndCdataSectionHandler (void *pUserData)
642 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
643 pThis ->OnEndCdataSection ();
646 // @cmember Default wrapper
648 static void __cdecl DefaultHandler (void *pUserData,
649 const XML_Char *pszData, int nLength)
651 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
652 pThis ->OnDefault (pszData, nLength);
655 // @cmember External entity ref wrapper
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)
661 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
662 return pThis ->OnExternalEntityRef (pszContext,
663 pszBase, pszSystemID, pszPublicID) ? 1 : 0;
666 // @cmember Unknown encoding wrapper
668 static int __cdecl UnknownEncodingHandler (void *pUserData,
669 const XML_Char *pszName, XML_Encoding *pInfo)
671 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
672 return pThis ->OnUnknownEncoding (pszName, pInfo) ? 1 : 0;
675 // @cmember Start namespace decl wrapper
677 static void __cdecl StartNamespaceDeclHandler (void *pUserData,
678 const XML_Char *pszPrefix, const XML_Char *pszURI)
680 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
681 pThis ->OnStartNamespaceDecl (pszPrefix, pszURI);
684 // @cmember End namespace decl wrapper
686 static void __cdecl EndNamespaceDeclHandler (void *pUserData,
687 const XML_Char *pszPrefix)
689 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
690 pThis ->OnEndNamespaceDecl (pszPrefix);
693 // @cmember XML declaration wrapper
695 static void __cdecl XmlDeclHandler (void *pUserData,
696 const XML_Char *pszVersion, const XML_Char *pszEncoding,
699 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
700 pThis ->OnXmlDecl (pszVersion, pszEncoding, nStandalone != 0);
703 // @cmember Start Doctype declaration wrapper
705 static void __cdecl StartDoctypeDeclHandler (void *pUserData,
706 const XML_Char *pszDoctypeName, const XML_Char *pszSysID,
707 const XML_Char *pszPubID, int nHasInternalSubset)
709 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
710 pThis ->OnStartDoctypeDecl (pszDoctypeName, pszSysID,
711 pszPubID, nHasInternalSubset != 0);
714 // @cmember End Doctype declaration wrapper
716 static void __cdecl EndDoctypeDeclHandler (void *pUserData)
718 _T *pThis = static_cast <_T *> ((CExpatImpl <_T> *) pUserData);
719 pThis ->OnEndDoctypeDecl ();
722 // @access Protected members
728 //-----------------------------------------------------------------------------
730 // Virtual method class definition
732 //-----------------------------------------------------------------------------
734 class CExpat : public CExpatImpl <CExpat>
736 // @access Constructors and destructors
743 // @access Public handler methods
746 // @cmember Start element handler
748 virtual void OnStartElement (const XML_Char *pszName,
749 const XML_Char **papszAttrs)
754 // @cmember End element handler
756 virtual void OnEndElement (const XML_Char *pszName)
761 // @cmember Character data handler
763 virtual void OnCharacterData (const XML_Char *pszData, int nLength)
768 // @cmember Processing instruction handler
770 virtual void OnProcessingInstruction (const XML_Char *pszTarget,
771 const XML_Char *pszData)
776 // @cmember Comment handler
778 virtual void OnComment (const XML_Char *pszData)
783 // @cmember Start CDATA section handler
785 virtual void OnStartCdataSection ()
790 // @cmember End CDATA section handler
792 virtual void OnEndCdataSection ()
797 // @cmember Default handler
799 virtual void OnDefault (const XML_Char *pszData, int nLength)
804 // @cmember External entity ref handler
806 virtual bool OnExternalEntityRef (const XML_Char *pszContext,
807 const XML_Char *pszBase, const XML_Char *pszSystemID,
808 const XML_Char *pszPublicID)
813 // @cmember Unknown encoding handler
815 virtual bool OnUnknownEncoding (const XML_Char *pszName, XML_Encoding *pInfo)
820 // @cmember Start namespace declaration handler
822 virtual void OnStartNamespaceDecl (const XML_Char *pszPrefix,
823 const XML_Char *pszURI)
828 // @cmember End namespace declaration handler
830 virtual void OnEndNamespaceDecl (const XML_Char *pszPrefix)
835 // @cmember XML declaration handler
837 virtual void OnXmlDecl (const XML_Char *pszVersion,
838 const XML_Char *pszEncoding, bool fStandalone)
843 // @cmember Start DOCTYPE declaration handler
845 virtual void OnStartDoctypeDecl (const XML_Char *pszDoctypeName,
846 const XML_Char *pszSysID, const XML_Char *pszPubID,
847 bool fHasInternalSubset)
852 // @cmember End DOCTYPE declaration handler
854 virtual void OnEndDoctypeDecl ()
860 #endif // DSSI_EXPATIMPL_H