GPL v2.
[metaproxy-moved-to-github.git] / src / metaproxy_prog.cpp
1 /* $Id: metaproxy_prog.cpp,v 1.10 2007-05-09 21:23:09 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4 This file is part of Metaproxy.
5
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include "config.hpp"
23
24 #include <boost/program_options.hpp>
25 namespace po = boost::program_options;
26
27 #include <iostream>
28 #include <stdexcept>
29 #include <libxml/xinclude.h>
30
31 #include "filter.hpp"
32 #include "package.hpp"
33 #include "router_flexml.hpp"
34 #include "factory_static.hpp"
35
36 namespace mp = metaproxy_1;
37
38 int main(int argc, char **argv)
39 {
40     try 
41     {
42         po::options_description desc("Allowed options");
43         desc.add_options()
44             ("help,h", "produce help message")
45             ("version,V", "show version")
46             ("config", po::value< std::vector<std::string> >(), "xml config")
47             ;
48         
49         po::positional_options_description p;
50         p.add("config", -1);
51         
52         po::variables_map vm;        
53         po::store(po::command_line_parser(argc, argv).
54                   options(desc).positional(p).run(), vm);
55         po::notify(vm);
56         
57         if (vm.count("help")) {
58             std::cout << desc << "\n";
59             return 1;
60         }
61         if (vm.count("version")) {
62             std::cout << "Metaproxy " VERSION "\n";
63             return 0;
64         }
65         xmlDocPtr doc = 0;
66         if (vm.count("config"))
67         {
68             std::vector<std::string> config_fnames = 
69                 vm["config"].as< std::vector<std::string> >();
70
71             if (config_fnames.size() != 1)
72             {
73                 std::cerr << "Only one configuration must be given\n";
74                 std::exit(1);
75             }
76             
77             // need to parse with xinclude tags 
78             // XML_PARSE_XINCLUDE XML_PARSE_NOBLANKS  
79             // XML_PARSE_NSCLEAN XML_PARSE_NONET 
80             doc = xmlReadFile(config_fnames[0].c_str(), 
81                               NULL, 
82                               XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
83                               + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
84
85             if (!doc)
86             {
87                 std::cerr << "XML parsing failed\n";
88                 std::exit(1);
89             }
90             // and perform Xinclude then
91             if (xmlXIncludeProcess(doc) > 0) {
92                 std::cerr << "processing XInclude directive\n";
93             }
94         }
95         else
96         {
97             std::cerr << "No configuration given\n";
98             std::exit(1);
99         }
100         if (doc)
101         {
102             try {
103                 mp::FactoryStatic factory;
104                 mp::RouterFleXML router(doc, factory);
105                 mp::Package pack;
106                 pack.router(router).move();
107             }
108             catch (std::runtime_error &e) {
109                 std::cerr << "std::runtime error: " << e.what() << "\n";
110                 std::exit(1);
111             }
112             xmlFreeDoc(doc);
113         }
114     }
115     catch (po::unknown_option &e) {
116         std::cerr << e.what() << "; use --help for list of options\n";
117         std::exit(1);
118     }
119     catch (std::logic_error &e) {
120         std::cerr << "std::logic error: " << e.what() << "\n";
121         std::exit(1);
122     }
123     catch (std::runtime_error &e) {
124         std::cerr << "std::runtime error: " << e.what() << "\n";
125         std::exit(1);
126     }
127     catch ( ... ) {
128         std::cerr << "Unknown Exception" << std::endl;
129         std::exit(1);
130     }
131     std::exit(0);
132 }
133
134
135 /*
136  * Local variables:
137  * c-basic-offset: 4
138  * indent-tabs-mode: nil
139  * c-file-style: "stroustrup"
140  * End:
141  * vim: shiftwidth=4 tabstop=8 expandtab
142  */