added first attempt to read xml config files
authorMarc Cromme <marc@indexdata.dk>
Tue, 25 Oct 2005 09:06:44 +0000 (09:06 +0000)
committerMarc Cromme <marc@indexdata.dk>
Tue, 25 Oct 2005 09:06:44 +0000 (09:06 +0000)
src/ex_libxml2_conf.cpp

index 2c85cfa..9ec8491 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: ex_libxml2_conf.cpp,v 1.1 2005-10-24 13:31:36 marc Exp $
+/* $Id: ex_libxml2_conf.cpp,v 1.2 2005-10-25 09:06:44 marc Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
 #include <boost/program_options.hpp>
 namespace po = boost::program_options;
 
+#include <libxml/xmlreader.h>
 
-class Config 
+class Configuration 
 {
-   public:
-      Config(int argc, char **argv)
-      {
-         
-         po::options_description generic("Generic options");
-         generic.add_options()
-            ("help,h", "produce help message")
-            ("version,v", "version number")
-            ;
-
-         po::options_description config("Config options");
+public:
+    Configuration(int argc, char **argv)
+        {            
+            m_config = "";
+            m_duration = 0;
+            m_xinclude = false;
+            m_xml_conf_doc = 0;
+   
+            parse_command_line(argc, argv);
+            parse_xml_config();
+        }
+    
+public:
+    std::string config()
+        {
+         return m_config;
+        }
+    int duration()
+        {
+            return m_duration;
+        }
+    
+    
+private:
+    std::string m_config;
+    int m_duration;      
+    bool m_xinclude;
+    xmlDoc * m_xml_conf_doc;
+    
+    
+    void parse_command_line(int argc, char **argv)
+        {
+            po::options_description generic("Generic options");
+            generic.add_options()
+                ("help,h", "produce help message")
+                ("version,v", "version number")
+                ("xinclude,x", "allow Xinclude on XML config files")
+                ;
+            
+            po::options_description config("Configuration options");
          config.add_options()
-            ("config,c", po::value<std::string>(&m_config)->default_value("ex_libxml2_conf.xml"),
+            ("config,c", po::value<std::string>(&m_config)->default_value("../etc/config1.xml"),
              "config XML file path (string)")
-            ("duration,d", po::value<int>(&m_duration)->default_value(3),
+            ("duration,d", po::value<int>(&m_duration)->default_value(0),
              "number of seconds for server to exist (int)")
             //("commands", po::value< std::vector<std::string> >(), 
             //  "listener ports (string)")
@@ -78,6 +108,11 @@ class Config
            std::cout << "--version" << std::endl;
            std::exit(0);
         }
+         
+        if (vm.count("xinclude")) {
+           std::cout << "--xinclude" << std::endl;
+           m_xinclude = true;
+        }
         
         if (vm.count("duration")) {
            std::cout << "--duration " 
@@ -99,29 +134,61 @@ class Config
         //       std::cout << "port " << i << " " << ports[i] << std::endl;
             
         //}
-      }
-      
-   public:
-      std::string config()
-      {
-         return m_config;
-      }
-      int duration()
-      {
-         return m_duration;
-      }
-      
-
-   private:
-      std::string m_config;
-      int m_duration;      
-      //xmlDoc * m_conf_doc;
+
+        }
+    
+    void parse_xml_config() {   
+        LIBXML_TEST_VERSION
+
+        xmlTextReader* reader;
+        //reader->SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1);
+        int ret;
+        reader = xmlReaderForFile(m_config.c_str(), NULL, 0);
  
-      bool load_xml_doc()
-      {
-         return true;
-      }
-      
+        if (reader == NULL) {
+            std::cerr << "failed to open XML config file " 
+                      << m_config << std::endl;
+            std::exit(1);
+        }
+
+
+        // skipping pi, comments, text nodes ...  untilt root element
+        //while((ret = xmlTextReaderRead(reader))
+              //&& 0 == xmlTextReaderDepth(reader) 
+              //&& xmlTextReaderNodeType(reader) !=  XML_ELEMENT_NODE
+        //) 
+        //std::cout << xmlTextReaderConstName(reader) << std::endl;
+    
+        // root element processing
+        if ((ret = xmlTextReaderMoveToElement(reader)))    
+            std::cout << xmlTextReaderConstName(reader) << std::endl;
+        //if (xmlTextReaderHasAttributes(reader))
+        //    std::cout << "AttributeCount "
+        //              << xmlTextReaderAttributeCount(reader) << std::endl;
+
+        while (xmlTextReaderMoveToNextAttribute(reader))
+              std::cout << xmlTextReaderConstName(reader) << " "  
+                        << xmlTextReaderConstValue(reader) << " "  
+                  //<< xmlTextReaderNodeType(reader) << " "  
+                  //<< xmlTextReaderDepth(reader) << " "  
+                        << std::endl;
+
+        // processing all other elements
+        while ((ret = xmlTextReaderRead(reader))) 
+            std::cout << xmlTextReaderConstName(reader) << " "  
+                      << xmlTextReaderDepth(reader) << " "
+                      << xmlTextReaderNodeType(reader) << std::endl;
+        
+
+        xmlFreeTextReader(reader);
+        if (ret != 0) {
+            std::cerr << "Parsing failed of XML config file " 
+                      << m_config << std::endl;
+            std::exit(1);
+        }
+    }
+    
+    
 };
 
 
@@ -131,12 +198,14 @@ int main(int argc, char **argv)
    //try 
    //{
 
-   Config conf(argc, argv);
+   Configuration conf(argc, argv);
 
    std::cout << "config " << conf.config() << std::endl;
    std::cout << "duration " << conf.duration() << std::endl;
    
 
+
+
         // }
         //catch ( ... ) {
         //std::cerr << "Unknown Exception" << std::endl;