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 /****************************************************************************/
17 // A SAX-Handler for loading options
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <algorithm>
27 #include <string>
28 #include <vector>
29 #include <xercesc/sax/HandlerBase.hpp>
30 #include <xercesc/sax/AttributeList.hpp>
31 #include <xercesc/sax/SAXParseException.hpp>
32 #include <xercesc/sax/SAXException.hpp>
35 #include "OptionsLoader.h"
36 #include "OptionsCont.h"
40 #include <utils/common/ToString.h>
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 OptionsLoader::OptionsLoader(const bool rootOnly)
47  : myRootOnly(rootOnly), myError(false), myOptions(OptionsCont::getOptions()), myItem() {}
48 
49 
51 
52 
53 void OptionsLoader::startElement(const XMLCh* const name,
54  XERCES_CPP_NAMESPACE::AttributeList& attributes) {
56  if (!myRootOnly) {
57  for (int i = 0; i < (int)attributes.getLength(); i++) {
58  std::string key = StringUtils::transcode(attributes.getName(i));
59  std::string value = StringUtils::transcode(attributes.getValue(i));
60  if (key == "value" || key == "v") {
61  setValue(myItem, value);
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 
158 /****************************************************************************/
159 
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:93
OptionsLoader::warning
void warning(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Called on an XML-warning.
Definition: OptionsLoader.cpp:118
ToString.h
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
OptionsLoader::error
void error(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Called on an XML-error.
Definition: OptionsLoader.cpp:128
OptionsLoader::OptionsLoader
OptionsLoader(const bool routeOnly=false)
Constructor.
Definition: OptionsLoader.cpp:46
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:53
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:244
MsgHandler.h
FileHelpers.h
XERCES3_SIZE_t
#define XERCES3_SIZE_t
Definition: config.h:213
OptionsLoader::myItem
std::string myItem
The name of the currently parsed option.
Definition: OptionsLoader.h:170
OptionsLoader.h
OptionsLoader::~OptionsLoader
~OptionsLoader()
Definition: OptionsLoader.cpp:50
OptionsLoader::myRootOnly
bool myRootOnly
The information whether only the root element should be parsed.
Definition: OptionsLoader.h:161
OptionsLoader::errorOccurred
bool errorOccurred() const
Returns the information whether an error occurred.
Definition: OptionsLoader.cpp:152
OptionsLoader::characters
void characters(const XMLCh *const chars, const XERCES3_SIZE_t length)
Called on the occurence of character data.
Definition: OptionsLoader.cpp:86
ProcessError
Definition: UtilExceptions.h:40
UtilExceptions.h
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:90
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
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:132
OptionsLoader::myError
bool myError
The information whether an error occurred.
Definition: OptionsLoader.h:164
OptionsLoader::myValue
std::string myValue
The currently read characters string.
Definition: OptionsLoader.h:173
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:70
StringTokenizer.h
OptionsLoader::fatalError
void fatalError(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Called on an XML-fatal error.
Definition: OptionsLoader.cpp:140
OptionsLoader::myOptions
OptionsCont & myOptions
The options to fill.
Definition: OptionsLoader.h:167
OptionsCont::isWriteable
bool isWriteable(const std::string &name)
Returns the information whether the named option may be set.
Definition: OptionsCont.cpp:453
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
OptionsLoader::endElement
void endElement(const XMLCh *const name)
Called on the end of an element.
Definition: OptionsLoader.cpp:104