|
SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00007 // Utility methods for initialising, closing and using the XML-subsystem 00008 /****************************************************************************/ 00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors 00011 /****************************************************************************/ 00012 // 00013 // This program is free software; you can redistribute it and/or modify 00014 // it under the terms of the GNU General Public License as published by 00015 // the Free Software Foundation; either version 2 of the License, or 00016 // (at your option) any later version. 00017 // 00018 /****************************************************************************/ 00019 00020 00021 // =========================================================================== 00022 // included modules 00023 // =========================================================================== 00024 #ifdef _MSC_VER 00025 #include <windows_config.h> 00026 #else 00027 #include <config.h> 00028 #endif 00029 00030 #include <iostream> 00031 #include <xercesc/sax2/XMLReaderFactory.hpp> 00032 #include <xercesc/util/PlatformUtils.hpp> 00033 #include <utils/common/TplConvert.h> 00034 #include <utils/common/MsgHandler.h> 00035 #include "SUMOSAXHandler.h" 00036 #include "XMLSubSys.h" 00037 00038 #ifdef CHECK_MEMORY_LEAKS 00039 #include <foreign/nvwa/debug_new.h> 00040 #endif // CHECK_MEMORY_LEAKS 00041 00042 00043 // =========================================================================== 00044 // static member variables 00045 // =========================================================================== 00046 std::vector<SAX2XMLReader*> XMLSubSys::myReaders; 00047 unsigned int XMLSubSys::myNextFreeReader; 00048 bool XMLSubSys::myEnableValidation; 00049 00050 00051 // =========================================================================== 00052 // method definitions 00053 // =========================================================================== 00054 void 00055 XMLSubSys::init(bool enableValidation) throw(ProcessError) { 00056 myEnableValidation = enableValidation; 00057 try { 00058 XMLPlatformUtils::Initialize(); 00059 myReaders.push_back(getSAXReader()); 00060 myNextFreeReader = 0; 00061 } catch (const XMLException& e) { 00062 throw ProcessError("Error during XML-initialization:\n " + TplConvert<XMLCh>::_2str(e.getMessage())); 00063 } 00064 } 00065 00066 00067 void 00068 XMLSubSys::close() throw() { 00069 for (std::vector<SAX2XMLReader*>::iterator i=myReaders.begin(); i!=myReaders.end(); ++i) { 00070 delete *i; 00071 } 00072 myReaders.clear(); 00073 XMLPlatformUtils::Terminate(); 00074 } 00075 00076 00077 SAX2XMLReader* 00078 XMLSubSys::getSAXReader(SUMOSAXHandler& handler) throw() { 00079 SAX2XMLReader* reader = getSAXReader(); 00080 if (reader==0) { 00081 return 0; 00082 } 00083 reader->setContentHandler(&handler); 00084 reader->setErrorHandler(&handler); 00085 return reader; 00086 } 00087 00088 00089 void 00090 XMLSubSys::setHandler(GenericSAXHandler& handler) { 00091 myReaders[myNextFreeReader-1]->setContentHandler(&handler); 00092 myReaders[myNextFreeReader-1]->setErrorHandler(&handler); 00093 } 00094 00095 00096 bool 00097 XMLSubSys::runParser(GenericSAXHandler& handler, 00098 const std::string& file) throw() { 00099 try { 00100 if (myNextFreeReader == myReaders.size()) { 00101 myReaders.push_back(getSAXReader()); 00102 } 00103 myNextFreeReader++; 00104 setHandler(handler); 00105 std::string prevFile = handler.getFileName(); 00106 handler.setFileName(file); 00107 myReaders[myNextFreeReader-1]->parse(file.c_str()); 00108 handler.setFileName(prevFile); 00109 myNextFreeReader--; 00110 } catch (ProcessError& e) { 00111 if (std::string(e.what())!=std::string("Process Error") && std::string(e.what())!=std::string("")) { 00112 WRITE_ERROR(e.what()); 00113 } 00114 return false; 00115 } catch (...) { 00116 WRITE_ERROR("An error occured."); 00117 return false; 00118 } 00119 return !MsgHandler::getErrorInstance()->wasInformed(); 00120 } 00121 00122 00123 SAX2XMLReader* 00124 XMLSubSys::getSAXReader() throw() { 00125 SAX2XMLReader* reader = XMLReaderFactory::createXMLReader(); 00126 if (reader==0) { 00127 WRITE_ERROR("The XML-parser could not be build"); 00128 return 0; 00129 } 00130 if (!myEnableValidation) { 00131 reader->setProperty(XMLUni::fgXercesScannerName, (void*)XMLUni::fgWFXMLScanner); 00132 } 00133 setFeature(*reader, "http://xml.org/sax/features/namespaces", false); 00134 setFeature(*reader, "http://apache.org/xml/features/validation/schema", myEnableValidation); 00135 setFeature(*reader, "http://apache.org/xml/features/validation/schema-full-checking", myEnableValidation); 00136 setFeature(*reader, "http://xml.org/sax/features/validation", myEnableValidation); 00137 setFeature(*reader, "http://apache.org/xml/features/validation/dynamic", myEnableValidation); 00138 return reader; 00139 } 00140 00141 00142 void 00143 XMLSubSys::setFeature(XERCES_CPP_NAMESPACE_QUALIFIER SAX2XMLReader& reader, 00144 const std::string& feature, bool value) throw() { 00145 XMLCh* xmlFeature = XMLString::transcode(feature.c_str()); 00146 reader.setFeature(xmlFeature, value); 00147 XMLString::release(&xmlFeature); 00148 } 00149 00150 00151 /****************************************************************************/ 00152