Skip to content

Commit

Permalink
fixing xml validation without SUMO_HOME fix #14830
Browse files Browse the repository at this point in the history
  • Loading branch information
behrisch committed May 17, 2024
1 parent bdcd545 commit 922c8ef
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 39 deletions.
1 change: 1 addition & 0 deletions src/utils/xml/SUMOSAXReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,5 @@ SUMOSAXReader::LocalSchemaResolver::resolveEntity(const XMLCh* const /* publicId
return new XERCES_CPP_NAMESPACE::MemBufInputSource((const XMLByte*)"", 0, "");
}


/****************************************************************************/
69 changes: 30 additions & 39 deletions src/utils/xml/XMLSubSys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ std::string XMLSubSys::myValidationScheme = "local";
std::string XMLSubSys::myNetValidationScheme = "local";
std::string XMLSubSys::myRouteValidationScheme = "local";
XERCES_CPP_NAMESPACE::XMLGrammarPool* XMLSubSys::myGrammarPool = nullptr;
bool XMLSubSys::myNeedsValidationWarning = true;


// ===========================================================================
Expand All @@ -62,56 +63,45 @@ XMLSubSys::init() {
}


void
XMLSubSys::setValidation(const std::string& validationScheme, const std::string& netValidationScheme, const std::string& routeValidationScheme) {
if (validationScheme != "never" && validationScheme != "auto" && validationScheme != "always" && validationScheme != "local") {
throw ProcessError("Unknown xml validation scheme + '" + validationScheme + "'.");
}
myValidationScheme = validationScheme;
if (netValidationScheme != "never" && netValidationScheme != "auto" && netValidationScheme != "always" && netValidationScheme != "local") {
throw ProcessError("Unknown network validation scheme + '" + netValidationScheme + "'.");
std::string
XMLSubSys::warnLocalScheme(const std::string& newScheme, const bool haveSUMO_HOME) {
if (newScheme != "never" && newScheme != "auto" && newScheme != "always" && newScheme != "local") {
throw ProcessError("Unknown xml validation scheme + '" + newScheme + "'.");
}
myNetValidationScheme = netValidationScheme;
if (routeValidationScheme != "never" && routeValidationScheme != "auto" && routeValidationScheme != "always" && routeValidationScheme != "local") {
throw ProcessError("Unknown route validation scheme + '" + routeValidationScheme + "'.");
if (!haveSUMO_HOME && newScheme == "local") {
if (myNeedsValidationWarning) {
WRITE_WARNING(TL("Environment variable SUMO_HOME is not set properly, disabling XML validation. Set 'auto' or 'always' for web lookups."));
myNeedsValidationWarning = false;
}
return "never";
}
myRouteValidationScheme = routeValidationScheme;
return newScheme;
}


void
XMLSubSys::setValidation(const std::string& validationScheme, const std::string& netValidationScheme, const std::string& routeValidationScheme) {
const char* sumoPath = std::getenv("SUMO_HOME");
const bool haveSUMO_HOME = sumoPath != nullptr && FileHelpers::isReadable(sumoPath + std::string("/data/xsd/net_file.xsd"));
myValidationScheme = warnLocalScheme(validationScheme, haveSUMO_HOME);
myNetValidationScheme = warnLocalScheme(netValidationScheme, haveSUMO_HOME);
myRouteValidationScheme = warnLocalScheme(routeValidationScheme, haveSUMO_HOME);
if (myGrammarPool == nullptr &&
(myValidationScheme != "never" ||
myNetValidationScheme != "never" ||
myRouteValidationScheme != "never")) {
if (!haveSUMO_HOME) {
if (myNeedsValidationWarning) {
WRITE_WARNING(TL("Environment variable SUMO_HOME is not set properly, XML validation will fail or use slow website lookups."));
myNeedsValidationWarning = false;
}
return;
}
myGrammarPool = new XERCES_CPP_NAMESPACE::XMLGrammarPoolImpl(XMLPlatformUtils::fgMemoryManager);
SAX2XMLReader* parser(XMLReaderFactory::createXMLReader(XMLPlatformUtils::fgMemoryManager, myGrammarPool));
#if _XERCES_VERSION >= 30100
parser->setFeature(XERCES_CPP_NAMESPACE::XMLUni::fgXercesHandleMultipleImports, true);
#endif
const char* sumoPath = std::getenv("SUMO_HOME");
if (sumoPath == nullptr || !FileHelpers::isReadable(sumoPath + std::string("/data/xsd/net_file.xsd"))) {
bool needWarning = true;
if (validationScheme == "local") {
WRITE_WARNING(TL("Environment variable SUMO_HOME is not set properly, disabling XML validation. Set 'auto' or 'always' for web lookups."));
needWarning = false;
myValidationScheme = "never";
}
if (netValidationScheme == "local") {
if (needWarning) {
WRITE_WARNING(TL("Environment variable SUMO_HOME is not set properly, disabling XML validation. Set 'auto' or 'always' for web lookups."));
needWarning = false;
}
myNetValidationScheme = "never";
}
if (routeValidationScheme == "local") {
if (needWarning) {
WRITE_WARNING(TL("Environment variable SUMO_HOME is not set properly, disabling XML validation. Set 'auto' or 'always' for web lookups."));
needWarning = false;
}
myRouteValidationScheme = "never";
}
if (needWarning) {
WRITE_WARNING(TL("Environment variable SUMO_HOME is not set properly, XML validation will fail or use slow website lookups."));
}
return;
}
for (const char* const& filetype : {
"additional", "routes", "net"
}) {
Expand All @@ -120,6 +110,7 @@ XMLSubSys::setValidation(const std::string& validationScheme, const std::string&
WRITE_WARNINGF(TL("Cannot read local schema '%'."), file);
}
}
delete parser;
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/utils/xml/XMLSubSys.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ class XMLSubSys {
const bool isExternal = false, const bool catchExceptions = true);


private:
static std::string warnLocalScheme(const std::string& newScheme, const bool haveSUMO_HOME);

private:
/// @brief The XML Readers used for repeated parsing
static std::vector<SUMOSAXReader*> myReaders;
Expand All @@ -163,4 +166,7 @@ class XMLSubSys {
/// @brief Schema cache to be used for grammars which are not declared
static XERCES_CPP_NAMESPACE::XMLGrammarPool* myGrammarPool;

/// @brief Whether a warning about missing SUMO_HOME should be emitted
static bool myNeedsValidationWarning;

};

0 comments on commit 922c8ef

Please sign in to comment.