27 #include "../core/translator/Translator.h" 28 #include "../xml/Exception.h" 33 #include <xercesc/parsers/XercesDOMParser.hpp> 34 #include <xercesc/sax/SAXException.hpp> 35 #include <xercesc/sax2/XMLReaderFactory.hpp> 36 #include <xercesc/util/XMLException.hpp> 43 const bool doNamespace,
47 assert(xmlFile.empty() ==
false);
49 std::unique_ptr<xercesc::XercesDOMParser> parser(
new xercesc::XercesDOMParser());
50 std::unique_ptr<ErrorHandler> errHandler(
new ErrorHandler);
51 parser->setErrorHandler(errHandler.get());
53 xercesc::XercesDOMParser::ValSchemes valScheme = valid ? xercesc::XercesDOMParser::Val_Always : xercesc::XercesDOMParser::Val_Never;
57 parser->setDoNamespaces(doNamespace);
58 parser->setDoSchema(doSchema);
59 parser->setValidationScheme(valScheme);
60 parser->cacheGrammarFromParse(
true);
62 parser->setLoadExternalDTD(
false);
66 parser->parse(xmlFile.c_str());
68 catch(
const xercesc::SAXException& e)
70 std::string m =
TE_TR(
"Could not read the XML file: ");
72 m +=
TE_TR(
", due to the following error: ");
75 throw te::xml::Exception(m);
77 catch(
const xercesc::DOMException& e)
79 std::string m =
TE_TR(
"Could not read the XML file: ");
81 m +=
TE_TR(
", due to the following error: ");
84 throw te::xml::Exception(m);
86 catch(
const xercesc::XMLException& e)
88 std::string m =
TE_TR(
"Could not read the XML file: ");
90 m +=
TE_TR(
", due to the following error: ");
93 throw te::xml::Exception(m);
96 if(parser->getErrorCount() > 0)
98 std::string m =
TE_TR(
"Could not read the XML file: ");
100 m +=
", due to the following problem: ";
101 m += errHandler->getErrors();
103 throw te::xml::Exception(m);
107 xercesc::DOMDocument* doc = parser->adoptDocument();
110 throw te::xml::Exception(
TE_TR(
"Unable to read the XML document!"));
This class implements Xerces interface for error handlers.
#define TE_TR(message)
It marks a string in order to get translated.
TEXMLEXPORT xercesc::DOMDocument * readXML(const std::string &xmlFile, const bool doNamespace=false, const bool doSchema=false, const bool valid=false)
It reads the XML file using the DOM API.
This class implements Xerces interface for error handlers.
std::string ToString(const XMLCh *const value)
It converts the XML string to a standard C++ string.