SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00009 // Encapsulated 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 <string> 00034 #include "SUMOSAXAttributes.h" 00035 #include <utils/common/MsgHandler.h> 00036 #include <utils/common/StringTokenizer.h> 00037 #include <iostream> 00038 #include <sstream> 00039 00040 #ifdef CHECK_MEMORY_LEAKS 00041 #include <foreign/nvwa/debug_new.h> 00042 #endif // CHECK_MEMORY_LEAKS 00043 00044 00045 // =========================================================================== 00046 // static members 00047 // =========================================================================== 00048 bool SUMOSAXAttributes::myHaveInformedAboutDeprecatedDivider = false; 00049 const std::string SUMOSAXAttributes::ENCODING = " encoding=\"UTF-8\""; 00050 00051 00052 // =========================================================================== 00053 // method definitions 00054 // =========================================================================== 00055 SUMOSAXAttributes::SUMOSAXAttributes(const std::string& objectType): 00056 myObjectType(objectType) {} 00057 00058 00059 int 00060 SUMOSAXAttributes::getIntReporting(int attr, const char* objectid, 00061 bool& ok, bool report) const { 00062 if (!hasAttribute(attr)) { 00063 if (report) { 00064 emitUngivenError(getName(attr), objectid); 00065 } 00066 ok = false; 00067 return -1; 00068 } 00069 try { 00070 return getInt(attr); 00071 } catch (NumberFormatException&) { 00072 if (report) { 00073 emitFormatError(getName(attr), "an int", objectid); 00074 } 00075 } catch (EmptyData&) { 00076 if (report) { 00077 emitEmptyError(getName(attr), objectid); 00078 } 00079 } 00080 ok = false; 00081 return -1; 00082 } 00083 00084 00085 int 00086 SUMOSAXAttributes::getOptIntReporting(int attr, const char* objectid, 00087 bool& ok, int defaultValue, bool report) const { 00088 if (!hasAttribute(attr)) { 00089 return defaultValue; 00090 } 00091 try { 00092 return getInt(attr); 00093 } catch (NumberFormatException&) { 00094 if (report) { 00095 emitFormatError(getName(attr), "an int", objectid); 00096 } 00097 } catch (EmptyData&) { 00098 if (report) { 00099 emitEmptyError(getName(attr), objectid); 00100 } 00101 } 00102 ok = false; 00103 return -1; 00104 } 00105 00106 00107 long 00108 SUMOSAXAttributes::getLongReporting(int attr, const char* objectid, 00109 bool& ok, bool report) const { 00110 if (!hasAttribute(attr)) { 00111 if (report) { 00112 emitUngivenError(getName(attr), objectid); 00113 } 00114 ok = false; 00115 return -1; 00116 } 00117 try { 00118 return getLong(attr); 00119 } catch (NumberFormatException&) { 00120 if (report) { 00121 emitFormatError(getName(attr), "an int", objectid); 00122 } 00123 } catch (EmptyData&) { 00124 if (report) { 00125 emitEmptyError(getName(attr), objectid); 00126 } 00127 } 00128 ok = false; 00129 return -1; 00130 } 00131 00132 00133 SUMOReal 00134 SUMOSAXAttributes::getSUMORealReporting(int attr, const char* objectid, 00135 bool& ok, bool report) const { 00136 if (!hasAttribute(attr)) { 00137 if (report) { 00138 emitUngivenError(getName(attr), objectid); 00139 } 00140 ok = false; 00141 return -1; 00142 } 00143 try { 00144 return getFloat(attr); 00145 } catch (NumberFormatException&) { 00146 if (report) { 00147 emitFormatError(getName(attr), "a real number", objectid); 00148 } 00149 } catch (EmptyData&) { 00150 if (report) { 00151 emitEmptyError(getName(attr), objectid); 00152 } 00153 } 00154 ok = false; 00155 return (SUMOReal) - 1; 00156 } 00157 00158 00159 SUMOReal 00160 SUMOSAXAttributes::getOptSUMORealReporting(int attr, const char* objectid, 00161 bool& ok, SUMOReal defaultValue, bool report) const { 00162 if (!hasAttribute(attr)) { 00163 return defaultValue; 00164 } 00165 try { 00166 return getFloat(attr); 00167 } catch (NumberFormatException&) { 00168 if (report) { 00169 emitFormatError(getName(attr), "a real number", objectid); 00170 } 00171 } catch (EmptyData&) { 00172 if (report) { 00173 emitEmptyError(getName(attr), objectid); 00174 } 00175 } 00176 ok = false; 00177 return (SUMOReal) - 1; 00178 } 00179 00180 00181 bool 00182 SUMOSAXAttributes::getBoolReporting(int attr, const char* objectid, 00183 bool& ok, bool report) const { 00184 if (!hasAttribute(attr)) { 00185 if (report) { 00186 emitUngivenError(getName(attr), objectid); 00187 } 00188 ok = false; 00189 return false; 00190 } 00191 try { 00192 return getBool(attr); 00193 } catch (BoolFormatException&) { 00194 if (report) { 00195 emitFormatError(getName(attr), "a boolean", objectid); 00196 } 00197 } catch (EmptyData&) { 00198 if (report) { 00199 emitEmptyError(getName(attr), objectid); 00200 } 00201 } 00202 ok = false; 00203 return false; 00204 } 00205 00206 00207 bool 00208 SUMOSAXAttributes::getOptBoolReporting(int attr, const char* objectid, 00209 bool& ok, bool defaultValue, bool report) const { 00210 if (!hasAttribute(attr)) { 00211 return defaultValue; 00212 } 00213 try { 00214 return getBool(attr); 00215 } catch (BoolFormatException&) { 00216 if (report) { 00217 emitFormatError(getName(attr), "a boolean", objectid); 00218 } 00219 } catch (EmptyData&) { 00220 if (report) { 00221 emitEmptyError(getName(attr), objectid); 00222 } 00223 } 00224 ok = false; 00225 return false; 00226 } 00227 00228 00229 std::string 00230 SUMOSAXAttributes::getStringReporting(int attr, const char* objectid, 00231 bool& ok, bool report) const { 00232 if (!hasAttribute(attr)) { 00233 if (report) { 00234 emitUngivenError(getName(attr), objectid); 00235 } 00236 ok = false; 00237 return ""; 00238 } 00239 try { 00240 std::string ret = getString(attr); 00241 if (ret == "") { 00242 throw EmptyData(); 00243 } 00244 return ret; 00245 } catch (EmptyData&) { 00246 if (report) { 00247 emitEmptyError(getName(attr), objectid); 00248 } 00249 } 00250 ok = false; 00251 return ""; 00252 } 00253 00254 00255 std::string 00256 SUMOSAXAttributes::getOptStringReporting(int attr, const char* objectid, 00257 bool& ok, const std::string& defaultValue, bool report) const { 00258 if (!hasAttribute(attr)) { 00259 return defaultValue; 00260 } 00261 try { 00262 return getString(attr); 00263 } catch (EmptyData&) { 00264 if (report) { 00265 emitEmptyError(getName(attr), objectid); 00266 } 00267 } 00268 ok = false; 00269 return ""; 00270 } 00271 00272 00273 SUMOTime 00274 SUMOSAXAttributes::getSUMOTimeReporting(int attr, const char* objectid, 00275 bool& ok, bool report) const { 00276 #ifdef HAVE_SUBSECOND_TIMESTEPS 00277 if (!hasAttribute(attr)) { 00278 if (report) { 00279 emitUngivenError(getName(attr), objectid); 00280 } 00281 ok = false; 00282 return -1; 00283 } 00284 try { 00285 return (SUMOTime)(getFloat(attr) * 1000.); 00286 } catch (NumberFormatException&) { 00287 if (report) { 00288 emitFormatError(getName(attr), "a time value", objectid); 00289 } 00290 } catch (EmptyData&) { 00291 if (report) { 00292 emitEmptyError(getName(attr), objectid); 00293 } 00294 } 00295 ok = false; 00296 return (SUMOTime) - 1; 00297 #else 00298 return getIntReporting(attr, objectid, ok, report); 00299 #endif 00300 } 00301 00302 00303 SUMOTime 00304 SUMOSAXAttributes::getOptSUMOTimeReporting(int attr, const char* objectid, 00305 bool& ok, SUMOTime defaultValue, bool report) const { 00306 #ifdef HAVE_SUBSECOND_TIMESTEPS 00307 if (!hasAttribute(attr)) { 00308 return defaultValue; 00309 } 00310 try { 00311 return (SUMOTime)(getFloat(attr) * 1000.); 00312 } catch (NumberFormatException&) { 00313 if (report) { 00314 emitFormatError(getName(attr), "a real number", objectid); 00315 } 00316 } catch (EmptyData&) { 00317 if (report) { 00318 emitEmptyError(getName(attr), objectid); 00319 } 00320 } 00321 ok = false; 00322 return (SUMOTime) - 1; 00323 #else 00324 return getOptIntReporting(attr, objectid, ok, defaultValue, report); 00325 #endif 00326 } 00327 00328 00329 00330 00331 00332 void 00333 SUMOSAXAttributes::emitUngivenError(const std::string& attrname, const char* objectid) const { 00334 std::ostringstream oss; 00335 oss << "Attribute '" << attrname << "' is missing in definition of "; 00336 if (objectid == 0) { 00337 oss << "a "; 00338 } 00339 oss << myObjectType; 00340 if (objectid != 0) { 00341 oss << " '" << objectid << "'"; 00342 } 00343 oss << "."; 00344 WRITE_ERROR(oss.str()); 00345 } 00346 00347 00348 void 00349 SUMOSAXAttributes::emitEmptyError(const std::string& attrname, const char* objectid) const { 00350 std::ostringstream oss; 00351 oss << "Attribute '" << attrname << "' in definition of "; 00352 if (objectid == 0) { 00353 oss << "a "; 00354 } 00355 oss << myObjectType; 00356 if (objectid != 0) { 00357 oss << " '" << objectid << "'"; 00358 } 00359 oss << " is empty."; 00360 WRITE_ERROR(oss.str()); 00361 } 00362 00363 00364 void 00365 SUMOSAXAttributes::emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const { 00366 std::ostringstream oss; 00367 oss << "Attribute '" << attrname << "' in definition of "; 00368 if (objectid == 0) { 00369 oss << "a "; 00370 } 00371 oss << myObjectType; 00372 if (objectid != 0) { 00373 oss << " '" << objectid << "'"; 00374 } 00375 oss << " is not " << type << "."; 00376 WRITE_ERROR(oss.str()); 00377 } 00378 00379 00380 void 00381 SUMOSAXAttributes::parseStringVector(const std::string& def, std::vector<std::string> &into) { 00382 if (def.find(';') != std::string::npos || def.find(',') != std::string::npos) { 00383 if (!myHaveInformedAboutDeprecatedDivider) { 00384 WRITE_WARNING("Please note that using ';' and ',' as XML list separators is deprecated.\n From 1.0 onwards, only ' ' will be accepted."); 00385 myHaveInformedAboutDeprecatedDivider = true; 00386 } 00387 } 00388 StringTokenizer st(def, ";, ", true); 00389 while (st.hasNext()) { 00390 into.push_back(st.next()); 00391 } 00392 } 00393 00394 00395 /****************************************************************************/ 00396