PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
xslt_proc.c File Reference
#include "postgres.h"
#include "executor/spi.h"
#include "fmgr.h"
#include "funcapi.h"
#include "miscadmin.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 ( xslt_process  )
Datum xslt_process ( PG_FUNCTION_ARGS  )

Definition at line 48 of file xslt_proc.c.

References cstring_to_text_with_len(), ereport, errcode(), errmsg(), ERROR, NULL, 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(), result, VARDATA_ANY, VARSIZE_ANY_EXHDR, and xml_ereport().

49 {
50 #ifdef USE_LIBXSLT
51 
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;
66 
67  if (fcinfo->nargs == 3)
68  {
69  paramstr = PG_GETARG_TEXT_PP(2);
70  params = parse_params(paramstr);
71  }
72  else
73  {
74  /* No parameters */
75  params = (const char **) palloc(sizeof(char *));
76  params[0] = NULL;
77  }
78 
79  /* Setup parser */
81 
82  PG_TRY();
83  {
84  xmlDocPtr ssdoc;
85  bool xslt_sec_prefs_error;
86 
87  /* Parse document */
88  doctree = xmlParseMemory((char *) VARDATA_ANY(doct),
89  VARSIZE_ANY_EXHDR(doct));
90 
91  if (doctree == NULL)
92  xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
93  "error parsing XML document");
94 
95  /* Same for stylesheet */
96  ssdoc = xmlParseMemory((char *) VARDATA_ANY(ssheet),
97  VARSIZE_ANY_EXHDR(ssheet));
98 
99  if (ssdoc == NULL)
100  xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
101  "error parsing stylesheet as XML document");
102 
103  /* After this call we need not free ssdoc separately */
104  stylesheet = xsltParseStylesheetDoc(ssdoc);
105 
106  if (stylesheet == NULL)
107  xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
108  "failed to parse stylesheet");
109 
110  xslt_ctxt = xsltNewTransformContext(stylesheet, doctree);
111 
112  xslt_sec_prefs_error = false;
113  if ((xslt_sec_prefs = xsltNewSecurityPrefs()) == NULL)
114  xslt_sec_prefs_error = true;
115 
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;
133 
134  if (xslt_sec_prefs_error)
135  ereport(ERROR,
136  (errmsg("could not set libxslt security preferences")));
137 
138  restree = xsltApplyStylesheetUser(stylesheet, doctree, params,
139  NULL, NULL, xslt_ctxt);
140 
141  if (restree == NULL)
142  xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
143  "failed to apply stylesheet");
144 
145  resstat = xsltSaveResultToString(&resstr, &reslen, restree, stylesheet);
146  }
147  PG_CATCH();
148  {
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();
160 
161  pg_xml_done(xmlerrcxt, true);
162 
163  PG_RE_THROW();
164  }
165  PG_END_TRY();
166 
167  xmlFreeDoc(restree);
168  xsltFreeTransformContext(xslt_ctxt);
169  xsltFreeSecurityPrefs(xslt_sec_prefs);
170  xsltFreeStylesheet(stylesheet);
171  xmlFreeDoc(doctree);
172  xsltCleanupGlobals();
173 
174  pg_xml_done(xmlerrcxt, false);
175 
176  /* XXX this is pretty dubious, really ought to throw error instead */
177  if (resstat < 0)
178  PG_RETURN_NULL();
179 
180  result = cstring_to_text_with_len((char *) resstr, reslen);
181 
182  if (resstr)
183  xmlFree(resstr);
184 
185  PG_RETURN_TEXT_P(result);
186 #else /* !USE_LIBXSLT */
187 
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 */
193 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
struct PgXmlErrorContext PgXmlErrorContext
Definition: xml.h:48
int errcode(int sqlerrcode)
Definition: elog.c:575
PgXmlErrorContext * pgxml_parser_init(PgXmlStrictness strictness)
Definition: xpath.c:65
return result
Definition: formatting.c:1632
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define ERROR
Definition: elog.h:43
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:229
#define PG_RE_THROW()
Definition: elog.h:314
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
#define PG_TRY()
Definition: elog.h:284
#define PG_RETURN_NULL()
Definition: fmgr.h:305
#define PG_END_TRY()
Definition: elog.h:300
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)