Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2024 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 : /****************************************************************************/
14 : /// @file DataHandler.cpp
15 : /// @author Jakob Erdmann
16 : /// @date Jun 2021
17 : ///
18 : // The XML-Handler for data elements loading
19 : /****************************************************************************/
20 : #include <config.h>
21 :
22 : #include <utils/common/MsgHandler.h>
23 : #include <utils/common/StringUtils.h>
24 : #include <utils/xml/XMLSubSys.h>
25 :
26 : #include "DataHandler.h"
27 :
28 :
29 : // ===========================================================================
30 : // method definitions
31 : // ===========================================================================
32 :
33 0 : DataHandler::DataHandler(const std::string& file) :
34 0 : SUMOSAXHandler(file) {
35 0 : }
36 :
37 :
38 0 : DataHandler::~DataHandler() {}
39 :
40 :
41 : bool
42 0 : DataHandler::parse() {
43 : // run parser and return result
44 0 : return XMLSubSys::runParser(*this, getFileName());
45 : }
46 :
47 :
48 : void
49 0 : DataHandler::parseSumoBaseObject(CommonXMLStructure::SumoBaseObject* obj) {
50 : // switch tag
51 0 : switch (obj->getTag()) {
52 : // Stopping Places
53 0 : case SUMO_TAG_INTERVAL:
54 0 : buildDataInterval(obj,
55 : obj->getStringAttribute(SUMO_ATTR_ID),
56 : obj->getDoubleAttribute(SUMO_ATTR_BEGIN),
57 : obj->getDoubleAttribute(SUMO_ATTR_END));
58 0 : break;
59 0 : case SUMO_TAG_EDGE:
60 0 : buildEdgeData(obj,
61 : obj->getStringAttribute(SUMO_ATTR_ID),
62 0 : obj->getParameters());
63 0 : break;
64 0 : case SUMO_TAG_EDGEREL:
65 0 : buildEdgeRelationData(obj,
66 : obj->getStringAttribute(SUMO_ATTR_FROM),
67 : obj->getStringAttribute(SUMO_ATTR_TO),
68 0 : obj->getParameters());
69 0 : break;
70 0 : case SUMO_TAG_TAZREL:
71 0 : buildTAZRelationData(obj,
72 : obj->getStringAttribute(SUMO_ATTR_FROM),
73 : obj->getStringAttribute(SUMO_ATTR_TO),
74 0 : obj->getParameters());
75 0 : break;
76 : default:
77 : break;
78 : }
79 : // now iterate over childrens
80 0 : for (const auto& child : obj->getSumoBaseObjectChildren()) {
81 : // call this function recursively
82 0 : parseSumoBaseObject(child);
83 : }
84 0 : }
85 :
86 :
87 : void
88 0 : DataHandler::myStartElement(int element, const SUMOSAXAttributes& attrs) {
89 : // obtain tag
90 : const SumoXMLTag tag = (element == 0) ? SUMO_TAG_ROOTFILE : static_cast<SumoXMLTag>(element);
91 : // open SUMOBaseOBject
92 0 : myCommonXMLStructure.openSUMOBaseOBject();
93 : // check tag
94 : try {
95 0 : switch (tag) {
96 : // interval
97 0 : case SUMO_TAG_INTERVAL:
98 0 : parseInterval(attrs);
99 : break;
100 : // datas
101 0 : case SUMO_TAG_EDGE:
102 0 : parseEdgeData(attrs);
103 : break;
104 0 : case SUMO_TAG_EDGEREL:
105 0 : parseEdgeRelationData(attrs);
106 : break;
107 0 : case SUMO_TAG_TAZREL:
108 0 : parseTAZRelationData(attrs);
109 : break;
110 0 : case SUMO_TAG_PARAM:
111 0 : WRITE_WARNING(TL("Data elements cannot load attributes as params"));
112 0 : break;
113 : default:
114 : break;
115 : }
116 0 : } catch (InvalidArgument& e) {
117 0 : writeError(e.what());
118 0 : }
119 0 : }
120 :
121 :
122 : void
123 0 : DataHandler::myEndElement(int element) {
124 : // obtain tag
125 : const SumoXMLTag tag = static_cast<SumoXMLTag>(element);
126 : // get last inserted object
127 0 : CommonXMLStructure::SumoBaseObject* obj = myCommonXMLStructure.getCurrentSumoBaseObject();
128 : // close SUMOBaseOBject
129 0 : myCommonXMLStructure.closeSUMOBaseOBject();
130 : // check tag
131 0 : switch (tag) {
132 : // only interval
133 0 : case SUMO_TAG_INTERVAL:
134 : // parse object and all their childrens
135 0 : parseSumoBaseObject(obj);
136 : // delete object (and all of their childrens)
137 0 : delete obj;
138 : break;
139 : default:
140 : break;
141 : }
142 0 : }
143 :
144 :
145 : bool
146 0 : DataHandler::isErrorCreatingElement() const {
147 0 : return myErrorCreatingElement;
148 : }
149 :
150 :
151 : void
152 0 : DataHandler::writeError(const std::string& error) {
153 0 : WRITE_ERROR(error);
154 0 : myErrorCreatingElement = true;
155 0 : }
156 :
157 :
158 : void
159 0 : DataHandler::parseInterval(const SUMOSAXAttributes& attrs) {
160 : // declare Ok Flag
161 0 : bool parsedOk = true;
162 : // needed attributes
163 0 : const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
164 0 : const double begin = attrs.get<double>(SUMO_ATTR_BEGIN, "", parsedOk);
165 0 : const double end = attrs.get<double>(SUMO_ATTR_END, "", parsedOk);
166 : // continue if flag is ok
167 0 : if (parsedOk) {
168 : // set tag
169 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_INTERVAL);
170 : // add all attributes
171 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_ID, id);
172 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addDoubleAttribute(SUMO_ATTR_BEGIN, begin);
173 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addDoubleAttribute(SUMO_ATTR_END, end);
174 : }
175 0 : }
176 :
177 :
178 : void
179 0 : DataHandler::parseEdgeData(const SUMOSAXAttributes& attrs) {
180 : // declare Ok Flag
181 0 : bool parsedOk = true;
182 : // needed attributes
183 0 : const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "", parsedOk);
184 : // fill attributes
185 0 : getAttributes(attrs, {SUMO_ATTR_ID});
186 : // continue if flag is ok
187 0 : if (parsedOk) {
188 : // set tag
189 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_EDGE);
190 : // add all attributes
191 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_ID, id);
192 : }
193 0 : }
194 :
195 :
196 : void
197 0 : DataHandler::parseEdgeRelationData(const SUMOSAXAttributes& attrs) {
198 : // declare Ok Flag
199 0 : bool parsedOk = true;
200 : // needed attributes
201 0 : const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, "", parsedOk);
202 0 : const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, "", parsedOk);
203 : // fill attributes
204 0 : getAttributes(attrs, {SUMO_ATTR_FROM, SUMO_ATTR_TO});
205 : // continue if flag is ok
206 0 : if (parsedOk) {
207 : // set tag
208 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_EDGEREL);
209 : // add all attributes
210 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_FROM, from);
211 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_TO, to);
212 : }
213 0 : }
214 :
215 :
216 : void
217 0 : DataHandler::parseTAZRelationData(const SUMOSAXAttributes& attrs) {
218 : // declare Ok Flag
219 0 : bool parsedOk = true;
220 : // needed attributes
221 0 : const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, "", parsedOk);
222 0 : const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, "", parsedOk);
223 : // fill attributes
224 0 : getAttributes(attrs, {SUMO_ATTR_FROM, SUMO_ATTR_TO});
225 : // continue if flag is ok
226 0 : if (parsedOk) {
227 : // set tag
228 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->setTag(SUMO_TAG_TAZREL);
229 : // add all attributes
230 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_FROM, from);
231 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addStringAttribute(SUMO_ATTR_TO, to);
232 : }
233 0 : }
234 :
235 :
236 : void
237 0 : DataHandler::getAttributes(const SUMOSAXAttributes& attrs, const std::vector<SumoXMLAttr> avoidAttributes) const {
238 : // transform avoidAttributes to strings
239 : std::vector<std::string> avoidAttributesStr;
240 0 : for (const SumoXMLAttr& avoidAttribute : avoidAttributes) {
241 0 : avoidAttributesStr.push_back(toString(avoidAttribute));
242 : }
243 : // iterate over attributes and fill parameters map
244 0 : for (const std::string& attribute : attrs.getAttributeNames()) {
245 0 : if (std::find(avoidAttributesStr.begin(), avoidAttributesStr.end(), attribute) == avoidAttributesStr.end()) {
246 0 : myCommonXMLStructure.getCurrentSumoBaseObject()->addParameter(attribute, attrs.getStringSecure(attribute, ""));
247 : }
248 0 : }
249 0 : }
250 :
251 :
252 : void
253 0 : DataHandler::checkParent(const SumoXMLTag currentTag, const SumoXMLTag parentTag, bool& ok) {
254 : // check that parent SUMOBaseObject's tag is the parentTag
255 0 : if ((myCommonXMLStructure.getCurrentSumoBaseObject()->getParentSumoBaseObject() &&
256 0 : (myCommonXMLStructure.getCurrentSumoBaseObject()->getParentSumoBaseObject()->getTag() == parentTag)) == false) {
257 0 : writeError(toString(currentTag) + " must be defined within the definition of a " + toString(parentTag));
258 0 : ok = false;
259 : }
260 0 : }
261 :
262 : /****************************************************************************/
|