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 restree = NULL;
61 volatile int resstat = -1;
62 xmlChar *volatile resstr = NULL;
63
64 if (fcinfo->nargs == 3)
65 {
67 params = parse_params(paramstr);
68 }
69 else
70 {
71 /* No parameters */
72 params = palloc_object(const char *);
73 params[0] = NULL;
74 }
75
76 /* Setup parser */
78
79 PG_TRY();
80 {
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 /* After this call we need not free ssdoc separately */
105
108 "failed to parse stylesheet");
109
111
112 xslt_sec_prefs_error = false;
115
117 xsltSecurityForbid) != 0)
120 xsltSecurityForbid) != 0)
123 xsltSecurityForbid) != 0)
126 xsltSecurityForbid) != 0)
129 xsltSecurityForbid) != 0)
133
136 (errmsg("could not set libxslt security preferences")));
137
138 restree = xsltApplyStylesheetUser(stylesheet, doctree, params,
140
143 "failed to apply stylesheet");
144
147
148 if (resstat >= 0)
149 {
150 /*
151 * If an empty string has been returned, resstr would be NULL. In
152 * this case, assume that the result is an empty string.
153 */
154 if (reslen == 0)
155 result = cstring_to_text("");
156 else
157 result = cstring_to_text_with_len((char *) resstr, reslen);
158 }
159 }
160 PG_CATCH();
161 {
162 if (restree != NULL)
164 if (xslt_ctxt != NULL)
166 if (xslt_sec_prefs != NULL)
168 if (stylesheet != NULL)
170 if (doctree != NULL)
171 xmlFreeDoc(doctree);
172 if (resstr != NULL)
175
176 pg_xml_done(xmlerrcxt, true);
177
178 PG_RE_THROW();
179 }
180 PG_END_TRY();
181
186 xmlFreeDoc(doctree);
188
189 if (resstr)
191
192 pg_xml_done(xmlerrcxt, false);
193
194 /* XXX this is pretty dubious, really ought to throw error instead */
195 if (resstat < 0)
197
198 PG_RETURN_TEXT_P(result);
199#else /* !USE_LIBXSLT */
200
203 errmsg("xslt_process() is not available without libxslt")));
205#endif /* USE_LIBXSLT */
206}
207
208#ifdef USE_LIBXSLT
209
210static const char **
212{
213 char *pos;
214 char *pstr;
215 char *nvsep = "=";
216 char *itsep = ",";
217 const char **params;
218 int max_params;
219 int nparams;
220
222
223 max_params = 20; /* must be even! */
224 params = (const char **) palloc((max_params + 1) * sizeof(char *));
225 nparams = 0;
226
227 pos = pstr;
228
229 while (*pos != '\0')
230 {
231 if (nparams >= max_params)
232 {
233 max_params *= 2;
234 params = (const char **) repalloc(params,
235 (max_params + 1) * sizeof(char *));
236 }
237 params[nparams++] = pos;
238 pos = strstr(pos, nvsep);
239 if (pos != NULL)
240 {
241 *pos = '\0';
242 pos++;
243 }
244 else
245 {
246 /* No equal sign, so ignore this "parameter" */
247 nparams--;
248 break;
249 }
250
251 /* since max_params is even, we still have nparams < max_params */
252 params[nparams++] = pos;
253 pos = strstr(pos, itsep);
254 if (pos != NULL)
255 {
256 *pos = '\0';
257 pos++;
258 }
259 else
260 break;
261 }
262
263 /* Add the terminator marker; we left room for it in the palloc's */
264 params[nparams] = NULL;
265
266 return params;
267}
268
269#endif /* USE_LIBXSLT */
int errcode(int sqlerrcode)
Definition elog.c:874
#define PG_RE_THROW()
Definition elog.h:405
#define PG_TRY(...)
Definition elog.h:372
#define PG_END_TRY(...)
Definition elog.h:397
#define ERROR
Definition elog.h:39
#define PG_CATCH(...)
Definition elog.h:382
#define ereport(elevel,...)
Definition elog.h:150
#define palloc_object(type)
Definition fe_memutils.h:74
#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:1632
void * palloc(Size size)
Definition mcxt.c:1387
static char * errmsg
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
Definition c.h:778
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