25#include <xercesc/util/PlatformUtils.hpp>
26#include <xercesc/sax2/XMLReaderFactory.hpp>
27#include <xercesc/framework/XMLGrammarPoolImpl.hpp>
35using XERCES_CPP_NAMESPACE::SAX2XMLReader;
36using XERCES_CPP_NAMESPACE::XMLPlatformUtils;
37using XERCES_CPP_NAMESPACE::XMLReaderFactory;
58 XMLPlatformUtils::Initialize();
60 }
catch (
const XERCES_CPP_NAMESPACE::XMLException& e) {
68 if (newScheme !=
"never" && newScheme !=
"auto" && newScheme !=
"always" && newScheme !=
"local") {
69 throw ProcessError(
"Unknown xml validation scheme + '" + newScheme +
"'.");
71 if (!haveSUMO_HOME && newScheme ==
"local") {
73 WRITE_WARNING(
TL(
"Environment variable SUMO_HOME is not set properly, disabling XML validation. Set 'auto' or 'always' for web lookups."));
83XMLSubSys::setValidation(
const std::string& validationScheme,
const std::string& netValidationScheme,
const std::string& routeValidationScheme) {
84 const char* sumoPath = std::getenv(
"SUMO_HOME");
85 const bool haveSUMO_HOME = sumoPath !=
nullptr &&
FileHelpers::isReadable(sumoPath + std::string(
"/data/xsd/net_file.xsd"));
95 WRITE_WARNING(
TL(
"Environment variable SUMO_HOME is not set properly, XML validation will fail or use slow website lookups."));
100 myGrammarPool =
new XERCES_CPP_NAMESPACE::XMLGrammarPoolImpl(XMLPlatformUtils::fgMemoryManager);
101 SAX2XMLReader* parser(XMLReaderFactory::createXMLReader(XMLPlatformUtils::fgMemoryManager,
myGrammarPool));
102#if _XERCES_VERSION >= 30100
103 parser->setFeature(XERCES_CPP_NAMESPACE::XMLUni::fgXercesHandleMultipleImports,
true);
105 for (
const char*
const& filetype : {
106 "additional",
"routes",
"net"
108 const std::string file = sumoPath + std::string(
"/data/xsd/") + filetype +
"_file.xsd";
109 if (!parser->loadGrammar(file.c_str(), XERCES_CPP_NAMESPACE::Grammar::SchemaGrammarType,
true)) {
120 for (std::vector<SUMOSAXReader*>::iterator i =
myReaders.begin(); i !=
myReaders.end(); ++i) {
126 XMLPlatformUtils::Terminate();
149 const bool isNet,
const bool isRoute,
const bool isExternal,
const bool catchExceptions) {
151 std::string errorMsg =
"";
157 if (isExternal && validationScheme ==
"local") {
158 WRITE_MESSAGEF(
TL(
"Disabling XML validation for external file '%'. Use 'auto' or 'always' to enable."), file);
159 validationScheme =
"never";
174 if (catchExceptions) {
175 errorMsg = std::string(e.what()) != std::string(
"") ? e.what() :
TL(
"Process Error");
179 }
catch (
const std::runtime_error& re) {
180 errorMsg =
TLF(
"Runtime error: % while parsing '%'", re.what(), file);
181 }
catch (
const std::exception& ex) {
182 errorMsg =
TLF(
"Error occurred: % while parsing '%'", ex.what(), file);
183 }
catch (
const XERCES_CPP_NAMESPACE::SAXException& e) {
186 errorMsg =
TLF(
"Unspecified error occurred while parsing '%'", file);
188 if (errorMsg !=
"") {
189 if (catchExceptions) {
#define WRITE_WARNINGF(...)
#define WRITE_MESSAGEF(...)
#define WRITE_WARNING(msg)
static bool isReadable(std::string path)
Checks whether the given file is readable.
A handler which converts occurring 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 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 std::string warnLocalScheme(const std::string &newScheme, const bool haveSUMO_HOME)
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.
static std::string myRouteValidationScheme
Information whether built reader/parser shall validate SUMO routes against schemata.
static bool myNeedsValidationWarning
Whether a warning about missing SUMO_HOME should be emitted.
static std::string myNetValidationScheme
Information whether built reader/parser shall validate SUMO networks against schemata.