Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ccontenthandler.cpp
#include "ccontenthandler.h"
using namespace P6R;
P6DECLARE_IID( IContentHandlerInit );
namespace P6EXAMPLES {
CContentHandler::CContentHandler() : m_bInitialized( P6FALSE ),
m_cpStr(),
m_cpConsole(),
m_charState( 0 ),
m_startState( 0 ),
m_endState( 0 ),
m_startDoc( 0 ),
m_endDoc( 0 )
{ }
//
CContentHandler::~CContentHandler()
{
}
//
// P6COM Helper macro to provide the implementation
// of standard COM Methods:
//
// createInstance() - Used to easily create instance of this component.
// Using this method to create components is preferred
// because it returns an interface which provides
// a well defined API for this component and prevents
// access to the classes internals.
// queryInterface() - Used to query the component for interfaces
// addref() - Increased the interfaces reference count
// release() - Decreases the interfaces reference count
// and destroys the component when the count
// reaches zero.
//
// These helper macros are provided for convenience and currently support
// up to 17 interfaces (this example exposes 2 interfaces). The implementation
// provided by these macros are threadsafe. You can find the defintions
// in p6comhlpr.h. If you need something more that what they provide,
// you are always free implement your own methods.
//
// See CConsoleStream in this example (in ex-load-jsn.cpp) for a non-threadsafe
// example of implementing these methods.
//
// Please note that these macros may only be used in code that is called
// after p6InitializeLoader() has been called.
//
P6_IMPLEMENT_ICOM2(CContentHandler,IContentHandlerInit,p6ISAX2ContentHandler);
//
P6COMMETHODIMPL CContentHandler::initialize( P6R::p6IConsole *pConsole, P6R::p6ISafeString* pStr )
{
if (m_bInitialized) return eAlreadyInitialized;
if (NULL == pStr ) return eInvalidArg;
//
// p6ComPtr<> automatcally calls
// addref() on assignment.
//
m_cpConsole = pConsole;
m_cpStr = pStr;
m_bInitialized= P6TRUE;
return eOk;
}
//
P6COMMETHODIMPL CContentHandler::characters( P6SAX2STRING* pBuffer, P6SAX2METADATA meta )
{
P6INT32 iResult = -1;
P6ERR err = eOk;
P6ARG args[1];
if (!m_bInitialized) return eNotInitialized;
if (NULL == pBuffer) return eInvalidArg;
switch( m_charState ) {
case 0:
if (45 != pBuffer->length) {
P6AI_UINT32(&args[0],pBuffer->length);
m_cpConsole->writeStdout("case 0: Expected 45 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "\n Now is the time for all good men to spend ", pBuffer->pStart, 45, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 0: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
case 1:
if (12 != pBuffer->length) {
P6AI_UINT32(&args[0],pBuffer->length);
m_cpConsole->writeStdout("case 1: Expected 12 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "55 <dollars>", pBuffer->pStart, 12, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 1: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
case 2:
if (6 != pBuffer->length) {
P6AI_UINT32(&args[0],pBuffer->length);
m_cpConsole->writeStdout("case 2: Expected 6 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "on \n ", pBuffer->pStart, 6, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 2: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
case 3:
if (2 != pBuffer->length) {
P6AI_UINT32(&args[0],pBuffer->length);
m_cpConsole->writeStdout("case 3: Expected 2 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "33", pBuffer->pStart, 2, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 3: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
case 4:
if (24 != pBuffer->length) {
P6AI_UINT32(&args[0],pBuffer->length);
m_cpConsole->writeStdout("case 4: Expected 2 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "now that we are open.\n ", pBuffer->pStart, 24, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 4: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
case 5:
if (24 != pBuffer->length) {
P6AI_UINT32(&args[0],pBuffer->length);
m_cpConsole->writeStdout("case 5: Expected 24 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( " using a namespace here ", pBuffer->pStart, 24, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 5: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
default:
P6AI_UINT32(&args[0],m_charState);
m_cpConsole->writeStdout("Error: charstate out of range got [ %1$ ]\n",&args[0],1,NULL);
break;
}
m_charState++;
P6AI_CHARPTR(&args[0],(1 == meta ? "CDATA" : "NONE"));
m_cpConsole->writeStdout("\n-> TOKEN_ELEMENT_VALUE meta:%1$\n",&args[0],1,NULL);
debugPrint( "-> TOKEN_ELEMENT_VALUE\n", pBuffer );
return err;
}
//
P6COMMETHODIMPL CContentHandler::endDocument()
{
if (!m_bInitialized) return eNotInitialized;
m_endDoc++;
m_cpConsole->writeStdout("-> TOKEN_END_DOCUMENT\n",NULL,0,NULL);
return eOk;
}
//
P6COMMETHODIMPL CContentHandler::endElement( P6SAX2STRING* pURI, P6SAX2STRING* pLocalName, P6SAX2STRING* pQName )
{
P6INT32 iResult = -1;
P6ERR err = eOk;
P6ARG args[1];
if (!m_bInitialized) return eNotInitialized;
if (NULL == pURI || NULL == pLocalName || NULL == pQName) return eInvalidArg;
switch( m_endState ) {
case 0:
if (5 != pLocalName->length) {
P6AI_UINT32(&args[0],pLocalName->length);
m_cpConsole->writeStdout("case 0: Expected 5 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "money", pLocalName->pStart, 5, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 0: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
case 1:
if (4 != pLocalName->length) {
P6AI_UINT32(&args[0],pLocalName->length);
m_cpConsole->writeStdout("case 1: Expected 4 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "item", pLocalName->pStart, 4, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 1: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
case 2:
if (5 != pLocalName->length) {
P6AI_UINT32(&args[0],pLocalName->length);
m_cpConsole->writeStdout("case 2: Expected 5 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "short", pLocalName->pStart, 5, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 2: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
case 3:
if (7 != pLocalName->length) {
P6AI_UINT32(&args[0],pLocalName->length);
m_cpConsole->writeStdout("case 3: Expected 7 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "comment", pLocalName->pStart, 5, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 3: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
case 4:
if (5 != pLocalName->length) {
P6AI_UINT32(&args[0],pLocalName->length);
m_cpConsole->writeStdout("case 4: Expected 5 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "mixed", pLocalName->pStart, 5, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 4: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
case 5:
if (5 != pLocalName->length) {
P6AI_UINT32(&args[0],pLocalName->length);
m_cpConsole->writeStdout("case 5: Expected 5 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "first", pLocalName->pStart, 5, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 5: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
default:
P6AI_UINT32(&args[0],m_endState);
m_cpConsole->writeStdout("Error: endState out of range got [ %1$ ]\n",&args[0],1,NULL);
break;
}
debugPrint( "\n-> TOKEN_END_ELEMENT QName ", pQName );
debugPrint( "-> TOKEN_END_ELEMENT localName ", pLocalName );
debugPrint( "-> TOKEN_END_ELEMENT URI ", pURI );
m_endState++;
return eOk;
}
//
P6COMMETHODIMPL CContentHandler::endPrefixMapping( P6SAX2STRING* pPrefix )
{
if (!m_bInitialized) return eNotInitialized;
if (NULL == pPrefix) return eInvalidArg;
debugPrint( "\n-> endPrefixMapping prefix ", pPrefix );
return eOk;
}
//
P6COMMETHODIMPL CContentHandler::ignorableWhitespace( const P6CHAR* pBuffer, P6UINT32 start, P6UINT32 length )
{
P6ARG args[1];
if (!m_bInitialized) return eNotInitialized;
P6AI_UINT32(&args[0],length);
m_cpConsole->writeStdout("-> ignorableWhitespace, length:%1$\n",&args[0],1,NULL);
return eOk;
}
//
P6COMMETHODIMPL CContentHandler::processingInstruction( P6SAX2STRING* pTarget, P6SAX2STRING* pData )
{
if (!m_bInitialized) return eNotInitialized;
if (NULL == pTarget || NULL == pData) return eInvalidArg;
debugPrint( "\n-> processingInstruction target ", pTarget );
debugPrint( "-> processingInstruction target ", pData );
return eOk;
}
// p6ISax2Locator has several uses, one is to tell this content handler approximately what line
// and what character is the parser into the XML stream it is parsing. This can be used for
// error logging.
//
P6COMMETHODIMPL CContentHandler::setDocumentLocator( p6ISAX2Locator* pObject )
{
if (!m_bInitialized) return eNotInitialized;
if (NULL == pObject) return eInvalidArg;
m_cpConsole->writeStdout("-> setDocumentLocator\n",NULL,0,NULL);
return eOk;
}
//
P6COMMETHODIMPL CContentHandler::skippedEntity( P6SAX2STRING* pName )
{
if (!m_bInitialized) return eNotInitialized;
if (NULL == pName ) return eInvalidArg;
debugPrint( "-> skippedEntity ", pName );
return eOk;
}
//
P6COMMETHODIMPL CContentHandler::startDocument()
{
if (!m_bInitialized) return eNotInitialized;
m_startDoc++;
m_cpConsole->writeStdout("-> TOKEN_START_DOCUMENT\n",NULL,0,NULL);
return eOk;
}
//
P6COMMETHODIMPL CContentHandler::startElement( P6SAX2STRING* pURI,
P6SAX2STRING* pLocalName,
P6SAX2STRING* pQName,
P6SAX2STRING* pRawElement,
)
{
P6INT32 iResult = -1;
P6ERR err = eOk;
P6ARG args[1];
if (!m_bInitialized) return eNotInitialized;
if (NULL == pURI || NULL == pLocalName || NULL == pQName) return eInvalidArg;
if (m_startDoc != 1) {
P6AI_UINT32(&args[0],m_startDoc);
m_cpConsole->writeStdout("Expected 1 but got %1$",&args[0],1,NULL);
}
switch( m_startState ) {
case 0:
if (5 != pLocalName->length) {
P6AI_UINT32(&args[0],pLocalName->length);
m_cpConsole->writeStdout("case 0: Expected 5 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "first", pLocalName->pStart, 5, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 0: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
case 1:
if (5 != pLocalName->length) {
P6AI_UINT32(&args[0],pLocalName->length);
m_cpConsole->writeStdout("case 1: Expected 5 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "mixed", pLocalName->pStart, 5, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 1: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
case 2:
if (5 != pLocalName->length) {
P6AI_UINT32(&args[0],pLocalName->length);
m_cpConsole->writeStdout("case 2: Expected 5 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "money", pLocalName->pStart, 5, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 2: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
case 3:
if (4 != pLocalName->length) {
P6AI_UINT32(&args[0],pLocalName->length);
m_cpConsole->writeStdout("case 3: Expected 4 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "item", pLocalName->pStart, 4, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 3: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
case 4:
if (5 != pLocalName->length) {
P6AI_UINT32(&args[0],pLocalName->length);
m_cpConsole->writeStdout("case 4: Expected 5 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "short", pLocalName->pStart, 5, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 4: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
case 5:
if (7 != pLocalName->length) {
P6AI_UINT32(&args[0],pLocalName->length);
m_cpConsole->writeStdout("case 5: Expected 7 but got [ %1$ ]\n",&args[0],1,NULL);
}
err = m_cpStr->strncmp( "comment", pLocalName->pStart, 5, &iResult );
if (0 != iResult) {
P6AI_INT32(&args[0],iResult);
m_cpConsole->writeStdout("case 5: Expected 0 but got [ %1$ ]\n",&args[0],1,NULL);
}
break;
default:
P6AI_UINT32(&args[0],m_startState);
m_cpConsole->writeStdout("Error: startState out of range got [ %1$ ]\n",&args[0],1,NULL);
break;
}
debugPrint( "\n-> TOKEN_START_ELEMENT QName ", pQName );
debugPrint( "-> TOKEN_START_ELEMENT localName ", pLocalName );
debugPrint( "-> TOKEN_START_ELEMENT URI ", pURI );
m_startState++;
return eOk;
}
//
P6COMMETHODIMPL CContentHandler::startPrefixMapping( P6SAX2STRING* pPrefix, P6SAX2STRING* pURI )
{
if (!m_bInitialized) return eNotInitialized;
if (NULL == pPrefix || NULL == pURI) return eInvalidArg;
debugPrint( "\n-> startPrefixMapping prefix ", pPrefix );
debugPrint( "-> startPrefixMapping URI ", pURI );
return eOk;
}
// The P6SAX2STRING pointers point into the original XML documents so the tokens passed to this
// content handler are NOT NULL terminated strings. This function just prints out the token without the
// trailing rest of the XML input.
//
P6R::P6VOID CContentHandler::debugPrint( const P6CHAR* pHeader, P6SAX2STRING* pString )
{
P6BCSTR bStr;
P6ARG args[3];
if ( NULL != pString && NULL != pString->pStart) {
P6AI_CHARPTR(&args[0],pHeader);
bStr.pString = (const P6UCHAR*)pString->pStart;
bStr.length = pString->length;
P6AI_BCSTR(&args[1],&bStr);
P6AI_SIZE(&args[2],bStr.length);
m_cpConsole->writeStdout("%1$ [%2$] (%3$)\n",&args[0],3,NULL);
}
else {
P6AI_CHARPTR(&args[0],pHeader);
m_cpConsole->writeStdout("%1$ []\n",&args[0],1,NULL);
}
}
} // namespace