27#include <xercesc/sax2/XMLReaderFactory.hpp>
28#include <xercesc/framework/LocalFileInputSource.hpp>
29#include <xercesc/framework/MemBufInputSource.hpp>
42using XERCES_CPP_NAMESPACE::SAX2XMLReader;
43using XERCES_CPP_NAMESPACE::XMLUni;
52 myValidationScheme(validationScheme),
53 myGrammarPool(grammarPool),
56 myInputStream(nullptr),
57 mySchemaResolver(true, false),
58 myLocalResolver(false, false),
59 myNoOpResolver(false, true),
60 myNextSection(-1, nullptr) {
87 if (validationScheme ==
"") {
91 if (validationScheme ==
"never") {
93 myXMLReader->setProperty(XMLUni::fgXercesScannerName, (
void*)XMLUni::fgWFXMLScanner);
96 myXMLReader->setProperty(XMLUni::fgXercesScannerName, (
void*)XMLUni::fgIGXMLScanner);
97 myXMLReader->setFeature(XMLUni::fgXercesSchema,
true);
98 myXMLReader->setFeature(XMLUni::fgSAX2CoreValidation,
true);
99 myXMLReader->setFeature(XMLUni::fgXercesDynamic, validationScheme ==
"local" || validationScheme ==
"auto");
110 throw IOError(
TLF(
"Cannot read file '%'!", systemID));
113 throw IOError(
TLF(
"File '%' is a directory!", systemID));
128 XERCES_CPP_NAMESPACE::MemBufInputSource memBufIS((
const XMLByte*)content.c_str(), content.size(),
"registrySettings");
136 throw IOError(
TLF(
"Cannot read file '%'!", systemID));
139 throw IOError(
TLF(
"File '%' is a directory!", systemID));
142 myToken = XERCES_CPP_NAMESPACE::XMLPScanToken();
167 bool started =
false;
194 myXMLReader = XERCES_CPP_NAMESPACE::XMLReaderFactory::createXMLReader(XERCES_CPP_NAMESPACE::XMLPlatformUtils::fgMemoryManager,
myGrammarPool);
206 myHaveFallback(haveFallback),
211XERCES_CPP_NAMESPACE::InputSource*
214 return new XERCES_CPP_NAMESPACE::MemBufInputSource((
const XMLByte*)
"", 0,
"");
217 const std::string::size_type pos = url.find(
"/xsd/");
218 if (pos != std::string::npos) {
219 const char* sumoPath = std::getenv(
"SUMO_HOME");
221 if (sumoPath !=
nullptr) {
222 const std::string file = sumoPath + std::string(
"/data") + url.substr(pos);
224 XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(file.c_str());
225 XERCES_CPP_NAMESPACE::InputSource*
const result =
new XERCES_CPP_NAMESPACE::LocalFileInputSource(t);
226 XERCES_CPP_NAMESPACE::XMLString::release(&t);
229 WRITE_WARNING(
"Cannot read local schema '" + file + (myHaveFallback ?
"', will try website lookup." :
"', XML validation will fail."));
236 return new XERCES_CPP_NAMESPACE::MemBufInputSource((
const XMLByte*)
"", 0,
"");
#define WRITE_WARNINGF(...)
#define WRITE_WARNING(msg)
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
static bool isReadable(std::string path)
Checks whether the given file is readable.
static bool isDirectory(std::string path)
Checks whether the given file is a directory.
A handler which converts occurring elements and attributes into enums.
bool sectionFinished() const
void setSection(const int element, const bool seen)
std::pair< int, SUMOSAXAttributes * > retrieveNextSectionStart()
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Callback method for an opening tag to implement by derived classes.
LocalSchemaResolver(const bool haveFallback, const bool noOp)
constructor
XERCES_CPP_NAMESPACE::InputSource * resolveEntity(const XMLCh *const publicId, const XMLCh *const systemId)
resolve entity
std::unique_ptr< IStreamInputSource > myInputStream
input stream
std::string myValidationScheme
Information whether built reader/parser shall validate XML-documents against schemata.
SUMOSAXReader(GenericSAXHandler &handler, const std::string &validationScheme, XERCES_CPP_NAMESPACE::XMLGrammarPool *grammarPool)
Constructor.
std::pair< int, SUMOSAXAttributes * > myNextSection
next section
void setHandler(GenericSAXHandler &handler)
Sets the given handler as content and error handler for the reader.
void parseString(std::string content)
Parse XML from the given string.
LocalSchemaResolver myLocalResolver
local resolver
bool parseFirst(std::string systemID)
Start parsing the given file using parseFirst of myXMLReader.
std::unique_ptr< std::istream > myIStream
istream
LocalSchemaResolver mySchemaResolver
schema resolver
bool parseSection(SumoXMLTag element)
Continue a progressive parse started by parseFirst until the given element is encountered.
void setValidation(std::string validationScheme="")
Sets a new validation scheme and applies the validation settings to the XML reader.
~SUMOSAXReader()
Destructor.
GenericSAXHandler * myHandler
generic SAX Handler
void parse(std::string systemID)
Parse the given file completely by calling parse of myXMLReader.
XERCES_CPP_NAMESPACE::SAX2XMLReader * myXMLReader
XML reader.
LocalSchemaResolver myNoOpResolver
no operation resolver
XERCES_CPP_NAMESPACE::XMLGrammarPool * myGrammarPool
Schema cache to be used for grammars which are not declared.
XERCES_CPP_NAMESPACE::XMLPScanToken myToken
token
void ensureSAXReader()
Builds a reader, if needed.
bool parseNext()
Continue a progressive parse started by parseFirst.
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 std::string transcodeToLocal(const std::string &utf8String)
convert a string from UTF-8 to the local codepage