Require cmake 2.6, and use its FindPkgConfig.cmake rules
[opensync/xsltformat-cdf.git] / src / xslt_aux.h
blob1d4c54d13cd6deee369e25193987beadcd0c651e
1 /*
2 Copyright (c) 2008 Instituto Nokia de Tecnologia
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13 * Neither the name of the INdT nor the names of its contributors
14 may be used to endorse or promote products derived from this software
15 without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
29 /**
30 * @file xslt_aux.h
31 * @author Adenilson Cavalcanti da Silva <adenilson.silva@indt.org.br>
32 * @date Mon Aug 25 15:50:20 2008
34 * @brief A XSLT helper module, converts a XML doc to another format
35 * using a xslt file.
37 * Depends on libxml and libxslt.
39 * \todo:
40 * - doxygen comments about use
41 * - move code to a '.c' file or make functions static
42 * - make 'xslt_resources' an abstract type
46 #ifndef __XSLT_AUX__
47 #define __XSLT_AUX__
49 #include <libxml/HTMLtree.h>
50 #include <libxml/catalog.h>
51 #include <libxml/debugXML.h>
52 #include <libxml/xinclude.h>
53 #include <libxml/xmlIO.h>
54 #include <libxml/xmlmemory.h>
55 #include <libxslt/transform.h>
56 #include <libxslt/xslt.h>
57 #include <libxslt/xsltInternals.h>
58 #include <libxslt/xsltutils.h>
59 #include <string.h>
61 struct xslt_resources {
62 xmlDocPtr output;
63 xmlDocPtr doc;
64 xsltStylesheetPtr cur;
65 xmlChar *xml_str;
66 int length;
67 char init_flag;
70 struct xslt_resources *xslt_new(void)
72 struct xslt_resources *result;
73 result = (struct xslt_resources *)malloc(sizeof(struct xslt_resources));
74 if (result)
75 memset(result, 0, sizeof(struct xslt_resources));
77 return result;
80 int xslt_initialize(struct xslt_resources *ctx, const char *stylesheet_path)
82 int result = -1;
83 if (!stylesheet_path || !ctx)
84 goto exit;
86 xmlSubstituteEntitiesDefault(1);
87 xmlLoadExtDtdDefaultValue = 1;
89 if (ctx->cur)
90 xsltFreeStylesheet(ctx->cur);
92 ctx->cur = xsltParseStylesheetFile((const xmlChar *)stylesheet_path);
93 if (!ctx->cur) {
94 fprintf(stderr, "Cannot create XSLT context!\n");
95 goto exit;
98 result = 0;
99 ctx->init_flag = 1;
100 exit:
101 return result;
104 int xslt_transform(struct xslt_resources *ctx, const char *document)
106 int result = -1;
107 if (!ctx || !document)
108 goto exit;
110 if (ctx->doc)
111 xmlFreeDoc(ctx->doc);
112 if (ctx->output)
113 xmlFreeDoc(ctx->output);
114 ctx->output = NULL;
116 ctx->doc = xmlReadMemory(document, strlen(document), "noname.xml",
117 NULL, 0);
118 if (!ctx->doc) {
119 fprintf(stderr, "Cannot create document with "
120 "entry!\n");
121 goto cleanup;
124 ctx->output = xsltApplyStylesheet(ctx->cur, ctx->doc, NULL);
125 if (!ctx->output) {
126 fprintf(stderr, "Cannot create document with "
127 "output!\n");
128 goto cleanup;
131 if (ctx->xml_str) {
132 xmlFree(ctx->xml_str);
133 ctx->xml_str = NULL;
135 xmlDocDumpMemory(ctx->output, &(ctx->xml_str), &(ctx->length));
137 result = 0;
139 cleanup:
140 if (ctx->doc)
141 xmlFreeDoc(ctx->doc);
142 ctx->doc = NULL;
144 if (ctx->output)
145 xmlFreeDoc(ctx->output);
146 ctx->output = NULL;
147 exit:
149 return result;
152 void xslt_delete(struct xslt_resources *ctx)
154 if (!ctx)
155 return;
157 /* TODO: 'output' is pointing to a non null value (probably an
158 * overflow in some other place) even when not initialized.
159 * It happens just after all clients are disconnected.
160 * I must investigate it further, for while the flag is a work
161 * around to stop msync to crash.
163 /* fprintf(stderr, "\ndoc: %x\tout: %x\txml: %x\tcur: %x\n\n", */
164 /* ctx->doc, ctx->output, ctx->xml_str, ctx->cur); */
166 if (!ctx->init_flag)
167 goto exit;
170 if (ctx->doc)
171 xmlFreeDoc(ctx->doc);
172 if (ctx->output)
173 xmlFreeDoc(ctx->output);
174 if (ctx->xml_str)
175 xmlFree(ctx->xml_str);
176 if (ctx->cur) {
177 xsltFreeStylesheet(ctx->cur);
178 xsltCleanupGlobals();
179 xmlCleanupParser();
182 exit:
184 free(ctx);
187 #endif