Update information_schema definition for not-null constraints
[pgsql.git] / contrib / xml2 / xslt_proc.c
blob2189bca86ff63b6b544e2e9ea248d9a0a322a27a
1 /*
2 * contrib/xml2/xslt_proc.c
4 * XSLT processing functions (requiring libxslt)
6 * John Gray, for Torchbox 2003-04-01
7 */
8 #include "postgres.h"
10 #include "executor/spi.h"
11 #include "fmgr.h"
12 #include "funcapi.h"
13 #include "miscadmin.h"
14 #include "utils/builtins.h"
15 #include "utils/xml.h"
17 #ifdef USE_LIBXSLT
19 /* libxml includes */
21 #include <libxml/xpath.h>
22 #include <libxml/tree.h>
23 #include <libxml/xmlmemory.h>
25 /* libxslt includes */
27 #include <libxslt/xslt.h>
28 #include <libxslt/xsltInternals.h>
29 #include <libxslt/security.h>
30 #include <libxslt/transform.h>
31 #include <libxslt/xsltutils.h>
32 #endif /* USE_LIBXSLT */
35 #ifdef USE_LIBXSLT
37 /* declarations to come from xpath.c */
38 extern PgXmlErrorContext *pgxml_parser_init(PgXmlStrictness strictness);
40 /* local defs */
41 static const char **parse_params(text *paramstr);
42 #endif /* USE_LIBXSLT */
45 PG_FUNCTION_INFO_V1(xslt_process);
47 Datum
48 xslt_process(PG_FUNCTION_ARGS)
50 #ifdef USE_LIBXSLT
52 text *doct = PG_GETARG_TEXT_PP(0);
53 text *ssheet = PG_GETARG_TEXT_PP(1);
54 text *result;
55 text *paramstr;
56 const char **params;
57 PgXmlErrorContext *xmlerrcxt;
58 volatile xsltStylesheetPtr stylesheet = NULL;
59 volatile xmlDocPtr doctree = NULL;
60 volatile xmlDocPtr restree = NULL;
61 volatile xsltSecurityPrefsPtr xslt_sec_prefs = NULL;
62 volatile xsltTransformContextPtr xslt_ctxt = NULL;
63 volatile int resstat = -1;
64 xmlChar *resstr = NULL;
65 int reslen = 0;
67 if (fcinfo->nargs == 3)
69 paramstr = PG_GETARG_TEXT_PP(2);
70 params = parse_params(paramstr);
72 else
74 /* No parameters */
75 params = (const char **) palloc(sizeof(char *));
76 params[0] = NULL;
79 /* Setup parser */
80 xmlerrcxt = pgxml_parser_init(PG_XML_STRICTNESS_LEGACY);
82 PG_TRY();
84 xmlDocPtr ssdoc;
85 bool xslt_sec_prefs_error;
87 /* Parse document */
88 doctree = xmlParseMemory((char *) VARDATA_ANY(doct),
89 VARSIZE_ANY_EXHDR(doct));
91 if (doctree == NULL)
92 xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
93 "error parsing XML document");
95 /* Same for stylesheet */
96 ssdoc = xmlParseMemory((char *) VARDATA_ANY(ssheet),
97 VARSIZE_ANY_EXHDR(ssheet));
99 if (ssdoc == NULL)
100 xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
101 "error parsing stylesheet as XML document");
103 /* After this call we need not free ssdoc separately */
104 stylesheet = xsltParseStylesheetDoc(ssdoc);
106 if (stylesheet == NULL)
107 xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
108 "failed to parse stylesheet");
110 xslt_ctxt = xsltNewTransformContext(stylesheet, doctree);
112 xslt_sec_prefs_error = false;
113 if ((xslt_sec_prefs = xsltNewSecurityPrefs()) == NULL)
114 xslt_sec_prefs_error = true;
116 if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_READ_FILE,
117 xsltSecurityForbid) != 0)
118 xslt_sec_prefs_error = true;
119 if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_WRITE_FILE,
120 xsltSecurityForbid) != 0)
121 xslt_sec_prefs_error = true;
122 if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_CREATE_DIRECTORY,
123 xsltSecurityForbid) != 0)
124 xslt_sec_prefs_error = true;
125 if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_READ_NETWORK,
126 xsltSecurityForbid) != 0)
127 xslt_sec_prefs_error = true;
128 if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_WRITE_NETWORK,
129 xsltSecurityForbid) != 0)
130 xslt_sec_prefs_error = true;
131 if (xsltSetCtxtSecurityPrefs(xslt_sec_prefs, xslt_ctxt) != 0)
132 xslt_sec_prefs_error = true;
134 if (xslt_sec_prefs_error)
135 ereport(ERROR,
136 (errmsg("could not set libxslt security preferences")));
138 restree = xsltApplyStylesheetUser(stylesheet, doctree, params,
139 NULL, NULL, xslt_ctxt);
141 if (restree == NULL)
142 xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
143 "failed to apply stylesheet");
145 resstat = xsltSaveResultToString(&resstr, &reslen, restree, stylesheet);
147 PG_CATCH();
149 if (restree != NULL)
150 xmlFreeDoc(restree);
151 if (xslt_ctxt != NULL)
152 xsltFreeTransformContext(xslt_ctxt);
153 if (xslt_sec_prefs != NULL)
154 xsltFreeSecurityPrefs(xslt_sec_prefs);
155 if (stylesheet != NULL)
156 xsltFreeStylesheet(stylesheet);
157 if (doctree != NULL)
158 xmlFreeDoc(doctree);
159 xsltCleanupGlobals();
161 pg_xml_done(xmlerrcxt, true);
163 PG_RE_THROW();
165 PG_END_TRY();
167 xmlFreeDoc(restree);
168 xsltFreeTransformContext(xslt_ctxt);
169 xsltFreeSecurityPrefs(xslt_sec_prefs);
170 xsltFreeStylesheet(stylesheet);
171 xmlFreeDoc(doctree);
172 xsltCleanupGlobals();
174 pg_xml_done(xmlerrcxt, false);
176 /* XXX this is pretty dubious, really ought to throw error instead */
177 if (resstat < 0)
178 PG_RETURN_NULL();
180 result = cstring_to_text_with_len((char *) resstr, reslen);
182 if (resstr)
183 xmlFree(resstr);
185 PG_RETURN_TEXT_P(result);
186 #else /* !USE_LIBXSLT */
188 ereport(ERROR,
189 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
190 errmsg("xslt_process() is not available without libxslt")));
191 PG_RETURN_NULL();
192 #endif /* USE_LIBXSLT */
195 #ifdef USE_LIBXSLT
197 static const char **
198 parse_params(text *paramstr)
200 char *pos;
201 char *pstr;
202 char *nvsep = "=";
203 char *itsep = ",";
204 const char **params;
205 int max_params;
206 int nparams;
208 pstr = text_to_cstring(paramstr);
210 max_params = 20; /* must be even! */
211 params = (const char **) palloc((max_params + 1) * sizeof(char *));
212 nparams = 0;
214 pos = pstr;
216 while (*pos != '\0')
218 if (nparams >= max_params)
220 max_params *= 2;
221 params = (const char **) repalloc(params,
222 (max_params + 1) * sizeof(char *));
224 params[nparams++] = pos;
225 pos = strstr(pos, nvsep);
226 if (pos != NULL)
228 *pos = '\0';
229 pos++;
231 else
233 /* No equal sign, so ignore this "parameter" */
234 nparams--;
235 break;
238 /* since max_params is even, we still have nparams < max_params */
239 params[nparams++] = pos;
240 pos = strstr(pos, itsep);
241 if (pos != NULL)
243 *pos = '\0';
244 pos++;
246 else
247 break;
250 /* Add the terminator marker; we left room for it in the palloc's */
251 params[nparams] = NULL;
253 return params;
256 #endif /* USE_LIBXSLT */