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< XMLCoreNodeparse (const std::string &file_name)
static std::unique_ptr< XMLCoreNodeparse_string (const std::string &text)
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 configure_parser (XML_Parser p)
XMLCoreNodeadd_node (std::unique_ptr< XMLCoreNode > node)
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 stream_parse (XML_Parser p, std::istream &is, const std::string &source_name={})
int do_external_entity (XML_Parser parser, const XML_Char *context, const XML_Char *systemId)
std::unique_ptr< XMLCoreNodeget_document ()
std::string find_external_entity (const std::string &name)
std::string find_text_entity (const std::string &name)
std::string find_entity (const std::string &name, const ExternalEntityMap &mapChoice)
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 *)

Private Attributes

std::unique_ptr< XMLCoreNodem_top
XMLCoreNodem_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 19 of file ExpatCoreParser.h.

Member Typedef Documentation

◆ ExternalEntityMap

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

Definition at line 23 of file ExpatCoreParser.h.

◆ lock_t

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

Definition at line 62 of file ExpatCoreParser.h.

Constructor & Destructor Documentation

◆ ExpatCoreParser()

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

Definition at line 94 of file ExpatCoreParser.cxx.

95 : m_top (nullptr),
96 m_last (nullptr),
97 m_prefix (prefix){
98}
std::string m_prefix
XMLCoreNode * m_last
std::unique_ptr< XMLCoreNode > m_top

Member Function Documentation

◆ add_node()

XMLCoreNode * ExpatCoreParser::add_node ( std::unique_ptr< XMLCoreNode > node)
private

Definition at line 111 of file ExpatCoreParser.cxx.

111 {
112 if (!m_top){
113 m_top = std::make_unique<XMLCoreNode> (XMLCoreNode::DOCUMENT_NODE);
114 m_last = m_top.get();
115 }
116
117 return m_last->add_child (std::move(node));
118}

◆ char_data()

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

Definition at line 45 of file ExpatCoreParser.cxx.

45 {
46 auto& me = *static_cast<ExpatCoreParser*> (user_data);
47 me.do_char_data (s, len);
48}
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 57 of file ExpatCoreParser.cxx.

57 {
58 auto& me = *static_cast<ExpatCoreParser*> (user_data);
59 me.do_comment (s);
60}

◆ configure_parser()

void ExpatCoreParser::configure_parser ( XML_Parser p)
private

Definition at line 101 of file ExpatCoreParser.cxx.

101 {
102 XML_SetParamEntityParsing(p, XML_PARAM_ENTITY_PARSING_ALWAYS);
103 XML_SetElementHandler(p, start, end);
104 XML_SetCharacterDataHandler(p, char_data);
105 XML_SetExternalEntityRefHandler(p, external_entity);
106 XML_SetCommentHandler(p, comment);
107 XML_SetUserData(p, this);
108}
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)

◆ default_handler()

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

Definition at line 51 of file ExpatCoreParser.cxx.

51 {
52 auto& me = *static_cast<ExpatCoreParser*> (user_data);
53 me.do_default_handler (s, len);
54}

◆ do_char_data()

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

Definition at line 143 of file ExpatCoreParser.cxx.

143 {
144 std::string text = rtrim(s, len);
145 if (text.empty()) return;
146 if (debug_enabled()) {
147 std::cout << label()<<"> [" << text << "]\n";
148 }
149 add_node (std::make_unique<XMLCoreNode>(XMLCoreNode::TEXT_NODE, "", text));
150}
XMLCoreNode * add_node(std::unique_ptr< XMLCoreNode > node)
bool debug_enabled()
true if XML debug mode is enabled in the environment
std::string label(const std::source_location loc=std::source_location::current())
report function name in a string, for debugging
std::string rtrim(const XML_Char *s, int len)
Trim newline from end of a XML_Char * , return the trimmed string as std::string.

◆ do_comment()

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

Definition at line 162 of file ExpatCoreParser.cxx.

162 {
163 auto node = std::make_unique<XMLCoreNode> (XMLCoreNode::COMMENT_NODE, "", s);
164 if (debug_enabled()) {
165 std::cout << label()<<"> s=" << s << " top=" << m_top.get() << " last=" << m_last << " node=" << node << "\n";
166 }
167 add_node (std::move(node));
168}

◆ do_default_handler()

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

Definition at line 153 of file ExpatCoreParser.cxx.

153 {
154 std::string text = rtrim(s, len);
155 if (text.empty()) return;
156 if (debug_enabled()) {
157 std::cout << label()<<"> [" << text << "]\n";
158 }
159}

◆ do_end()

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

Definition at line 135 of file ExpatCoreParser.cxx.

135 {
136 if (debug_enabled()){
137 std::cout <<label()<<"> el=" << el << "\n";
138 }
139 m_last = m_last->get_parent();
140}

◆ do_external_entity()

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

Definition at line 235 of file ExpatCoreParser.cxx.

235 {
236 const std::string context_str = context ? context : "none";
237 if (context != nullptr) {
238 const std::string replacement = find_text_entity(context_str);
239 if (!replacement.empty()) {
240 if (debug_enabled()) {
241 std::cout << label() << "> context=[" << context_str
242 << "] replacement=[" << replacement << "]\n";
243 }
244 XMLParserPtr p{XML_ExternalEntityParserCreate(parser, context, nullptr)};
245 if (!p) return errorValue;
246 return generic_text_parse(p.get(), replacement);
247 }
248 }
249 std::string replacement = find_external_entity(context_str);
250 if (replacement == "NULL") {
251 return ok;
252 }
253 std::string originalSystemId = systemId ? systemId : "";
254 std::string effectiveSystemId = originalSystemId;
255 if (!replacement.empty()) {
256 effectiveSystemId = replacement;
257 }
258 if (debug_enabled()) {
259 std::cout << label() << "> context=[" << context_str
260 << "] systemId=[" << originalSystemId
261 << "] replacement=[" << replacement << "]\n";
262 }
263 XMLParserPtr p{XML_ExternalEntityParserCreate(parser, context, nullptr)};
264 if (!p) return errorValue;
265 return generic_parse(p.get(), effectiveSystemId);
266}
int generic_text_parse(XML_Parser p, const std::string &text)
std::string find_text_entity(const std::string &name)
std::string find_external_entity(const std::string &name)
int generic_parse(XML_Parser p, const std::string &file_name)
std::unique_ptr< std::remove_pointer_t< XML_Parser >, XMLParserDeleter > XMLParserPtr
RAII XMLParser pointer.

◆ do_start()

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

Definition at line 121 of file ExpatCoreParser.cxx.

121 {
122 auto node = std::make_unique<XMLCoreNode> (XMLCoreNode::ELEMENT_NODE, el);
123 if (debug_enabled()) {
124 std::cout << label()<< "> el=" << el << " top=" << m_top.get() << " last=" << m_last << " node=" << node << "\n";
125 }
126 for (int i = 0; attr[i]; i += 2) {
127 const char* name = attr[i];
128 const char* value = attr[i+1];
129 node->set_attrib (name, value);
130 }
131 m_last = add_node (std::move(node));
132}

◆ end()

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

Definition at line 39 of file ExpatCoreParser.cxx.

39 {
40 auto& me = *static_cast<ExpatCoreParser*> (user_data);
41 me.do_end (el);
42}

◆ 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 63 of file ExpatCoreParser.cxx.

64 {
65 void* user_data = XML_GetUserData (parser);
66 auto& me = *static_cast<ExpatCoreParser*> (user_data);
67 return (me.do_external_entity (parser, context, systemId));
68}

◆ find_entity()

std::string ExpatCoreParser::find_entity ( const std::string & name,
const ExternalEntityMap & mapChoice )
private

Definition at line 269 of file ExpatCoreParser.cxx.

269 {
270 //lock is held here, doesn't need to be held by caller
272 ExternalEntityMap::const_iterator it = mapChoice.find(name);
273 return (it == mapChoice.end()) ? emptyString : it->second;
274}
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
std::lock_guard< std::mutex > lock_t
static std::mutex s_mutex

◆ find_external_entity()

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

Definition at line 276 of file ExpatCoreParser.cxx.

276 {
277 //coverity[MISSING_LOCK:FALSE]
278 return find_entity(name, s_entities);
279}
std::string find_entity(const std::string &name, const ExternalEntityMap &mapChoice)

◆ find_text_entity()

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

Definition at line 282 of file ExpatCoreParser.cxx.

282 {
283 //coverity[MISSING_LOCK:FALSE]
284 return find_entity(name, s_text_entities);
285}

◆ generic_parse()

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

Definition at line 210 of file ExpatCoreParser.cxx.

210 {
211 const std::string& path = xmlFileName(file_name, m_prefix);
212 if (path.empty()) return errorValue;
213 if (debug_enabled()) {
214 std::cout << label() << "> file_name=" << file_name
215 << " prefix=" << m_prefix << "\n";
216 }
217 std::ifstream fs{path, std::ios::binary};
218 if (!fs) {
219 std::cout << "Could not open file " << path << "\n";
220 return errorValue;
221 }
222 return stream_parse(p, fs, file_name);
223}
static Double_t fs
int stream_parse(XML_Parser p, std::istream &is, const std::string &source_name={})
std::string xmlFileName(const std::string &fname, const std::string &prefix)
find the xml file locally or on the datapath, return the full filename
path
python interpreter configuration --------------------------------------—
Definition athena.py:130

◆ generic_text_parse()

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

Definition at line 226 of file ExpatCoreParser.cxx.

226 {
227 if (debug_enabled()) {
228 std::cout << label() << ">\n";
229 }
230 std::istringstream is(text);
231 return stream_parse(p, is, {});
232}

◆ get_document()

std::unique_ptr< XMLCoreNode > ExpatCoreParser::get_document ( )
private

Definition at line 90 of file ExpatCoreParser.cxx.

90 {
91 return std::move(m_top);
92}

◆ parse()

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

Definition at line 288 of file ExpatCoreParser.cxx.

288 {
289 const std::filesystem::path path{file_name};
290 ExpatCoreParser me{path.parent_path().string()};
291 auto p = make_parser();
292 int result = me.generic_parse (p.get(), path.filename().string());
293 if (result == errorValue) return nullptr;
294 return me.get_document ();
295}
std::unique_ptr< XMLCoreNode > get_document()
XMLParserPtr make_parser()
Create an RAII XMLParser pointer.

◆ parse_string()

std::unique_ptr< XMLCoreNode > ExpatCoreParser::parse_string ( const std::string & text)
static

Definition at line 299 of file ExpatCoreParser.cxx.

299 {
300 ExpatCoreParser me ("");
301 auto p = make_parser();
302 int result = me.generic_text_parse (p.get(), text);
303 if (result == errorValue) return nullptr;
304 return me.get_document ();
305}

◆ register_external_entity()

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

Definition at line 71 of file ExpatCoreParser.cxx.

71 {
72 if (debug_enabled()){
73 std::cout << label()<<"> name=" << name << " file_name=" << file_name << "\n";
74 }
76 s_entities[name] = file_name;
77}

◆ register_text_entity()

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

Definition at line 80 of file ExpatCoreParser.cxx.

80 {
81 if (debug_enabled()) {
82 std::cout << label()<<"> name=" << name<< "\n";
83 }
85 s_text_entities[name] = text;
86}

◆ start()

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

Definition at line 33 of file ExpatCoreParser.cxx.

33 {
34 auto& me = *static_cast<ExpatCoreParser*> (user_data);
35 me.do_start (el, attr);
36}

◆ stream_parse()

int ExpatCoreParser::stream_parse ( XML_Parser p,
std::istream & is,
const std::string & source_name = {} )
private

Definition at line 171 of file ExpatCoreParser.cxx.

171 {
172 int result = ok;
174 if (debug_enabled()) {
175 std::cout << label() << "> starting\n";
176 }
177 std::array<char, BUFFSIZE> buff{};
178 for (;;) {
179 is.read(buff.data(), buff.size());
180 const auto nbytes = is.gcount();
181 const bool done = is.eof();
182 if (is.bad()) {
183 std::cout << "Read error";
184 if (!source_name.empty()) {
185 std::cout << " in " << source_name;
186 }
187 std::cout << "\n";
188 result = errorValue;
189 break;
190 }
191 if (XML_Parse(p, buff.data(), static_cast<int>(nbytes), done) == XML_STATUS_ERROR) {
192 std::cout << "ExpatCoreParser::Parse error at line "
193 << XML_GetCurrentLineNumber(p);
194 if (!source_name.empty()) {
195 std::cout << " of " << source_name;
196 }
197 std::cout << ":\n" << XML_ErrorString(XML_GetErrorCode(p)) << "\n";
198 result = errorValue;
199 break;
200 }
201 if (done) {
202 break;
203 }
204 }
205 return result;
206}
void configure_parser(XML_Parser p)

Member Data Documentation

◆ ATLAS_THREAD_SAFE [1/2]

ExternalEntityMap s_text_entities ExpatCoreParser::ATLAS_THREAD_SAFE
staticprivate

Definition at line 64 of file ExpatCoreParser.h.

◆ ATLAS_THREAD_SAFE [2/2]

ExternalEntityMap s_entities ExpatCoreParser::ATLAS_THREAD_SAFE
staticprivate

Definition at line 63 of file ExpatCoreParser.h.

◆ m_last

XMLCoreNode* ExpatCoreParser::m_last {}
private

Definition at line 59 of file ExpatCoreParser.h.

59{};

◆ m_prefix

std::string ExpatCoreParser::m_prefix
private

Definition at line 60 of file ExpatCoreParser.h.

◆ m_top

std::unique_ptr<XMLCoreNode> ExpatCoreParser::m_top
private

Definition at line 58 of file ExpatCoreParser.h.

◆ s_mutex

std::mutex ExpatCoreParser::s_mutex
staticprivate

Definition at line 61 of file ExpatCoreParser.h.


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