-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
150 lines (117 loc) · 3.93 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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <dtd.h>
#include <document.h>
#include <dtd.tab.h>
#include <xml.tab.h>
#include <xsl.tab.h>
#include <Validate.h>
#include <html_builder.h>
using namespace std;
#define EXIT(success) \
if (xmlDocument != NULL) delete xmlDocument;\
if (dtd != NULL) delete dtd;\
if (xsl != NULL) delete xsl;\
return success ? EXIT_SUCCESS : EXIT_FAILURE;\
int main(int argc, char** argv) {
extern FILE *dtdin, *xmlin, *xslin;
extern int dtdparse(Dtd**);
extern int xmlparse(Document**);
extern int xslparse(Document**);
int err;
string xmlfile, dtdfile, xslfile;
Document* xmlDocument = NULL;
Document* xsl = NULL;
Dtd* dtd = NULL;
/** Lecture paramètres ligne de commande ***********************************/
xmlfile = string(argv[1]);
if(argc > 2 && strcmp("_", argv[2]) != 0) {
dtdfile = string(argv[2]);
}
if(argc > 3) {
xslfile = string(argv[3]);
}
//dtddebug = 1; // pour désactiver l'affichage de l'exécution du parser LALR, commenter cette ligne
/** Lecture XML ************************************************************/
cerr << "Fichier XML: " << xmlfile << endl;
xmlin = fopen(xmlfile.c_str(), "r");
if (!xmlin) {
cerr << "Impossible d'ouvrir le fichier nommé '" << xmlfile << "'" << endl;
EXIT(false);
}
err = xmlparse(&xmlDocument);
fclose(xmlin);
if (err != 0) {
cerr << "Analyse du XML terminée avec " << err << " erreurs" << endl;
EXIT(false);
}
/** Lecture DTD ***********************************************************/
if ( dtdfile.empty() && !xmlDocument->getDtdFileName().empty()) {
string sep = "/";
string tmp = string(xmlfile);
// Reconstitution du chemin du fichier DTD à partir de celui du XML et de la valeur indiquée
// dans la balise <!DOCTYPE> du XML. Ne supporte que les chemins relatifs.
unsigned found = tmp.rfind(sep); // position de la dernière occurence de sep
if ( found != std::string::npos) {
tmp.replace(found+1, tmp.substr(found+1).length(), xmlDocument->getDtdFileName());
}
dtdfile = string(tmp.c_str());
}
cerr << "Fichier DTD: " << dtdfile << endl;
if (dtdfile.empty()) {
cerr << "Pas de fichier DTD." << endl;
} else {
dtdin = fopen(dtdfile.c_str(), "r");
if (!dtdin) {
cerr << "Impossible d'ouvrir le fichier nommé '" << dtdfile << "'" << endl;
EXIT(false);
}
err = dtdparse(&dtd);
fclose(dtdin);
if (err != 0) {
cerr << "Analyse du DTD terminée avec " << err << " erreurs" << endl;
EXIT(false);
}
/** Analyse XML par rapport à la DTD ***************************************/
if (!dtd->isValid()) {
cerr << "La DTD n'est pas cohérente. (Des éléments non déclarés peut être?)" << endl;
EXIT(false);
}
//Validating xml with dtd
Validate validator(xmlDocument, dtd);
if (validator.isValid()) {
cerr << "Le document XML est conforme à la DTD." << endl;
} else {
cerr << "Le document XML n'est pas conforme à la DTD." << endl;
EXIT(false);
}
}
/** Transformation XSL *****************************************************/
cerr << "Fichier XSL: " << xslfile << endl;
if (xslfile.empty()) {
cerr << "Pas de fichier XSL. " << endl;
} else {
xslin = fopen(xslfile.c_str(), "r");
if (!xslin) {
cerr << "Impossible d'ouvrir le fichier nommé '" << xslfile << "'" << endl;
EXIT(false);
}
err = xslparse(&xsl);
fclose(xslin);
if (err != 0) {
cerr << "Analyse du XSL terminée avec " << err << " erreurs" << endl;
EXIT(false);
}
// cerr << "------------------------------------" << endl;
// cerr << xsl->getRoot()->toXML() << endl;
// cerr << "------------------------------------" << endl;
// cerr << xmlDocument->getRoot()->toXML() << endl;
// cerr << "------------------------------------" << endl;
HTMLBuilder htmlb((XSLElement*)xsl->getRoot(), xmlDocument->getRoot());
cout << htmlb.html() << endl;
}
/****************************************************************/
EXIT(true);
}