Eclipse 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-2022 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // A SAX-Handler for loading options
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <algorithm>
25 #include <string>
26 #include <vector>
27 #include <xercesc/sax/HandlerBase.hpp>
28 #include <xercesc/sax/AttributeList.hpp>
29 #include <xercesc/sax/SAXParseException.hpp>
30 #include <xercesc/sax/SAXException.hpp>
33 #include "OptionsLoader.h"
34 #include "OptionsCont.h"
38 #include <utils/common/ToString.h>
39 
40 
41 // ===========================================================================
42 // method definitions
43 // ===========================================================================
44 OptionsLoader::OptionsLoader(const bool rootOnly)
45  : myRootOnly(rootOnly), myError(false), myOptions(OptionsCont::getOptions()), myItem() {}
46 
47 
49 
50 
51 void OptionsLoader::startElement(const XMLCh* const name,
52  XERCES_CPP_NAMESPACE::AttributeList& attributes) {
54  if (!myRootOnly) {
55  for (int i = 0; i < (int)attributes.getLength(); i++) {
56  std::string key = StringUtils::transcode(attributes.getName(i));
57  std::string value = StringUtils::transcode(attributes.getValue(i));
58  if (key == "value" || key == "v") {
59  // Substitute environment variables defined by ${NAME} with their value
60  std::string cleanValue = StringUtils::substituteEnvironment(value);
61  setValue(myItem, cleanValue);
62  }
63  // could give a hint here about unsupported attributes in configuration files
64  }
65  myValue = "";
66  }
67 }
68 
69 
70 void OptionsLoader::setValue(const std::string& key,
71  std::string& value) {
72  if (value.length() > 0) {
73  try {
74  if (!setSecure(key, value)) {
75  WRITE_ERROR("Could not set option '" + key + "' (probably defined twice).");
76  myError = true;
77  }
78  } catch (ProcessError& e) {
79  WRITE_ERROR(e.what());
80  myError = true;
81  }
82  }
83 }
84 
85 
86 void OptionsLoader::characters(const XMLCh* const chars,
87  const XERCES3_SIZE_t length) {
88  myValue = myValue + StringUtils::transcode(chars, (int) length);
89 }
90 
91 
92 bool
93 OptionsLoader::setSecure(const std::string& name,
94  const std::string& value) const {
95  if (myOptions.isWriteable(name)) {
96  myOptions.set(name, value);
97  return true;
98  }
99  return false;
100 }
101 
102 
103 void
104 OptionsLoader::endElement(const XMLCh* const /*name*/) {
105  if (myItem.length() == 0 || myValue.length() == 0) {
106  return;
107  }
108  if (myValue.find_first_not_of("\n\t \a") == std::string::npos) {
109  return;
110  }
112  myItem = "";
113  myValue = "";
114 }
115 
116 
117 void
118 OptionsLoader::warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
119  WRITE_WARNING(StringUtils::transcode(exception.getMessage()));
120  WRITE_WARNING(" (At line/column " \
121  + toString(exception.getLineNumber() + 1) + '/' \
122  + toString(exception.getColumnNumber()) + ").");
123  myError = true;
124 }
125 
126 
127 void
128 OptionsLoader::error(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
129  WRITE_ERROR(
130  StringUtils::transcode(exception.getMessage()));
131  WRITE_ERROR(
132  " (At line/column "
133  + toString(exception.getLineNumber() + 1) + '/'
134  + toString(exception.getColumnNumber()) + ").");
135  myError = true;
136 }
137 
138 
139 void
140 OptionsLoader::fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
141  WRITE_ERROR(
142  StringUtils::transcode(exception.getMessage()));
143  WRITE_ERROR(
144  " (At line/column "
145  + toString(exception.getLineNumber() + 1) + '/'
146  + toString(exception.getColumnNumber()) + ").");
147  myError = true;
148 }
149 
150 
151 bool
153  return myError;
154 }
155 
156 
157 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:288
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:280
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
A storage for options typed value containers)
Definition: OptionsCont.h:89
bool isWriteable(const std::string &name)
Returns the information whether the named option may be set.
bool set(const std::string &name, const std::string &value)
Sets the given value for the named option.
OptionsLoader(const bool routeOnly=false)
Constructor.
bool setSecure(const std::string &name, const std::string &value) const
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 myError
The information whether an error occurred.
void setValue(const std::string &key, std::string &value)
Tries to set the named option to the given value.
void characters(const XMLCh *const chars, const XERCES3_SIZE_t length)
Called on the occurence of character data.
bool errorOccurred() const
Returns the information whether an error occurred.
void warning(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Called on an XML-warning.
std::string myValue
The currently read characters string.
void endElement(const XMLCh *const name)
Called on the end of an element.
bool myRootOnly
The information whether only the root element should be parsed.
OptionsCont & myOptions
The options to fill.
std::string myItem
The name of the currently parsed option.
void error(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Called on an XML-error.
static std::string substituteEnvironment(std::string str)
static std::string transcode(const XMLCh *const data)
converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8
Definition: StringUtils.h:137