ATLAS Offline Software
Loading...
Searching...
No Matches
ExpatCoreParser Class Reference

#include <ExpatCoreParser.h>

Collaboration diagram for ExpatCoreParser:

Public Types

typedef std::map< std::string, std::string > ExternalEntityMap

Static Public Member Functions

static std::unique_ptr< CoreParser::DOMNodeparse (const std::string &file_name)
static void register_external_entity (const std::string &name, const std::string &file_name)
static void register_text_entity (const std::string &name, const std::string &text)

Private Types

typedef std::lock_guard< std::mutex > lock_t

Private Member Functions

 ExpatCoreParser (const std::string &prefix)
void do_start (const char *el, const char **attr)
void do_end (const char *el)
void do_char_data (const XML_Char *s, int len)
void do_default_handler (const XML_Char *s, int len)
void do_comment (const XML_Char *s)
int generic_parse (XML_Parser p, const std::string &file_name)
int generic_text_parse (XML_Parser p, const std::string &text)
int do_external_entity (XML_Parser parser, const XML_Char *context, const XML_Char *systemId)
std::unique_ptr< CoreParser::DOMNodeget_document ()
void clean ()

Static Private Member Functions

static void start (void *, const char *el, const char **attr)
static void end (void *, const char *el)
static void char_data (void *, const XML_Char *s, int len)
static void default_handler (void *, const XML_Char *s, int len)
static void comment (void *, const XML_Char *s)
static int external_entity (XML_Parser parser, const XML_Char *context, const XML_Char *, const XML_Char *systemId, const XML_Char *)
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 *)
static const std::string & find_external_entity (const std::string &name)
static const std::string & find_text_entity (const std::string &name)

Private Attributes

std::unique_ptr< CoreParser::DOMNodem_top
CoreParser::DOMNodem_last
std::string m_prefix

Static Private Attributes

static std::mutex s_mutex
static ExternalEntityMap s_entities ATLAS_THREAD_SAFE
static ExternalEntityMap s_text_entities ATLAS_THREAD_SAFE

Detailed Description

Definition at line 15 of file ExpatCoreParser.h.

Member Typedef Documentation

◆ ExternalEntityMap

typedef std::map<std::string, std::string> ExpatCoreParser::ExternalEntityMap

Definition at line 19 of file ExpatCoreParser.h.

◆ lock_t

typedef std::lock_guard<std::mutex> ExpatCoreParser::lock_t
private

Definition at line 71 of file ExpatCoreParser.h.

Constructor & Destructor Documentation

◆ ExpatCoreParser()

ExpatCoreParser::ExpatCoreParser ( const std::string & prefix)
private

Definition at line 135 of file ExpatCoreParser.cxx.

136 : m_top (nullptr),
137 m_last (nullptr),
138 m_prefix (prefix){
139}
std::unique_ptr< CoreParser::DOMNode > m_top
std::string m_prefix
CoreParser::DOMNode * m_last

Member Function Documentation

◆ char_data()

void ExpatCoreParser::char_data ( void * user_data,
const XML_Char * s,
int len )
staticprivate

Definition at line 53 of file ExpatCoreParser.cxx.

53 {
54 ExpatCoreParser& me = *reinterpret_cast<ExpatCoreParser*> (user_data);
55 me.do_char_data (s, len);
56}
void do_char_data(const XML_Char *s, int len)
ExpatCoreParser(const std::string &prefix)

◆ clean()

void ExpatCoreParser::clean ( )
private

◆ comment()

void ExpatCoreParser::comment ( void * user_data,
const XML_Char * s )
staticprivate

Definition at line 65 of file ExpatCoreParser.cxx.

65 {
66 ExpatCoreParser& me = *reinterpret_cast<ExpatCoreParser*> (user_data);
67 me.do_comment (s);
68}
void do_comment(const XML_Char *s)

◆ default_handler()

void ExpatCoreParser::default_handler ( void * user_data,
const XML_Char * s,
int len )
staticprivate

Definition at line 59 of file ExpatCoreParser.cxx.

59 {
60 ExpatCoreParser& me = *reinterpret_cast<ExpatCoreParser*> (user_data);
61 me.do_default_handler (s, len);
62}
void do_default_handler(const XML_Char *s, int len)

◆ do_char_data()

void ExpatCoreParser::do_char_data ( const XML_Char * s,
int len )
private

Definition at line 170 of file ExpatCoreParser.cxx.

170 {
171 if (len == 0) return;
172 while ((len > 0) && (s[len-1] == '\n')) len--;
173 if (len == 0) return;
174 std::string temp (s, len);
176 std::cout << "ExpatCoreParser::do_char_data> [" << temp << "]" << std::endl;
177 }
178}

◆ do_comment()

void ExpatCoreParser::do_comment ( const XML_Char * s)
private

Definition at line 192 of file ExpatCoreParser.cxx.

192 {
193 if (!m_top) {
194 m_top = std::make_unique<CoreParser::DOMNode> ();
195 m_last = m_top.get();
196 }
197 CoreParser::DOMNode* node = new CoreParser::DOMNode (CoreParser::DOMNode::COMMENT_NODE, s, m_last);
199 std::cout << "ExpatCoreParser::do_comment> s=" << s << " top=" << m_top.get() << " last=" << m_last << " node=" << node << std::endl;
200 }
201 // Node is owned by m_last.
202 // cppcheck-suppress memleak
203 node = nullptr;
204}

◆ do_default_handler()

void ExpatCoreParser::do_default_handler ( const XML_Char * s,
int len )
private

Definition at line 181 of file ExpatCoreParser.cxx.

181 {
182 if (len == 0) return;
183 while ((len > 0) && (s[len-1] == '\n')) len--;
184 if (len == 0) return;
185 std::string temp (s, len);
187 std::cout << "ExpatCoreParser::do_default_handler> [" << temp << "]" << std::endl;
188 }
189}

◆ do_end()

void ExpatCoreParser::do_end ( const char * el)
private

Definition at line 162 of file ExpatCoreParser.cxx.

162 {
164 std::cout << "ExpatCoreParser::do_end> el=" << el << std::endl;
165 }
166 m_last = m_last->m_parent;
167}

◆ do_external_entity()

int ExpatCoreParser::do_external_entity ( XML_Parser parser,
const XML_Char * context,
const XML_Char * systemId )
private

Definition at line 337 of file ExpatCoreParser.cxx.

338 {
339 std::string context_str;
340 if (context == 0) context_str = "none";
341 else context_str = context;
342 bool done = false;
343 int status = 0;
344 if (context != 0) {
345 std::string replacement = find_text_entity (context_str);
346 if (replacement != "") {
348 std::cout << "ExpatCoreParser::do_external_entity> "
349 << " context=[" << context_str << "]"
350 << " replacement=[" << replacement << "]"
351 << std::endl;
352 }
353 XML_Parser p = XML_ExternalEntityParserCreate (parser, context, NULL);
354 status = generic_text_parse (p, replacement);
355 XML_ParserFree (p);
356 done = true;
357 }
358 }
359 if (!done) {
360 std::string replacement = find_external_entity (context_str);
361 if (replacement == "NULL") {
362 return (1);
363 }
364 if (replacement != "") {
365 systemId = replacement.c_str ();
366 }
367
369 std::cout << "ExpatCoreParser::do_external_entity> "
370 << " context=[" << context_str << "]"
371 << " systemId=[" << systemId << "]"
372 << " replacement=[" << replacement << "]"
373 << std::endl;
374 }
375 XML_Parser p = XML_ExternalEntityParserCreate (parser, context, NULL);
376 status = generic_parse (p, systemId);
377 XML_ParserFree (p);
378 }
379 return (status);
380}
int generic_text_parse(XML_Parser p, const std::string &text)
int generic_parse(XML_Parser p, const std::string &file_name)
static const std::string & find_external_entity(const std::string &name)
static const std::string & find_text_entity(const std::string &name)
status
Definition merge.py:16

◆ do_start()

void ExpatCoreParser::do_start ( const char * el,
const char ** attr )
private

Definition at line 142 of file ExpatCoreParser.cxx.

142 {
143 int i;
144 std::map <std::string, std::string> a;
145 if (!m_top){
146 m_top = std::make_unique<CoreParser::DOMNode> ();
147 m_last = m_top.get();
148 }
149 CoreParser::DOMNode* node = new CoreParser::DOMNode (CoreParser::DOMNode::ELEMENT_NODE, el, m_last);
151 std::cout << "ExpatCoreParser::do_start> el=" << el << " top=" << m_top.get() << " last=" << m_last << " node=" << node << std::endl;
152 }
153 m_last = node;
154 for (i = 0; attr[i]; i += 2) {
155 const char* name = attr[i];
156 const char* value = attr[i+1];
157 node->m_attributes[name] = value;
158 }
159}
static Double_t a

◆ end()

void ExpatCoreParser::end ( void * user_data,
const char * el )
staticprivate

Definition at line 47 of file ExpatCoreParser.cxx.

47 {
48 ExpatCoreParser& me = *reinterpret_cast<ExpatCoreParser*> (user_data);
49 me.do_end (el);
50}
void do_end(const char *el)

◆ entity()

void ExpatCoreParser::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 *  )
staticprivate

Definition at line 102 of file ExpatCoreParser.cxx.

110 {
111 if (!base) base = "none";
112 if (!systemId) systemId = "none";
113 if (!publicId) publicId = "none";
114 std::string temp = "none";
115 if (value) {
116 temp.assign (value, strnlen(value, value_length));
117 }
118
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;
127 }
128}
std::string base
Definition hcg.cxx:81

◆ external_entity()

int ExpatCoreParser::external_entity ( XML_Parser parser,
const XML_Char * context,
const XML_Char * ,
const XML_Char * systemId,
const XML_Char *  )
staticprivate

Definition at line 71 of file ExpatCoreParser.cxx.

75 {
76 void* user_data = XML_GetUserData (parser);
77 ExpatCoreParser& me = *reinterpret_cast<ExpatCoreParser*> (user_data);
78 return (me.do_external_entity (parser, context, systemId));
79}
int do_external_entity(XML_Parser parser, const XML_Char *context, const XML_Char *systemId)

◆ find_external_entity()

const std::string & ExpatCoreParser::find_external_entity ( const std::string & name)
staticprivate

Definition at line 383 of file ExpatCoreParser.cxx.

383 {
384 lock_t lock (s_mutex);
385 ExternalEntityMap::const_iterator it = s_entities.find (name);
386 if (it == s_entities.end ()) {
387 static const std::string empty;
388 return (empty);
389 } else {
390 return ((*it).second);
391 }
392}
static const Attributes_t empty
std::lock_guard< std::mutex > lock_t
static std::mutex s_mutex

◆ find_text_entity()

const std::string & ExpatCoreParser::find_text_entity ( const std::string & name)
staticprivate

Definition at line 395 of file ExpatCoreParser.cxx.

395 {
396 lock_t lock (s_mutex);
397 ExternalEntityMap::const_iterator it = s_text_entities.find (name);
398 if (it == s_text_entities.end ()) {
399 static const std::string empty;
400 return (empty);
401 } else {
402 return ((*it).second);
403 }
404}

◆ generic_parse()

int ExpatCoreParser::generic_parse ( XML_Parser p,
const std::string & file_name )
private

Definition at line 207 of file ExpatCoreParser.cxx.

207 {
208 FILE* docfd;
210 std::cout << "ExpatCoreParser::generic_parse> file_name=" << file_name << " prefix=" << m_prefix << std::endl;
211 }
212
213 std::string temp_name = file_name;
214 docfd = fopen (temp_name.c_str (), "r");
215 if (!docfd){
216 temp_name = m_prefix + "/" + file_name;
217 docfd = fopen (temp_name.c_str (), "r");
218 }
219
220 if (docfd == 0){
221 const char* xmlpathenv = ::getenv ("XMLPATH");
222 if (xmlpathenv == 0) return (0);
223 std::string xmlpath = xmlpathenv;
224 std::string::size_type pos = 0;
225 while (pos != std::string::npos){
226 std::string::size_type sep = xmlpath.find (":", pos);
227 if (sep == std::string::npos){
228 temp_name = xmlpath.substr (pos);
229 pos = std::string::npos;
230 } else {
231 temp_name = xmlpath.substr (pos, sep - pos);
232 pos = sep + 1;
233 }
234 if (temp_name.empty()) continue;
235 std::string last_temp_name = temp_name;
236 temp_name += "/";
237 temp_name += file_name;
239 std::cout << "ExpatCoreParser::generic_parse> file_name=" << file_name
240 << " xmlpath=" << xmlpath
241 << " temp_name=" << temp_name
242 << std::endl;
243 }
244
245 docfd = fopen (temp_name.c_str (), "r");
246 if (docfd != 0) break;
247 // Test whether prefix is a relative path and if so use it
248 if (m_prefix != "" && '/' != m_prefix[0]) {
249 temp_name = std::move(last_temp_name);
250 temp_name += "/";
251 temp_name += m_prefix;
252 temp_name += "/";
253 temp_name += file_name;
255 std::cout << "ExpatCoreParser::generic_parse> file_name=" << file_name
256 << " xmlpath=" << xmlpath
257 << " temp_name=" << temp_name
258 << std::endl;
259 }
260 docfd = fopen (temp_name.c_str (), "r");
261 if (docfd != 0) break;
262 }
263 }
264 }
265
266 if (docfd == 0){
267 return (0);
268 }
269 int result = 1;
270 XML_SetParamEntityParsing (p, XML_PARAM_ENTITY_PARSING_ALWAYS);
271 XML_SetElementHandler (p, start, end);
272 XML_SetCharacterDataHandler (p, char_data);
273 XML_SetExternalEntityRefHandler (p, external_entity);
274 XML_SetCommentHandler (p, comment);
275 XML_SetUserData (p, this);
277 std::cout << "ExpatCoreParser::generic_parse> starting" << std::endl;
278 }
279 for (;;) {
280 int items;
281 char* buff = (char*) XML_GetBuffer (p, BUFFSIZE + 1);
282 items = fread (buff, 1, BUFFSIZE, docfd);
283 if (ferror (docfd)){
284 std::cout << "Read error" << std::endl;
285 result = 0;
286 break;
287 }
288 int done = feof (docfd);
289 buff[items] = 0;
290 if (XML_ParseBuffer (p, BUFFSIZE, done) == XML_STATUS_ERROR) {
291 if (!done) {
292 std::cout << "ExpatCoreParser::Parse error at line " << XML_GetCurrentLineNumber(p)
293 << " of file " << file_name
294 << ":" << std::endl
295 << XML_ErrorString (XML_GetErrorCode(p)) << std::endl;
296 result = 0;
297 }
298 /* handle parse error */
299 }
300 if (done) break;
301 }
302 fclose (docfd);
303 return (result);
304}
#define BUFFSIZE
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 *)
static void start(void *, const char *el, const char **attr)
static void comment(void *, const XML_Char *s)
static void end(void *, const char *el)

◆ generic_text_parse()

int ExpatCoreParser::generic_text_parse ( XML_Parser p,
const std::string & text )
private

Definition at line 307 of file ExpatCoreParser.cxx.

307 {
309 std::cout << "ExpatCoreParser::generic_text_parse> " << std::endl;
310 }
311 int result = 1;
312 XML_SetParamEntityParsing (p, XML_PARAM_ENTITY_PARSING_ALWAYS);
313 XML_SetElementHandler (p, start, end);
314 XML_SetCharacterDataHandler (p, char_data);
315 XML_SetExternalEntityRefHandler (p, external_entity);
316 XML_SetCommentHandler (p, comment);
317 XML_SetUserData (p, this);
319 std::cout << "ExpatCoreParser::generic_text_parse> starting" << std::endl;
320 }
321 {
322 char* buff = (char*) XML_GetBuffer (p, text.size() + 1);
323 strcpy (buff, text.c_str ());
324 int done = 1;
325 if (XML_ParseBuffer (p, text.size(), done) == XML_STATUS_ERROR) {
326 std::cout << "ExpatCoreParser::Parse error at line " << XML_GetCurrentLineNumber(p)
327 << ":" << std::endl
328 << XML_ErrorString (XML_GetErrorCode(p)) << std::endl;
329 result = 0;
330 /* handle parse error */
331 }
332 }
333 return (result);
334}

◆ get_document()

std::unique_ptr< CoreParser::DOMNode > ExpatCoreParser::get_document ( )
private

Definition at line 131 of file ExpatCoreParser.cxx.

131 {
132 return std::move(m_top);
133}

◆ parse()

std::unique_ptr< CoreParser::DOMNode > ExpatCoreParser::parse ( const std::string & file_name)
static

Definition at line 407 of file ExpatCoreParser.cxx.

407 {
408 std::string name = file_name;
409 std::string::size_type pos = file_name.rfind ('/');
410 std::string prefix;
411 if (pos != std::string::npos){
412 prefix = file_name.substr (0, pos);
413 name = file_name.substr (pos + 1);
414 }
415 ExpatCoreParser me (prefix);
416 XML_Parser p = XML_ParserCreate (NULL);
417 if (!p) {
418 std::cout << "ExpatCoreParser::Couldn't allocate memory for parser" << std::endl;
419 std::abort();
420 }
421 int result = me.generic_parse (p, name);
422 XML_ParserFree (p);
423 if (result == 0) return nullptr;
424 return me.get_document ();
425}

◆ register_external_entity()

void ExpatCoreParser::register_external_entity ( const std::string & name,
const std::string & file_name )
static

Definition at line 82 of file ExpatCoreParser.cxx.

82 {
84 std::cout << "ExpatCoreParser::register_external_entity> name=" << name
85 << " file_name=" << file_name << std::endl;
86 }
87 lock_t lock (s_mutex);
88 s_entities[name] = file_name;
89}

◆ register_text_entity()

void ExpatCoreParser::register_text_entity ( const std::string & name,
const std::string & text )
static

Definition at line 92 of file ExpatCoreParser.cxx.

92 {
94 std::cout << "ExpatCoreParser::register_text_entity> name=" << name
95 << std::endl;
96 }
97 lock_t lock (s_mutex);
98 s_text_entities[name] = text;
99}

◆ start()

void ExpatCoreParser::start ( void * user_data,
const char * el,
const char ** attr )
staticprivate

Definition at line 41 of file ExpatCoreParser.cxx.

41 {
42 ExpatCoreParser& me = *reinterpret_cast<ExpatCoreParser*> (user_data);
43 me.do_start (el, attr);
44}
void do_start(const char *el, const char **attr)

Member Data Documentation

◆ ATLAS_THREAD_SAFE [1/2]

ExternalEntityMap s_entities ExpatCoreParser::ATLAS_THREAD_SAFE
staticprivate

Definition at line 72 of file ExpatCoreParser.h.

◆ ATLAS_THREAD_SAFE [2/2]

ExternalEntityMap s_text_entities ExpatCoreParser::ATLAS_THREAD_SAFE
staticprivate

Definition at line 73 of file ExpatCoreParser.h.

◆ m_last

CoreParser::DOMNode* ExpatCoreParser::m_last
private

Definition at line 67 of file ExpatCoreParser.h.

◆ m_prefix

std::string ExpatCoreParser::m_prefix
private

Definition at line 68 of file ExpatCoreParser.h.

◆ m_top

std::unique_ptr<CoreParser::DOMNode> ExpatCoreParser::m_top
private

Definition at line 66 of file ExpatCoreParser.h.

◆ s_mutex

std::mutex ExpatCoreParser::s_mutex
staticprivate

Definition at line 70 of file ExpatCoreParser.h.


The documentation for this class was generated from the following files: