Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
ParseExpat.cpp File Reference
#include "stdafx.h"
#include "xmlparse.h"

Go to the source code of this file.

Classes

struct  ParserState
struct  ParseData

Defines

#define XML_UNICODE
#define X2OLE   W2COLE
#define X2T   W2T

Functions

static void OnStartElement (void *userData, const XML_Char *name, const XML_Char **atts)
static void OnEndElement (void *userData, const XML_Char *name)
static void OnCharacterData (void *userData, const XML_Char *s, int len)
static void OnDefault (void *userData, const XML_Char *s, int len)
HRESULT ParseExpat (const char *pBuffer, unsigned long cbBufSize, IXMLDocument *pDocument, IXMLElement **ppElement)

Variables

static ParserState cParserState

Class Documentation

struct ParserState

Definition at line 15 of file ParseExpat.cpp.

Class Members
CComQIPtr< IXMLDocument,&IID_IXMLDocument > spXMLDocument
CComQIPtr< IXMLElement,&IID_IXMLElement > spXMLParent
CComQIPtr< IXMLElement,&IID_IXMLElement > spXMLRoot
struct ParseData

Definition at line 32 of file ParseExpat.cpp.

Class Members
CComQIPtr< IXMLDocument,&IID_IXMLDocument > spDocument
CComQIPtr< IXMLElement,&IID_IXMLElement > spRoot

Define Documentation

#define X2OLE   W2COLE

Definition at line 7 of file ParseExpat.cpp.

#define X2T   W2T

Definition at line 8 of file ParseExpat.cpp.

Definition at line 3 of file ParseExpat.cpp.


Function Documentation

void OnCharacterData ( void userData,
const XML_Char s,
int  len 
) [static]

Definition at line 162 of file ParseExpat.cpp.

{
       ParserState *pState = (ParserState *) userData;
       if (pState)
       {
              // TODO create TEXT element
       }
}

Here is the caller graph for this function:

void OnDefault ( void userData,
const XML_Char s,
int  len 
) [static]

Definition at line 140 of file ParseExpat.cpp.

{
       XML_Char *pString = new XML_Char[len + 1];
       memset(pString, 0, sizeof(XML_Char) * (len + 1));
       memcpy(pString, s, sizeof(XML_Char) * len);

       USES_CONVERSION;
       ATLTRACE(_T("OnDefault: \"%s\"\n"), X2T(pString));

       // TODO test if the buffer contains <?xml version="X"?>
       //      and store version in XML document

       // TODO test if the buffer contains DTD and store it
       //      in the XML document

       // TODO test if the buffer contains a comment, i.e. <!--.*-->
       //      and create a comment XML element

       delete []pString;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void OnEndElement ( void userData,
const XML_Char name 
) [static]

Definition at line 125 of file ParseExpat.cpp.

{
       ParserState *pState = (ParserState *) userData;
       if (pState)
       {
              CComQIPtr<IXMLElement, &IID_IXMLElement> spNewParent;
              if (pState->spXMLParent)
              {
                     pState->spXMLParent->get_parent(&spNewParent);
                     pState->spXMLParent = spNewParent;
              }
       }
}

Here is the caller graph for this function:

void OnStartElement ( void userData,
const XML_Char name,
const XML_Char **  atts 
) [static]

Definition at line 85 of file ParseExpat.cpp.

{
       ParserState *pState = (ParserState *) userData;
       if (pState)
       {
              USES_CONVERSION;

              CComQIPtr<IXMLElement, &IID_IXMLElement> spXMLElement;

              // Create a new element
              pState->spXMLDocument->createElement(
                            CComVariant(XMLELEMTYPE_ELEMENT),
                            CComVariant(X2OLE(name)),
                            &spXMLElement);

              if (spXMLElement)
              {
                     // Create each attribute
                     for (int i = 0; atts[i] != NULL; i += 2)
                     {
                            const XML_Char *pszName = atts[i];
                            const XML_Char *pszValue = atts[i+1];
                            spXMLElement->setAttribute((BSTR) X2OLE(pszName), CComVariant(X2OLE(pszValue)));
                     }

                     if (pState->spXMLRoot == NULL)
                     {
                            pState->spXMLRoot = spXMLElement;
                     }
                     if (pState->spXMLParent)
                     {
                            // Add the element to the end of the list
                            pState->spXMLParent->addChild(spXMLElement, -1, -1);
                     }
                     pState->spXMLParent = spXMLElement;
              }
       }
}

Here is the caller graph for this function:

HRESULT ParseExpat ( const char *  pBuffer,
unsigned long  cbBufSize,
IXMLDocument *  pDocument,
IXMLElement **  ppElement 
)

Definition at line 38 of file ParseExpat.cpp.

{
       if (pDocument == NULL)
       {
              return E_INVALIDARG;
       }

       XML_Parser parser = XML_ParserCreate(NULL);
       HRESULT hr = S_OK;

       cParserState.spXMLDocument = pDocument;
       pDocument->get_root(&cParserState.spXMLParent);

       // Initialise the XML parser
       XML_SetUserData(parser, &cParserState);

       // Initialise the data handlers
       XML_SetElementHandler(parser, OnStartElement, OnEndElement);
       XML_SetCharacterDataHandler(parser, OnCharacterData);
       XML_SetDefaultHandler(parser, OnDefault);

       // Parse the data
       if (!XML_Parse(parser, pBuffer, cbBufSize, 1))
       {
              /* TODO Create error code
                 fprintf(stderr,
                     "%s at line %d\n",
                 XML_ErrorString(XML_GetErrorCode(parser)),
                 XML_GetCurrentLineNumber(parser));
               */
              hr = E_FAIL;
       }

       // Cleanup
       XML_ParserFree(parser);

       cParserState.spXMLRoot->QueryInterface(IID_IXMLElement, (void **) ppElement);
       cParserState.spXMLDocument.Release();
       cParserState.spXMLParent.Release();

       return S_OK;
}

Here is the call graph for this function:


Variable Documentation

Definition at line 22 of file ParseExpat.cpp.