core/load_xml.cpp

00001 /*  gld_load.cpp
00002  * 
00003  * Authors:
00004  *  Matthew Hauer <matthew.hauer@pnl.gov>, 6 Nov 07 -
00005  *
00006  * Versions:
00007  *  1.0 - MH - initial version
00008  *
00009  *  @file load_xml.cpp
00010  *  @addtogroup load XML file loader
00011  *  @ingroup core
00012  *
00013  */
00014 
00015 #include "load_xml.h"
00016 
00017 /* included in here to avoid the dubious joys of C/C++ mixes -mh */
00018 #include <xercesc/util/PlatformUtils.hpp>
00019 #include <xercesc/sax2/SAX2XMLReader.hpp>
00020 #include <xercesc/sax2/XMLReaderFactory.hpp>
00021 #include <xercesc/sax2/DefaultHandler.hpp>
00022 #include <xercesc/util/XMLString.hpp>
00023 
00024 #include "load_xml_handle.h"
00025 
00026 #ifdef __cplusplus
00027 extern "C" {
00028     int loadall_xml(char *file);
00029 }
00030 #else
00031 //int loadall_xml(char *file);
00032 #endif
00033 
00034 // Other include files, declarations, and non-Xerces-C++ initializations.
00035 XERCES_CPP_NAMESPACE_USE 
00036   
00037 /*int main(int argc, char** argv){
00038     char* xmlFile = "well-formatted-new.xml";
00039     //char *xmlFile = "GridLABD_Multi_Example.xml";
00040     return loadall_xml(xmlFile);
00041 }*/
00042 
00043 int loadall_xml(char *filename){
00044     if(filename == NULL)
00045         return 0;
00046     try{
00047         XMLPlatformUtils::Initialize();
00048     } catch(const XMLException& /*toCatch*/){
00049         output_error("Load_XML: Xerces Initialization failed.");
00050         output_debug(" * something really spectacularly nasty happened inside Xerces and outside our control.");
00051         return 0;
00052     }
00053 
00054     SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
00055     //parser->setFeature(XMLUni::fgSAX2CoreValidation, true);   
00056     //parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);   // optional
00057 
00058     gld_loadHndl *defaultHandler = new gld_loadHndl();
00059     parser->setContentHandler(defaultHandler);
00060     parser->setErrorHandler(defaultHandler);
00061     
00062     try {
00063         parser->parse(filename);
00064     } catch (const XMLException& toCatch){
00065         char* message = XMLString::transcode(toCatch.getMessage());
00066         output_error("Load_XML: XMLException from Xerces: %s", message);
00067         XMLString::release(&message);
00068         return 0;
00069     } catch (const SAXParseException& toCatch){
00070         char* message = XMLString::transcode(toCatch.getMessage());
00071         output_error("Load_XML: SAXParseException from Xerces: %s", message);
00072         XMLString::release(&message);
00073         return 0;
00074     } catch (...) {
00075         output_error("Load_XML: unexpected exception from Xerces.");
00076         return 0;
00077     }
00078     if(!defaultHandler->did_load()){
00079         output_error("Load_XML: loading failed.");
00080         return 0;
00081     }
00082     delete parser;
00083     delete defaultHandler;
00084     return 1;
00085 }
00086 
00087 /* EOF */

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy