25#include <xercesc/util/PlatformUtils.hpp>
26#include <xercesc/util/TransService.hpp>
27#include <xercesc/util/TranscodingException.hpp>
28#include <xercesc/util/XMLString.hpp>
29#include <xercesc/sax2/XMLReaderFactory.hpp>
30#include <xercesc/framework/XMLGrammarPoolImpl.hpp>
39using XERCES_CPP_NAMESPACE::SAX2XMLReader;
40using XERCES_CPP_NAMESPACE::XMLPlatformUtils;
41using XERCES_CPP_NAMESPACE::XMLReaderFactory;
62 XMLPlatformUtils::Initialize();
64 }
catch (
const XERCES_CPP_NAMESPACE::XMLException& e) {
72 if (newScheme !=
"never" && newScheme !=
"auto" && newScheme !=
"always" && newScheme !=
"local") {
73 throw ProcessError(
"Unknown xml validation scheme + '" + newScheme +
"'.");
75 if (!haveSUMO_HOME && newScheme ==
"local") {
77 WRITE_WARNING(
TL(
"Environment variable SUMO_HOME is not set properly, disabling XML validation. Set 'auto' or 'always' for web lookups."));
87XMLSubSys::setValidation(
const std::string& validationScheme,
const std::string& netValidationScheme,
const std::string& routeValidationScheme) {
88 const char* sumoPath = std::getenv(
"SUMO_HOME");
89 const bool haveSUMO_HOME = sumoPath !=
nullptr &&
FileHelpers::isReadable(sumoPath + std::string(
"/data/xsd/net_file.xsd"));
99 WRITE_WARNING(
TL(
"Environment variable SUMO_HOME is not set properly, XML validation will fail or use slow website lookups."));
104 myGrammarPool =
new XERCES_CPP_NAMESPACE::XMLGrammarPoolImpl(XMLPlatformUtils::fgMemoryManager);
105 SAX2XMLReader* parser(XMLReaderFactory::createXMLReader(XMLPlatformUtils::fgMemoryManager,
myGrammarPool));
106#if _XERCES_VERSION >= 30100
107 parser->setFeature(XERCES_CPP_NAMESPACE::XMLUni::fgXercesHandleMultipleImports,
true);
109 for (
const char*
const& filetype : {
110 "additional",
"routes",
"net"
112 const std::string file = sumoPath + std::string(
"/data/xsd/") + filetype +
"_file.xsd";
113 if (!parser->loadGrammar(file.c_str(), XERCES_CPP_NAMESPACE::Grammar::SchemaGrammarType,
true)) {
124 for (std::vector<SUMOSAXReader*>::iterator i =
myReaders.begin(); i !=
myReaders.end(); ++i) {
130 XMLPlatformUtils::Terminate();
152 const bool isNet,
const bool isRoute,
const bool isExternal,
const bool catchExceptions) {
154 std::string errorMsg =
"";
160 if (isExternal && validationScheme ==
"local") {
161 WRITE_MESSAGEF(
TL(
"Disabling XML validation for external file '%'. Use 'auto' or 'always' to enable."), file);
162 validationScheme =
"never";
177 if (catchExceptions) {
178 errorMsg = std::string(e.what()) != std::string(
"") ? e.what() :
TL(
"Process Error");
182 }
catch (
const std::runtime_error& re) {
183 errorMsg =
TLF(
"Runtime error: % while parsing '%'", re.what(), file);
184 }
catch (
const std::exception& ex) {
185 errorMsg =
TLF(
"Error occurred: % while parsing '%'", ex.what(), file);
186 }
catch (
const XERCES_CPP_NAMESPACE::SAXException& e) {
189 errorMsg =
TLF(
"Unspecified error occurred while parsing '%'", file);
191 if (errorMsg !=
"") {
192 if (catchExceptions) {
208 length = (int)XERCES_CPP_NAMESPACE::XMLString::stringLen(data);
215 ascii.reserve(length);
217 for (; i < length && data[i] != 0 && data[i] < 0x80; i++) {
218 ascii += (char)data[i];
220 if (i == length || data[i] == 0) {
223#if _XERCES_VERSION < 30100
224 char* t = XERCES_CPP_NAMESPACE::XMLString::transcode(data);
225 std::string result(t);
226 XERCES_CPP_NAMESPACE::XMLString::release(&t);
230 XERCES_CPP_NAMESPACE::TranscodeToStr utf8(data,
"UTF-8");
231 return reinterpret_cast<const char*
>(utf8.str());
232 }
catch (XERCES_CPP_NAMESPACE::TranscodingException&) {
#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 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 std::string transcode(const XMLCh *const data, int length=-1)
converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8
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.