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;
57 XMLPlatformUtils::Initialize();
59 }
catch (
const XERCES_CPP_NAMESPACE::XMLException& e) {
66XMLSubSys::setValidation(
const std::string& validationScheme,
const std::string& netValidationScheme,
const std::string& routeValidationScheme) {
67 if (validationScheme !=
"never" && validationScheme !=
"auto" && validationScheme !=
"always" && validationScheme !=
"local") {
68 throw ProcessError(
"Unknown xml validation scheme + '" + validationScheme +
"'.");
71 if (netValidationScheme !=
"never" && netValidationScheme !=
"auto" && netValidationScheme !=
"always" && netValidationScheme !=
"local") {
72 throw ProcessError(
"Unknown network validation scheme + '" + netValidationScheme +
"'.");
75 if (routeValidationScheme !=
"never" && routeValidationScheme !=
"auto" && routeValidationScheme !=
"always" && routeValidationScheme !=
"local") {
76 throw ProcessError(
"Unknown route validation scheme + '" + routeValidationScheme +
"'.");
83 myGrammarPool =
new XERCES_CPP_NAMESPACE::XMLGrammarPoolImpl(XMLPlatformUtils::fgMemoryManager);
84 SAX2XMLReader* parser(XMLReaderFactory::createXMLReader(XMLPlatformUtils::fgMemoryManager,
myGrammarPool));
85#if _XERCES_VERSION >= 30100
86 parser->setFeature(XERCES_CPP_NAMESPACE::XMLUni::fgXercesHandleMultipleImports,
true);
88 const char* sumoPath = std::getenv(
"SUMO_HOME");
90 bool needWarning =
true;
91 if (validationScheme ==
"local") {
92 WRITE_WARNING(
TL(
"Environment variable SUMO_HOME is not set properly, disabling XML validation. Set 'auto' or 'always' for web lookups."));
96 if (netValidationScheme ==
"local") {
98 WRITE_WARNING(
TL(
"Environment variable SUMO_HOME is not set properly, disabling XML validation. Set 'auto' or 'always' for web lookups."));
103 if (routeValidationScheme ==
"local") {
105 WRITE_WARNING(
TL(
"Environment variable SUMO_HOME is not set properly, disabling XML validation. Set 'auto' or 'always' for web lookups."));
111 WRITE_WARNING(
TL(
"Environment variable SUMO_HOME is not set properly, XML validation will fail or use slow website lookups."));
115 for (
const char*
const& filetype : {
116 "additional",
"routes",
"net"
118 const std::string file = sumoPath + std::string(
"/data/xsd/") + filetype +
"_file.xsd";
119 if (!parser->loadGrammar(file.c_str(), XERCES_CPP_NAMESPACE::Grammar::SchemaGrammarType,
true)) {
129 for (std::vector<SUMOSAXReader*>::iterator i =
myReaders.begin(); i !=
myReaders.end(); ++i) {
135 XMLPlatformUtils::Terminate();
158 const bool isNet,
const bool isRoute,
const bool isExternal,
const bool catchExceptions) {
160 std::string errorMsg =
"";
166 if (isExternal && validationScheme ==
"local") {
167 WRITE_MESSAGEF(
TL(
"Disabling XML validation for external file '%'. Use 'auto' or 'always' to enable."), file);
168 validationScheme =
"never";
183 if (catchExceptions) {
184 errorMsg = std::string(e.what()) != std::string(
"") ? e.what() :
TL(
"Process Error");
188 }
catch (
const std::runtime_error& re) {
189 errorMsg =
TLF(
"Runtime error: % while parsing '%'", re.what(), file);
190 }
catch (
const std::exception& ex) {
191 errorMsg =
TLF(
"Error occurred: % while parsing '%'", ex.what(), file);
192 }
catch (
const XERCES_CPP_NAMESPACE::SAXException& e) {
195 errorMsg =
TLF(
"Unspecified error occurred wile parsing '%'", file);
197 if (errorMsg !=
"") {
198 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 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 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, 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 std::string myNetValidationScheme
Information whether built reader/parser shall validate SUMO networks against schemata.