DTS Application Library  0.2.3
Application library containing referenced objects and interfaces to common libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
libxslt.c
Go to the documentation of this file.
1 
7 #include <stdint.h>
8 #ifdef __WIN32__
9 #include <winsock2.h>
10 #include <windows.h>
11 #endif
12 #include <string.h>
13 
14 #include <libxslt/xsltutils.h>
15 #include <libxslt/transform.h>
16 
17 #include "include/dtsapp.h"
18 #include "include/priv_xml.h"
19 
21 struct xslt_doc {
23  xsltStylesheetPtr doc;
26 };
27 
29 struct xslt_param {
31  const char *name;
33  const char *value;
34 };
35 
36 static void *xslt_has_init_parser = NULL;
37 
38 static void free_xsltdoc(void *data) {
39  struct xslt_doc *xsltdoc = data;
40 
41  xsltFreeStylesheet(xsltdoc->doc);
42  objunref(xsltdoc->params);
43  xslt_close();
44 }
45 
46 static void free_parser(void *data) {
47  xsltCleanupGlobals();
48  xmlCleanupParser();
49 }
50 
51 static int32_t xslt_hash(const void *data, int key) {
52  int ret;
53  const struct xslt_param *xp = data;
54  const char *hashkey = (key) ? data : xp->name;
55 
56  if (hashkey) {
57  ret = jenhash(hashkey, strlen(hashkey), 0);
58  } else {
59  ret = jenhash(xp, sizeof(xp), 0);
60  }
61  return(ret);
62 }
63 
67 extern struct xslt_doc *xslt_open(const char *xsltfile) {
68  struct xslt_doc *xsltdoc;
69 
70  if (!(xsltdoc = objalloc(sizeof(*xsltdoc), free_xsltdoc))) {
71  return NULL;
72  }
73  xslt_init();
74 
75  xsltdoc->doc = xsltParseStylesheetFile((const xmlChar *)xsltfile);
76  xsltdoc->params = create_bucketlist(0, xslt_hash);
77  return xsltdoc;
78 }
79 
80 static void free_param(void *data) {
81  struct xslt_param *param = data;
82  if (param->name) {
83  free((void *)param->name);
84  }
85  if (param->value) {
86  free((void *)param->value);
87  }
88 }
89 
94 extern void xslt_addparam(struct xslt_doc *xsltdoc, const char *param, const char *value) {
95  struct xslt_param *xparam;
96  int size;
97 
98  if (!xsltdoc || !xsltdoc->params || !objref(xsltdoc) || !(xparam = objalloc(sizeof(*xparam), free_param))) {
99  return;
100  }
101 
102  size = strlen(value) + 3;
103  ALLOC_CONST(xparam->name, param);
104  xparam->value = malloc(size);
105  snprintf((char *)xparam->value, size, "'%s'", value);
106  objlock(xsltdoc);
107  addtobucket(xsltdoc->params, xparam);
108  objunlock(xsltdoc);
109  objunref(xparam);
110  objunref(xsltdoc);
111 }
112 
115 void xslt_clearparam(struct xslt_doc *xsltdoc) {
116  if (!xsltdoc || !xsltdoc->params) {
117  return;
118  }
119 
120  objlock(xsltdoc);
121  objunref(xsltdoc->params);
122  xsltdoc->params = create_bucketlist(0, xslt_hash);
123  objunlock(xsltdoc);
124 }
125 
126 /* grabs ref to xmldoc/xsltdoc and locks xsltdoc*/
127 static const char **xslt_params(struct xml_doc *xmldoc, struct xslt_doc *xsltdoc) {
128  const char **params = NULL;
129  struct xslt_param *xparam;
130  struct bucket_loop *bloop;
131  int cnt=0;
132 
133  if (!objref(xmldoc)) {
134  return NULL;
135  }
136 
137  if (!objref(xsltdoc)) {
138  objunref(xmldoc);
139  return NULL;
140  }
141 
142  objlock(xsltdoc);
143  if (!(params = malloc(sizeof(void *) * (bucket_list_cnt(xsltdoc->params)*2 + 2)))) {
144  objunlock(xsltdoc);
145  objunref(xsltdoc);
146  objunref(xmldoc);
147  return NULL;
148  }
149 
150  bloop = init_bucket_loop(xsltdoc->params);
151  while(bloop && (xparam = next_bucket_loop(bloop))) {
152  params[cnt] = xparam->name;
153  cnt++;
154  params[cnt] = xparam->value;
155  cnt++;
156  objunref(xparam);
157  };
158  params[cnt] = NULL;
159  return params;
160 }
161 
167 extern void xslt_apply(struct xml_doc *xmldoc, struct xslt_doc *xsltdoc, const char *filename, int comp) {
168  const char **params = NULL;
169  xmlDocPtr res;
170 
171  /* ref's xml/xslt locks xslt IF set*/
172  if (!(params = xslt_params(xmldoc, xsltdoc))) {
173  return;
174  }
175 
176 #ifndef __WIN32__
177  touch(filename, 80, 80);
178 #else
179  touch(filename);
180 #endif
181  objlock(xmldoc);
182  res = xsltApplyStylesheet(xsltdoc->doc, xmldoc->doc, params);
183  xsltSaveResultToFilename(filename, res, xsltdoc->doc, comp);
184  objunlock(xmldoc);
185  objunref(xmldoc);
186  objunlock(xsltdoc);
187 
188  free(params);
189  xmlFreeDoc(res);
190  xslt_clearparam(xsltdoc);
191  objunref(xsltdoc);
192 }
193 
198 extern void *xslt_apply_buffer(struct xml_doc *xmldoc, struct xslt_doc *xsltdoc) {
199  struct xml_buffer *xmlbuf;
200  const char **params;
201  xmlDocPtr res;
202 
203  if (!(xmlbuf = objalloc(sizeof(*xmlbuf),xml_free_buffer))) {
204  return NULL;
205  }
206 
207  if (!(params = xslt_params(xmldoc, xsltdoc))) {
208  objunref(xmlbuf);
209  return NULL;
210  }
211 
212  objlock(xmldoc);
213  res = xsltApplyStylesheet(xsltdoc->doc, xmldoc->doc, params);
214  xsltSaveResultToString(&xmlbuf->buffer, &xmlbuf->size, res, xsltdoc->doc);
215  objunlock(xmldoc);
216  objunref(xmldoc);
217  objunlock(xsltdoc);
218 
219  free(params);
220  xmlFreeDoc(res);
221  xslt_clearparam(xsltdoc);
222  objunref(xsltdoc);
223 
224  return xmlbuf;
225 }
226 
230 extern void xslt_init() {
231  if (!xslt_has_init_parser) {
232  xslt_has_init_parser=objalloc(0, free_parser);
233  } else {
234  objref(xslt_has_init_parser);
235  }
236 }
237 
241 extern void xslt_close() {
242  if (xslt_has_init_parser) {
243  objunref(xslt_has_init_parser);
244  }
245 }
246 
struct xslt_doc * xslt_open(const char *xsltfile)
Open a XSLT file returning reference to it.
Definition: libxslt.c:67
Bucket iterator.
Definition: refobj.c:97
void xslt_clearparam(struct xslt_doc *xsltdoc)
Delete all parameters of a XSLT document.
Definition: libxslt.c:115
void * create_bucketlist(int bitmask, blisthash hash_function)
Definition: refobj.c:356
int objref(void *data)
Reference a object.
Definition: refobj.c:153
struct xml_doc xml_doc
Forward decleration of structure.
Definition: dtsapp.h:631
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
const char * value
value of paramater.
Definition: libxslt.c:33
void * xslt_apply_buffer(struct xml_doc *xmldoc, struct xslt_doc *xsltdoc)
Apply XSLT document to a XML document returning result in buffer.
Definition: libxslt.c:198
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
void xml_free_buffer(void *data)
Reference destructor for xml_buffer.
Definition: libxml2.c:46
int bucket_list_cnt(struct bucket_list *blist)
Return number of items in the list.
Definition: refobj.c:552
void * next_bucket_loop(struct bucket_loop *bloop)
Return a reference to the next item in the list this could be the first item.
Definition: refobj.c:662
void xslt_init()
Reference the XSLT parser.
Definition: libxslt.c:230
DTS Application library API Include file.
const char * name
Name of paramater.
Definition: libxslt.c:31
void xslt_close()
Release reference to XSLT parser.
Definition: libxslt.c:241
void touch(const char *filename, uid_t user, gid_t group)
Create a file and set user and group.
Definition: util.c:484
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
void xslt_addparam(struct xslt_doc *xsltdoc, const char *param, const char *value)
Add a parameter to the XSLT document.
Definition: libxslt.c:94
xsltStylesheetPtr doc
Pointer to the document.
Definition: libxslt.c:23
#define jenhash(key, length, initval)
Define jenhash as hashlittle on big endian it should be hashbig.
Definition: dtsapp.h:914
#define ALLOC_CONST(const_var, val)
Macro to assign values to char const.
Definition: dtsapp.h:959
int addtobucket(struct bucket_list *blist, void *data)
Add a reference to the bucketlist.
Definition: refobj.c:428
XSLT Parameter name/value pair.
Definition: libxslt.c:29
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
XSLT Document.
Definition: libxslt.c:21
struct bucket_loop * init_bucket_loop(struct bucket_list *blist)
Create a bucket list iterator to safely iterate the list.
Definition: refobj.c:640
struct bucket_list * params
Bucket list of paramaters to apply to the document.
Definition: libxslt.c:25
Bucket list, hold hashed objects in buckets.
Definition: refobj.c:75
void xslt_apply(struct xml_doc *xmldoc, struct xslt_doc *xsltdoc, const char *filename, int comp)
Apply XSLT document to a XML document.
Definition: libxslt.c:167