SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
OptionsLoader.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A SAX-Handler for loading options
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2016 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 <algorithm>
34 #include <string>
35 #include <vector>
36 #include <xercesc/sax/HandlerBase.hpp>
37 #include <xercesc/sax/AttributeList.hpp>
38 #include <xercesc/sax/SAXParseException.hpp>
39 #include <xercesc/sax/SAXException.hpp>
42 #include "OptionsLoader.h"
43 #include "OptionsCont.h"
47 #include <utils/common/ToString.h>
48 
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
53 OptionsLoader::OptionsLoader(const bool rootOnly)
54  : myRootOnly(rootOnly), myError(false), myOptions(OptionsCont::getOptions()), myItem() {}
55 
56 
58 
59 
60 void OptionsLoader::startElement(const XMLCh* const name,
61  XERCES_CPP_NAMESPACE::AttributeList& attributes) {
62  myItem = TplConvert::_2str(name);
63  if (!myRootOnly) {
64  for (int i = 0; i < (int)attributes.getLength(); i++) {
65  std::string key = TplConvert::_2str(attributes.getName(i));
66  std::string value = TplConvert::_2str(attributes.getValue(i));
67  if (key == "value" || key == "v") {
68  setValue(myItem, value);
69  }
70  // could give a hint here about unsupported attributes in configuration files
71  }
72  myValue = "";
73  }
74 }
75 
76 
77 void OptionsLoader::setValue(const std::string& key,
78  std::string& value) {
79  if (value.length() > 0) {
80  try {
81  if (!setSecure(key, value)) {
82  WRITE_ERROR("Could not set option '" + key + "' (probably defined twice).");
83  myError = true;
84  }
85  } catch (ProcessError& e) {
86  WRITE_ERROR(e.what());
87  myError = true;
88  }
89  }
90 }
91 
92 
93 void OptionsLoader::characters(const XMLCh* const chars,
94  const XERCES3_SIZE_t length) {
95  myValue = myValue + TplConvert::_2str(chars, (int) length);
96 }
97 
98 
99 bool
100 OptionsLoader::setSecure(const std::string& name,
101  const std::string& value) const {
102  if (myOptions.isWriteable(name)) {
103  myOptions.set(name, value);
104  return true;
105  }
106  return false;
107 }
108 
109 
110 void
111 OptionsLoader::endElement(const XMLCh* const /*name*/) {
112  if (myItem.length() == 0 || myValue.length() == 0) {
113  return;
114  }
115  if (myValue.find_first_not_of("\n\t \a") == std::string::npos) {
116  return;
117  }
119  myItem = "";
120  myValue = "";
121 }
122 
123 
124 void
125 OptionsLoader::warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
126  WRITE_WARNING(TplConvert::_2str(exception.getMessage()));
127  WRITE_WARNING(" (At line/column " \
128  + toString(exception.getLineNumber() + 1) + '/' \
129  + toString(exception.getColumnNumber()) + ").");
130  myError = true;
131 }
132 
133 
134 void
135 OptionsLoader::error(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
136  WRITE_ERROR(
137  TplConvert::_2str(exception.getMessage()));
138  WRITE_ERROR(
139  " (At line/column "
140  + toString(exception.getLineNumber() + 1) + '/'
141  + toString(exception.getColumnNumber()) + ").");
142  myError = true;
143 }
144 
145 
146 void
147 OptionsLoader::fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
148  WRITE_ERROR(
149  TplConvert::_2str(exception.getMessage()));
150  WRITE_ERROR(
151  " (At line/column "
152  + toString(exception.getLineNumber() + 1) + '/'
153  + toString(exception.getColumnNumber()) + ").");
154  myError = true;
155 }
156 
157 
158 bool
160  return myError;
161 }
162 
163 
164 
165 /****************************************************************************/
166 
std::string myItem
The name of the currently parsed option.
std::string myValue
The currently read characters string.
void endElement(const XMLCh *const name)
Called on the end of an element.
OptionsCont & myOptions
The options to fill.
void error(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Called on an XML-error.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
bool myError
The information whether an error occured.
OptionsLoader(const bool routeOnly=false)
Constructor.
bool myRootOnly
The information whether only the root element should be parsed.
void setValue(const std::string &key, std::string &value)
Tries to set the named option to the given value.
virtual void startElement(const XMLCh *const name, XERCES_CPP_NAMESPACE::AttributeList &attributes)
Called on the occurence of the beginning of a tag.
void fatalError(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Called on an XML-fatal error.
bool errorOccured() const
Returns the information whether an error occured.
bool isWriteable(const std::string &name)
Returns the information whether the named option may be set.
#define XERCES3_SIZE_t
Definition: config.h:232
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
bool setSecure(const std::string &name, const std::string &value) const
Tries to set the named option to the given value.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
void warning(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Called on an XML-warning.
bool set(const std::string &name, const std::string &value)
Sets the given value for the named option.
void characters(const XMLCh *const chars, const XERCES3_SIZE_t length)
Called on the occurence of character data.
A storage for options typed value containers)
Definition: OptionsCont.h:99
static std::string _2str(const int var)
convert int to string
Definition: TplConvert.h:57