SUMO - Simulation of Urban MObility
OptionsLoader.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // A SAX-Handler for loading options
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <algorithm>
33 #include <string>
34 #include <vector>
35 #include <xercesc/sax/HandlerBase.hpp>
36 #include <xercesc/sax/AttributeList.hpp>
37 #include <xercesc/sax/SAXParseException.hpp>
38 #include <xercesc/sax/SAXException.hpp>
41 #include "OptionsLoader.h"
42 #include "OptionsCont.h"
46 #include <utils/common/ToString.h>
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 OptionsLoader::OptionsLoader(const bool rootOnly)
53  : myRootOnly(rootOnly), myError(false), myOptions(OptionsCont::getOptions()), myItem() {}
54 
55 
57 
58 
59 void OptionsLoader::startElement(const XMLCh* const name,
60  XERCES_CPP_NAMESPACE::AttributeList& attributes) {
61  myItem = TplConvert::_2str(name);
62  if (!myRootOnly) {
63  for (int i = 0; i < (int)attributes.getLength(); i++) {
64  std::string key = TplConvert::_2str(attributes.getName(i));
65  std::string value = TplConvert::_2str(attributes.getValue(i));
66  if (key == "value" || key == "v") {
67  setValue(myItem, value);
68  }
69  // could give a hint here about unsupported attributes in configuration files
70  }
71  myValue = "";
72  }
73 }
74 
75 
76 void OptionsLoader::setValue(const std::string& key,
77  std::string& value) {
78  if (value.length() > 0) {
79  try {
80  if (!setSecure(key, value)) {
81  WRITE_ERROR("Could not set option '" + key + "' (probably defined twice).");
82  myError = true;
83  }
84  } catch (ProcessError& e) {
85  WRITE_ERROR(e.what());
86  myError = true;
87  }
88  }
89 }
90 
91 
92 void OptionsLoader::characters(const XMLCh* const chars,
93  const XERCES3_SIZE_t length) {
94  myValue = myValue + TplConvert::_2str(chars, (int) length);
95 }
96 
97 
98 bool
99 OptionsLoader::setSecure(const std::string& name,
100  const std::string& value) const {
101  if (myOptions.isWriteable(name)) {
102  myOptions.set(name, value);
103  return true;
104  }
105  return false;
106 }
107 
108 
109 void
110 OptionsLoader::endElement(const XMLCh* const /*name*/) {
111  if (myItem.length() == 0 || myValue.length() == 0) {
112  return;
113  }
114  if (myValue.find_first_not_of("\n\t \a") == std::string::npos) {
115  return;
116  }
118  myItem = "";
119  myValue = "";
120 }
121 
122 
123 void
124 OptionsLoader::warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
125  WRITE_WARNING(TplConvert::_2str(exception.getMessage()));
126  WRITE_WARNING(" (At line/column " \
127  + toString(exception.getLineNumber() + 1) + '/' \
128  + toString(exception.getColumnNumber()) + ").");
129  myError = true;
130 }
131 
132 
133 void
134 OptionsLoader::error(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
135  WRITE_ERROR(
136  TplConvert::_2str(exception.getMessage()));
137  WRITE_ERROR(
138  " (At line/column "
139  + toString(exception.getLineNumber() + 1) + '/'
140  + toString(exception.getColumnNumber()) + ").");
141  myError = true;
142 }
143 
144 
145 void
146 OptionsLoader::fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
147  WRITE_ERROR(
148  TplConvert::_2str(exception.getMessage()));
149  WRITE_ERROR(
150  " (At line/column "
151  + toString(exception.getLineNumber() + 1) + '/'
152  + toString(exception.getColumnNumber()) + ").");
153  myError = true;
154 }
155 
156 
157 bool
159  return myError;
160 }
161 
162 
163 
164 /****************************************************************************/
165 
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:199
bool setSecure(const std::string &name, const std::string &value) const
Tries to set the named option to the given value.
bool myError
The information whether an error occured.
OptionsLoader(const bool routeOnly=false)
Constructor.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
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:216
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
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:98
static std::string _2str(const int var)
convert int to string
Definition: TplConvert.h:56