Year 2007.
[metaproxy-moved-to-github.git] / src / metaproxy_prog.cpp
1 /* $Id: metaproxy_prog.cpp,v 1.9 2007-01-25 14:05:54 adam Exp $
2    Copyright (c) 2005-2007, 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 << "processing XInclude directive\n";
78             }
79         }
80         else
81         {
82             std::cerr << "No configuration given\n";
83             std::exit(1);
84         }
85         if (doc)
86         {
87             try {
88                 mp::FactoryStatic factory;
89                 mp::RouterFleXML router(doc, factory);
90                 mp::Package pack;
91                 pack.router(router).move();
92             }
93             catch (std::runtime_error &e) {
94                 std::cerr << "std::runtime error: " << e.what() << "\n";
95                 std::exit(1);
96             }
97             xmlFreeDoc(doc);
98         }
99     }
100     catch (po::unknown_option &e) {
101         std::cerr << e.what() << "; use --help for list of options\n";
102         std::exit(1);
103     }
104     catch (std::logic_error &e) {
105         std::cerr << "std::logic error: " << e.what() << "\n";
106         std::exit(1);
107     }
108     catch (std::runtime_error &e) {
109         std::cerr << "std::runtime error: " << e.what() << "\n";
110         std::exit(1);
111     }
112     catch ( ... ) {
113         std::cerr << "Unknown Exception" << std::endl;
114         std::exit(1);
115     }
116     std::exit(0);
117 }
118
119
120 /*
121  * Local variables:
122  * c-basic-offset: 4
123  * indent-tabs-mode: nil
124  * c-file-style: "stroustrup"
125  * End:
126  * vim: shiftwidth=4 tabstop=8 expandtab
127  */