- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Utils/Io/Xml/XercesSetup.cpp

    00001
    00002 //
    00003 // Copyright (c) 2005           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00010
    00011 #include "XercesSetup.h"
    00012 #include "ScopedTranscoder.h"
    00013 #include <Mers/Utils/Exceptions/RuntimeException.h>
    00014 #include <xercesc/util/PlatformUtils.hpp>
    00015 #include <xercesc/util/OutOfMemoryException.hpp>
    00016 #include <cassert>
    00017
    00019
    00020 using namespace Mers::Utils::Io::Xml;
    00021 using Mers::Utils::Exceptions::RuntimeException;
    00022 using XERCES_CPP_NAMESPACE::XMLPlatformUtils;
    00023 using XERCES_CPP_NAMESPACE::XMLException;
    00024 using XERCES_CPP_NAMESPACE::XMLUni;
    00025 using XERCES_CPP_NAMESPACE::MemoryManager;
    00026 using XERCES_CPP_NAMESPACE::OutOfMemoryException;
    00027 using log4cplus::Logger;
    00028
    00030
    00031 XercesSetup * XercesSetup::s_unique(NULL);
    00032
    00033 Logger XercesSetup::s_logger
    00034 ( Logger::getInstance( "Mers.Utils.Io.Xml.XercesSetup" ) );
    00035
    00037
    00038 namespace {
    00039
    00043 class MersMemoryManager : public MemoryManager
    00044 {
    00045 public:
    00046         MersMemoryManager()
    00047         {
    00048         }
    00049
    00050         virtual ~MersMemoryManager()
    00051         {
    00052         }
    00053
    00054         virtual void * allocate(size_t size)
    00055         {
    00056                 void* memptr;
    00057                 try {
    00058                         memptr = malloc( size );
    00059                 } catch(...) {
    00060                         throw OutOfMemoryException();
    00061                 }
    00062
    00063                 if (memptr != NULL) {
    00064                         return memptr;
    00065                 }
    00066
    00067                 throw OutOfMemoryException();
    00068         }
    00069
    00070         virtual void deallocate( void * p )
    00071         {
    00072                 free( p );
    00073         }
    00074 };
    00075
    00076 MersMemoryManager * s_memoryManager;
    00077 }
    00078
    00080
    00081 XercesSetup::XercesSetup()
    00082 {
    00083         // Make sure we don't already exist.
    00084         assert( s_unique == NULL );
    00085
    00086         // Initialize Xerces.
    00087         try {
    00088                 s_memoryManager = new MersMemoryManager();
    00089                 XMLPlatformUtils::Initialize( XMLUni::fgXercescDefaultLocale, NULL, NULL,
    00090                         s_memoryManager );
    00091         } catch( const XMLException & toCatch ) {
    00092                 CScopedTranscoder message( toCatch.getMessage() );
    00093                 LOG4CPLUS_ERROR( s_logger, "Error initializing Xerces: " << message );
    00094
    00095                 throw RuntimeException( message, __FILE__, __LINE__ );
    00096         }
    00097         s_unique = this;
    00098 }
    00099
    00101
    00102 XercesSetup::~XercesSetup()
    00103 {
    00104         s_unique = NULL;
    00105         XMLPlatformUtils::Terminate();
    00106         delete s_memoryManager;
    00107         s_memoryManager = NULL;
    00108 }
    00109
    00111