/***********************************************************************/ /* Copyright (C) 2002 Definitive Solutions, Inc. All Rights Reserved. */ /* THIS COMPUTER PROGRAM IS PROPRIETARY AND CONFIDENTIAL TO DEFINITIVE */ /* SOLUTIONS, INC. AND ITS LICENSORS AND CONTAINS TRADE SECRETS OF */ /* DEFINITIVE SOLUTIONS, INC. THAT ARE PROVIDED PURSUANT TO A WRITTEN */ /* AGREEMENT CONTAINING RESTRICTIONS ON USE AND DISCLOSURE. ANY USE, */ /* REPRODUCTION, OR TRANSFER EXCEPT AS PROVIDED IN SUCH AGREEMENT */ /* IS STRICTLY PROHIBITED. */ /***********************************************************************/ ///////////////////////////////////////////////////////////////////////////// // // How to use this class: // // 0). Be sure you have MSXML3.DLL on your system. If not, download it for // free from: // // msdn.microsoft.com/downloads/default.asp?URL=/downloads/sample.asp?url=/msdn-files/027/001/596/msdncompositedoc.xml // // 1). Derive a class of your own from MySax2, and (most commonly) override // the 5 methods that are usually used (see the MySax2 class declaration // below), or (less commonly) any virtual methods you want. // // 2). If you want to do your own error handling (my default implementation // just asserts and logs), override any of the ISAXErrorHandler virtual // methods you like. // // 3). In your application, instantiate an object of your MySax2-derived class. // Then call Initialize() exactly once: // // YourSax2 ys2; // ys2.Initialize(); // // 4). Then, as many times as desired, call Parse(). // // 5). No cleanup is necessary (other than to delete any objects that you // created on the heap, of course). // ///////////////////////////////////////////////////////////////////////////// #ifndef MYSAX2H_0FF05840_A94D_11d5_8A48_00B0D0529ED2 #define MYSAX2H_0FF05840_A94D_11d5_8A48_00B0D0529ED2 #pragma once #include "MyApp.h" #include "MyLog.h" #include // USES_CONVERSION // Import the SAX2 stuff. #import raw_interfaces_only using namespace MSXML2; ///////////////////////////////////////////////////////////////////////////// // MySax2 // Declare the class depending on this file is in an EXE project or a DLL one. class #ifdef _WINDLL AFX_EXT_CLASS #endif MySax2 : public ISAXContentHandler, public ISAXErrorHandler { // Construction. public: // The constructor does almost nothing. See Initialize() for the actual // initialization of this class. MySax2(); // The destructor handles all cleanup. virtual ~MySax2(); // Deliberately declared but not defined. private: MySax2(const MySax2& rhs); MySax2& operator=(const MySax2& rhs); // Interface. public: // This must be called after the constructor; must be called exactly once // during the object's lifetime. HRESULT Initialize(); // This parses the sent XML document name. HRESULT Parse(const CString& sXmlName) const; // Implementation. These are the 5 ISAXContentHandler methods you most // commonly will override. This class provides default implementations that // just log the events. protected: // Receives notification of the beginning of a document. virtual HRESULT STDMETHODCALLTYPE startDocument(); // Receives notification of the end of a document. virtual HRESULT STDMETHODCALLTYPE endDocument(); // Receives notification of the beginning of an element. virtual HRESULT STDMETHODCALLTYPE startElement( /* [in] */ wchar_t __RPC_FAR *pwchNamespaceUri, /* [in] */ int cchNamespaceUri, /* [in] */ wchar_t __RPC_FAR *pwchLocalName, /* [in] */ int cchLocalName, /* [in] */ wchar_t __RPC_FAR *pwchRawName, /* [in] */ int cchRawName, /* [in] */ ISAXAttributes __RPC_FAR *pAttributes); // Receives notification of character data. virtual HRESULT STDMETHODCALLTYPE characters( /* [in] */ wchar_t __RPC_FAR *pwchChars, /* [in] */ int cchChars); // Receives notification of the end of an element. virtual HRESULT STDMETHODCALLTYPE endElement( /* [in] */ wchar_t __RPC_FAR *pwchNamespaceUri, /* [in] */ int cchNamespaceUri, /* [in] */ wchar_t __RPC_FAR *pwchLocalName, /* [in] */ int cchLocalName, /* [in] */ wchar_t __RPC_FAR *pwchRawName, /* [in] */ int cchRawName); // Implementation. These are the ISAXContentHandler methods you most commonly // will NOT override. This class provides inline default implementations // that do nothing. protected: // Receives an interface for locating the origin of SAX (Simple API for XML) // document events. The reader supplies a locator to the application by // invoking this method before invoking any other methods in the // ISAXContentHandler interface. This locator allows the application to // determine the end position of any document-related event, even if the // parser is not reporting an error. virtual HRESULT STDMETHODCALLTYPE putDocumentLocator( /* [in] */ ISAXLocator __RPC_FAR *pLocator); virtual HRESULT STDMETHODCALLTYPE startPrefixMapping( /* [in] */ wchar_t __RPC_FAR *pwchPrefix, /* [in] */ int cchPrefix, /* [in] */ wchar_t __RPC_FAR *pwchUri, /* [in] */ int cchUri) { return S_OK; } virtual HRESULT STDMETHODCALLTYPE endPrefixMapping( /* [in] */ wchar_t __RPC_FAR *pwchPrefix, /* [in] */ int cchPrefix) { return S_OK; } virtual HRESULT STDMETHODCALLTYPE ignorableWhitespace( /* [in] */ wchar_t __RPC_FAR *pwchChars, /* [in] */ int cchChars) { return S_OK; } virtual HRESULT STDMETHODCALLTYPE processingInstruction( /* [in] */ wchar_t __RPC_FAR *pwchTarget, /* [in] */ int cchTarget, /* [in] */ wchar_t __RPC_FAR *pwchData, /* [in] */ int cchData) { return S_OK; } virtual HRESULT STDMETHODCALLTYPE skippedEntity( /* [in] */ wchar_t __RPC_FAR *pwchName, /* [in] */ int cchName) { return S_OK; } // These are the ISAXErrorHandler methods that your derived class may override // if desired. This class provides an default implementations that assert and // log the error. public: // Receives notification of a recoverable error. Microsoft says: "This // method is not called in the current SAX2 (Simple API for XML) // implementation... Currently, all errors are fatal." virtual HRESULT STDMETHODCALLTYPE error( /* [in] */ ISAXLocator __RPC_FAR *pLocator, /* [in] */ unsigned short * pwchErrorMessage, /* [in] */ HRESULT errCode); // Receives notification of a non-recoverable error. virtual HRESULT STDMETHODCALLTYPE fatalError( /* [in] */ ISAXLocator __RPC_FAR *pLocator, /* [in] */ unsigned short * pwchErrorMessage, /* [in] */ HRESULT errCode); // Receives notification of a warning. Microsoft says: "This method is not // called in the current SAX2 (Simple API for XML) implementation." virtual HRESULT STDMETHODCALLTYPE ignorableWarning( /* [in] */ ISAXLocator __RPC_FAR *pLocator, /* [in] */ unsigned short * pwchErrorMessage, /* [in] */ HRESULT errCode); // Implementation. These are internal methods that you should not need to // ever override. protected: // Releases any resources we may have allocated. void Cleanup(); // Implementation. These are the famous three IUnknown methods that must be // correctly implemented if your handler is to be a COM Object. The default // implementation here will suffice if your handler is not a COM object. protected: HRESULT STDMETHODCALLTYPE QueryInterface(const struct _GUID& riid, PVOID* ppvObject) { return S_OK; } unsigned long __stdcall AddRef() { return S_OK; } unsigned long __stdcall Release() { return S_OK; } // Data for derived classes. protected: // The SAX2 parser object. ISAXXMLReader* m_pReader; // A cached copy of the Locator that the parser hands us. ISAXLocator* m_pLocator; // The stack of Element names, built as they are parsed. CStringList m_slElements; // The characters() call can technically give you the data piece-wise, so // we build up this buffer each time it is called. CString m_sCharacters; }; #endif // MYSAX2H_0FF05840_A94D_11d5_8A48_00B0D0529ED2