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>
37#include "OptionsIO.h"
38#include "OptionsCont.h"
39#include "OptionsLoader.h"
40
41
42// ===========================================================================
43// method definitions
44// ===========================================================================
45OptionsLoader::OptionsLoader(const bool rootOnly)
46 : myRootOnly(rootOnly), myError(false), myOptions(OptionsCont::getOptions()), myItem() {}
47
48
50
51
52void 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 const std::string& key = StringUtils::transcode(attributes.getName(i));
58 const std::string& value = StringUtils::transcode(attributes.getValue(i));
59 if (key == "value" || key == "v") {
60 setValue(myItem, value);
61 }
62 // could give a hint here about unsupported attributes in configuration files
63 }
64 myValue = "";
65 }
66}
67
68
69void OptionsLoader::setValue(const std::string& key,
70 const std::string& value) {
71 if (value.length() > 0) {
72 try {
73 if (!setSecure(key, value)) {
74 WRITE_ERROR("Could not set option '" + key + "' (probably defined twice).");
75 myError = true;
76 }
77 } catch (ProcessError& e) {
78 WRITE_ERROR(e.what());
79 myError = true;
80 }
81 }
82}
83
84
85void OptionsLoader::characters(const XMLCh* const chars,
86 const XERCES3_SIZE_t length) {
87 myValue = myValue + StringUtils::transcode(chars, (int) length);
88}
89
90
91bool
92OptionsLoader::setSecure(const std::string& name,
93 const std::string& value) const {
94 if (myOptions.isWriteable(name)) {
95 myOptions.set(name, value);
96 return true;
97 }
98 return false;
99}
100
101
102void
103OptionsLoader::endElement(const XMLCh* const /*name*/) {
104 if (myItem.length() == 0 || myValue.length() == 0) {
105 return;
106 }
107 if (myValue.find_first_not_of("\n\t \a") == std::string::npos) {
108 return;
109 }
111 myItem = "";
112 myValue = "";
113}
114
115
116void
117OptionsLoader::warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
118 WRITE_WARNING(StringUtils::transcode(exception.getMessage()));
119 WRITE_WARNING(" (At line/column " \
120 + toString(exception.getLineNumber() + 1) + '/' \
121 + toString(exception.getColumnNumber()) + ").");
122 myError = true;
123}
124
125
126void
127OptionsLoader::error(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
129 StringUtils::transcode(exception.getMessage()));
131 " (At line/column "
132 + toString(exception.getLineNumber() + 1) + '/'
133 + toString(exception.getColumnNumber()) + ").");
134 myError = true;
135}
136
137
138void
139OptionsLoader::fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
141 StringUtils::transcode(exception.getMessage()));
143 " (At line/column "
144 + toString(exception.getLineNumber() + 1) + '/'
145 + toString(exception.getColumnNumber()) + ").");
146 myError = true;
147}
148
149
150bool
152 return myError;
153}
154
155
156/****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:274
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:265
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, const bool append=false)
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 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 setValue(const std::string &key, const std::string &value)
Tries to set the named option to the given value.
void error(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Called on an XML-error.
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:140