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_cpLoc(),
m_startPair( 0 ),
m_startObj( 0 ),
m_startArray( 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,p6IJSONContentHandler);
//
P6R::P6ERR CContentHandler::initialize( p6IConsole *pConsole, p6ISafeString* pStr )
{
if (m_bInitialized) return eAlreadyInitialized;
if (!pStr || !pConsole ) return eInvalidArg;
m_cpStr = pStr;
m_cpConsole = pConsole;
m_bInitialized = P6TRUE;
return eOk;
}
//
P6COMMETHODIMPL CContentHandler:: endDocument()
{
if (!m_bInitialized) return eNotInitialized;
m_cpConsole->writeStdout("-> endDocument()\n",NULL,0,NULL);
return eOk;
}
//
P6COMMETHODIMPL CContentHandler:: endObject( P6UINT32 nestingLevel )
{
P6ARG args[1];
if (!m_bInitialized) return eNotInitialized;
P6AI_UINT32(&args[0],nestingLevel);
m_cpConsole->writeStdout("-> entered endObject() Level [ %1$ ]\n",&args[0],1,NULL);
m_startObj--;
return eOk;
}
//
P6COMMETHODIMPL CContentHandler::endArray( P6UINT32 nestingLevel )
{
P6ARG args[1];
if (!m_bInitialized) return eNotInitialized;
P6AI_UINT32(&args[0],nestingLevel);
m_cpConsole->writeStdout("-> entered endArray() Level [ %1$ ]\n",&args[0],1,NULL);
m_startArray--;
return eOk;
}
// The given locator is just like a SAX2 locator, it can be used to identify where in the stream of JSON we are currently
// parsing. Thus this can be used for warning messages, logging, etc.
//
P6COMMETHODIMPL CContentHandler::setDocumentLocator( p6IJSONLocator* pObject )
{
if (!m_bInitialized) return eNotInitialized;
if (NULL == pObject) return eInvalidArg;
m_cpLoc = pObject; // Smart pointer Automatically addref()'s pObject
return eOk;
}
//
// Called when reach the beginning of the JSON Document
//
P6COMMETHODIMPL CContentHandler::startDocument()
{
if (!m_bInitialized) return eNotInitialized;
m_cpConsole->writeStdout("-> startDocument()\n",NULL,0,NULL);
return eOk;
}
//
// Called when reach the start of a JSON object
// An object is an unordered set of name/value pairs
// that begins with a left-brace "{" and ends with a
// right-brace "}".
//
P6COMMETHODIMPL CContentHandler::startObject( P6UINT32 nestingLevel )
{
P6ARG args[1];
if (!m_bInitialized) return eNotInitialized;
P6AI_UINT32(&args[0],nestingLevel);
m_cpConsole->writeStdout("-> startObject() Level [ %1$ ]\n",&args[0],1,NULL);
m_startObj++;
return eOk;
}
//
// Called when reaching the beginning of a JSON
// name/value pair.
//
P6COMMETHODIMPL CContentHandler::startPair( P6JSONSTRING* pName )
{
P6BSTR bStr = {0};
P6ARG args[2];
if (!m_bInitialized) return eNotInitialized;
bStr.pString = (P6UCHAR*)pName->pStart;
bStr.length = pName->length;
P6AI_BSTR(&args[0],&bStr);
P6AI_SIZE(&args[1],bStr.length);
m_cpConsole->writeStdout("-> startPair() [ %1$ ] %2$\n",&args[0],2,NULL);
m_startPair++;
return eOk;
}
//
P6COMMETHODIMPL CContentHandler::startArray( P6UINT32 nestingLevel )
{
P6ARG args[1];
if (!m_bInitialized) return eNotInitialized;
P6AI_UINT32(&args[0],nestingLevel);
m_cpConsole->writeStdout("-> startArray() Level [ %1$ ]\n",&args[0],1,NULL);
m_startArray++;
return eOk;
}
//
{
P6BSTR bStr = {0};
P6ARG args[2];
if (!m_bInitialized) return eNotInitialized;
switch(pValue->type) {
m_cpConsole->writeStdout("-> value() P6JSON_TYPE_NULL\n",NULL,0,NULL);
break;
}
bStr.pString = (P6UCHAR*) pValue->jstring.pStart;
bStr.length = pValue->jstring.length;
P6AI_BSTR(&args[0],&bStr);
P6AI_SIZE(&args[1],bStr.length);
m_cpConsole->writeStdout("-> value() P6JSON_TYPE_STR ( str[ %1$ ] len[ %2$ ] )\n",&args[0],2,NULL);
break;
}
bStr.pString = (P6UCHAR*) pValue->jstring.pStart;
bStr.length = pValue->jstring.length;
P6AI_BSTR(&args[0],&bStr);
P6AI_SIZE(&args[1],bStr.length);
m_cpConsole->writeStdout("-> value() P6JSON_TYPE_STRPART ( str[ %1$ ] len[ %2$ ] )\n",&args[0],2,NULL);
break;
}
bStr.pString = (P6UCHAR*) pValue->jstring.pStart;
bStr.length = pValue->jstring.length;
P6AI_BSTR(&args[0],&bStr);
P6AI_SIZE(&args[1],bStr.length);
m_cpConsole->writeStdout("-> value() P6JSON_TYPE_STREND ( str[ %1$ ] len[ %2$ ] )\n",&args[0],2,NULL);
break;
}
P6AI_BOOL(&args[0],pValue->boolean);
m_cpConsole->writeStdout("-> value() P6JSON_TYPE_BOOL ( %1$ )\n",&args[0],1,NULL);
break;
}
P6AI_INT32(&args[0],pValue->integer);
P6AI_INT32_HEXL(&args[0],pValue->integer);
m_cpConsole->writeStdout("-> value() P6JSON_TYPE_INTEGER ( %1$ [ %2$ ] )\n",&args[0],2,NULL);
break;
}
P6AI_FLOAT(&args[0],pValue->real);
m_cpConsole->writeStdout("-> value() P6JSON_TYPE_REAL ( %1$ )\n",&args[0],1,NULL);
break;
}
default: {
P6AI_UINT32(&args[0],(P6UINT32)pValue->type);
m_cpConsole->writeStdout("ERROR: Unknow Type [ %1$ ]\n",&args[0],1,NULL);
break;
}
}
return eOk;
}
} // namespace