SUMO - Simulation of Urban MObility

SUMOSAXAttributesImpl_Xerces.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00009 // Encapsulated Xerces-SAX-attributes
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ===========================================================================
00025 // included modules
00026 // ===========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif
00032 
00033 #include <cassert>
00034 #include <xercesc/sax2/Attributes.hpp>
00035 #include <xercesc/sax2/DefaultHandler.hpp>
00036 #include <xercesc/util/XercesVersion.hpp>
00037 #include <xercesc/util/TransService.hpp>
00038 #include "SUMOSAXAttributesImpl_Xerces.h"
00039 #include <utils/common/TplConvert.h>
00040 #include <utils/common/TplConvertSec.h>
00041 
00042 #ifdef CHECK_MEMORY_LEAKS
00043 #include <foreign/nvwa/debug_new.h>
00044 #endif // CHECK_MEMORY_LEAKS
00045 
00046 
00047 // ===========================================================================
00048 // class definitions
00049 // ===========================================================================
00050 SUMOSAXAttributesImpl_Xerces::SUMOSAXAttributesImpl_Xerces(const Attributes& attrs,
00051         const std::map<int, XMLCh*> &predefinedTags,
00052         const std::map<int, std::string> &predefinedTagsMML,
00053         const std::string& objectType) :
00054     SUMOSAXAttributes(objectType),
00055     myAttrs(attrs),
00056     myPredefinedTags(predefinedTags),
00057     myPredefinedTagsMML(predefinedTagsMML) { }
00058 
00059 
00060 SUMOSAXAttributesImpl_Xerces::~SUMOSAXAttributesImpl_Xerces() {
00061 }
00062 
00063 
00064 bool
00065 SUMOSAXAttributesImpl_Xerces::hasAttribute(int id) const {
00066     AttrMap::const_iterator i = myPredefinedTags.find(id);
00067     if (i == myPredefinedTags.end()) {
00068         return false;
00069     }
00070     return myAttrs.getIndex((*i).second) >= 0;
00071 }
00072 
00073 
00074 bool
00075 SUMOSAXAttributesImpl_Xerces::getBool(int id) const throw(EmptyData, BoolFormatException) {
00076     return TplConvert<XMLCh>::_2bool(getAttributeValueSecure(id));
00077 }
00078 
00079 
00080 bool
00081 SUMOSAXAttributesImpl_Xerces::getBoolSecure(int id, bool val) const throw(EmptyData) {
00082     return TplConvertSec<XMLCh>::_2boolSec(getAttributeValueSecure(id), val);
00083 }
00084 
00085 
00086 int
00087 SUMOSAXAttributesImpl_Xerces::getInt(int id) const throw(EmptyData, NumberFormatException) {
00088     return TplConvert<XMLCh>::_2int(getAttributeValueSecure(id));
00089 }
00090 
00091 
00092 int
00093 SUMOSAXAttributesImpl_Xerces::getIntSecure(int id,
00094         int def) const throw(EmptyData, NumberFormatException) {
00095     return TplConvertSec<XMLCh>::_2intSec(getAttributeValueSecure(id), def);
00096 }
00097 
00098 
00099 std::string
00100 SUMOSAXAttributesImpl_Xerces::getString(int id) const throw(EmptyData) {
00101     const XMLCh* utf16 = getAttributeValueSecure(id);
00102 #if _XERCES_VERSION < 30000
00103     return TplConvert<XMLCh>::_2str(utf16);
00104 #else
00105     if (XMLString::stringLen(utf16) == 0) {
00106         // TranscodeToStr and debug_new interact badly in this case;
00107         return "";
00108     } else {
00109         TranscodeToStr utf8(utf16, "UTF-8");
00110         return TplConvert<XMLByte>::_2str(utf8.str(), (unsigned)utf8.length());
00111     }
00112 #endif
00113 }
00114 
00115 
00116 std::string
00117 SUMOSAXAttributesImpl_Xerces::getStringSecure(int id,
00118         const std::string& str) const throw(EmptyData) {
00119     const XMLCh* utf16 = getAttributeValueSecure(id);
00120 #if _XERCES_VERSION < 30000
00121     return TplConvertSec<XMLCh>::_2strSec(utf16, str);
00122 #else
00123     if (XMLString::stringLen(utf16) == 0) {
00124         // TranscodeToStr and debug_new interact badly in this case;
00125         return "";
00126     } else {
00127         TranscodeToStr utf8(utf16, "UTF-8");
00128         return TplConvertSec<XMLByte>::_2strSec(utf8.str(), (int)utf8.length(), str);
00129     }
00130 #endif
00131 }
00132 
00133 
00134 SUMOReal
00135 SUMOSAXAttributesImpl_Xerces::getFloat(int id) const throw(EmptyData, NumberFormatException) {
00136     return TplConvert<XMLCh>::_2SUMOReal(getAttributeValueSecure(id));
00137 }
00138 
00139 
00140 SUMOReal
00141 SUMOSAXAttributesImpl_Xerces::getFloatSecure(int id,
00142         SUMOReal def) const throw(EmptyData, NumberFormatException) {
00143     return TplConvertSec<XMLCh>::_2SUMORealSec(getAttributeValueSecure(id), def);
00144 }
00145 
00146 
00147 const XMLCh*
00148 SUMOSAXAttributesImpl_Xerces::getAttributeValueSecure(int id) const {
00149     AttrMap::const_iterator i = myPredefinedTags.find(id);
00150     assert(i != myPredefinedTags.end());
00151     return myAttrs.getValue((*i).second);
00152 }
00153 
00154 
00155 SUMOReal
00156 SUMOSAXAttributesImpl_Xerces::getFloat(const std::string& id) const throw(EmptyData, NumberFormatException) {
00157     XMLCh* t = XMLString::transcode(id.c_str());
00158     SUMOReal result = TplConvert<XMLCh>::_2SUMOReal(myAttrs.getValue(t));
00159     XMLString::release(&t);
00160     return result;
00161 }
00162 
00163 
00164 bool
00165 SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const {
00166     XMLCh* t = XMLString::transcode(id.c_str());
00167     bool result = myAttrs.getIndex(t) >= 0;
00168     XMLString::release(&t);
00169     return result;
00170 }
00171 
00172 
00173 std::string
00174 SUMOSAXAttributesImpl_Xerces::getStringSecure(const std::string& id,
00175         const std::string& str) const {
00176     XMLCh* t = XMLString::transcode(id.c_str());
00177     std::string result = TplConvertSec<XMLCh>::_2strSec(myAttrs.getValue(t), str);
00178     XMLString::release(&t);
00179     return result;
00180 }
00181 
00182 
00183 std::string
00184 SUMOSAXAttributesImpl_Xerces::getName(int attr) const {
00185     if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
00186         return "?";
00187     }
00188     return myPredefinedTagsMML.find(attr)->second;
00189 }
00190 
00191 
00192 /****************************************************************************/
00193 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines