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-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 /****************************************************************************/
18 // Perfoms network import
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <string>
31 #include <utils/options/Option.h>
34 #include <utils/common/ToString.h>
38 #include <utils/xml/XMLSubSys.h>
39 #include "NITypeLoader.h"
40 
41 
42 void
43 NITypeLoader::load(SUMOSAXHandler* handler, const std::vector<std::string>& files,
44  const std::string& type, const bool stringParse) {
45  // build parser
46  std::string exceptMsg = "";
47  // start the parsing
48  try {
49  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
50  if (stringParse) {
51  handler->setFileName("built in type map");
52  SUMOSAXReader* reader = XMLSubSys::getSAXReader(*handler);
53  reader->parseString(*file);
54  delete reader;
55  continue;
56  }
57  if (!FileHelpers::isReadable(*file)) {
58  WRITE_ERROR("Could not open " + type + "-file '" + *file + "'.");
59  exceptMsg = "Process Error";
60  continue;
61  }
62  PROGRESS_BEGIN_MESSAGE("Parsing " + type + " from '" + *file + "'");
63  XMLSubSys::runParser(*handler, *file);
65  }
66  } catch (const XERCES_CPP_NAMESPACE::XMLException& toCatch) {
67  exceptMsg = StringUtils::transcode(toCatch.getMessage())
68  + "\n The " + type + " could not be loaded from '" + handler->getFileName() + "'.";
69  } catch (const ProcessError& toCatch) {
70  exceptMsg =
71  std::string(toCatch.what()) + "\n The " + type + " could not be loaded from '" + handler->getFileName() + "'.";
72  } catch (...) {
73  exceptMsg = "The " + type + " could not be loaded from '" + handler->getFileName() + "'.";
74  }
75  delete handler;
76  if (exceptMsg != "") {
77  throw ProcessError(exceptMsg);
78  }
79 }
80 
81 /****************************************************************************/
ToString.h
XMLSubSys::runParser
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:112
SUMOSAXHandler
SAX-handler base for SUMO-files.
Definition: SUMOSAXHandler.h:41
OptionsCont.h
MsgHandler.h
SUMOSAXReader
SAX-reader encapsulation containing binary reader.
Definition: SUMOSAXReader.h:55
SUMOSAXHandler.h
NITypeLoader::load
static void load(SUMOSAXHandler *handler, const std::vector< std::string > &files, const std::string &type, const bool stringParse=false)
Definition: NITypeLoader.cpp:43
FileHelpers.h
NITypeLoader.h
ProcessError
Definition: UtilExceptions.h:39
UtilExceptions.h
SUMOSAXReader::parseString
void parseString(std::string content)
Definition: SUMOSAXReader.cpp:117
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:136
PROGRESS_BEGIN_MESSAGE
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:278
Option.h
GenericSAXHandler::getFileName
const std::string & getFileName() const
returns the current file name
Definition: GenericSAXHandler.cpp:74
PROGRESS_DONE_MESSAGE
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:279
FileHelpers::isReadable
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:49
config.h
SUMOSAXReader.h
XMLSubSys::getSAXReader
static SUMOSAXReader * getSAXReader(SUMOSAXHandler &handler)
Builds a reader and assigns the handler to it.
Definition: XMLSubSys.cpp:100
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
GenericSAXHandler::setFileName
void setFileName(const std::string &name)
Sets the current file name.
Definition: GenericSAXHandler.cpp:68
XMLSubSys.h