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