added Xinclude restricted to localhost for partitioning of XML config files
[metaproxy-moved-to-github.git] / src / metaproxy_prog.cpp
1 /* $Id: metaproxy_prog.cpp,v 1.7 2006-11-29 22:37:08 marc Exp $
2    Copyright (c) 2005-2006, Index Data.
3
4    See the LICENSE file for details
5  */
6
7 #include "config.hpp"
8
9 #include <boost/program_options.hpp>
10 namespace po = boost::program_options;
11
12 #include <iostream>
13 #include <stdexcept>
14 #include <libxml/xinclude.h>
15
16 #include "filter.hpp"
17 #include "package.hpp"
18 #include "router_flexml.hpp"
19 #include "factory_static.hpp"
20
21 namespace mp = metaproxy_1;
22
23 int main(int argc, char **argv)
24 {
25     try 
26     {
27         po::options_description desc("Allowed options");
28         desc.add_options()
29             ("help,h", "produce help message")
30             ("version,V", "show version")
31             ("config", po::value< std::vector<std::string> >(), "xml config")
32             ;
33         
34         po::positional_options_description p;
35         p.add("config", -1);
36         
37         po::variables_map vm;        
38         po::store(po::command_line_parser(argc, argv).
39                   options(desc).positional(p).run(), vm);
40         po::notify(vm);
41         
42         if (vm.count("help")) {
43             std::cout << desc << "\n";
44             return 1;
45         }
46         if (vm.count("version")) {
47             std::cout << "Metaproxy " VERSION "\n";
48             return 0;
49         }
50         xmlDocPtr doc = 0;
51         if (vm.count("config"))
52         {
53             std::vector<std::string> config_fnames = 
54                 vm["config"].as< std::vector<std::string> >();
55
56             if (config_fnames.size() != 1)
57             {
58                 std::cerr << "Only one configuration must be given\n";
59                 std::exit(1);
60             }
61             
62             // need to parse with xinclude tags 
63             // XML_PARSE_XINCLUDE XML_PARSE_NOBLANKS  
64             // XML_PARSE_NSCLEAN XML_PARSE_NONET 
65             doc = xmlReadFile(config_fnames[0].c_str(), 
66                               NULL, 
67                               XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
68                               + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
69
70             if (!doc)
71             {
72                 std::cerr << "XML parsing failed\n";
73                 std::exit(1);
74             }
75             // and perform Xinclude then
76             if (xmlXIncludeProcess(doc) <= 0) {
77                 std::cerr << "XInclude processing failed\n";
78                 std::exit(1);
79             }
80         }
81         else
82         {
83             std::cerr << "No configuration given\n";
84             std::exit(1);
85         }
86         if (doc)
87         {
88             try {
89                 mp::FactoryStatic factory;
90                 mp::RouterFleXML router(doc, factory);
91                 mp::Package pack;
92                 pack.router(router).move();
93             }
94             catch (std::runtime_error &e) {
95                 std::cerr << "std::runtime error: " << e.what() << "\n";
96                 std::exit(1);
97             }
98             xmlFreeDoc(doc);
99         }
100     }
101     catch (po::unknown_option &e) {
102         std::cerr << e.what() << "; use --help for list of options\n";
103         std::exit(1);
104     }
105     catch (std::logic_error &e) {
106         std::cerr << "std::logic error: " << e.what() << "\n";
107         std::exit(1);
108     }
109     catch (std::runtime_error &e) {
110         std::cerr << "std::runtime error: " << e.what() << "\n";
111         std::exit(1);
112     }
113     catch ( ... ) {
114         std::cerr << "Unknown Exception" << std::endl;
115         std::exit(1);
116     }
117     std::exit(0);
118 }
119
120
121 /*
122  * Local variables:
123  * c-basic-offset: 4
124  * indent-tabs-mode: nil
125  * c-file-style: "stroustrup"
126  * End:
127  * vim: shiftwidth=4 tabstop=8 expandtab
128  */