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

# /Users/fpy/titan-1-5-MBARI/Mers/Utils/Io/Xml/Sax/XMLLoader.h

    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 #ifndef MERS_UTILS_IO_XML_SAX_XMLLOADER_HEADER_FILE
    00012 #define MERS_UTILS_IO_XML_SAX_XMLLOADER_HEADER_FILE
    00013
    00014 #include "CallbackStore.h"
    00015 #include "ContentLauncher.h"
    00016 #include <Mers/Utils/Lang/String.h>
    00017 #include <Mers/Utils/Logging/logger.h>
    00018 #include <xercesc/sax2/SAX2XMLReader.hpp>
    00019 #include <xercesc/sax2/XMLReaderFactory.hpp>
    00020
    00021 namespace Mers {
    00022         namespace Utils {
    00023                 namespace Io {
    00024                         namespace Xml {
    00025                                 namespace Sax {
    00026
    00027                                         class ContentHandler;
    00028
    00032 class XMLLoader
    00033 {
    00034 public:
    00040         static void initialize();
    00041
    00059         static int loadContent( XERCES_CPP_NAMESPACE::SAX2XMLReader * reader,
    00060                 const Mers::Utils::Lang::String &fileName, ContentHandler * cHandler );
    00061
    00077         template< typename ObjectType, typename ObjectDataType,
    00078                 class ObjectContentHandler >
    00079         static int load( const Mers::Utils::Lang::String &fileName,
    00080                 ObjectType &out_obj, ObjectDataType data ) {
    00081
    00082                 // These typedefs are her to make the rest of the function more readable.
    00083                 typedef CallbackStore< ObjectType > ObjectCallbackStore;
    00084                 typedef ContentCallback< ObjectType > ObjectContentCallback;
    00085                 typedef ContentLauncher< ObjectContentHandler, ObjectContentCallback,
    00086                         ObjectDataType > ObjectContentLauncher;
    00087
    00088                 // Try to create a reader.
    00089                 XERCES_CPP_NAMESPACE::SAX2XMLReader * reader = NULL;
    00090                 try {
    00091                         // Get a reader
    00092                         reader = XERCES_CPP_NAMESPACE::XMLReaderFactory::createXMLReader();
    00093                 } catch( const XERCES_CPP_NAMESPACE::XMLException &exc ) {
    00094                         // There was an exception creating the reader.
    00095                         char * msg =
    00096                                 XERCES_CPP_NAMESPACE::XMLString::transcode( exc.getMessage() );
    00097                         LOG4CPLUS_ERROR( m_Logger, "XML Exception: " << msg );
    00098                         XERCES_CPP_NAMESPACE::XMLString::release(&msg);
    00099
    00100                         return 1;
    00101                 }
    00102
    00103                 // We store the result here.
    00104                 ObjectCallbackStore objectStore;
    00105
    00106                 // We load the file with a launcher.
    00107                 ObjectContentLauncher * launcher = new
    00108                         ObjectContentLauncher( reader, data, &objectStore );
    00109
    00110                 // Load our content.
    00111                 int ret = loadContent( reader, fileName, launcher );
    00112
    00113                 // Get our data.
    00114                 out_obj = objectStore.get();
    00115
    00116                 return ret;
    00117         }
    00118
    00119 private:
    00120         // This logger is used to log events while loading XML content.
    00121         static log4cplus::Logger m_Logger;
    00122 };
    00123
    00124
    00125                                 }
    00126                         }
    00127                 }
    00128         }
    00129 }
    00130
    00131 #endif