Eclipse SUMO - Simulation of Urban MObility
NITypeLoader.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/****************************************************************************/
22// Perfoms network import
23/****************************************************************************/
24#include <config.h>
25
26#include <string>
37#include <utils/xml/XMLSubSys.h>
38#include "NITypeLoader.h"
39
40
41bool
42NITypeLoader::load(SUMOSAXHandler* handler, const std::vector<std::string>& files,
43 const std::string& type, const bool stringParse) {
44 // build parser
45 std::string exceptMsg = "";
46 std::string fileName = "";
47 // start the parsing
48 bool ok = true;
49 bool raise = false;
50 try {
51 for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
52 fileName = *file;
53 if (stringParse) {
54 fileName = "built in type map";
55 handler->setFileName(fileName);
56 SUMOSAXReader* reader = XMLSubSys::getSAXReader(*handler);
57 reader->parseString(*file);
58 delete reader;
59 continue;
60 }
61 if (!FileHelpers::isReadable(fileName)) {
62 WRITE_ERROR("Could not open " + type + "-file '" + fileName + "'.");
63 return false;
64 }
65 PROGRESS_BEGIN_MESSAGE("Parsing " + type + " from '" + fileName + "'");
66 ok &= XMLSubSys::runParser(*handler, fileName);
68 }
69 } catch (const XERCES_CPP_NAMESPACE::XMLException& toCatch) {
70 exceptMsg = StringUtils::transcode(toCatch.getMessage()) + "\n ";
71 raise = true;
72 } catch (const ProcessError& toCatch) {
73 exceptMsg = std::string(toCatch.what()) + "\n ";
74 raise = true;
75 } catch (...) {
76 raise = true;
77 }
78 delete handler;
79 if (raise) {
80 throw ProcessError(exceptMsg + "The " + type + " could not be loaded from '" + fileName + "'.");
81 }
82 return ok;
83}
84
85
86/****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:274
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:270
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:269
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:51
void setFileName(const std::string &name)
Sets the current file name.
static bool load(SUMOSAXHandler *handler, const std::vector< std::string > &files, const std::string &type, const bool stringParse=false)
SAX-handler base for SUMO-files.
SAX-reader encapsulation containing binary reader.
Definition: SUMOSAXReader.h:53
void parseString(std::string content)
Parse XML from the given string.
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
static SUMOSAXReader * getSAXReader(SUMOSAXHandler &handler, const bool isNet=false, const bool isRoute=false)
Builds a reader and assigns the handler to it.
Definition: XMLSubSys.cpp:121
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