Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
namespaces.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory>
#include "namespaces.h"
P6DECLARE_IID( p6ICom );
P6DECLARE_IID( p6IXMLCompile );
P6DECLARE_IID( INamespacesInit );
using namespace P6R;
namespace P6EXAMPLES {
CNamespaces::CNamespaces()
: m_bInitialized( false ),
m_pMapping( NULL ),
m_mapSize( 0 ),
m_nextIndex( 0 )
{
}
CNamespaces::~CNamespaces()
{
if (NULL != m_pMapping)
{
for( P6UINT32 i=0; i < m_mapSize; i++ )
{
if (NULL != m_pMapping[i].pPrefix) free( m_pMapping[i].pPrefix );
if (NULL != m_pMapping[i].pURI ) free( m_pMapping[i].pURI );
}
delete [] m_pMapping;
}
}
//
// 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(CNamespaces,INamespacesInit,p6IXMLCompile);
P6COMMETHODIMPL CNamespaces::initialize( P6UINT32 mapSize )
{
if (m_bInitialized) return eAlreadyInitialized;
m_mapSize = mapSize;
if (NULL == (m_pMapping = new (std::nothrow) prefixToURL_t[ m_mapSize ])) return eNoMemory;
for( P6UINT32 i=0; i < m_mapSize; i++ )
{
m_pMapping[i].pPrefix = NULL;
m_pMapping[i].prefixLength = 0;
m_pMapping[i].pURI = NULL;
m_pMapping[i].uriLength = 0;
}
m_bInitialized = P6TRUE;
return eOk;
}
P6COMMETHODIMPL CNamespaces::defineNamespace( const P6CHAR *pPrefix, P6UINT32 prefixLength, const P6CHAR *pURI, P6UINT32 uriLength )
{
P6ERR err = eOk;
if (!m_bInitialized) return eNotInitialized;
if (NULL == pPrefix || 0 == prefixLength || NULL == pURI || 0 == uriLength) return eInvalidArg;
if (m_nextIndex == m_mapSize) return eTooBig; // -> the mapping table is full
m_pMapping[ m_nextIndex ].pPrefix = strdup( pPrefix );
m_pMapping[ m_nextIndex ].prefixLength = prefixLength;
m_pMapping[ m_nextIndex ].pURI = strdup( pURI );
m_pMapping[ m_nextIndex ].uriLength = uriLength;
if (NULL == m_pMapping[ m_nextIndex ].pPrefix || NULL == m_pMapping[ m_nextIndex ].pURI) err = eNoMemory;
m_nextIndex++;
return err;
}
// If pURI is NULL, then the caller wants to get the size of the buffer required to hold the matching output.
//
P6COMMETHODIMPL CNamespaces::nameSpaceInScope( const P6CHAR* pPrefix, P6UINT32 prefixLength, P6CHAR *pURI, P6UINT32 *pLength )
{
P6ERR err = eOk;
if (!m_bInitialized) return eNotInitialized;
if (NULL == pPrefix || 0 == prefixLength || NULL == pLength) return eInvalidArg;
for( P6UINT32 i=0; i < m_nextIndex; i++ )
{
if (NULL != m_pMapping[i].pPrefix && NULL != m_pMapping[i].pURI)
{
if (0 == strcmp( pPrefix, m_pMapping[i].pPrefix ))
{
if (NULL != pURI)
{
if ( *pLength > m_pMapping[i].uriLength )
{
strcpy( pURI, m_pMapping[i].pURI );
*pLength = strlen( pURI );
return eOk;
}
else err = eTooSmall;
}
*pLength = m_pMapping[i].uriLength + 1;
return err;
}
}
}
return eNotFound;
}
} // namespace