25#include <xercesc/util/PlatformUtils.hpp>
26#include <xercesc/sax2/XMLReaderFactory.hpp>
27#include <xercesc/framework/XMLGrammarPoolImpl.hpp>
34using XERCES_CPP_NAMESPACE::SAX2XMLReader;
35using XERCES_CPP_NAMESPACE::XMLPlatformUtils;
36using XERCES_CPP_NAMESPACE::XMLReaderFactory;
56 XMLPlatformUtils::Initialize();
58 }
catch (
const XERCES_CPP_NAMESPACE::XMLException& e) {
65XMLSubSys::setValidation(
const std::string& validationScheme,
const std::string& netValidationScheme,
const std::string& routeValidationScheme) {
66 if (validationScheme !=
"never" && validationScheme !=
"auto" && validationScheme !=
"always" && validationScheme !=
"local") {
67 throw ProcessError(
"Unknown xml validation scheme + '" + validationScheme +
"'.");
70 if (netValidationScheme !=
"never" && netValidationScheme !=
"auto" && netValidationScheme !=
"always" && netValidationScheme !=
"local") {
71 throw ProcessError(
"Unknown network validation scheme + '" + netValidationScheme +
"'.");
74 if (routeValidationScheme !=
"never" && routeValidationScheme !=
"auto" && routeValidationScheme !=
"always" && routeValidationScheme !=
"local") {
75 throw ProcessError(
"Unknown route validation scheme + '" + routeValidationScheme +
"'.");
82 myGrammarPool =
new XERCES_CPP_NAMESPACE::XMLGrammarPoolImpl(XMLPlatformUtils::fgMemoryManager);
83 SAX2XMLReader* parser(XMLReaderFactory::createXMLReader(XMLPlatformUtils::fgMemoryManager,
myGrammarPool));
84#if _XERCES_VERSION >= 30100
85 parser->setFeature(XERCES_CPP_NAMESPACE::XMLUni::fgXercesHandleMultipleImports,
true);
87 const char* sumoPath = std::getenv(
"SUMO_HOME");
88 if (sumoPath ==
nullptr) {
89 WRITE_WARNING(
TL(
"Environment variable SUMO_HOME is not set, XML validation will fail or use slow website lookups."));
93 throw ProcessError(
"SUMO_HOME looks like an URL, aborting to avoid inadvertent network access.");
95 for (
const char*
const& filetype : {
96 "additional",
"routes",
"net"
98 const std::string file = sumoPath + std::string(
"/data/xsd/") + filetype +
"_file.xsd";
99 if (!parser->loadGrammar(file.c_str(), XERCES_CPP_NAMESPACE::Grammar::SchemaGrammarType,
true)) {
109 for (std::vector<SUMOSAXReader*>::iterator i =
myReaders.begin(); i !=
myReaders.end(); ++i) {
115 XMLPlatformUtils::Terminate();
138 const bool isNet,
const bool isRoute) {
158 WRITE_ERROR(std::string(e.what()) != std::string(
"") ? std::string(e.what()) : std::string(
"Process Error"));
160 }
catch (
const std::runtime_error& re) {
161 WRITE_ERROR(
"Runtime error: " + std::string(re.what()) +
" while parsing '" + file +
"'");
163 }
catch (
const std::exception& ex) {
164 WRITE_ERROR(
"Error occurred: " + std::string(ex.what()) +
" while parsing '" + file +
"'");
167 WRITE_ERROR(
"Unspecified error occurred wile parsing '" + file +
"'");
#define WRITE_WARNING(msg)
A handler which converts occuring elements and attributes into enums.
void setFileName(const std::string &name)
Sets the current file name.
const std::string & getFileName() const
returns the current file name
bool wasInformed() const
Returns the information whether any messages were added.
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
virtual void clear(bool resetInformed=true)
Clears information whether an error occurred previously and print aggregated message summary.
SAX-handler base for SUMO-files.
SAX-reader encapsulation containing binary reader.
static void resetTranscoder()
must be called when shutting down the xml subsystem
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
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
static int myNextFreeReader
Information whether the reader is parsing.
static std::vector< SUMOSAXReader * > myReaders
The XML Readers used for repeated parsing.
static void setHandler(GenericSAXHandler &handler)
Sets the given handler for the default reader.
static std::string myValidationScheme
Information whether built reader/parser shall validate XML-documents against schemata.
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme, const std::string &routeValidationScheme)
Enables or disables validation.
static XERCES_CPP_NAMESPACE::XMLGrammarPool * myGrammarPool
Schema cache to be used for grammars which are not declared.
static SUMOSAXReader * getSAXReader(SUMOSAXHandler &handler, const bool isNet=false, const bool isRoute=false)
Builds a reader and assigns the handler to it.
static void close()
Closes the xml-subsystem.
static void init()
Initialises the xml-subsystem.
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.
static std::string myRouteValidationScheme
Information whether built reader/parser shall validate SUMO routes against schemata.
static std::string myNetValidationScheme
Information whether built reader/parser shall validate SUMO networks against schemata.