9ec849132e7194e140d23334cfafcf5c04f428e2
[metaproxy-moved-to-github.git] / src / ex_libxml2_conf.cpp
1 /* $Id: ex_libxml2_conf.cpp,v 1.2 2005-10-25 09:06:44 marc Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include <iostream>
8 #include <stdexcept>
9
10 #include <boost/program_options.hpp>
11 namespace po = boost::program_options;
12
13 #include <libxml/xmlreader.h>
14
15 class Configuration 
16 {
17 public:
18     Configuration(int argc, char **argv)
19         {            
20             m_config = "";
21             m_duration = 0;
22             m_xinclude = false;
23             m_xml_conf_doc = 0;
24    
25             parse_command_line(argc, argv);
26             parse_xml_config();
27         }
28     
29 public:
30     std::string config()
31         {
32          return m_config;
33         }
34     int duration()
35         {
36             return m_duration;
37         }
38     
39     
40 private:
41     std::string m_config;
42     int m_duration;      
43     bool m_xinclude;
44     xmlDoc * m_xml_conf_doc;
45     
46     
47     void parse_command_line(int argc, char **argv)
48         {
49             po::options_description generic("Generic options");
50             generic.add_options()
51                 ("help,h", "produce help message")
52                 ("version,v", "version number")
53                 ("xinclude,x", "allow Xinclude on XML config files")
54                 ;
55             
56             po::options_description config("Configuration options");
57          config.add_options()
58             ("config,c", po::value<std::string>(&m_config)->default_value("../etc/config1.xml"),
59              "config XML file path (string)")
60             ("duration,d", po::value<int>(&m_duration)->default_value(0),
61              "number of seconds for server to exist (int)")
62             //("commands", po::value< std::vector<std::string> >(), 
63             //  "listener ports (string)")
64             ;
65          
66          //po::positional_options_description p;
67          // p.add("port", -1);
68          
69          po::options_description cmdline_options;
70          cmdline_options.add(generic).add(config);
71          po::variables_map vm;        
72
73          try 
74          {
75             //po::store(po::command_line_parser(argc, argv).
76             //       options(cmdline_options).positional(p).run(), vm);
77             //po::store(po::command_line_parser(argc, argv).
78             //     options(cmdline_options).run(), vm);
79             po::store(po::parse_command_line(argc, argv,  cmdline_options), vm);
80             po::notify(vm);    
81          }
82          catch ( po::invalid_command_line_syntax &e) {      
83             std::cerr << "ex_libxml2_conf error: " << e.what() << std::endl;
84             std::cerr << generic << config << std::endl;
85             std::exit(1);
86         }
87          catch ( po::invalid_option_value &e) {      
88             //std::cerr << "ex_libxml2_conf error: " << e.what() << std::endl;
89             std::cerr << "invalid option value" << std::endl;
90             std::cerr << generic << config << std::endl;
91             std::exit(1);
92          }
93          catch ( po::unknown_option &e) {      
94             std::cerr << "ex_libxml2_conf error: " << e.what() << std::endl;
95             std::cerr << generic << config << std::endl;
96            std::exit(1);
97          }
98          
99          std::cout << "ex_libxml2_conf ";
100          
101          if (vm.count("help")) {
102             std::cout << "--help" << std::endl;
103             std::cout << generic << config << std::endl;
104             std::exit(0);
105          }
106          
107         if (vm.count("version")) {
108            std::cout << "--version" << std::endl;
109            std::exit(0);
110         }
111          
112         if (vm.count("xinclude")) {
113            std::cout << "--xinclude" << std::endl;
114            m_xinclude = true;
115         }
116         
117         if (vm.count("duration")) {
118            std::cout << "--duration " 
119                      << vm["duration"].as<int>() << " ";
120         }
121         if (vm.count("config")) {
122            std::cout << "--config " 
123                      << vm["config"].as<std::string>() << " ";
124         }
125         
126         std::cout << std::endl;
127
128         //if (vm.count("port"))
129         //{
130         //    std::vector<std::string> ports = 
131         //       vm["port"].as< std::vector<std::string> >();
132         //    
133         //    for (size_t i = 0; i<ports.size(); i++)
134         //       std::cout << "port " << i << " " << ports[i] << std::endl;
135             
136         //}
137
138         }
139     
140     void parse_xml_config() {   
141         LIBXML_TEST_VERSION
142
143         xmlTextReader* reader;
144         //reader->SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1);
145         int ret;
146         reader = xmlReaderForFile(m_config.c_str(), NULL, 0);
147  
148         if (reader == NULL) {
149             std::cerr << "failed to open XML config file " 
150                       << m_config << std::endl;
151             std::exit(1);
152         }
153
154
155         // skipping pi, comments, text nodes ...  untilt root element
156         //while((ret = xmlTextReaderRead(reader))
157               //&& 0 == xmlTextReaderDepth(reader) 
158               //&& xmlTextReaderNodeType(reader) !=  XML_ELEMENT_NODE
159         //) 
160         //std::cout << xmlTextReaderConstName(reader) << std::endl;
161     
162         // root element processing
163         if ((ret = xmlTextReaderMoveToElement(reader)))    
164             std::cout << xmlTextReaderConstName(reader) << std::endl;
165         //if (xmlTextReaderHasAttributes(reader))
166         //    std::cout << "AttributeCount "
167         //              << xmlTextReaderAttributeCount(reader) << std::endl;
168
169         while (xmlTextReaderMoveToNextAttribute(reader))
170               std::cout << xmlTextReaderConstName(reader) << " "  
171                         << xmlTextReaderConstValue(reader) << " "  
172                   //<< xmlTextReaderNodeType(reader) << " "  
173                   //<< xmlTextReaderDepth(reader) << " "  
174                         << std::endl;
175
176         // processing all other elements
177         while ((ret = xmlTextReaderRead(reader))) 
178             std::cout << xmlTextReaderConstName(reader) << " "  
179                       << xmlTextReaderDepth(reader) << " "
180                       << xmlTextReaderNodeType(reader) << std::endl;
181         
182
183         xmlFreeTextReader(reader);
184         if (ret != 0) {
185             std::cerr << "Parsing failed of XML config file " 
186                       << m_config << std::endl;
187             std::exit(1);
188         }
189     }
190     
191     
192 };
193
194
195
196 int main(int argc, char **argv)
197 {
198    //try 
199    //{
200
201    Configuration conf(argc, argv);
202
203    std::cout << "config " << conf.config() << std::endl;
204    std::cout << "duration " << conf.duration() << std::endl;
205    
206
207
208
209         // }
210         //catch ( ... ) {
211         //std::cerr << "Unknown Exception" << std::endl;
212         //throw();
213         //std::exit(1);
214         //}
215    std::exit(0);
216 }
217
218
219
220
221 /*
222  * Local variables:
223  * c-basic-offset: 4
224  * indent-tabs-mode: nil
225  * c-file-style: "stroustrup"
226  * End:
227  * vim: shiftwidth=4 tabstop=8 expandtab
228  */