SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SUMOSAXAttributes.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Encapsulated SAX-Attributes
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include "SUMOSAXAttributes.h"
37 #include <iostream>
38 #include <sstream>
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // static members
47 // ===========================================================================
49 const std::string SUMOSAXAttributes::ENCODING = " encoding=\"UTF-8\"";
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
55 SUMOSAXAttributes::SUMOSAXAttributes(const std::string& objectType):
56  myObjectType(objectType) {}
57 
58 
59 int
60 SUMOSAXAttributes::getIntReporting(int attr, const char* objectid,
61  bool& ok, bool report) const {
62  if (!hasAttribute(attr)) {
63  if (report) {
64  emitUngivenError(getName(attr), objectid);
65  }
66  ok = false;
67  return -1;
68  }
69  try {
70  return getInt(attr);
71  } catch (NumberFormatException&) {
72  if (report) {
73  emitFormatError(getName(attr), "an int", objectid);
74  }
75  } catch (EmptyData&) {
76  if (report) {
77  emitEmptyError(getName(attr), objectid);
78  }
79  }
80  ok = false;
81  return -1;
82 }
83 
84 
85 int
86 SUMOSAXAttributes::getOptIntReporting(int attr, const char* objectid,
87  bool& ok, int defaultValue, bool report) const {
88  if (!hasAttribute(attr)) {
89  return defaultValue;
90  }
91  try {
92  return getInt(attr);
93  } catch (NumberFormatException&) {
94  if (report) {
95  emitFormatError(getName(attr), "an int", objectid);
96  }
97  } catch (EmptyData&) {
98  if (report) {
99  emitEmptyError(getName(attr), objectid);
100  }
101  }
102  ok = false;
103  return -1;
104 }
105 
106 
107 SUMOLong
108 SUMOSAXAttributes::getLongReporting(int attr, const char* objectid,
109  bool& ok, bool report) const {
110  if (!hasAttribute(attr)) {
111  if (report) {
112  emitUngivenError(getName(attr), objectid);
113  }
114  ok = false;
115  return -1;
116  }
117  try {
118  return getLong(attr);
119  } catch (NumberFormatException&) {
120  if (report) {
121  emitFormatError(getName(attr), "an int", objectid);
122  }
123  } catch (EmptyData&) {
124  if (report) {
125  emitEmptyError(getName(attr), objectid);
126  }
127  }
128  ok = false;
129  return -1;
130 }
131 
132 
133 SUMOReal
134 SUMOSAXAttributes::getSUMORealReporting(int attr, const char* objectid,
135  bool& ok, bool report) const {
136  if (!hasAttribute(attr)) {
137  if (report) {
138  emitUngivenError(getName(attr), objectid);
139  }
140  ok = false;
141  return -1;
142  }
143  try {
144  return getFloat(attr);
145  } catch (NumberFormatException&) {
146  if (report) {
147  emitFormatError(getName(attr), "a real number", objectid);
148  }
149  } catch (EmptyData&) {
150  if (report) {
151  emitEmptyError(getName(attr), objectid);
152  }
153  }
154  ok = false;
155  return (SUMOReal) - 1;
156 }
157 
158 
159 SUMOReal
160 SUMOSAXAttributes::getOptSUMORealReporting(int attr, const char* objectid,
161  bool& ok, SUMOReal defaultValue, bool report) const {
162  if (!hasAttribute(attr)) {
163  return defaultValue;
164  }
165  try {
166  return getFloat(attr);
167  } catch (NumberFormatException&) {
168  if (report) {
169  emitFormatError(getName(attr), "a real number", objectid);
170  }
171  } catch (EmptyData&) {
172  if (report) {
173  emitEmptyError(getName(attr), objectid);
174  }
175  }
176  ok = false;
177  return (SUMOReal) - 1;
178 }
179 
180 
181 bool
182 SUMOSAXAttributes::getBoolReporting(int attr, const char* objectid,
183  bool& ok, bool report) const {
184  if (!hasAttribute(attr)) {
185  if (report) {
186  emitUngivenError(getName(attr), objectid);
187  }
188  ok = false;
189  return false;
190  }
191  try {
192  return getBool(attr);
193  } catch (BoolFormatException&) {
194  if (report) {
195  emitFormatError(getName(attr), "a boolean", objectid);
196  }
197  } catch (EmptyData&) {
198  if (report) {
199  emitEmptyError(getName(attr), objectid);
200  }
201  }
202  ok = false;
203  return false;
204 }
205 
206 
207 bool
208 SUMOSAXAttributes::getOptBoolReporting(int attr, const char* objectid,
209  bool& ok, bool defaultValue, bool report) const {
210  if (!hasAttribute(attr)) {
211  return defaultValue;
212  }
213  try {
214  return getBool(attr);
215  } catch (BoolFormatException&) {
216  if (report) {
217  emitFormatError(getName(attr), "a boolean", objectid);
218  }
219  } catch (EmptyData&) {
220  if (report) {
221  emitEmptyError(getName(attr), objectid);
222  }
223  }
224  ok = false;
225  return false;
226 }
227 
228 
229 std::string
230 SUMOSAXAttributes::getStringReporting(int attr, const char* objectid,
231  bool& ok, bool report) const {
232  if (!hasAttribute(attr)) {
233  if (report) {
234  emitUngivenError(getName(attr), objectid);
235  }
236  ok = false;
237  return "";
238  }
239  try {
240  std::string ret = getString(attr);
241  if (ret == "") {
242  throw EmptyData();
243  }
244  return ret;
245  } catch (EmptyData&) {
246  if (report) {
247  emitEmptyError(getName(attr), objectid);
248  }
249  }
250  ok = false;
251  return "";
252 }
253 
254 
255 std::string
256 SUMOSAXAttributes::getOptStringReporting(int attr, const char* objectid,
257  bool& ok, const std::string& defaultValue, bool report) const {
258  if (!hasAttribute(attr)) {
259  return defaultValue;
260  }
261  try {
262  return getString(attr);
263  } catch (EmptyData&) {
264  if (report) {
265  emitEmptyError(getName(attr), objectid);
266  }
267  }
268  ok = false;
269  return "";
270 }
271 
272 
273 SUMOTime
274 SUMOSAXAttributes::getSUMOTimeReporting(int attr, const char* objectid,
275  bool& ok, bool report) const {
276 #ifdef HAVE_SUBSECOND_TIMESTEPS
277  if (!hasAttribute(attr)) {
278  if (report) {
279  emitUngivenError(getName(attr), objectid);
280  }
281  ok = false;
282  return -1;
283  }
284  try {
285  return (SUMOTime)(getFloat(attr) * 1000.);
286  } catch (NumberFormatException&) {
287  if (report) {
288  emitFormatError(getName(attr), "a time value", objectid);
289  }
290  } catch (EmptyData&) {
291  if (report) {
292  emitEmptyError(getName(attr), objectid);
293  }
294  }
295  ok = false;
296  return (SUMOTime) - 1;
297 #else
298  return getIntReporting(attr, objectid, ok, report);
299 #endif
300 }
301 
302 
303 SUMOTime
304 SUMOSAXAttributes::getOptSUMOTimeReporting(int attr, const char* objectid,
305  bool& ok, SUMOTime defaultValue, bool report) const {
306 #ifdef HAVE_SUBSECOND_TIMESTEPS
307  if (!hasAttribute(attr)) {
308  return defaultValue;
309  }
310  try {
311  return (SUMOTime)(getFloat(attr) * 1000.);
312  } catch (NumberFormatException&) {
313  if (report) {
314  emitFormatError(getName(attr), "a real number", objectid);
315  }
316  } catch (EmptyData&) {
317  if (report) {
318  emitEmptyError(getName(attr), objectid);
319  }
320  }
321  ok = false;
322  return (SUMOTime) - 1;
323 #else
324  return getOptIntReporting(attr, objectid, ok, defaultValue, report);
325 #endif
326 }
327 
328 
329 
330 
331 
332 void
333 SUMOSAXAttributes::emitUngivenError(const std::string& attrname, const char* objectid) const {
334  std::ostringstream oss;
335  oss << "Attribute '" << attrname << "' is missing in definition of ";
336  if (objectid == 0) {
337  oss << "a ";
338  }
339  oss << myObjectType;
340  if (objectid != 0) {
341  oss << " '" << objectid << "'";
342  }
343  oss << ".";
344  WRITE_ERROR(oss.str());
345 }
346 
347 
348 void
349 SUMOSAXAttributes::emitEmptyError(const std::string& attrname, const char* objectid) const {
350  std::ostringstream oss;
351  oss << "Attribute '" << attrname << "' in definition of ";
352  if (objectid == 0) {
353  oss << "a ";
354  }
355  oss << myObjectType;
356  if (objectid != 0) {
357  oss << " '" << objectid << "'";
358  }
359  oss << " is empty.";
360  WRITE_ERROR(oss.str());
361 }
362 
363 
364 void
365 SUMOSAXAttributes::emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const {
366  std::ostringstream oss;
367  oss << "Attribute '" << attrname << "' in definition of ";
368  if (objectid == 0) {
369  oss << "a ";
370  }
371  oss << myObjectType;
372  if (objectid != 0) {
373  oss << " '" << objectid << "'";
374  }
375  oss << " is not " << type << ".";
376  WRITE_ERROR(oss.str());
377 }
378 
379 
380 void
381 SUMOSAXAttributes::parseStringVector(const std::string& def, std::vector<std::string>& into) {
382  if (def.find(';') != std::string::npos || def.find(',') != std::string::npos) {
384  WRITE_WARNING("Please note that using ';' and ',' as XML list separators is deprecated.\n From 1.0 onwards, only ' ' will be accepted.");
386  }
387  }
388  StringTokenizer st(def, ";, ", true);
389  while (st.hasNext()) {
390  into.push_back(st.next());
391  }
392 }
393 
394 
395 /****************************************************************************/
396