Adding a huge todo list in source files.
[gdataplugin.git] / src / xslt_aux.h
blobf28da851a0ad0f2cc7b55cfc1812a288e3b931d8
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;
87 ctx->cur = xsltParseStylesheetFile((const xmlChar *)stylesheet_path);
88 if (!ctx->cur) {
89 fprintf(stderr, "Cannot create XSLT context!\n");
90 goto exit;
93 result = 0;
94 exit:
95 return result;
98 int xslt_transform(struct xslt_resources *ctx, const char *document)
100 int result = -1;
101 if (!ctx || !document)
102 goto exit;
104 if (ctx->doc)
105 xmlFreeDoc(ctx->doc);
106 if (ctx->output)
107 xmlFreeDoc(ctx->output);
108 ctx->output = NULL;
110 ctx->doc = xmlReadMemory(document, strlen(document), "noname.xml",
111 NULL, 0);
112 if (!ctx->doc) {
113 fprintf(stderr, "Cannot create document with "
114 "entry!\n");
115 goto cleanup;
118 ctx->output = xsltApplyStylesheet(ctx->cur, ctx->doc, NULL);
119 if (!ctx->output) {
120 fprintf(stderr, "Cannot create document with "
121 "output!\n");
122 goto cleanup;
125 if (ctx->xml_str) {
126 xmlFree(ctx->xml_str);
127 ctx->xml_str = NULL;
129 xmlDocDumpMemory(ctx->output, &(ctx->xml_str), &(ctx->length));
131 result = 0;
133 cleanup:
134 if (ctx->doc)
135 xmlFreeDoc(ctx->doc);
136 ctx->doc = NULL;
138 if (ctx->output)
139 xmlFreeDoc(ctx->output);
140 ctx->output = NULL;
141 exit:
143 return result;
146 void xslt_delete(struct xslt_resources *ctx)
148 if (!ctx)
149 return;
151 if (ctx->doc)
152 xmlFreeDoc(ctx->doc);
153 if (ctx->output)
154 xmlFreeDoc(ctx->output);
155 if (ctx->xml_str)
156 xmlFree(ctx->xml_str);
158 xsltFreeStylesheet(ctx->cur);
159 xsltCleanupGlobals();
160 xmlCleanupParser();
162 free(ctx);
165 #endif