Setting config template to use gdata-format (It seems that I will
[gdataplugin.git] / src / xslt_aux.h
blobf61ff6221ca127fde8bc125ae78163cc07051a83
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;
69 struct xslt_resources *xslt_new(void)
71 struct xslt_resources *result;
72 result = (struct xslt_resources *)malloc(sizeof(struct xslt_resources));
73 if (result)
74 memset(result, 0, sizeof(struct xslt_resources));
76 return result;
79 int xslt_initialize(struct xslt_resources *ctx, const char *stylesheet_path)
81 int result = -1;
82 if (!stylesheet_path || !ctx)
83 goto exit;
85 xmlSubstituteEntitiesDefault(1);
86 xmlLoadExtDtdDefaultValue = 1;
88 if (ctx->cur)
89 xsltFreeStylesheet(ctx->cur);
91 ctx->cur = xsltParseStylesheetFile((const xmlChar *)stylesheet_path);
92 if (!ctx->cur) {
93 fprintf(stderr, "Cannot create XSLT context!\n");
94 goto exit;
97 result = 0;
98 exit:
99 return result;
102 int xslt_transform(struct xslt_resources *ctx, const char *document)
104 int result = -1;
105 if (!ctx || !document)
106 goto exit;
108 if (ctx->doc)
109 xmlFreeDoc(ctx->doc);
110 if (ctx->output)
111 xmlFreeDoc(ctx->output);
112 ctx->output = NULL;
114 ctx->doc = xmlReadMemory(document, strlen(document), "noname.xml",
115 NULL, 0);
116 if (!ctx->doc) {
117 fprintf(stderr, "Cannot create document with "
118 "entry!\n");
119 goto cleanup;
122 ctx->output = xsltApplyStylesheet(ctx->cur, ctx->doc, NULL);
123 if (!ctx->output) {
124 fprintf(stderr, "Cannot create document with "
125 "output!\n");
126 goto cleanup;
129 if (ctx->xml_str) {
130 xmlFree(ctx->xml_str);
131 ctx->xml_str = NULL;
133 xmlDocDumpMemory(ctx->output, &(ctx->xml_str), &(ctx->length));
135 result = 0;
137 cleanup:
138 if (ctx->doc)
139 xmlFreeDoc(ctx->doc);
140 ctx->doc = NULL;
142 if (ctx->output)
143 xmlFreeDoc(ctx->output);
144 ctx->output = NULL;
145 exit:
147 return result;
150 void xslt_delete(struct xslt_resources *ctx)
152 if (!ctx)
153 return;
155 if (ctx->doc)
156 xmlFreeDoc(ctx->doc);
157 if (ctx->output)
158 xmlFreeDoc(ctx->output);
159 if (ctx->xml_str)
160 xmlFree(ctx->xml_str);
162 xsltFreeStylesheet(ctx->cur);
163 xsltCleanupGlobals();
164 xmlCleanupParser();
166 free(ctx);
169 #endif