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.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 /****************************************************************************/
22 // Perfoms network import
23 /****************************************************************************/
24 #include <config.h>
25 
26 #include <string>
30 #include <utils/options/Option.h>
33 #include <utils/common/ToString.h>
37 #include <utils/xml/XMLSubSys.h>
38 #include "NITypeLoader.h"
39 
40 
41 bool
42 NITypeLoader::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_ERRORF(TL("Could not open %-file '%'."), type, 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  if (raise) {
79  throw ProcessError(exceptMsg + "The " + type + " could not be loaded from '" + fileName + "'.");
80  }
81  return ok;
82 }
83 
84 
85 /****************************************************************************/
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:305
#define TL(string)
Definition: MsgHandler.h:315
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:300
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:299
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:146
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:132
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false, const bool isRoute=false, const bool isExternal=false, const bool catchExceptions=true)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:148