28 return ::getenv (
"XMLDEBUG") != 0;
43 me.do_start (el, attr);
55 me.do_char_data (s, len);
61 me.do_default_handler (s, len);
72 const XML_Char* context,
74 const XML_Char* systemId,
76 void* user_data = XML_GetUserData (parser);
78 return (me.do_external_entity (parser, context, systemId));
84 std::cout <<
"ExpatCoreParser::register_external_entity> name=" << name
85 <<
" file_name=" << file_name << std::endl;
88 s_entities[name] = file_name;
94 std::cout <<
"ExpatCoreParser::register_text_entity> name=" << name
98 s_text_entities[name] = text;
103 const XML_Char* entityName,
104 int is_parameter_entity,
105 const XML_Char* value,
107 const XML_Char*
base,
108 const XML_Char* systemId,
109 const XML_Char* publicId,
112 if (!systemId) systemId =
"none";
113 if (!publicId) publicId =
"none";
114 std::string temp =
"none";
116 temp.assign (value, strnlen(value, value_length));
120 std::cout <<
"ExpatCoreParser::entity[" << 0 <<
"]> " << entityName;
121 std::cout <<
" is_parameter=" << is_parameter_entity;
122 std::cout <<
" value=[" << temp <<
"]";
123 std::cout <<
" base=[" <<
base <<
"]";
124 std::cout <<
" systemId=[" << systemId <<
"]";
125 std::cout <<
" publicId=[" << publicId <<
"]";
126 std::cout << std::endl;
130std::unique_ptr<XMLCoreNode>
132 return std::move(
m_top);
157 std::cout <<
"ExpatCoreParser::do_start> el=" << el <<
" top=" <<
m_top.get() <<
" last=" <<
m_last <<
" node=" <<
node << std::endl;
159 for (
int i = 0; attr[i]; i += 2) {
160 const char* name = attr[i];
161 const char* value = attr[i+1];
162 node->set_attrib (name, value);
171 std::cout <<
"ExpatCoreParser::do_end> el=" << el << std::endl;
178 if (len == 0)
return;
179 while ((len > 0) && (s[len-1] ==
'\n')) len--;
180 if (len == 0)
return;
181 std::string temp (s, len);
183 std::cout <<
"ExpatCoreParser::do_char_data> [" << temp <<
"]" << std::endl;
192 if (len == 0)
return;
193 while ((len > 0) && (s[len-1] ==
'\n')) len--;
194 if (len == 0)
return;
195 std::string temp (s, len);
197 std::cout <<
"ExpatCoreParser::do_default_handler> [" << temp <<
"]" << std::endl;
205 std::cout <<
"ExpatCoreParser::do_comment> s=" << s <<
" top=" <<
m_top.get() <<
" last=" <<
m_last <<
" node=" <<
node << std::endl;
215 std::cout <<
"ExpatCoreParser::generic_parse> file_name=" << file_name <<
" prefix=" <<
m_prefix << std::endl;
218 std::string temp_name = file_name;
219 docfd = fopen (temp_name.c_str (),
"r");
221 temp_name =
m_prefix +
"/" + file_name;
222 docfd = fopen (temp_name.c_str (),
"r");
226 const char* xmlpathenv = ::getenv (
"XMLPATH");
227 if (xmlpathenv == 0)
return (0);
228 std::string xmlpath = xmlpathenv;
229 std::string::size_type pos = 0;
230 while (pos != std::string::npos){
231 std::string::size_type sep = xmlpath.find (
":", pos);
232 if (sep == std::string::npos){
233 temp_name = xmlpath.substr (pos);
234 pos = std::string::npos;
236 temp_name = xmlpath.substr (pos, sep - pos);
239 if (temp_name.empty())
continue;
240 std::string last_temp_name = temp_name;
242 temp_name += file_name;
244 std::cout <<
"ExpatCoreParser::generic_parse> file_name=" << file_name
245 <<
" xmlpath=" << xmlpath
246 <<
" temp_name=" << temp_name
250 docfd = fopen (temp_name.c_str (),
"r");
251 if (docfd != 0)
break;
254 temp_name = std::move(last_temp_name);
258 temp_name += file_name;
260 std::cout <<
"ExpatCoreParser::generic_parse> file_name=" << file_name
261 <<
" xmlpath=" << xmlpath
262 <<
" temp_name=" << temp_name
265 docfd = fopen (temp_name.c_str (),
"r");
266 if (docfd != 0)
break;
275 XML_SetParamEntityParsing (p, XML_PARAM_ENTITY_PARSING_ALWAYS);
276 XML_SetElementHandler (p,
start,
end);
277 XML_SetCharacterDataHandler (p,
char_data);
279 XML_SetCommentHandler (p,
comment);
280 XML_SetUserData (p,
this);
282 std::cout <<
"ExpatCoreParser::generic_parse> starting" << std::endl;
286 char* buff = (
char*) XML_GetBuffer (p,
BUFFSIZE + 1);
287 items = fread (buff, 1,
BUFFSIZE, docfd);
289 std::cout <<
"Read error" << std::endl;
293 int done = feof (docfd);
295 if (XML_ParseBuffer (p,
BUFFSIZE, done) == XML_STATUS_ERROR) {
297 std::cout <<
"ExpatCoreParser::Parse error at line " << XML_GetCurrentLineNumber(p)
298 <<
" of file " << file_name
300 << XML_ErrorString (XML_GetErrorCode(p)) << std::endl;
314 std::cout <<
"ExpatCoreParser::generic_text_parse> " << std::endl;
317 XML_SetParamEntityParsing (p, XML_PARAM_ENTITY_PARSING_ALWAYS);
318 XML_SetElementHandler (p,
start,
end);
319 XML_SetCharacterDataHandler (p,
char_data);
321 XML_SetCommentHandler (p,
comment);
322 XML_SetUserData (p,
this);
324 std::cout <<
"ExpatCoreParser::generic_text_parse> starting" << std::endl;
327 char* buff = (
char*) XML_GetBuffer (p, text.size() + 1);
328 strcpy (buff, text.c_str ());
330 if (XML_ParseBuffer (p, text.size(), done) == XML_STATUS_ERROR) {
331 std::cout <<
"ExpatCoreParser::Parse error at line " << XML_GetCurrentLineNumber(p)
333 << XML_ErrorString (XML_GetErrorCode(p)) << std::endl;
343 const XML_Char* context,
344 const XML_Char* systemId){
345 std::string context_str;
346 if (context == 0) context_str =
"none";
347 else context_str = context;
352 if (replacement !=
"") {
354 std::cout <<
"ExpatCoreParser::do_external_entity> "
355 <<
" context=[" << context_str <<
"]"
356 <<
" replacement=[" << replacement <<
"]"
359 XML_Parser p = XML_ExternalEntityParserCreate (parser, context, NULL);
367 if (replacement ==
"NULL") {
370 if (replacement !=
"") {
371 systemId = replacement.c_str ();
375 std::cout <<
"ExpatCoreParser::do_external_entity> "
376 <<
" context=[" << context_str <<
"]"
377 <<
" systemId=[" << systemId <<
"]"
378 <<
" replacement=[" << replacement <<
"]"
381 XML_Parser p = XML_ExternalEntityParserCreate (parser, context, NULL);
391 ExternalEntityMap::const_iterator it = s_entities.find (name);
392 if (it == s_entities.end ()) {
393 static const std::string
empty;
396 return ((*it).second);
403 ExternalEntityMap::const_iterator it = s_text_entities.find (name);
404 if (it == s_text_entities.end ()) {
405 static const std::string
empty;
408 return ((*it).second);
412std::unique_ptr<XMLCoreNode>
414 std::string name = file_name;
415 std::string::size_type pos = file_name.rfind (
'/');
417 if (pos != std::string::npos){
418 prefix = file_name.substr (0, pos);
419 name = file_name.substr (pos + 1);
422 XML_Parser p = XML_ParserCreate (NULL);
424 std::cout <<
"ExpatCoreParser::Couldn't allocate memory for parser" << std::endl;
429 if (result == 0)
return nullptr;
434std::unique_ptr<XMLCoreNode>
437 XML_Parser p = XML_ParserCreate (NULL);
439 std::cout <<
"ExpatCoreParser::Couldn't allocate memory for parser" << std::endl;
444 if (result == 0)
return nullptr;
static const Attributes_t empty
static bool get_debug_state()
int generic_text_parse(XML_Parser p, const std::string &text)
void do_start(const char *el, const char **attr)
static void register_text_entity(const std::string &name, const std::string &text)
void do_default_handler(const XML_Char *s, int len)
static void entity(void *userData, const XML_Char *entityName, int is_parameter_entity, const XML_Char *value, int value_length, const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId, const XML_Char *)
XMLCoreNode * add_node(std::unique_ptr< XMLCoreNode > node)
static void char_data(void *, const XML_Char *s, int len)
static int external_entity(XML_Parser parser, const XML_Char *context, const XML_Char *, const XML_Char *systemId, const XML_Char *)
std::lock_guard< std::mutex > lock_t
int generic_parse(XML_Parser p, const std::string &file_name)
static void start(void *, const char *el, const char **attr)
static std::unique_ptr< XMLCoreNode > parse(const std::string &file_name)
void do_end(const char *el)
static void default_handler(void *, const XML_Char *s, int len)
std::unique_ptr< XMLCoreNode > get_document()
void do_comment(const XML_Char *s)
void do_char_data(const XML_Char *s, int len)
static const std::string & find_external_entity(const std::string &name)
static std::unique_ptr< XMLCoreNode > parse_string(const std::string &text)
static void register_external_entity(const std::string &name, const std::string &file_name)
std::unique_ptr< XMLCoreNode > m_top
static const std::string & find_text_entity(const std::string &name)
static void comment(void *, const XML_Char *s)
std::map< std::string, std::string > ExternalEntityMap
int do_external_entity(XML_Parser parser, const XML_Char *context, const XML_Char *systemId)
static void end(void *, const char *el)
ExpatCoreParser(const std::string &prefix)
static std::mutex s_mutex
Simple DOM-like node structure to hold the result of XML parsing.