PostgreSQL Source Code git master
Loading...
Searching...
No Matches
xslt_proc.c
Go to the documentation of this file.
1/*
2 * contrib/xml2/xslt_proc.c
3 *
4 * XSLT processing functions (requiring libxslt)
5 *
6 * John Gray, for Torchbox 2003-04-01
7 */
8#include "postgres.h"
9
10#include "fmgr.h"
11#include "utils/builtins.h"
12#include "utils/xml.h"
13#include "varatt.h"
14
15#ifdef USE_LIBXSLT
16
17/* libxml includes */
18
19#include <libxml/xpath.h>
20#include <libxml/tree.h>
21#include <libxml/xmlmemory.h>
22
23/* libxslt includes */
24
25#include <libxslt/xslt.h>
26#include <libxslt/xsltInternals.h>
27#include <libxslt/security.h>
28#include <libxslt/transform.h>
29#include <libxslt/xsltutils.h>
30#endif /* USE_LIBXSLT */
31
32
33#ifdef USE_LIBXSLT
34
35/* declarations to come from xpath.c */
37
38/* local defs */
39static const char **parse_params(text *paramstr);
40#endif /* USE_LIBXSLT */
41
42
44
47{
48#ifdef USE_LIBXSLT
49
52 text *volatile result = NULL;
54 const char **params;
57 volatile xmlDocPtr doctree = NULL;
58 volatile xmlDocPtr ssdoc = NULL;
59 volatile xmlDocPtr restree = NULL;
62 volatile int resstat = -1;
63 xmlChar *volatile resstr = NULL;
64
65 if (fcinfo->nargs == 3)
66 {
68 params = parse_params(paramstr);
69 }
70 else
71 {
72 /* No parameters */
73 params = palloc_object(const char *);
74 params[0] = NULL;
75 }
76
77 /* Setup parser */
79
80 PG_TRY();
81 {
83 int reslen = 0;
84
85 /* Parse document */
86 doctree = xmlReadMemory((char *) VARDATA_ANY(doct),
89
90 if (doctree == NULL || pg_xml_error_occurred(xmlerrcxt))
92 "error parsing XML document");
93
94 /* Same for stylesheet */
98
101 "error parsing stylesheet as XML document");
102
103 /*
104 * On success, the stylesheet owns ssdoc, with xsltFreeStylesheet()
105 * calling xmlFreeDoc() on its associated doc.
106 */
108 if (stylesheet != NULL)
109 ssdoc = NULL;
110
113 "failed to parse stylesheet");
114
116
117 xslt_sec_prefs_error = false;
120
122 xsltSecurityForbid) != 0)
125 xsltSecurityForbid) != 0)
128 xsltSecurityForbid) != 0)
131 xsltSecurityForbid) != 0)
134 xsltSecurityForbid) != 0)
138
141 (errmsg("could not set libxslt security preferences")));
142
143 restree = xsltApplyStylesheetUser(stylesheet, doctree, params,
145
148 "failed to apply stylesheet");
149
152
153 if (resstat >= 0)
154 {
155 /*
156 * If an empty string has been returned, resstr would be NULL. In
157 * this case, assume that the result is an empty string.
158 */
159 if (reslen == 0)
161 else
162 result = cstring_to_text_with_len((char *) resstr, reslen);
163 }
164 }
165 PG_CATCH();
166 {
167 if (restree != NULL)
169 if (xslt_ctxt != NULL)
171 if (xslt_sec_prefs != NULL)
173 if (stylesheet != NULL)
175 if (ssdoc != NULL)
177 if (doctree != NULL)
178 xmlFreeDoc(doctree);
179 if (resstr != NULL)
182
183 pg_xml_done(xmlerrcxt, true);
184
185 PG_RE_THROW();
186 }
187 PG_END_TRY();
188
193 xmlFreeDoc(doctree);
195
196 if (resstr)
198
199 pg_xml_done(xmlerrcxt, false);
200
201 /* XXX this is pretty dubious, really ought to throw error instead */
202 if (resstat < 0)
204
206#else /* !USE_LIBXSLT */
207
210 errmsg("xslt_process() is not available without libxslt")));
212#endif /* USE_LIBXSLT */
213}
214
215#ifdef USE_LIBXSLT
216
217static const char **
219{
220 char *pos;
221 char *pstr;
222 char *nvsep = "=";
223 char *itsep = ",";
224 const char **params;
225 int max_params;
226 int nparams;
227
229
230 max_params = 20; /* must be even! */
231 params = (const char **) palloc((max_params + 1) * sizeof(char *));
232 nparams = 0;
233
234 pos = pstr;
235
236 while (*pos != '\0')
237 {
238 if (nparams >= max_params)
239 {
240 max_params *= 2;
241 params = (const char **) repalloc(params,
242 (max_params + 1) * sizeof(char *));
243 }
244 params[nparams++] = pos;
245 pos = strstr(pos, nvsep);
246 if (pos != NULL)
247 {
248 *pos = '\0';
249 pos++;
250 }
251 else
252 {
253 /* No equal sign, so ignore this "parameter" */
254 nparams--;
255 break;
256 }
257
258 /* since max_params is even, we still have nparams < max_params */
259 params[nparams++] = pos;
260 pos = strstr(pos, itsep);
261 if (pos != NULL)
262 {
263 *pos = '\0';
264 pos++;
265 }
266 else
267 break;
268 }
269
270 /* Add the terminator marker; we left room for it in the palloc's */
271 params[nparams] = NULL;
272
273 return params;
274}
275
276#endif /* USE_LIBXSLT */
uint32 result
int errcode(int sqlerrcode)
Definition elog.c:875
#define PG_RE_THROW()
Definition elog.h:407
#define PG_TRY(...)
Definition elog.h:374
#define PG_END_TRY(...)
Definition elog.h:399
#define ERROR
Definition elog.h:40
#define PG_CATCH(...)
Definition elog.h:384
#define ereport(elevel,...)
Definition elog.h:152
#define palloc_object(type)
Definition fe_memutils.h:89
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_RETURN_NULL()
Definition fmgr.h:346
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1635
void * palloc(Size size)
Definition mcxt.c:1390
static char * errmsg
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
Definition c.h:776
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486
text * cstring_to_text_with_len(const char *s, int len)
Definition varlena.c:196
text * cstring_to_text(const char *s)
Definition varlena.c:184
char * text_to_cstring(const text *t)
Definition varlena.c:217
struct PgXmlErrorContext PgXmlErrorContext
Definition xml.h:48
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
bool pg_xml_error_occurred(PgXmlErrorContext *errcxt)
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
PgXmlStrictness
Definition xml.h:40
@ PG_XML_STRICTNESS_LEGACY
Definition xml.h:41
PgXmlErrorContext * pgxml_parser_init(PgXmlStrictness strictness)
Definition xpath.c:68
Datum xslt_process(PG_FUNCTION_ARGS)
Definition xslt_proc.c:46