/
Main.cpp
88 lines (75 loc) · 2.06 KB
/
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <boost/program_options.hpp>
#include <iostream>
#include <iterator>
#include <string>
#include <fstream>
#include "xml/xml_processor.h"
#include "dtd/dtd.h"
#include "validation/Validateur.hpp"
using namespace std;
namespace po = boost::program_options;
int main(int ac, char * av[])
{
try {
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("xml", po::value<string>(), "Set xml file to parse")
("dtd", po::value<string>(), "Set dtd file to parse")
("validate", "Validates the xml file with the dtd file.")
("showxml", "Shows the content of the xml file, if one is given.")
("showdtd", "Shows the content of the dtd file, if one is given.")
;
po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);
bool atLeastOneOption = false;
bool hasXml = false, hasDtd = false;
xml::Document* xmlDoc = NULL;
if (vm.count("xml")) {
xmlDoc = parseXML(vm["xml"].as<string>().c_str());
if (xmlDoc!= NULL)
{
hasXml = true;
if (vm.count("showxml")) cout << *xmlDoc << "\n" << endl;
}
atLeastOneOption = true;
}
dtd::Document* dtdDoc = NULL;
if (vm.count("dtd"))
{
dtdDoc = parseDTD(vm["dtd"].as<string>().c_str());
if (dtdDoc != NULL)
{
hasDtd = true;
if (vm.count("showdtd")) cout << dtdDoc << "\n" << endl;
}
atLeastOneOption = true;
}
if (vm.count("validate"))
{
if (hasXml && hasDtd)
{
if (Validateur::validationDocument(*dtdDoc, *xmlDoc))
{
cout << "The XML file is conform to the DTD file." << endl;
} else {
cout << "Error : the XML file is not conform to the DTD file." << endl;
}
} else {
cout << "There is either no dtd or xml file. Please retry." << endl;
}
atLeastOneOption = true;
}
if (hasXml) delete xmlDoc;
if (hasDtd) delete dtdDoc;
if (vm.count("help") || !atLeastOneOption) {
cout << desc << endl;
return 1;
}
} catch(exception& e) {
cerr << "error: " << e.what() << "\n";
return 1;
}
return 0;
}