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