/
xsltproc.cpp
176 lines (154 loc) · 4.28 KB
/
xsltproc.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
167
168
169
170
171
172
173
174
175
176
#include "xsltproc.h"
#include <string.h>
#include <libxml/xmlmemory.h>
#include <libxml/debugXML.h>
#include <libxml/HTMLtree.h>
#include <libxml/xmlIO.h>
#include <libxml/DOCBparser.h>
#include <libxml/xinclude.h>
#include <libxml/catalog.h>
#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>
#include <libexslt/exslt.h>
#include <QFileInfo>
#include <QList>
#include <QtDebug>
Xsltproc::Xsltproc()
{
exsltRegisterAll();
xmlSubstituteEntitiesDefault(1);
xmlLoadExtDtdDefaultValue = 1;
mStylesheet = nullptr;
mOutput = nullptr;
mXml = nullptr;
mParams = nullptr;
mNParams = 0;
}
Xsltproc::~Xsltproc()
{
xsltCleanupGlobals();
xmlCleanupParser();
}
void Xsltproc::setStyleSheet(const QString & filename)
{
mStyleSheetFilename = filename;
}
void Xsltproc::setXmlFilename(const QString & filename)
{
mXmlFilename = filename;
}
void Xsltproc::setOutputFilename(const QString & filename)
{
mOutputFilename = filename;
}
void Xsltproc::setParameters(const QHash<QString,QString> & parameters)
{
for(unsigned int i=0; i<mNParams; i++)
{
delete mParams[i];
}
if( mParams != nullptr ) delete mParams;
mNParams = static_cast<unsigned int>(parameters.count());
mParams = new char*[2*mNParams+1];
QList<QByteArray*> byteArrays;
QHashIterator<QString,QString> iter(parameters);
int i=0;
while(iter.hasNext())
{
iter.next();
byteArrays << new QByteArray(iter.key().toUtf8());
mParams[i++] = byteArrays.last()->data();
QString paramValue = "\"" + iter.value() + "\"";
byteArrays << new QByteArray(paramValue.toUtf8());
mParams[i++] = byteArrays.last()->data();
}
mParams[i] = nullptr;
}
Xsltproc::ReturnValue Xsltproc::execute()
{
Xsltproc::ReturnValue retval = Xsltproc::Success;
try
{
if( freopen(mErrorFilename.toUtf8().data(),"w",stderr) == nullptr ) throw Xsltproc::GenericFailure;
mStylesheet = xsltParseStylesheetFile( reinterpret_cast<const xmlChar*>(mStyleSheetFilename.toUtf8().data()) );
if(mStylesheet == nullptr || getErrorFileHasContents())
throw Xsltproc::InvalidStylesheet;
mXml = xmlParseFile( static_cast<const char*>(mXmlFilename.toUtf8().data()) );
if(mXml == nullptr || getErrorFileHasContents())
throw Xsltproc::InvalidXmlFile;
mOutput = xsltApplyStylesheet(mStylesheet, mXml, const_cast<const char**>(mParams) );
if(mOutput == nullptr || getErrorFileHasContents())
throw Xsltproc::ApplyStylesheetFailure;
if( 0 == xsltSaveResultToFilename(mOutputFilename.toUtf8().data(), mOutput, mStylesheet, 0) )
throw Xsltproc::CouldNotOpenOutput;
}
catch(Xsltproc::ReturnValue e)
{
retval = e;
}
fclose(stderr);
freeResources();
return retval;
}
void Xsltproc::freeResources()
{
if( mStylesheet != nullptr )
{
xsltFreeStylesheet(mStylesheet);
mStylesheet = nullptr;
}
if( mOutput != nullptr )
{
xmlFreeDoc(mOutput);
mOutput = nullptr;
}
if( mXml != nullptr )
{
xmlFreeDoc(mXml);
mXml=nullptr;
}
}
bool Xsltproc::getErrorFileHasContents()
{
fclose(stderr);
QFileInfo info(mErrorFilename);
if( info.size() > 0 )
{
return true;
}
else
{
if( freopen(mErrorFilename.toUtf8().data(),"w",stderr) == nullptr ) throw Xsltproc::GenericFailure;
return false;
}
}
void Xsltproc::setErrorFilename(const QString & filename)
{
mErrorFilename = filename;
}
QDebug operator<<(QDebug dbg, const Xsltproc::ReturnValue &value)
{
switch(value) {
case Xsltproc::Success:
dbg << "Xsltproc::Success";
break;
case Xsltproc::InvalidStylesheet:
dbg << "Xsltproc::InvalidStylesheet";
break;
case Xsltproc::InvalidXmlFile:
dbg << "Xsltproc::InvalidXmlFile";
break;
case Xsltproc::CouldNotOpenOutput:
dbg << "Xsltproc::CouldNotOpenOutput";
break;
case Xsltproc::ApplyStylesheetFailure:
dbg << "Xsltproc::ApplyStylesheetFailure";
break;
case Xsltproc::GenericFailure:
dbg << "Xsltproc::GenericFailure";
break;
}
return dbg;
}