Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ex-sax2-2.cpp
#include "p6loader.h"
#include "cconsolestream.h"
#include "ccontenthandler.h"
using namespace P6R;
using namespace P6EXAMPLES;
P6DECLARE_CID( p6SAX2XMLReader );
P6DECLARE_IID( IContentHandlerInit );
namespace P6EXAMPLES {
P6R::P6ERR getSAX2(P6R::p6IDataStream *pOutStream,P6R::p6ISAX2XMLReader **ppIface)
{
P6ERR err;
if(P6SUCCEEDED(err = p6CreateInstance(NULL,CID_p6SAX2XMLReader,VALIDATECOMPTR(p6ISAX2XMLReader,cpSAX)))) {
if (P6SUCCEEDED( err = cpSAX->initialize( P6SAX2_NOFLAGS ))) {
if (P6SUCCEEDED( err = cpSAX->setFeature( "http://xml.org/sax/features/namespaces", P6TRUE ))) {
if (P6SUCCEEDED( err = cpSAX->setFeature( "http://xml.org/sax/features/namespaces-prefixes", P6TRUE ))) {
//
// Using detach() saves a reference counting operation
// and gives ownership to *ppIface.
//
cpSAX.detach(ppIface);
}
}
}
}
return err;
}
P6R::P6ERR getContentHandler( P6R::p6IConsole *pConsole, P6R::p6ISafeString *pStr, P6R::p6ISAX2ContentHandler **ppIface)
{
P6ERR err;
if(P6SUCCEEDED(err = CContentHandler::createInstance(NULL,VALIDATECOMPTR(IContentHandlerInit,cpInit)))) {
if (P6SUCCEEDED( err = cpInit->initialize( pConsole, pStr ))) {
err = cpInit.queryInterface(IID_p6ISAX2ContentHandler,ppIface);
}
}
return err;
}
P6R::P6ERR runSAX2( P6R::p6IConsole *pConsole, p6IDataStream *pStreamDebug )
{
const P6CHAR* pBuffer = NULL;
P6UINT32 bufSize = 0;
P6UINT32 index = 0;
P6ERR err = eOk;
// -> this simulates XML (or JSON with the p6IJSONReader) being read into a buffer a "chunk" at a time
const P6CHAR* pChunk[20];
pChunk[ 0] = "<?xml version='1.0' enco";
pChunk[ 1] = "ding='UTF-8' ?>";
pChunk[ 2] = "<fir";
pChunk[ 3] = "st xmlns:xslt='http://www.w";
pChunk[ 4] = "3.org/1999/XSL/Transform'>";
pChunk[ 5] = " <second temp='55' xmlns:X='http://www.w3.org/TR/REC-html40'><xslt";
pChunk[ 6] = ":if test='123'><![CDATA[ one two three]]><";
pChunk[ 7] = "/xslt:if></second><third color = '> green' name='Jon\r\nSmith' weight='3453'>";
pChunk[ 8] = " four five si";
pChunk[ 9] = "x seven";
pChunk[10] = " </third>";
pChunk[11] = "<test>Now is the time<![CDATA[ for <all >good ]";
pChunk[12] = "]>men to come to the aid &";
pChunk[13] = "amp; support.</test></first>";
pChunk[14] = "";
pChunk[15] = NULL;
// [A] Create & initialize the components we need
if (P6FAILED( err = p6GetRuntimeIface( VALIDATECOMPTR( p6ISafeString, cpStr )))) return err;
if (P6FAILED( err = getSAX2(pStreamDebug,cpReader.addressof()))) return err;
err = cpReader.queryInterface(IID_p6IDataStream, cpStream.addressof());
// [B] Define the essential set of content handlers
if(P6SUCCEEDED(err = getContentHandler( pConsole, cpStr, cpContent.addressof() ))) {
err = cpReader->setContentHandler( cpContent );
}
// [C] SAX2 parser generates a stream of events like any SAX2 parser
if (P6SUCCEEDED( err )) err = cpStream->beginStream();
// -> in a real application the read into the buffer would happen here, while we fake it
pBuffer = pChunk[ index++ ];
err = cpStr->strlen( pBuffer, 100000, &bufSize );
while( 0 < bufSize && P6SUCCEEDED( err ))
{
// -> this call streams the XML into the SAX2 parser a buffer at a time
err = cpStream->processStream( pBuffer, (P6UINT32)bufSize );
// -> in a real application the next read into buffer would happen here, again we fake it
pBuffer = pChunk[ index++ ];
err = cpStr->strlen( pBuffer, 100000, &bufSize );
}
err = cpStream->endStream();
cpStream = NULL;
if (P6FAILED( err )) {
P6ARG args[1];
P6AI_ERR(&args[0],err);
pConsole->writeStdout("ERROR: Example failed with [ %1$ ]\n", &args[0], 1, NULL );
}
return err;
}
} // namespace
int main(int argc,char *argv[])
{
P6ERR err = eOk;
p6ComPtr<p6IDataStream> cpDebugStream;
P6ARG args[1];
if(P6SUCCEEDED(err = CConsoleStream::createInstance(NULL,VALIDATECOMPTR(p6IDataStream,cpDebugStream)))) {
if(P6SUCCEEDED(err = p6InitializeLoader(cpDebugStream,9,P6SCLF_NOFLAGS))) {
err = runSAX2( cpConsole, cpDebugStream );
P6AI_ERR(&args[0],err);
cpConsole->writeStdout("runSAX2 result: [ %1$ ]\n",&args[0],1,NULL);
//
// Make sure to release the console interface
// before calling p6CleanupLoader() !! In this
// instance we don't let the smart pointer
// handle the cleanup.
//
cpConsole = NULL;
}
else printf("ERROR: Failed to retrieve console interface [ %x ]\n", err );
}
else printf("ERROR: Failed to initialize the loader [ %x ]\n", err );
}
else printf( "ERROR: Failed to create CConsoleStream [ %x ]\n", err );
return err;
}