forked from H4311/Projet-Grammaire-Langages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
166 lines (148 loc) · 4.47 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#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"
#include "xsl/XSLProcessor.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")
("xsl", po::value<string>(), "Set xsl file to parse")
("dtdxml", po::value<string>(), "Set xml dtd file to parse")
("dtdxsl", po::value<string>(), "Set xml dtd file to parse")
("validate", "Validates the xml and/or xsl file with the dtd files.")
("showxml", "Shows the content of the xml file, if one is given.")
("showdtdxml", "Shows the content of the xml dtd file, if one is given.")
("showdtdxsl", "Shows the content of the xsl dtd file, if one is given.")
("showxsl", "Shows the content of the xsl file, if one is given.")
("showhtml", "Shows the content of the generated html file, if a xsl and xml files are 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, hasXsl = false, hasXslDtd = false;
bool xslProcessorOwnsXslDtd = 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("dtdxml"))
{
dtdDoc = parseDTD(vm["dtdxml"].as<string>().c_str());
if (dtdDoc != NULL)
{
hasDtd = true;
if (vm.count("showdtdxml")) cout << dtdDoc << "\n" << endl;
}
atLeastOneOption = true;
}
xml::Document* xslDoc = NULL;
if (vm.count("xsl")) {
xslDoc = parseXML(vm["xsl"].as<string>().c_str());
if (xslDoc!= NULL)
{
hasXsl = true;
if (vm.count("showxsl")) cout << *xslDoc << "\n" << endl;
}
atLeastOneOption = true;
}
dtd::Document* xslDtdDoc = NULL;
if (vm.count("dtdxsl"))
{
xslDtdDoc = parseDTD(vm["dtdxsl"].as<string>().c_str());
if (xslDtdDoc != NULL)
{
hasXslDtd = true;
if (vm.count("showdtdxsl")) cout << xslDtdDoc << "\n" << endl;
}
atLeastOneOption = true;
}
xsl::XSLProcessor xslProcessor = xsl::XSLProcessor();
bool xslValidated = false;
if (vm.count("validate"))
{
if (hasXml && hasDtd)
{
if (Validateur::validationDocument(*dtdDoc, *xmlDoc))
{
cout << "The XML file is conform to the XML-DTD file." << endl;
} else {
cout << "Error : the XML file is not conform to the DTD file." << endl;
}
}
else if (hasXsl && hasXslDtd) {
try{
xslProcessor.setXslDTD(xslDtdDoc);
xslProcessorOwnsXslDtd = true;
xslProcessor.processXslFile(xslDoc);
cout << "The XSL file is conform to the XSL-DTD file." << endl;
xslValidated = true;
}catch(string s){
cout << "Error : the XSL file is not conform to the XSL-DTD file. The error is :" << endl;
cout << s << endl;
}
}
else {
cout << "There is either no dtd or xml/xsl file. Please retry." << endl;
}
atLeastOneOption = true;
}
if (vm.count("showhtml"))
{
if (hasXml && hasXsl && hasXslDtd)
{
if (xslValidated) {
xml::Document* documentHTML = xslProcessor.generateHtmlFile(xmlDoc);
cout << *documentHTML << endl;
delete documentHTML;
}
else {
try{
xslProcessor.setXslDTD(xslDtdDoc);
xslProcessorOwnsXslDtd = true;
xslProcessor.processXslFile(xslDoc);
xml::Document* documentHTML = xslProcessor.generateHtmlFile(xmlDoc);
cout << *documentHTML << endl;
delete documentHTML;
}catch(string s){
cout << "Error : the XSL file is not conform to the XSL-DTD file. The error is :" << endl;
cout << s << endl;
}
}
}
else {
cout << "There is either no xsl-dtd or xml or xsl file. Please retry." << endl;
}
atLeastOneOption = true;
}
if (hasXml) delete xmlDoc;
if (hasDtd) delete dtdDoc;
if (hasXsl) delete xslDoc;
if (hasXslDtd && !xslProcessorOwnsXslDtd) delete xslDtdDoc;
if (vm.count("help") || !atLeastOneOption) {
cout << desc << endl;
return 1;
}
} catch(exception& e) {
cerr << "error: " << e.what() << "\n";
return 1;
}
return 0;
}