PostgreSQL Source Code  git master
xslt_proc.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "utils/xml.h"
Include dependency graph for xslt_proc.c:

Go to the source code of this file.

Functions

 PG_FUNCTION_INFO_V1 (xslt_process)
 
Datum xslt_process (PG_FUNCTION_ARGS)
 

Function Documentation

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( xslt_process  )

◆ xslt_process()

Datum xslt_process ( PG_FUNCTION_ARGS  )

Definition at line 45 of file xslt_proc.c.

46 {
47 #ifdef USE_LIBXSLT
48 
49  text *doct = PG_GETARG_TEXT_PP(0);
50  text *ssheet = PG_GETARG_TEXT_PP(1);
51  text *result;
52  text *paramstr;
53  const char **params;
54  PgXmlErrorContext *xmlerrcxt;
55  volatile xsltStylesheetPtr stylesheet = NULL;
56  volatile xmlDocPtr doctree = NULL;
57  volatile xmlDocPtr restree = NULL;
58  volatile xsltSecurityPrefsPtr xslt_sec_prefs = NULL;
59  volatile xsltTransformContextPtr xslt_ctxt = NULL;
60  volatile int resstat = -1;
61  xmlChar *resstr = NULL;
62  int reslen = 0;
63 
64  if (fcinfo->nargs == 3)
65  {
66  paramstr = PG_GETARG_TEXT_PP(2);
67  params = parse_params(paramstr);
68  }
69  else
70  {
71  /* No parameters */
72  params = (const char **) palloc(sizeof(char *));
73  params[0] = NULL;
74  }
75 
76  /* Setup parser */
78 
79  PG_TRY();
80  {
81  xmlDocPtr ssdoc;
82  bool xslt_sec_prefs_error;
83 
84  /* Parse document */
85  doctree = xmlReadMemory((char *) VARDATA_ANY(doct),
86  VARSIZE_ANY_EXHDR(doct), NULL, NULL,
87  XML_PARSE_NOENT);
88 
89  if (doctree == NULL)
90  xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_XML_DOCUMENT,
91  "error parsing XML document");
92 
93  /* Same for stylesheet */
94  ssdoc = xmlReadMemory((char *) VARDATA_ANY(ssheet),
95  VARSIZE_ANY_EXHDR(ssheet), NULL, NULL,
96  XML_PARSE_NOENT);
97 
98  if (ssdoc == NULL)
99  xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_XML_DOCUMENT,
100  "error parsing stylesheet as XML document");
101 
102  /* After this call we need not free ssdoc separately */
103  stylesheet = xsltParseStylesheetDoc(ssdoc);
104 
105  if (stylesheet == NULL)
106  xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
107  "failed to parse stylesheet");
108 
109  xslt_ctxt = xsltNewTransformContext(stylesheet, doctree);
110 
111  xslt_sec_prefs_error = false;
112  if ((xslt_sec_prefs = xsltNewSecurityPrefs()) == NULL)
113  xslt_sec_prefs_error = true;
114 
115  if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_READ_FILE,
116  xsltSecurityForbid) != 0)
117  xslt_sec_prefs_error = true;
118  if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_WRITE_FILE,
119  xsltSecurityForbid) != 0)
120  xslt_sec_prefs_error = true;
121  if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_CREATE_DIRECTORY,
122  xsltSecurityForbid) != 0)
123  xslt_sec_prefs_error = true;
124  if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_READ_NETWORK,
125  xsltSecurityForbid) != 0)
126  xslt_sec_prefs_error = true;
127  if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_WRITE_NETWORK,
128  xsltSecurityForbid) != 0)
129  xslt_sec_prefs_error = true;
130  if (xsltSetCtxtSecurityPrefs(xslt_sec_prefs, xslt_ctxt) != 0)
131  xslt_sec_prefs_error = true;
132 
133  if (xslt_sec_prefs_error)
134  ereport(ERROR,
135  (errmsg("could not set libxslt security preferences")));
136 
137  restree = xsltApplyStylesheetUser(stylesheet, doctree, params,
138  NULL, NULL, xslt_ctxt);
139 
140  if (restree == NULL)
141  xml_ereport(xmlerrcxt, ERROR, ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
142  "failed to apply stylesheet");
143 
144  resstat = xsltSaveResultToString(&resstr, &reslen, restree, stylesheet);
145  }
146  PG_CATCH();
147  {
148  if (restree != NULL)
149  xmlFreeDoc(restree);
150  if (xslt_ctxt != NULL)
151  xsltFreeTransformContext(xslt_ctxt);
152  if (xslt_sec_prefs != NULL)
153  xsltFreeSecurityPrefs(xslt_sec_prefs);
154  if (stylesheet != NULL)
155  xsltFreeStylesheet(stylesheet);
156  if (doctree != NULL)
157  xmlFreeDoc(doctree);
158  xsltCleanupGlobals();
159 
160  pg_xml_done(xmlerrcxt, true);
161 
162  PG_RE_THROW();
163  }
164  PG_END_TRY();
165 
166  xmlFreeDoc(restree);
167  xsltFreeTransformContext(xslt_ctxt);
168  xsltFreeSecurityPrefs(xslt_sec_prefs);
169  xsltFreeStylesheet(stylesheet);
170  xmlFreeDoc(doctree);
171  xsltCleanupGlobals();
172 
173  pg_xml_done(xmlerrcxt, false);
174 
175  /* XXX this is pretty dubious, really ought to throw error instead */
176  if (resstat < 0)
177  PG_RETURN_NULL();
178 
179  result = cstring_to_text_with_len((char *) resstr, reslen);
180 
181  if (resstr)
182  xmlFree(resstr);
183 
184  PG_RETURN_TEXT_P(result);
185 #else /* !USE_LIBXSLT */
186 
187  ereport(ERROR,
188  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
189  errmsg("xslt_process() is not available without libxslt")));
190  PG_RETURN_NULL();
191 #endif /* USE_LIBXSLT */
192 }
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define PG_RE_THROW()
Definition: elog.h:412
#define PG_TRY(...)
Definition: elog.h:371
#define PG_END_TRY(...)
Definition: elog.h:396
#define ERROR
Definition: elog.h:39
#define PG_CATCH(...)
Definition: elog.h:381
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
void * palloc(Size size)
Definition: mcxt.c:1317
Definition: c.h:641
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:196
struct PgXmlErrorContext PgXmlErrorContext
Definition: xml.h:48
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
@ PG_XML_STRICTNESS_LEGACY
Definition: xml.h:41
PgXmlErrorContext * pgxml_parser_init(PgXmlStrictness strictness)
Definition: xpath.c:64

References cstring_to_text_with_len(), ereport, errcode(), errmsg(), ERROR, palloc(), PG_CATCH, PG_END_TRY, PG_GETARG_TEXT_PP, PG_RE_THROW, PG_RETURN_NULL, PG_RETURN_TEXT_P, PG_TRY, pg_xml_done(), PG_XML_STRICTNESS_LEGACY, pgxml_parser_init(), VARDATA_ANY, VARSIZE_ANY_EXHDR, and xml_ereport().