SUMO - Simulation of Urban MObility

SUMOSAXAttributes.cpp

Go to the documentation of this file.
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 SUMOReal
00108 SUMOSAXAttributes::getSUMORealReporting(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 getFloat(attr);
00119     } catch (NumberFormatException&) {
00120         if (report) {
00121             emitFormatError(getName(attr), "a real number", objectid);
00122         }
00123     } catch (EmptyData&) {
00124         if (report) {
00125             emitEmptyError(getName(attr), objectid);
00126         }
00127     }
00128     ok = false;
00129     return (SUMOReal) - 1;
00130 }
00131 
00132 
00133 SUMOReal
00134 SUMOSAXAttributes::getOptSUMORealReporting(int attr, const char* objectid,
00135         bool& ok, SUMOReal defaultValue, bool report) const {
00136     if (!hasAttribute(attr)) {
00137         return defaultValue;
00138     }
00139     try {
00140         return getFloat(attr);
00141     } catch (NumberFormatException&) {
00142         if (report) {
00143             emitFormatError(getName(attr), "a real number", objectid);
00144         }
00145     } catch (EmptyData&) {
00146         if (report) {
00147             emitEmptyError(getName(attr), objectid);
00148         }
00149     }
00150     ok = false;
00151     return (SUMOReal) - 1;
00152 }
00153 
00154 
00155 bool
00156 SUMOSAXAttributes::getBoolReporting(int attr, const char* objectid,
00157                                     bool& ok, bool report) const {
00158     if (!hasAttribute(attr)) {
00159         if (report) {
00160             emitUngivenError(getName(attr), objectid);
00161         }
00162         ok = false;
00163         return false;
00164     }
00165     try {
00166         return getBool(attr);
00167     } catch (BoolFormatException&) {
00168         if (report) {
00169             emitFormatError(getName(attr), "a boolean", objectid);
00170         }
00171     } catch (EmptyData&) {
00172         if (report) {
00173             emitEmptyError(getName(attr), objectid);
00174         }
00175     }
00176     ok = false;
00177     return false;
00178 }
00179 
00180 
00181 bool
00182 SUMOSAXAttributes::getOptBoolReporting(int attr, const char* objectid,
00183                                        bool& ok, bool defaultValue, bool report) const {
00184     if (!hasAttribute(attr)) {
00185         return defaultValue;
00186     }
00187     try {
00188         return getBool(attr);
00189     } catch (BoolFormatException&) {
00190         if (report) {
00191             emitFormatError(getName(attr), "a boolean", objectid);
00192         }
00193     } catch (EmptyData&) {
00194         if (report) {
00195             emitEmptyError(getName(attr), objectid);
00196         }
00197     }
00198     ok = false;
00199     return false;
00200 }
00201 
00202 
00203 std::string
00204 SUMOSAXAttributes::getStringReporting(int attr, const char* objectid,
00205                                       bool& ok, bool report) const {
00206     if (!hasAttribute(attr)) {
00207         if (report) {
00208             emitUngivenError(getName(attr), objectid);
00209         }
00210         ok = false;
00211         return "";
00212     }
00213     try {
00214         std::string ret = getString(attr);
00215         if (ret == "") {
00216             throw EmptyData();
00217         }
00218         return ret;
00219     } catch (EmptyData&) {
00220         if (report) {
00221             emitEmptyError(getName(attr), objectid);
00222         }
00223     }
00224     ok = false;
00225     return "";
00226 }
00227 
00228 
00229 std::string
00230 SUMOSAXAttributes::getOptStringReporting(int attr, const char* objectid,
00231         bool& ok, const std::string& defaultValue, bool report) const {
00232     if (!hasAttribute(attr)) {
00233         return defaultValue;
00234     }
00235     try {
00236         return getString(attr);
00237     } catch (EmptyData&) {
00238         if (report) {
00239             emitEmptyError(getName(attr), objectid);
00240         }
00241     }
00242     ok = false;
00243     return "";
00244 }
00245 
00246 
00247 SUMOTime
00248 SUMOSAXAttributes::getSUMOTimeReporting(int attr, const char* objectid,
00249                                         bool& ok, bool report) const {
00250 #ifdef HAVE_SUBSECOND_TIMESTEPS
00251     if (!hasAttribute(attr)) {
00252         if (report) {
00253             emitUngivenError(getName(attr), objectid);
00254         }
00255         ok = false;
00256         return -1;
00257     }
00258     try {
00259         return (SUMOTime)(getFloat(attr) * 1000.);
00260     } catch (NumberFormatException&) {
00261         if (report) {
00262             emitFormatError(getName(attr), "a time value", objectid);
00263         }
00264     } catch (EmptyData&) {
00265         if (report) {
00266             emitEmptyError(getName(attr), objectid);
00267         }
00268     }
00269     ok = false;
00270     return (SUMOTime) - 1;
00271 #else
00272     return getIntReporting(attr, objectid, ok, report);
00273 #endif
00274 }
00275 
00276 
00277 SUMOTime
00278 SUMOSAXAttributes::getOptSUMOTimeReporting(int attr, const char* objectid,
00279         bool& ok, SUMOTime defaultValue, bool report) const {
00280 #ifdef HAVE_SUBSECOND_TIMESTEPS
00281     if (!hasAttribute(attr)) {
00282         return defaultValue;
00283     }
00284     try {
00285         return (SUMOTime)(getFloat(attr) * 1000.);
00286     } catch (NumberFormatException&) {
00287         if (report) {
00288             emitFormatError(getName(attr), "a real number", 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 getOptIntReporting(attr, objectid, ok, defaultValue, report);
00299 #endif
00300 }
00301 
00302 
00303 
00304 
00305 
00306 void
00307 SUMOSAXAttributes::emitUngivenError(const std::string& attrname, const char* objectid) const {
00308     std::ostringstream oss;
00309     oss << "Attribute '" << attrname << "' is missing in definition of ";
00310     if (objectid == 0) {
00311         oss << "a ";
00312     }
00313     oss << myObjectType;
00314     if (objectid != 0) {
00315         oss << " '" << objectid << "'";
00316     }
00317     oss << ".";
00318     WRITE_ERROR(oss.str());
00319 }
00320 
00321 
00322 void
00323 SUMOSAXAttributes::emitEmptyError(const std::string& attrname, const char* objectid) const {
00324     std::ostringstream oss;
00325     oss << "Attribute '" << attrname << "' in definition of ";
00326     if (objectid == 0) {
00327         oss << "a ";
00328     }
00329     oss << myObjectType;
00330     if (objectid != 0) {
00331         oss << " '" << objectid << "'";
00332     }
00333     oss << " is empty.";
00334     WRITE_ERROR(oss.str());
00335 }
00336 
00337 
00338 void
00339 SUMOSAXAttributes::emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const {
00340     std::ostringstream oss;
00341     oss << "Attribute '" << attrname << "' in definition of ";
00342     if (objectid == 0) {
00343         oss << "a ";
00344     }
00345     oss << myObjectType;
00346     if (objectid != 0) {
00347         oss << " '" << objectid << "'";
00348     }
00349     oss << " is not " << type << ".";
00350     WRITE_ERROR(oss.str());
00351 }
00352 
00353 
00354 void
00355 SUMOSAXAttributes::parseStringVector(const std::string& def, std::vector<std::string> &into) {
00356     if (def.find(';') != std::string::npos || def.find(',') != std::string::npos) {
00357         if (!myHaveInformedAboutDeprecatedDivider) {
00358             WRITE_WARNING("Please note that using ';' and ',' as XML list separators is deprecated.\n From 1.0 onwards, only ' ' will be accepted.");
00359             myHaveInformedAboutDeprecatedDivider = true;
00360         }
00361     }
00362     StringTokenizer st(def, ";, ", true);
00363     while (st.hasNext()) {
00364         into.push_back(st.next());
00365     }
00366 }
00367 
00368 
00369 /****************************************************************************/
00370 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines