|
SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00007 // Encapsulated Xerces-SAX-attributes 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 <xercesc/sax2/Attributes.hpp> 00032 #include <xercesc/sax2/DefaultHandler.hpp> 00033 #include "SUMOSAXAttributesImpl_Xerces.h" 00034 #include <utils/common/TplConvert.h> 00035 #include <utils/common/TplConvertSec.h> 00036 00037 #ifdef CHECK_MEMORY_LEAKS 00038 #include <foreign/nvwa/debug_new.h> 00039 #endif // CHECK_MEMORY_LEAKS 00040 00041 00042 // =========================================================================== 00043 // class definitions 00044 // =========================================================================== 00045 SUMOSAXAttributesImpl_Xerces::SUMOSAXAttributesImpl_Xerces(const Attributes& attrs, 00046 const std::map<int, XMLCh*> &predefinedTags, 00047 const std::map<int, std::string> &predefinedTagsMML, 00048 const std::string& objectType) throw() : 00049 SUMOSAXAttributes(objectType), 00050 myAttrs(attrs), 00051 myPredefinedTags(predefinedTags), 00052 myPredefinedTagsMML(predefinedTagsMML) { } 00053 00054 00055 00056 SUMOSAXAttributesImpl_Xerces::~SUMOSAXAttributesImpl_Xerces() throw() { 00057 } 00058 00059 00060 bool 00061 SUMOSAXAttributesImpl_Xerces::hasAttribute(int id) const throw() { 00062 AttrMap::const_iterator i=myPredefinedTags.find(id); 00063 if (i==myPredefinedTags.end()) { 00064 return false; 00065 } 00066 return myAttrs.getIndex((*i).second)>=0; 00067 } 00068 00069 00070 bool 00071 SUMOSAXAttributesImpl_Xerces::getBool(int id) const throw(EmptyData, BoolFormatException) { 00072 return TplConvert<XMLCh>::_2bool(getAttributeValueSecure(id)); 00073 } 00074 00075 00076 bool 00077 SUMOSAXAttributesImpl_Xerces::getBoolSecure(int id, bool val) const throw(EmptyData) { 00078 return TplConvertSec<XMLCh>::_2boolSec(getAttributeValueSecure(id), val); 00079 } 00080 00081 00082 int 00083 SUMOSAXAttributesImpl_Xerces::getInt(int id) const throw(EmptyData, NumberFormatException) { 00084 return TplConvert<XMLCh>::_2int(getAttributeValueSecure(id)); 00085 } 00086 00087 00088 int 00089 SUMOSAXAttributesImpl_Xerces::getIntSecure(int id, 00090 int def) const throw(EmptyData, NumberFormatException) { 00091 return TplConvertSec<XMLCh>::_2intSec(getAttributeValueSecure(id), def); 00092 } 00093 00094 00095 std::string 00096 SUMOSAXAttributesImpl_Xerces::getString(int id) const throw(EmptyData) { 00097 return TplConvert<XMLCh>::_2str(getAttributeValueSecure(id)); 00098 } 00099 00100 00101 std::string 00102 SUMOSAXAttributesImpl_Xerces::getStringSecure(int id, 00103 const std::string& str) const throw(EmptyData) { 00104 return TplConvertSec<XMLCh>::_2strSec(getAttributeValueSecure(id), str); 00105 } 00106 00107 00108 SUMOReal 00109 SUMOSAXAttributesImpl_Xerces::getFloat(int id) const throw(EmptyData, NumberFormatException) { 00110 return TplConvert<XMLCh>::_2SUMOReal(getAttributeValueSecure(id)); 00111 } 00112 00113 00114 SUMOReal 00115 SUMOSAXAttributesImpl_Xerces::getFloatSecure(int id, 00116 SUMOReal def) const throw(EmptyData, NumberFormatException) { 00117 return TplConvertSec<XMLCh>::_2SUMORealSec(getAttributeValueSecure(id), def); 00118 } 00119 00120 00121 const XMLCh* 00122 SUMOSAXAttributesImpl_Xerces::getAttributeValueSecure(int id) const throw() { 00123 AttrMap::const_iterator i=myPredefinedTags.find(id); 00124 assert(i!=myPredefinedTags.end()); 00125 return myAttrs.getValue((*i).second); 00126 } 00127 00128 00129 SUMOReal 00130 SUMOSAXAttributesImpl_Xerces::getFloat(const std::string& id) const throw(EmptyData, NumberFormatException) { 00131 XMLCh* t = XMLString::transcode(id.c_str()); 00132 SUMOReal result = TplConvert<XMLCh>::_2SUMOReal(myAttrs.getValue(t)); 00133 XMLString::release(&t); 00134 return result; 00135 } 00136 00137 00138 bool 00139 SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const throw() { 00140 XMLCh* t = XMLString::transcode(id.c_str()); 00141 bool result = myAttrs.getIndex(t)>=0; 00142 XMLString::release(&t); 00143 return result; 00144 } 00145 00146 00147 std::string 00148 SUMOSAXAttributesImpl_Xerces::getStringSecure(const std::string& id, 00149 const std::string& str) const throw() { 00150 XMLCh* t = XMLString::transcode(id.c_str()); 00151 std::string result = TplConvertSec<XMLCh>::_2strSec(myAttrs.getValue(t), str); 00152 XMLString::release(&t); 00153 return result; 00154 } 00155 00156 00157 std::string 00158 SUMOSAXAttributesImpl_Xerces::getName(int attr) const throw() { 00159 if (myPredefinedTagsMML.find(attr)==myPredefinedTagsMML.end()) { 00160 return "?"; 00161 } 00162 return myPredefinedTagsMML.find(attr)->second; 00163 } 00164 00165 00166 /****************************************************************************/ 00167