13 class XMLCoreParserDebugger{
15 static bool get_debug_state(){
16 return ::getenv (
"XMLDEBUG") != 0;
19 static const bool debug_state = get_debug_state();
35 if (XMLCoreParserDebugger::debug ()){
36 std::cout <<
"XMLCoreFactory::~XMLCoreFactory> factory=" <<
this << std::endl;
42 if (XMLCoreParserDebugger::debug ()){
43 std::cout <<
"XMLCoreFactory::start> factory=" <<
this << std::endl;
50 if (XMLCoreParserDebugger::debug ()){
51 std::cout <<
"XMLCoreFactory::end>" << std::endl;
58 if (XMLCoreParserDebugger::debug ()){
59 std::cout <<
"XMLCoreFactory::comment>" << std::endl;
66 if (XMLCoreParserDebugger::debug ()){
67 std::cout <<
"XMLCoreFactory::do_start>" << std::endl;
73 if (XMLCoreParserDebugger::debug ()){
74 std::cout <<
"XMLCoreFactory::do_end>" << std::endl;
80 if (XMLCoreParserDebugger::debug ()){
81 std::cout <<
"XMLCoreFactory::do_comment>" << std::endl;
87 return node.has_attrib (name);
94 sscanf (s.c_str (),
"%80d", &result);
102 sscanf (s.c_str (),
"%80lg", &result);
110 if (s ==
"TRUE") result =
true;
121 if (XMLCoreParserDebugger::debug ()){
122 std::cout <<
"XMLCoreFactory::get_value> name=" << name << std::endl;
125 if (
node.has_attrib (name))
126 result =
node.get_attrib (name);
127 if (XMLCoreParserDebugger::debug ()) {
128 std::cout <<
"XMLCoreFactory::get_value>2 value=" << result << std::endl;
137 while ((result.length () > 0) &&
138 (result.at(0) ==
' ')) result.erase (0, 1);
140 while ((result.length () > 0) &&
141 (result.at(result.length () - 1) ==
' ')) result.erase (result.length () - 1, 1);
143 for (std::string::size_type i = 0; i < result.length (); ++i){
144 result[i] = std::toupper (result[i]);
159 std::string t = checkstring +
" " + checkstring;
160 std::istringstream tmpstr (t.c_str());
161 while (tmpstr.good ()) {
166 if (counter/2 != n) {
167 std::cerr <<
"XMLCoreFactory::check_int error: no " << n
168 <<
" ints in \"" << checkstring <<
"\" for attribute " <<
169 name <<
". exit." << std::endl;
174 if (nodename !=
"" ) std::cerr <<
"for name=" << nodename << std::endl;
175 if (volume !=
"" ) std::cerr <<
"for volume=" << volume << std::endl;
191 std::string t = checkstring +
" " + checkstring;
192 std::istringstream tmpstr (t.c_str());
193 while (tmpstr.good ()) {
198 if (counter/2 != n) {
199 std::cerr <<
"XMLCoreFactory::check_double error: (" << counter <<
") no " << n
200 <<
" doubles in \"" << checkstring <<
"\" for attribute " <<
201 name <<
". exit." << std::endl;
204 if (name1 !=
"" ) std::cerr <<
"for name=" << name << std::endl;
205 if (volume !=
"" ) std::cerr <<
"for volume=" << volume << std::endl;
215std::unique_ptr<XMLCoreNode>
219 if (XMLCoreParserDebugger::debug ()){
220 if (doc !=
nullptr) doc->print (
"============ ALL =============");
223 throw std::runtime_error(
"XMLCoreParser: no such file ["+file_name+
"]");
228std::unique_ptr<XMLCoreNode>
232 if (XMLCoreParserDebugger::debug ()){
233 if (doc !=
nullptr) doc->print (
"============ ALL =============");
236 throw std::runtime_error(
"XMLCoreParser: cannot parse string");
243 if (XMLCoreParserDebugger::debug ()){
244 std::cout <<
"XMLCoreParser::visit file_name "
245 << file_name << std::endl;
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(const std::string &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(const std::string &file_name)
void visit(const std::string &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)