Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
OptionsLoader.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2026 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>
32#include <utils/xml/XMLSubSys.h>
38#include "OptionsIO.h"
39#include "OptionsCont.h"
40#include "OptionsLoader.h"
41
42
43// ===========================================================================
44// method definitions
45// ===========================================================================
46
47OptionsLoader::OptionsLoader(OptionsCont& customOptions, const bool rootOnly) :
48 myRootOnly(rootOnly),
49 myOptions(customOptions),
50 myItem() {
51}
52
53
55
56
57void OptionsLoader::startElement(const XMLCh* const name, XERCES_CPP_NAMESPACE::AttributeList& attributes) {
58 myFoundValue = false;
60 if (!myRootOnly) {
61 for (int i = 0; i < (int)attributes.getLength(); i++) {
62 const std::string& key = XMLSubSys::transcode(attributes.getName(i));
63 const std::string& value = XMLSubSys::transcode(attributes.getValue(i));
64 if (key == "value" || key == "v") {
65 setValue(myItem, value);
66 myFoundValue = true;
67 } else if (key != "xmlns:xsi"
68 && key != "xsi:noNamespaceSchemaLocation"
69 && key != "synonymes"
70 && key != "deprecated"
71 && key != "type"
72 && key != "help"
73 // parsing a network file as single argument
74 && (key != "version" && myItem != "net")) {
75 WRITE_WARNINGF(TL("Ignoring attribute '%' for option '%'"), key, myItem);
76 }
77 }
78 myValue = "";
79 }
80}
81
82
83void OptionsLoader::setValue(const std::string& key, const std::string& value) {
84 if (value.length() > 0) {
85 // try to add value in option container
86 try {
87 if (!setSecure(myOptions, key, value)) {
88 WRITE_ERRORF(TL("Could not set option '%' (probably defined twice)."), key);
89 myError = true;
90 }
91 } catch (ProcessError& e) {
92 WRITE_ERROR(e.what());
93 myError = true;
94 }
95 }
96}
97
98
99void OptionsLoader::characters(const XMLCh* const chars, const XERCES3_SIZE_t length) {
100 myValue = myValue + XMLSubSys::transcode(chars, (int) length);
101}
102
103
104bool
105OptionsLoader::setSecure(OptionsCont& options, const std::string& name, const std::string& value) const {
106 if (options.isWriteable(name)) {
107 options.set(name, value);
108 return true;
109 }
110 return false;
111}
112
113
114void
115OptionsLoader::endElement(const XMLCh* const /*name*/) {
116 if (myItem.length() == 0 || myValue.length() == 0) {
117 if (!myFoundValue) {
118 WRITE_ERRORF(TL("Could not set option '%' because attribute 'value' is missing."), myItem);
119 }
120 return;
121 }
122 if (myValue.find_first_not_of("\n\t \a") == std::string::npos) {
123 return;
124 }
126 myItem = "";
127 myValue = "";
128}
129
130
131void
132OptionsLoader::warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
133 WRITE_WARNING(XMLSubSys::transcode(exception.getMessage()));
134 WRITE_WARNING(" (At line/column " \
135 + toString(exception.getLineNumber() + 1) + '/' \
136 + toString(exception.getColumnNumber()) + ").");
137 myError = true;
138}
139
140
141void
142OptionsLoader::error(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
143 WRITE_ERROR(XMLSubSys::transcode(exception.getMessage()));
144 WRITE_ERROR(" (At line/column "
145 + toString(exception.getLineNumber() + 1) + '/'
146 + toString(exception.getColumnNumber()) + ").");
147 myError = true;
148}
149
150
151void
152OptionsLoader::fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
153 WRITE_ERROR(XMLSubSys::transcode(exception.getMessage()));
154 WRITE_ERROR(" (At line/column "
155 + toString(exception.getLineNumber() + 1) + '/'
156 + toString(exception.getColumnNumber()) + ").");
157 myError = true;
158}
159
160
161bool
163 return myError;
164}
165
166/****************************************************************************/
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:287
#define WRITE_ERRORF(...)
Definition MsgHandler.h:296
#define WRITE_ERROR(msg)
Definition MsgHandler.h:295
#define WRITE_WARNING(msg)
Definition MsgHandler.h:286
#define TL(string)
Definition MsgHandler.h:304
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:49
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()
destructor
OptionsLoader(OptionsCont &customOptions, const bool routeOnly=false)
Constructor for default option container.
virtual void startElement(const XMLCh *const name, XERCES_CPP_NAMESPACE::AttributeList &attributes)
Called on the occurrence of the beginning of a tag.
void fatalError(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Called on an XML-fatal error.
bool myFoundValue
Whether a value attribute was read.
bool myError
The information whether an error occurred.
void characters(const XMLCh *const chars, const XERCES3_SIZE_t length)
Called on the occurrence 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.
bool setSecure(OptionsCont &options, const std::string &name, const std::string &value) const
Tries to set the named option to the given value.
void endElement(const XMLCh *const name)
Called on the end of an element.
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.
const bool myRootOnly
The information whether only the root element should be parsed.
void error(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Called on an XML-error.
static std::string transcode(const XMLCh *const data, int length=-1)
converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8