5#include "xmlBase/XmlParser.h"
6#include "xmlBase/EResolver.h"
7#include "xmlBase/Dom.h"
8#include "facilities/Util.h"
9#include <xercesc/framework/LocalFileInputSource.hpp>
10#include <xercesc/framework/MemBufInputSource.hpp>
11#include <xercesc/util/XMLString.hpp>
12#include <xercesc/framework/XMLValidator.hpp>
13#include <xercesc/util/PlatformUtils.hpp>
16 XERCES_CPP_NAMESPACE_USE
17 bool checkDocType(
const DOMDocument* doc,
const std::string docType) {
19 const DOMDocumentType* typeDecl = doc->getDoctype();
21 const XMLCh* name = typeDecl->getName();
22 XMLCh* transDocType = XMLString::transcode(docType.c_str());
23 ret = XMLString::equals(name, transDocType);
24 XMLString::release(&transDocType);
34 XERCES_CPP_NAMESPACE_USE
35 int XmlParser::didInit = 0;
43 XMLPlatformUtils::Initialize();
45 catch(
const XMLException& toCatch)
47 char* charMsg = XMLString::transcode(toCatch.getMessage());
48 std::string msg = std::string(charMsg);
49 XMLString::release(&charMsg);
51 std::string errMsg(
"Error during Xerces-c Initialization: \n");
52 errMsg +=
" Exception message: ";
57 std::cerr << errMsg << std::endl;
63 m_parser =
new XercesDOMParser();
69 m_parser->setValidationScheme(AbstractDOMParser::Val_Auto);
77 m_parser->setXMLEntityResolver(m_resolver);
78 m_parser->setErrorHandler(m_errorHandler);
84 m_parser->setCreateEntityReferenceNodes(
false);
90 m_parser->setIncludeIgnorableWhitespace(
false);
96 m_parser->setDoNamespaces(doit);
97 m_parser->setDoSchema(doit);
98 m_parser->setValidationSchemaFullChecking(doit);
102 delete m_errorHandler;
109 const std::string& docType) {
110 XERCES_CPP_NAMESPACE_USE
112 m_errorsOccurred =
false;
116 std::string fname(filename);
119 std::string errMsg(
"xmlBase::XmlParser::parse ");
120 errMsg +=
"Filename arg. contained untranslatable env. variables";
125 std::cerr << errMsg << std::endl;
133 XMLCh* filenameXMLCh = XMLString::transcode(fname.c_str());
134 LocalFileInputSource fileSource(filenameXMLCh);
135 m_parser->parse(fileSource);
136 XMLString::release(&filenameXMLCh);
138 catch (
const XMLException& e) {
139 char* charMsg = XMLString::transcode(e.getMessage());
140 std::string msg = std::string(charMsg);
141 XMLString::release(&charMsg);
142 std::string errMsg(
"xmlBase::XmlParser::parse ");
143 errMsg +=
"Error occurred while parsing file ";
145 m_errorsOccurred =
true;
153 std::cerr << errMsg << std::endl
154 <<
"Message: " << msg << std::endl;
165 if (docType != std::string(
"")) {
166 bool ok = checkDocType(m_parser->getDocument(), docType);
168 if (ok)
return m_parser->getDocument();
175 return m_parser->getDocument();
181 const std::string& docType) {
182 XERCES_CPP_NAMESPACE_USE
185 m_errorsOccurred =
false;
188 XMLCh* buffer = XMLString::transcode(input.c_str());
191 unsigned int byteCount =
sizeof(XMLCh) * input.length();
193 MemBufInputSource source((
const unsigned char*)buffer, byteCount,
195 m_parser->parse(source);
196 XMLString::release(&buffer);
198 catch (
const XMLException& e) {
199 char* charMsg = XMLString::transcode(e.getMessage());
200 std::string msg = std::string(charMsg);
201 XMLString::release(&charMsg);
202 m_errorsOccurred =
true;
205 std::string errMsg(
"xmlBase::XmlParser::parse ");
207 "An error occurred while parsing MemBufInputSource\n Message: ";
211 std::cerr << errMsg << std::endl;
216 "An error occurred while parsing MemBufInputSource\n Message: " <<
227 if (docType != std::string(
"")) {
228 bool ok = checkDocType(m_parser->getDocument(), docType);
230 if (ok)
return m_parser->getDocument();
237 return m_parser->getDocument();
static int expandEnvVar(std::string *toExpand, const std::string &openDel=std::string("$("), const std::string &closeDel=std::string(")"))
Exception class for XmlParser, XmlErrorHandler.
int getFatalCount() const
int getErrorCount() const
DOMDocument * parse(const char *const filename, const std::string &docType=std::string(""))
Parse an xml file, returning document node if successful.
void doSchema(bool doit)
Call this method to turn on schema processing (else it's off)
XmlParser(bool throwErrors=false)