summaryrefslogtreecommitdiff
path: root/plugins/Variables/parse_xml.cpp
blob: 5d3b5778a0d4a58ed54b82ec52bd8ff8b811502c (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
    Variables Plugin for Miranda-IM (www.miranda-im.org)
    Copyright 2003-2006 P. Boon

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#include "variables.h"
#include "parse_xml.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>

xsltStylesheetPtr (*pXsltParseStylesheetDoc)(xmlDocPtr doc);
xmlDocPtr (*pXmlParseMemory)(const char * buffer, int size);

void (*pXmlFree)(void *mem);
xmlDocPtr (*pXmlParseFile)(const char * filename);
void (*pXmlFreeDoc)(xmlDocPtr cur);
void (*pXmlInitParser)(void);
void (*pXmlCleanupParser)(void);
int (*pXmlSubstituteEntitiesDefault)(int val);

xsltStylesheetPtr (*pXsltParseStylesheetFile)(const xmlChar * filename);
int (*pXsltSaveResultToString)(xmlChar ** doc_txt_ptr, int * doc_txt_len, xmlDocPtr result, xsltStylesheetPtr style);
void (*pXsltFreeStylesheet)(xsltStylesheetPtr sheet);
xmlDocPtr (*pXsltApplyStylesheet)(xsltStylesheetPtr style, xmlDocPtr doc, const char ** params);
void (*pXsltCleanupGlobals)(void);

/*
	pattern, subject
*/
static TCHAR *parseXslts(ARGUMENTSINFO *ai) {

	char *szStyleSheet, *szDoc;
	TCHAR *tszRes;
	xsltStylesheetPtr cur = NULL;
	xmlDocPtr doc, res, sdoc;
	xmlChar *xmlChRes = NULL;
	int resLen;
	
	if (ai->argc != 3) {
		return NULL;
	}
#ifdef UNICODE
	szStyleSheet = u2a(ai->targv[1]);
	szDoc = u2a(ai->targv[2]);
#else
	szStyleSheet = _strdup(ai->argv[1]);
	szDoc = _strdup(ai->argv[2]);
#endif

	log_debugA("calling xsltParseMemory");
	sdoc = pXmlParseMemory(szStyleSheet, strlen(szStyleSheet));
	if (sdoc == NULL) {
		
		return NULL;
	}
	
	log_debugA("calling xsltParseStylesheetDoc");
	cur = pXsltParseStylesheetDoc(sdoc);
	if (cur == NULL) {
		log_debugA("calling xsltFreeDoc");
		pXmlFreeDoc(sdoc);
	
		return NULL;
	}
	
	log_debugA("calling xsltParseMemory");
	doc = pXmlParseMemory(szDoc, strlen(szDoc));
	if (doc == NULL) {
		log_debugA("calling xsltFreeDoc");
		pXmlFreeDoc(sdoc);
		log_debugA("calling xsltFreeStylesheet");
		pXsltFreeStylesheet(cur);
	
		return NULL;
	}

	log_debugA("calling xsltApplyStylesheet");
	res = pXsltApplyStylesheet(cur, doc, NULL);
	if (res == NULL) {
		log_debugA("calling xsltFreeStylesheet");
		pXsltFreeStylesheet(cur);
		log_debugA("calling xsltFreeDoc");
		pXmlFreeDoc(doc);
		log_debugA("calling xsltFreeDoc");
		pXmlFreeDoc(sdoc);

		return NULL;
	}

	log_debugA("calling xsltSaveResultToString");
	pXsltSaveResultToString(&xmlChRes, &resLen, res, cur);
	log_debugA("calling xsltFreeStylesheet");
	pXsltFreeStylesheet(cur);
	log_debugA("calling xsltFreeDoc");
	pXmlFreeDoc(res);
	log_debugA("calling xsltFreeDoc");
	pXmlFreeDoc(doc);
	log_debugA("calling free");
	free(szStyleSheet);
	log_debugA("calling free");
	free(szDoc);
#ifdef UNICODE
	tszRes = a2u((char *)xmlChRes);
#else
	tszRes = _strdup((const char *)xmlChRes);
#endif
	log_debugA("calling xmlFree");
	pXmlFree(xmlChRes);
	
	return tszRes;
}

/*
	files
*/
static TCHAR *parseXsltf(ARGUMENTSINFO *ai) {

	char *szStyleSheet, *szDoc;
	TCHAR *tszRes;
	xsltStylesheetPtr cur = NULL;
	xmlDocPtr doc, res;
	xmlChar *xmlChRes = NULL;
	int resLen;
	
	if (ai->argc != 3) {
		return NULL;
	}
#ifdef UNICODE
	szStyleSheet = u2a(ai->targv[1]);
	szDoc = u2a(ai->targv[2]);
#else
	szStyleSheet = _strdup(ai->argv[1]);
	szDoc = _strdup(ai->argv[2]);
#endif

	log_debugA("xslt with %s and %s", szStyleSheet, szDoc);

	log_debugA("calling xsltParseStylesheetFile");
	cur = pXsltParseStylesheetFile((const xmlChar *)szStyleSheet);
	if (cur == NULL) {
	
		return NULL;
	}
	log_debugA("result: %x", cur);

	log_debugA("calling xmlParseFile");
	doc = pXmlParseFile(szDoc);
	if (doc == NULL) {
		pXsltFreeStylesheet(cur);
	
		return NULL;
	}
	log_debugA("result: %x", doc);

	log_debugA("calling xsltApplyStylesheet");
	res = pXsltApplyStylesheet(cur, doc, NULL);
	if (res == NULL) {
		pXsltFreeStylesheet(cur);
		pXmlFreeDoc(doc);

		return NULL;
	}
	log_debugA("result: %x", res);

	log_debugA("calling xsltSaveResultToString");
	pXsltSaveResultToString(&xmlChRes, &resLen, res, cur);

	log_debugA("calling xsltFreeStylesheet(cur)");
	pXsltFreeStylesheet(cur);

	log_debugA("calling xmlFreeDoc(res)");
	pXmlFreeDoc(res);

	log_debugA("calling xmlFreeDoc(doc)");
	pXmlFreeDoc(doc);

	//log_debug("calling xsltCleanupGlobals");
    //pXsltCleanupGlobals();

	//log_debug("calling xmlCleanupParser");
    //pXmlCleanupParser();

	free(szStyleSheet);
	free(szDoc);
#ifdef UNICODE
	tszRes = a2u((char *)xmlChRes);
#else
	tszRes = _strdup((const char *)xmlChRes);
#endif
	log_debugA("calling xmlFree");
	pXmlFree(xmlChRes);
	
	return tszRes;
}

int initXslt() {

	HMODULE hModule;
	
	hModule = LoadLibraryA("libxml2.dll");
	if (hModule == NULL) {
		char path[MAX_PATH];
		char *cur;

		GetModuleFileNameA(NULL, path, sizeof(path));
		cur = strrchr(path, '\\');
		if (cur != NULL)
			strcpy(cur+1, "libxml2.dll");
		else
			strcpy(path, "libxml2.dll");
		hModule = LoadLibraryA(path);
	}
	if (hModule == NULL) {
		return -1;
	}
	
	pXmlParseFile = (struct _xmlDoc *(__cdecl *)(const char *))GetProcAddress(hModule, "xmlParseFile");
	pXmlFreeDoc = (void (__cdecl *)(struct _xmlDoc *))GetProcAddress(hModule, "xmlFreeDoc");
	pXmlCleanupParser = (void (__cdecl *)(void ))GetProcAddress(hModule, "xmlCleanupParser");
	pXmlInitParser = (void (__cdecl *)(void ))GetProcAddress(hModule, "xmlInitParser");
	pXmlParseMemory = (struct _xmlDoc *(__cdecl *)(const char *,int ))GetProcAddress(hModule, "xmlParseMemory");
	pXmlFree = (void (__cdecl *)(void *))*((void (__cdecl **)(void *))GetProcAddress(hModule, "xmlFree"));
	pXmlSubstituteEntitiesDefault = (int (__cdecl *)(int ))GetProcAddress(hModule, "xmlSubstituteEntitiesDefault");

	hModule = LoadLibraryA("libxslt.dll");
	if (hModule == NULL) {
		char path[MAX_PATH];
		char *cur;

		GetModuleFileNameA(NULL, path, sizeof(path));
		cur = strrchr(path, '\\');
		if (cur != NULL)
			strcpy(cur+1, "libxslt.dll");
		else
			strcpy(path, "libxslt.dll");
		hModule = LoadLibraryA(path);
	}
	if (hModule == NULL) {
		return -1;
	}

	/* xmlFree !!! pthreads? */

	pXsltParseStylesheetFile = (struct _xsltStylesheet *(__cdecl *)(const unsigned char *))GetProcAddress(hModule, "xsltParseStylesheetFile");
	pXsltFreeStylesheet = (void (__cdecl *)(struct _xsltStylesheet *))GetProcAddress(hModule, "xsltFreeStylesheet");
	pXsltApplyStylesheet = (struct _xmlDoc *(__cdecl *)(struct _xsltStylesheet *,struct _xmlDoc *,const char ** ))GetProcAddress(hModule, "xsltApplyStylesheet");
	pXsltSaveResultToString = (int (__cdecl *)(unsigned char ** ,int *,struct _xmlDoc *,struct _xsltStylesheet *))GetProcAddress(hModule, "xsltSaveResultToString");
	pXsltCleanupGlobals = (void (__cdecl *)(void ))GetProcAddress(hModule, "xsltCleanupGlobals");
	pXsltParseStylesheetDoc = (struct _xsltStylesheet *(__cdecl *)(struct _xmlDoc *))GetProcAddress(hModule, "xsltParseStylesheetDoc");

	/* init */
	pXmlInitParser();
	pXmlSubstituteEntitiesDefault(1);

	return 0;
}

int registerXsltTokens() {

	if (initXslt() != 0) {
		log_infoA("Variables: XSLT library not initialized");
		return -1;
	}

	registerIntToken(_T(XSLTF), parseXsltf, TRF_FUNCTION, "XML\t(x,y)\tapply stylesheet file x to document file y");
	registerIntToken(_T(XSLTS), parseXslts, TRF_FUNCTION, "XML\t(x,y)\tapply stylesheet x to document y");

	return 0;
}