14 class XMLCoreParserDebugger{
16 static bool get_debug_state(){
17 return ::getenv (
"XMLDEBUG") != 0;
20 static const bool debug_state = get_debug_state();
36 if (XMLCoreParserDebugger::debug ()){
37 std::cout <<
"XMLCoreFactory::~XMLCoreFactory> factory=" <<
this << std::endl;
43 if (XMLCoreParserDebugger::debug ()){
44 std::cout <<
"XMLCoreFactory::start> factory=" <<
this << std::endl;
51 if (XMLCoreParserDebugger::debug ()){
52 std::cout <<
"XMLCoreFactory::end>" << std::endl;
59 if (XMLCoreParserDebugger::debug ()){
60 std::cout <<
"XMLCoreFactory::comment>" << std::endl;
67 if (XMLCoreParserDebugger::debug ()){
68 std::cout <<
"XMLCoreFactory::do_start>" << std::endl;
74 if (XMLCoreParserDebugger::debug ()){
75 std::cout <<
"XMLCoreFactory::do_end>" << std::endl;
81 if (XMLCoreParserDebugger::debug ()){
82 std::cout <<
"XMLCoreFactory::do_comment>" << std::endl;
88 return node.has_attrib (name);
95 sscanf (s.c_str (),
"%80d", &result);
103 sscanf (s.c_str (),
"%80lg", &result);
111 if (s ==
"TRUE") result =
true;
122 if (XMLCoreParserDebugger::debug ()){
123 std::cout <<
"XMLCoreFactory::get_value> name=" << name << std::endl;
126 if (
node.has_attrib (name))
127 result =
node.get_attrib (name);
128 if (XMLCoreParserDebugger::debug ()) {
129 std::cout <<
"XMLCoreFactory::get_value>2 value=" << result << std::endl;
138 while ((result.length () > 0) &&
139 (result.at(0) ==
' ')) result.erase (0, 1);
141 while ((result.length () > 0) &&
142 (result.at(result.length () - 1) ==
' ')) result.erase (result.length () - 1, 1);
144 for (std::string::size_type i = 0; i < result.length (); ++i){
145 result[i] = std::toupper (result[i]);
160 std::string t = checkstring +
" " + checkstring;
161 std::istringstream tmpstr (t.c_str());
162 while (tmpstr.good ()) {
167 if (counter/2 != n) {
168 std::cerr <<
"XMLCoreFactory::check_int error: no " << n
169 <<
" ints in \"" << checkstring <<
"\" for attribute " <<
170 name <<
". exit." << std::endl;
175 if (nodename !=
"" ) std::cerr <<
"for name=" << nodename << std::endl;
176 if (volume !=
"" ) std::cerr <<
"for volume=" << volume << std::endl;
192 std::string t = checkstring +
" " + checkstring;
193 std::istringstream tmpstr (t.c_str());
194 while (tmpstr.good ()) {
199 if (counter/2 != n) {
200 std::cerr <<
"XMLCoreFactory::check_double error: (" << counter <<
") no " << n
201 <<
" doubles in \"" << checkstring <<
"\" for attribute " <<
202 name <<
". exit." << std::endl;
205 if (name1 !=
"" ) std::cerr <<
"for name=" << name << std::endl;
206 if (volume !=
"" ) std::cerr <<
"for volume=" << volume << std::endl;
216std::unique_ptr<XMLCoreNode>
220 if (XMLCoreParserDebugger::debug ()){
221 if (doc !=
nullptr) doc->print (
"============ ALL =============");
224 throw std::runtime_error(std::format(
"XMLCoreParser: no such file [{}]", file_name));
229std::unique_ptr<XMLCoreNode>
233 if (XMLCoreParserDebugger::debug ()){
234 if (doc !=
nullptr) doc->print (
"============ ALL =============");
237 throw std::runtime_error(
"XMLCoreParser: cannot parse string");
244 if (XMLCoreParserDebugger::debug ()){
245 std::cout << std::format(
"XMLCoreParser::visit file_name {}\n", file_name);
247 std::unique_ptr<XMLCoreNode> n =
parse (file_name);
248 if (XMLCoreParserDebugger::debug ()){
249 std::cout <<
"XMLCoreParser::visit node=" << n.get() << std::endl;
257 const std::string& nodeName =
node.get_name();
258 const std::string& nodeValue =
node.get_value();
259 if (XMLCoreParserDebugger::debug ()){
260 std::cout <<
"XMLCoreParser::visit node(" << &
node <<
") " << nodeName << std::endl;
263 if (XMLCoreParserDebugger::debug ()){
264 std::cout <<
"XMLCoreParser::visit factory " << factory << std::endl;
267 switch (
node.get_type()) {
269 std::vector<const XMLCoreNode*> children =
node.get_children();
276 if (XMLCoreParserDebugger::debug ()){
277 std::cout <<
"XMLCoreParser::visit ELEMENT_NODE "
278 <<
" factory=" << factory
284 std::cerr <<
"XMLCoreParser> Cannot find factory for element "
285 << nodeName << std::endl;
288 std::vector<const XMLCoreNode*> children =
node.get_children();
292 if (factory != 0) factory->
end (*
this,
node);
296 if (factory != 0) factory->
comment (*
this, nodeValue);
300 std::cout <<
"ENTITY_NODE " << nodeValue << std::endl;
304 std::cout <<
"ENTITY_REFERENCE_NODE " << nodeValue << std::endl;
310 std::cerr <<
"Unrecognized node type = " << (long)
node.get_type() << std::endl;
313 if (XMLCoreParserDebugger::debug ()){
314 std::cout <<
"XMLCoreParser::visit-2" << std::endl;
325 std::unique_ptr<XMLCoreFactory> factory) {
326 if (XMLCoreParserDebugger::debug ()){
327 std::cout <<
"XMLCoreFactory::register_factory> name=" << name
328 <<
" factory=" << factory.get() << std::endl;
335 if (XMLCoreParserDebugger::debug ()){
336 std::cout <<
"XMLCoreParser::register_external_entity> name=" << name
337 <<
" file_name=" << file_name << std::endl;
344 if (XMLCoreParserDebugger::debug ()){
345 std::cout <<
"XMLCoreParser::register_text_entity> name=" << name
356 return (*it).second.get();
static void register_text_entity(const std::string &name, const std::string &text)
static std::unique_ptr< XMLCoreNode > parse(std::string_view file_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)
virtual void do_comment(XMLCoreParser &parser, const std::string &comment)
virtual void do_start(XMLCoreParser &parser, const XMLCoreNode &node)
static std::string get_value(const XMLCoreNode &node, const std::string &name)
void end(XMLCoreParser &parser, const XMLCoreNode &node)
static bool has_attribute(const XMLCoreNode &node, const std::string &name)
virtual ~XMLCoreFactory()
virtual void do_end(XMLCoreParser &parser, const XMLCoreNode &node)
static double get_double(const XMLCoreNode &node, const std::string &name)
static bool check_int(const int n, const XMLCoreNode &node, const std::string &name)
void comment(XMLCoreParser &parser, const std::string &comment)
static bool check_double(const int n, const XMLCoreNode &node, const std::string &name)
static int get_int(const XMLCoreNode &node, const std::string &name)
static std::string get_ID(const XMLCoreNode &node, const std::string &name)
void start(XMLCoreParser &parser, const XMLCoreNode &node)
static bool get_boolean(const XMLCoreNode &node, const std::string &name)
static std::string get_token(const XMLCoreNode &node, const std::string &name)
Simple DOM-like node structure to hold the result of XML parsing.
std::unique_ptr< XMLCoreFactory > m_default_factory
std::unique_ptr< XMLCoreNode > parse(std::string_view file_name)
void visit(std::string_view file_name)
void register_factory(const std::string &name, std::unique_ptr< XMLCoreFactory > factory)
void register_text_entity(const std::string &name, const std::string &text)
void register_external_entity(const std::string &name, const std::string &file_name)
XMLCoreFactory * find_factory(const std::string &name)
std::unique_ptr< XMLCoreNode > parse_string(const std::string &text)
void register_default_factory(std::unique_ptr< XMLCoreFactory > factory)