1 #include <utl/Reader.h>
2 #include <utl/ReaderErrorReporter.h>
3 #include <utl/XercesUtil.h>
4 #include <utl/ErrorLogger.h>
5 #include <utl/AugerUnits.h>
6 #include <utl/AugerException.h>
8 #include <xercesc/dom/DOM.hpp>
9 #include <xercesc/dom/DOMImplementation.hpp>
10 #include <xercesc/dom/DOMImplementationLS.hpp>
11 #include <xercesc/util/PlatformUtils.hpp>
12 #include <xercesc/parsers/XercesDOMParser.hpp>
13 #include <xercesc/sax/HandlerBase.hpp>
14 #include <xercesc/framework/MemBufInputSource.hpp>
15 #include <xercesc/framework/MemBufFormatTarget.hpp>
17 #include <boost/filesystem.hpp>
19 using namespace xercesc;
25 ReaderErrorReporter* Reader::fgErrReporter =
nullptr;
27 bool Reader::fgInitialized =
false;
37 fTopBranch = Parse(name, validationType, Reader::eFromFile);
48 fTopBranch = Parse(inputString.
GetInputString(), validationType, Reader::eFromMemBuf);
67 XMLPlatformUtils::Initialize();
72 if (std::atexit(Terminate))
73 WARNING(
"Could not register Xerces cleanup at exit.");
81 INFO(
"Xerces memory cleanup.");
83 fgErrReporter =
nullptr;
84 XMLPlatformUtils::Terminate();
94 DOMDocument*
const doc = fTopBranch.GetDOMNode()->getOwnerDocument();
96 const XMLCh*
const uri = doc->getDocumentURI();
98 name =
XercesPtrC(XMLString::transcode(uri)).Get();
106 Reader::Parse(
const string& input,
110 XercesDOMParser* parser =
new XercesDOMParser;
112 parser->setDoSchema(
false);
113 parser->setValidationScheme(XercesDOMParser::Val_Never);
114 parser->setCreateEntityReferenceNodes(
false);
116 switch (validationType) {
118 parser->setValidationScheme(XercesDOMParser::Val_Auto);
121 parser->setDoSchema(
true);
122 parser->setValidationScheme(XercesDOMParser::Val_Always);
123 parser->setValidationSchemaFullChecking(
true);
124 parser->setDoNamespaces(
true);
127 parser->setValidationScheme(XercesDOMParser::Val_Never);
130 ERROR(
"You have selected an invalid validation mode");
135 parser->setErrorHandler(fgErrReporter);
139 if (inputType == eFromFile)
140 parser->parse(input.c_str());
142 const MemBufInputSource memSource((
const XMLByte*)input.c_str(),
143 input.size(),
"",
false);
144 parser->parse(memSource);
147 if (fgErrReporter->getSawErrors()) {
149 const DOMNamedNodeMap*
const topAtts =
150 parser->getDocument()->getDocumentElement()->getAttributes();
152 for (
unsigned int i = 0; i < topAtts->getLength(); ++i) {
154 const DOMNode*
const attribute = topAtts->item(i);
155 const string attName =
AsString(attribute->getNodeName());
157 const string postNs = attName.substr(attName.find_first_of(
":") + 1);
159 if (postNs ==
"noNamespaceSchemaLocation") {
161 const string validationFile =
AsString(attribute->getNodeValue());
165 msg <<
"Could not find the schema validation file \""
166 << validationFile <<
"\" ";
167 if (inputType == eFromFile)
168 msg <<
"which was requested the by configuration file \"" << input <<
"\".";
170 if (inputType == eFromFile)
171 msg <<
"Schema validation failed for file \"" << input <<
"\". ";
173 msg <<
"Schema validation failed after parameter replacement";
175 msg <<
"The following errors were reported while parsing: "
176 << fgErrReporter->getMessages().str() <<
'\n';
177 if (inputType == eFromMemBuf) {
178 msg <<
"Here is the XML as it reads after parameter replacement:\n"
190 warn <<
"No schema (.xsd) document is available for ";
191 if (inputType == eFromFile)
192 warn <<
"file '" << input <<
"'. ";
194 warn <<
"in-memory string";
195 const int maxOutputSize = 200;
196 if (input.size() > maxOutputSize)
197 warn <<
" (truncated): \"" << input.substr(0, maxOutputSize) <<
"\". ";
199 warn <<
": \"" << input <<
"\". ";
201 warn <<
"It will not be validated!";
203 fgErrReporter->resetErrors();
206 }
catch (
const XMLException& e) {
209 msg <<
"An error occurred during parsing, message: "
218 const auto doc = parser->adoptDocument();
219 Branch branch(doc, doc->getDocumentElement());
228 return os << b.
String() <<
'\n';
Branch GetTopBranch() const
bool HasTopBranch() const
std::string String() const
Dump the branch into a string.
string AsString(DOMNode &n)
#define INFO(message)
Macro for logging informational messages.
ostream & operator<<(ostream &os, const StationTriggerInfo &info)
XercesPtr< char > XercesPtrC
Exception for errors encountered when parsing XML.
Class representing a document branch.
#define WARNING(message)
Macro for logging warning messages.
Exception for errors encountered when validating XML files.
Reports errors encountered during XML parsing.
#define ERROR(message)
Macro for logging error messages.