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 long
00100 SUMOSAXAttributesImpl_Xerces::getLong(int id) const throw(EmptyData, NumberFormatException) {
00101     return TplConvert<XMLCh>::_2long(getAttributeValueSecure(id));
00102 }
00103 
00104 
00105 std::string
00106 SUMOSAXAttributesImpl_Xerces::getString(int id) const throw(EmptyData) {
00107     const XMLCh* utf16 = getAttributeValueSecure(id);
00108 #if _XERCES_VERSION < 30000
00109     return TplConvert<XMLCh>::_2str(utf16);
00110 #else
00111     if (XMLString::stringLen(utf16) == 0) {
00112         // TranscodeToStr and debug_new interact badly in this case;
00113         return "";
00114     } else {
00115         TranscodeToStr utf8(utf16, "UTF-8");
00116         return TplConvert<XMLByte>::_2str(utf8.str(), (unsigned)utf8.length());
00117     }
00118 #endif
00119 }
00120 
00121 
00122 std::string
00123 SUMOSAXAttributesImpl_Xerces::getStringSecure(int id,
00124         const std::string& str) const throw(EmptyData) {
00125     const XMLCh* utf16 = getAttributeValueSecure(id);
00126 #if _XERCES_VERSION < 30000
00127     return TplConvertSec<XMLCh>::_2strSec(utf16, str);
00128 #else
00129     if (XMLString::stringLen(utf16) == 0) {
00130         // TranscodeToStr and debug_new interact badly in this case;
00131         return "";
00132     } else {
00133         TranscodeToStr utf8(utf16, "UTF-8");
00134         return TplConvertSec<XMLByte>::_2strSec(utf8.str(), (int)utf8.length(), str);
00135     }
00136 #endif
00137 }
00138 
00139 
00140 SUMOReal
00141 SUMOSAXAttributesImpl_Xerces::getFloat(int id) const throw(EmptyData, NumberFormatException) {
00142     return TplConvert<XMLCh>::_2SUMOReal(getAttributeValueSecure(id));
00143 }
00144 
00145 
00146 SUMOReal
00147 SUMOSAXAttributesImpl_Xerces::getFloatSecure(int id,
00148         SUMOReal def) const throw(EmptyData, NumberFormatException) {
00149     return TplConvertSec<XMLCh>::_2SUMORealSec(getAttributeValueSecure(id), def);
00150 }
00151 
00152 
00153 const XMLCh*
00154 SUMOSAXAttributesImpl_Xerces::getAttributeValueSecure(int id) const {
00155     AttrMap::const_iterator i = myPredefinedTags.find(id);
00156     assert(i != myPredefinedTags.end());
00157     return myAttrs.getValue((*i).second);
00158 }
00159 
00160 
00161 SUMOReal
00162 SUMOSAXAttributesImpl_Xerces::getFloat(const std::string& id) const throw(EmptyData, NumberFormatException) {
00163     XMLCh* t = XMLString::transcode(id.c_str());
00164     SUMOReal result = TplConvert<XMLCh>::_2SUMOReal(myAttrs.getValue(t));
00165     XMLString::release(&t);
00166     return result;
00167 }
00168 
00169 
00170 bool
00171 SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const {
00172     XMLCh* t = XMLString::transcode(id.c_str());
00173     bool result = myAttrs.getIndex(t) >= 0;
00174     XMLString::release(&t);
00175     return result;
00176 }
00177 
00178 
00179 std::string
00180 SUMOSAXAttributesImpl_Xerces::getStringSecure(const std::string& id,
00181         const std::string& str) const {
00182     XMLCh* t = XMLString::transcode(id.c_str());
00183     std::string result = TplConvertSec<XMLCh>::_2strSec(myAttrs.getValue(t), str);
00184     XMLString::release(&t);
00185     return result;
00186 }
00187 
00188 
00189 std::string
00190 SUMOSAXAttributesImpl_Xerces::getName(int attr) const {
00191     if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
00192         return "?";
00193     }
00194     return myPredefinedTagsMML.find(attr)->second;
00195 }
00196 
00197 
00198 /****************************************************************************/
00199 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines