Eclipse SUMO - Simulation of Urban MObility
ConfigHandler.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/****************************************************************************/
18// The XML-Handler for SUMOConfig loading
19/****************************************************************************/
20#include <config.h>
21
24#include <utils/xml/XMLSubSys.h>
25
26#include "ConfigHandler.h"
27
28
29// ===========================================================================
30// method definitions
31// ===========================================================================
32
33ConfigHandler::ConfigHandler(const std::string& file) :
34 SUMOSAXHandler(file) {
35}
36
37
39
40
41bool
43 // run parser and return result
44 return XMLSubSys::runParser(*this, getFileName());
45}
46
47
48void
50 // open SUMOBaseOBject and set tag
54}
55
56
57void
59 // declare Ok Flag
60 bool parsedOk = true;
61 // network file
62 const std::string value = attrs.get<std::string>(SUMO_ATTR_VALUE, "", parsedOk);
63 // continue if flag is ok
64 if (parsedOk) {
65 if (value.empty()) {
66 WRITE_ERROR(TL("Network file cannot be empty"));
67 } else if (myCommonXMLStructure.getCurrentSumoBaseObject() == nullptr) {
68 WRITE_ERROR(TL("Network file must be loaded within a configuration"));
69 } else {
70 // add it in SUMOConfig parent
72 }
73 }
74}
75
76
77void
79 // declare Ok Flag
80 bool parsedOk = true;
81 // additional file
82 const std::string value = attrs.get<std::string>(SUMO_ATTR_VALUE, "", parsedOk);
83 // continue if flag is ok
84 if (parsedOk) {
85 // avoid empty files
86 if (value.empty()) {
87 WRITE_ERROR(TL("Additional files cannot be empty"));
88 } else if (myCommonXMLStructure.getCurrentSumoBaseObject() == nullptr) {
89 WRITE_ERROR(TL("Additional files must be loaded within a configuration"));
90 } else {
91 // add it in SUMOConfig parent
93 }
94 }
95}
96
97
98void
100 // declare Ok Flag
101 bool parsedOk = true;
102 // route file
103 const std::string value = attrs.get<std::string>(SUMO_ATTR_VALUE, "", parsedOk);
104 // continue if flag is ok
105 if (parsedOk) {
106 // avoid empty files
107 if (value.empty()) {
108 WRITE_ERROR(TL("Route files cannot be empty"));
109 } else if (myCommonXMLStructure.getCurrentSumoBaseObject() == nullptr) {
110 WRITE_ERROR(TL("Route files must be loaded within a configuration"));
111 } else {
112 // add it in SUMOConfig parent
114 }
115 }
116}
117
118
119void
121 // declare Ok Flag
122 bool parsedOk = true;
123 // data file
124 const std::string value = attrs.get<std::string>(SUMO_ATTR_VALUE, "", parsedOk);
125 // continue if flag is ok
126 if (parsedOk) {
127 // avoid empty files
128 if (value.empty()) {
129 WRITE_ERROR(TL("Data files cannot be empty"));
130 } else if (myCommonXMLStructure.getCurrentSumoBaseObject() == nullptr) {
131 WRITE_ERROR(TL("Data files must be loaded within a configuration"));
132 } else {
133 // add it in SUMOConfig parent
135 }
136 }
137}
138
139
140void
142 // obtain tag
143 const SumoXMLTag tag = static_cast<SumoXMLTag>(element);
144 // check tag
145 try {
146 switch (tag) {
147 // Stopping Places
150 break;
151 case SUMO_TAG_NETFILE:
152 parseNetFile(attrs);
153 break;
156 break;
158 parseRouteFiles(attrs);
159 break;
161 parseDataFiles(attrs);
162 break;
163 default:
164 // tag cannot be parsed in ConfigHandler
165 break;
166 }
167 } catch (InvalidArgument& e) {
168 WRITE_ERROR(e.what());
169 }
170}
171
172
173void
175 // obtain tag
176 const SumoXMLTag tag = static_cast<SumoXMLTag>(element);
177 // get last inserted object
179 // check tag (only load after ending configuration)
180 if (tag == SUMO_TAG_CONFIGURATION) {
181 // close SUMOBaseOBject
183 // load config
184 loadConfig(obj);
185 // delete object (and all of their childrens)
186 delete obj;
187 }
188}
189
190/****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:274
#define TL(string)
Definition: MsgHandler.h:282
SumoXMLTag
Numbers representing SUMO-XML - element names.
@ SUMO_TAG_ADDITIONALFILES
additional files
@ SUMO_TAG_NETFILE
net file
@ SUMO_TAG_CONFIGURATION
@ SUMO_TAG_DATAFILES
data files
@ SUMO_TAG_ROUTEFILES
route files
@ SUMO_ATTR_DATAFILES
route files
@ SUMO_ATTR_CONFIGFILE
@ SUMO_ATTR_NETFILE
net file
@ SUMO_ATTR_VALUE
@ SUMO_ATTR_ADDITIONALFILES
additional files
@ SUMO_ATTR_ROUTEFILES
route files
void setTag(const SumoXMLTag tag)
set SumoBaseObject tag
void addStringAttribute(const SumoXMLAttr attr, const std::string &value)
CommonXMLStructure::SumoBaseObject * getCurrentSumoBaseObject() const
get current editedSumoBaseObject
void openSUMOBaseOBject()
open SUMOBaseOBject
void closeSUMOBaseOBject()
close myTag
virtual void loadConfig(CommonXMLStructure::SumoBaseObject *configObj)=0
Load net file.
bool parse()
parse
virtual void myEndElement(int element)
Called when a closing tag occurs.
virtual ~ConfigHandler()
Destructor.
void parseRouteFiles(const SUMOSAXAttributes &attrs)
parse route files attribute
CommonXMLStructure myCommonXMLStructure
common XML Structure
Definition: ConfigHandler.h:51
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
void parseDataFiles(const SUMOSAXAttributes &attrs)
parse data files attribute
ConfigHandler(const std::string &file)
Constructor.
void parseConfigFile()
parse config file attribute
void parseAdditionalFiles(const SUMOSAXAttributes &attrs)
parse additional files attribute
void parseNetFile(const SUMOSAXAttributes &attrs)
parse net file attribute
const std::string & getFileName() const
returns the current file name
Encapsulated SAX-Attributes.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
SAX-handler base for SUMO-files.
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false, const bool isRoute=false)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:137