|
SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00007 // A handler which converts occuring elements and attributes into enums 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 <cassert> 00031 #include "GenericSAXHandler.h" 00032 #include <utils/common/TplConvert.h> 00033 #include <utils/common/TplConvertSec.h> 00034 #include <utils/common/FileHelpers.h> 00035 #include <utils/common/MsgHandler.h> 00036 #include <utils/common/ToString.h> 00037 #include "SUMOSAXAttributesImpl_Xerces.h" 00038 #include "XMLSubSys.h" 00039 00040 #ifdef CHECK_MEMORY_LEAKS 00041 #include <foreign/nvwa/debug_new.h> 00042 #endif // CHECK_MEMORY_LEAKS 00043 00044 00045 // =========================================================================== 00046 // class definitions 00047 // =========================================================================== 00048 GenericSAXHandler::GenericSAXHandler( 00049 StringBijection<int>::Entry* tags, int terminatorTag, 00050 StringBijection<int>::Entry* attrs, int terminatorAttr, 00051 const std::string& file) 00052 : myParentHandler(0), myParentIndicator(SUMO_TAG_NOTHING), myFileName(file) { 00053 int i = 0; 00054 while (tags[i].key != terminatorTag) { 00055 myTagMap.insert(TagMap::value_type(tags[i].str, tags[i].key)); 00056 i++; 00057 } 00058 i = 0; 00059 while (attrs[i].key != terminatorAttr) { 00060 assert(myPredefinedTags.find(attrs[i].key)==myPredefinedTags.end()); 00061 myPredefinedTags[attrs[i].key] = convert(attrs[i].str); 00062 myPredefinedTagsMML[attrs[i].key] = attrs[i].str; 00063 i++; 00064 } 00065 } 00066 00067 00068 GenericSAXHandler::~GenericSAXHandler() { 00069 for (AttrMap::iterator i1=myPredefinedTags.begin(); i1!=myPredefinedTags.end(); i1++) { 00070 delete[](*i1).second; 00071 } 00072 } 00073 00074 00075 void 00076 GenericSAXHandler::setFileName(const std::string& name) throw() { 00077 myFileName = name; 00078 } 00079 00080 00081 const std::string& 00082 GenericSAXHandler::getFileName() const throw() { 00083 return myFileName; 00084 } 00085 00086 00087 XMLCh* 00088 GenericSAXHandler::convert(const std::string& name) const throw() { 00089 size_t len = name.length(); 00090 XMLCh* ret = new XMLCh[len+1]; 00091 size_t i=0; 00092 for (; i<len; i++) { 00093 ret[i] = (XMLCh) name[i]; 00094 } 00095 ret[i] = 0; 00096 return ret; 00097 } 00098 00099 00100 void 00101 GenericSAXHandler::startElement(const XMLCh* const /*uri*/, 00102 const XMLCh* const /*localname*/, 00103 const XMLCh* const qname, 00104 const Attributes& attrs) { 00105 std::string name = TplConvert<XMLCh>::_2str(qname); 00106 int element = convertTag(name); 00107 myCharactersVector.clear(); 00108 SUMOSAXAttributesImpl_Xerces na(attrs, myPredefinedTags, myPredefinedTagsMML, name); 00109 if (element == SUMO_TAG_INCLUDE) { 00110 std::string file = na.getString(SUMO_ATTR_HREF); 00111 if (!FileHelpers::isAbsolute(file)) { 00112 file = FileHelpers::getConfigurationRelative(getFileName(), file); 00113 } 00114 XMLSubSys::runParser(*this, file); 00115 } else { 00116 myStartElement(element, na); 00117 } 00118 } 00119 00120 00121 void 00122 GenericSAXHandler::endElement(const XMLCh* const /*uri*/, 00123 const XMLCh* const /*localname*/, 00124 const XMLCh* const qname) { 00125 std::string name = TplConvert<XMLCh>::_2str(qname); 00126 int element = convertTag(name); 00127 // collect characters 00128 if (myCharactersVector.size()!=0) { 00129 size_t len = 0; 00130 unsigned i; 00131 for (i=0; i<myCharactersVector.size(); ++i) { 00132 len += myCharactersVector[i].length(); 00133 } 00134 char* buf = new char[len+1]; 00135 size_t pos = 0; 00136 for (i=0; i<myCharactersVector.size(); ++i) { 00137 memcpy((unsigned char*) buf+pos, (unsigned char*) myCharactersVector[i].c_str(), 00138 sizeof(char)*myCharactersVector[i].length()); 00139 pos += myCharactersVector[i].length(); 00140 } 00141 buf[pos] = 0; 00142 00143 // call user handler 00144 try { 00145 myCharacters(element, buf); 00146 } catch (std::runtime_error&) { 00147 delete[] buf; 00148 throw; 00149 } 00150 delete[] buf; 00151 } 00152 if (element != SUMO_TAG_INCLUDE) { 00153 myEndElement(element); 00154 if (myParentHandler && myParentIndicator == element) { 00155 XMLSubSys::setHandler(*myParentHandler); 00156 myParentIndicator = SUMO_TAG_NOTHING; 00157 myParentHandler = 0; 00158 } 00159 } 00160 } 00161 00162 00163 void 00164 GenericSAXHandler::registerParent(const int tag, GenericSAXHandler* handler) { 00165 myParentHandler = handler; 00166 myParentIndicator = tag; 00167 XMLSubSys::setHandler(*this); 00168 } 00169 00170 00171 void 00172 GenericSAXHandler::characters(const XMLCh* const chars, 00173 const XERCES3_SIZE_t length) { 00174 myCharactersVector.push_back(TplConvert<XMLCh>::_2str(chars, static_cast<unsigned int>(length))); 00175 } 00176 00177 00178 int 00179 GenericSAXHandler::convertTag(const std::string& tag) const throw() { 00180 TagMap::const_iterator i=myTagMap.find(tag); 00181 if (i==myTagMap.end()) { 00182 return SUMO_TAG_NOTHING; 00183 } 00184 return (*i).second; 00185 } 00186 00187 00188 std::string 00189 GenericSAXHandler::buildErrorMessage(const SAXParseException& exception) throw() { 00190 std::ostringstream buf; 00191 char* pMsg = XMLString::transcode(exception.getMessage()); 00192 buf << pMsg << std::endl; 00193 buf << " In file '" << getFileName() << "'" << std::endl; 00194 buf << " At line/column " << exception.getLineNumber()+1 00195 << '/' << exception.getColumnNumber() << "." << std::endl; 00196 XMLString::release(&pMsg); 00197 return buf.str(); 00198 } 00199 00200 00201 void 00202 GenericSAXHandler::warning(const SAXParseException& exception) throw() { 00203 WRITE_WARNING(buildErrorMessage(exception)); 00204 } 00205 00206 00207 void 00208 GenericSAXHandler::error(const SAXParseException& exception) throw(ProcessError) { 00209 throw ProcessError(buildErrorMessage(exception)); 00210 } 00211 00212 00213 void 00214 GenericSAXHandler::fatalError(const SAXParseException& exception) throw(ProcessError) { 00215 throw ProcessError(buildErrorMessage(exception)); 00216 } 00217 00218 00219 void 00220 GenericSAXHandler::myStartElement(int, const SUMOSAXAttributes&) {} 00221 00222 00223 void 00224 GenericSAXHandler::myCharacters(int, const std::string&) {} 00225 00226 00227 void 00228 GenericSAXHandler::myEndElement(int) {} 00229 00230 00231 /****************************************************************************/ 00232